Skip to content

Commit

Permalink
initialize c2d communication parameter
Browse files Browse the repository at this point in the history
closes #2356
  • Loading branch information
baggepinnen committed Nov 16, 2023
1 parent 1e36fc7 commit 74a3814
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/systems/clock_inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function generate_discrete_affect(syss, inputs, continuous_id, id_to_clock;
param_to_idx = Dict{Any, Int}(reverse(en) for en in enumerate(appended_parameters))
offset = length(appended_parameters)
affect_funs = []
init_funs = []

Check warning on line 153 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L153

Added line #L153 was not covered by tests
svs = []
clocks = TimeDomain[]
for (i, (sys, input)) in enumerate(zip(syss, inputs))
Expand Down Expand Up @@ -202,6 +203,14 @@ function generate_discrete_affect(syss, inputs, continuous_id, id_to_clock;
push!(save_vec.args, :(p[$(input_offset + i)]))
end
empty_disc = isempty(disc_range)

disc_init = :(function (p, t)
d2c_obs = $disc_to_cont_obs
d2c_view = view(p, $disc_to_cont_idxs)
disc_state = view(p, $disc_range)
copyto!(d2c_view, d2c_obs(disc_state, p, t))

Check warning on line 211 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L207-L211

Added lines #L207 - L211 were not covered by tests
end)

affect! = :(function (integrator, saved_values)
@unpack u, p, t = integrator
c2d_obs = $cont_to_disc_obs
Expand All @@ -223,15 +232,20 @@ function generate_discrete_affect(syss, inputs, continuous_id, id_to_clock;
end)
sv = SavedValues(Float64, Vector{Float64})
push!(affect_funs, affect!)
push!(init_funs, disc_init)

Check warning on line 235 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L235

Added line #L235 was not covered by tests
push!(svs, sv)
end
if eval_expression
affects = map(affect_funs) do a
drop_expr(@RuntimeGeneratedFunction(eval_module, toexpr(LiteralExpr(a))))
end
inits = map(init_funs) do a
drop_expr(@RuntimeGeneratedFunction(eval_module, toexpr(LiteralExpr(a))))

Check warning on line 243 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L242-L243

Added lines #L242 - L243 were not covered by tests
end
else
affects = map(a -> toexpr(LiteralExpr(a)), affect_funs)
inits = map(a -> toexpr(LiteralExpr(a)), init_funs)

Check warning on line 247 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L247

Added line #L247 was not covered by tests
end
defaults = Dict{Any, Any}(v => 0.0 for v in Iterators.flatten(inputs))
return affects, clocks, svs, appended_parameters, defaults
return affects, inits, clocks, svs, appended_parameters, defaults

Check warning on line 250 in src/systems/clock_inference.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/clock_inference.jl#L250

Added line #L250 was not covered by tests
end
34 changes: 28 additions & 6 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,9 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map =
has_difference = has_difference,
check_length, kwargs...)
cbs = process_events(sys; callback, has_difference, kwargs...)
inits = []
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
affects, inits, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)

Check warning on line 943 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L943

Added line #L943 was not covered by tests
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
if clock isa Clock
PeriodicCallback(DiscreteSaveAffect(affect, sv), clock.dt)
Expand Down Expand Up @@ -969,7 +970,13 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map =
if svs !== nothing
kwargs1 = merge(kwargs1, (disc_saved_values = svs,))
end
ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)
prob = ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)
if !isempty(inits)
for init in inits
init(prob.p, tspan[1])
end

Check warning on line 977 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L975-L977

Added lines #L975 - L977 were not covered by tests
end
prob
end
get_callback(prob::ODEProblem) = prob.kwargs[:callback]

Expand Down Expand Up @@ -1038,8 +1045,9 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [],
h = h_oop
u0 = h(p, tspan[1])
cbs = process_events(sys; callback, has_difference, kwargs...)
inits = []

Check warning on line 1048 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1048

Added line #L1048 was not covered by tests
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
affects, inits, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)

Check warning on line 1050 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1050

Added line #L1050 was not covered by tests
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
if clock isa Clock
PeriodicCallback(DiscreteSaveAffect(affect, sv), clock.dt)
Expand Down Expand Up @@ -1068,7 +1076,13 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [],
if svs !== nothing
kwargs1 = merge(kwargs1, (disc_saved_values = svs,))
end
DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)
prob = DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)
if !isempty(inits)
for init in inits
init(prob.p, tspan[1])
end

Check warning on line 1083 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1079-L1083

Added lines #L1079 - L1083 were not covered by tests
end
prob

Check warning on line 1085 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1085

Added line #L1085 was not covered by tests
end

function DiffEqBase.SDDEProblem(sys::AbstractODESystem, args...; kwargs...)
Expand All @@ -1092,8 +1106,9 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [],
h(p, t) = h_oop(p, t)
u0 = h(p, tspan[1])
cbs = process_events(sys; callback, has_difference, kwargs...)
inits = []

Check warning on line 1109 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1109

Added line #L1109 was not covered by tests
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
affects, inits, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)

Check warning on line 1111 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1111

Added line #L1111 was not covered by tests
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
if clock isa Clock
PeriodicCallback(DiscreteSaveAffect(affect, sv), clock.dt)
Expand Down Expand Up @@ -1133,8 +1148,15 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [],
else
noise_rate_prototype = zeros(eltype(u0), size(noiseeqs))
end
SDDEProblem{iip}(f, f.g, u0, h, tspan, p; noise_rate_prototype =
prob = SDDEProblem{iip}(f, f.g, u0, h, tspan, p;

Check warning on line 1151 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1151

Added line #L1151 was not covered by tests
noise_rate_prototype =
noise_rate_prototype, kwargs1..., kwargs...)
if !isempty(inits)
for init in inits
init(prob.p, tspan[1])
end

Check warning on line 1157 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1154-L1157

Added lines #L1154 - L1157 were not covered by tests
end
prob

Check warning on line 1159 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1159

Added line #L1159 was not covered by tests
end

"""
Expand Down
3 changes: 1 addition & 2 deletions test/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ prob = ODEProblem(ssys,
(0.0, 10.0),
[model.controller.kp => 2.0; model.controller.ki => 2.0])

@test_broken prob.p[9] == 1 # constant output * kp issue https://github.com/SciML/ModelingToolkit.jl/issues/2356
prob.p[9] = 1 # constant output * kp
@test prob.p[9] == 1 # constant output * kp issue https://github.com/SciML/ModelingToolkit.jl/issues/2356
sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent)
# plot([sol(timevec .+ 1e-12, idxs=model.plant.output.u) y])

Expand Down

0 comments on commit 74a3814

Please sign in to comment.