Skip to content

Commit 4d1c2f6

Browse files
committed
Use getsym to access modified and observed values from the integrator
1 parent 78431af commit 4d1c2f6

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/systems/imperative_affect.jl

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,13 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs.
189189
else
190190
zeros(sz)
191191
end
192-
obs_fun = build_explicit_observed_function(
193-
sys, Symbolics.scalarize.(obs_exprs);
194-
mkarray = (es, _) -> MakeTuple(es))
195-
obs_sym_tuple = (obs_syms...,)
192+
geto_funs = NamedTuple{(obs_syms...,)}((getsym.((sys,), obs_exprs)...,))
196193

197194
# okay so now to generate the stuff to assign it back into the system
195+
getm_funs = NamedTuple{(mod_syms...,)}((getsym.((sys,), mod_exprs)...,))
196+
198197
mod_pairs = mod_exprs .=> mod_syms
199198
mod_names = (mod_syms...,)
200-
mod_og_val_fun = build_explicit_observed_function(
201-
sys, Symbolics.scalarize.(first.(mod_pairs));
202-
mkarray = (es, _) -> MakeTuple(es))
203-
204199
upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,))
205200

206201
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
@@ -212,12 +207,10 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs.
212207
let user_affect = func(affect), ctx = context(affect)
213208
function (integ)
214209
# update the to-be-mutated values; this ensures that if you do a no-op then nothing happens
215-
modvals = mod_og_val_fun(integ.u, integ.p, integ.t)
216-
upd_component_array = NamedTuple{mod_names}(modvals)
210+
upd_component_array = _generated_readback(integ, getm_funs)
217211

218212
# update the observed values
219-
obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(
220-
integ.u, integ.p, integ.t))
213+
obs_component_array = _generated_readback(integ, geto_funs)
221214

222215
# let the user do their thing
223216
upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ)

test/symbolic_events.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,3 +1461,26 @@ end
14611461
sys = structural_simplify(sys)
14621462
sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5())
14631463
end
1464+
1465+
@testset "Tuples in ImperativeAffect arguments" begin
1466+
@mtkmodel ImperativeAffectTupleMWE begin
1467+
@parameters begin
1468+
y(t) = 1.0
1469+
end
1470+
@variables begin
1471+
x(t) = 0.0
1472+
end
1473+
@equations begin
1474+
D(x) ~ y
1475+
end
1476+
@continuous_events begin
1477+
(x ~ 0.5) => ModelingToolkit.ImperativeAffect(
1478+
observed = (; mypars = (x, 2 * x)), modified = (; y)) do m, o, c, i
1479+
return (; y = 2 * o.mypars[1] + o.mypars[2])
1480+
end
1481+
end
1482+
end
1483+
@mtkbuild sys = ImperativeAffectTupleMWE()
1484+
prob = ODEProblem(sys, [], (0.0, 1.0))
1485+
sol = solve(prob, Tsit5())
1486+
end

0 commit comments

Comments
 (0)