Skip to content

Fix ImperativeAffect namespacing for arrays in non-root components #3593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/systems/imperative_affect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,22 @@ end

namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s)
function namespace_affect(affect::ImperativeAffect, s)
rmn = []
for modded in modified(affect)
if symbolic_type(modded) == NotSymbolic() && modded isa AbstractArray
res = []
for m in modded
push!(res, renamespace(s, m))
end
push!(rmn, res)
else
push!(rmn, renamespace(s, modded))
end
end
ImperativeAffect(func(affect),
namespace_expr.(observed(affect), (s,)),
observed_syms(affect),
renamespace.((s,), modified(affect)),
rmn,
modified_syms(affect),
context(affect),
affect.skip_checks)
Expand Down
27 changes: 27 additions & 0 deletions test/symbolic_events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1434,3 +1434,30 @@ end
sol2 = solve(ODEProblem(sys2, [], (0.0, 1.0)), Tsit5())
@test 100.0 ∈ sol2[sys2.wd2.θ]
end

@testset "Array parameter updates of parent components in ImperativeEffect" begin
function child(vals; name, max_time = 0.1)
vars = @variables begin
x(t) = 0.0
end
eqs = reduce(vcat, Symbolics.scalarize.([
D(x) ~ 1.0
]))
reset = ModelingToolkit.ImperativeAffect(
modified = (; vals = Symbolics.scalarize(ParentScope.(vals)), x)) do m, o, _, i
@set! m.vals = m.vals .+ 1
@set! m.x = 0.0
return m
end
return ODESystem(eqs, t, vars, []; name = name,
continuous_events = [[x ~ max_time] => reset])
end
shared_pars = @parameters begin
vals(t)[1:2] = 0.0
end

@named sys = ODESystem(Equation[], t, [], Symbolics.scalarize(vals);
systems = [child(vals; name = :child)])
sys = structural_simplify(sys)
sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5())
end
Loading