Skip to content

Commit 4715fd1

Browse files
committed
test: fix namespacing
1 parent 107fe75 commit 4715fd1

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/systems/callbacks.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,12 @@ function make_affect(affect::Vector{Equation}; warn = true)
258258
aff_map = Dict(zip(p_as_unknowns, discretes))
259259
rev_map = Dict([v => k for (k, v) in aff_map])
260260
affect = Symbolics.substitute(affect, rev_map)
261-
@mtkbuild affectsys = ImplicitDiscreteSystem(
262-
affect, iv, collect(union(unknowns, p_as_unknowns)), cb_params)
261+
@mtkbuild affectsys = ImplicitDiscreteSystem(affect, iv, collect(union(unknowns, p_as_unknowns)), cb_params)
263262
params = filter(isparameter, map(x -> only(arguments(unwrap(x))), cb_params))
264-
@show params
265-
266263
for u in unknowns
267264
aff_map[u] = u
268265
end
269266

270-
@show unknowns
271-
@show params
272-
273267
return AffectSystem(affectsys, collect(unknowns), params, discretes, aff_map)
274268
end
275269

@@ -494,16 +488,22 @@ function namespace_affect(affect::FunctionalAffect, s)
494488
context(affect))
495489
end
496490

497-
namespace_affect(affect::AffectSystem, s) = AffectSystem(system(affect), renamespace.((s,), discretes(affect)))
498-
namespace_affects(af::Union{Nothing, Affect}, s) = af isa Affect ? namespace_affect(af, s) : nothing
491+
function namespace_affect(affect::AffectSystem, s)
492+
AffectSystem(renamespace(s, system(affect)),
493+
renamespace.((s,), unknowns(affect)),
494+
renamespace.((s,), parameters(affect)),
495+
renamespace.((s,), discretes(affect)),
496+
Dict([k => renamespace(s, v) for (k, v) in aff_to_sys(affect)]))
497+
end
498+
namespace_affect(af::Nothing, s) = nothing
499499

500500
function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback
501501
SymbolicContinuousCallback(
502502
namespace_equation.(equations(cb), (s,)),
503-
namespace_affects(affects(cb), s),
504-
affect_neg = namespace_affects(affect_negs(cb), s),
505-
initialize = namespace_affects(initialize_affects(cb), s),
506-
finalize = namespace_affects(finalize_affects(cb), s),
503+
namespace_affect(affects(cb), s),
504+
affect_neg = namespace_affect(affect_negs(cb), s),
505+
initialize = namespace_affect(initialize_affects(cb), s),
506+
finalize = namespace_affect(finalize_affects(cb), s),
507507
rootfind = cb.rootfind)
508508
end
509509

@@ -794,9 +794,9 @@ function compile_affect(
794794

795795
ps = parameters(aff)
796796
dvs = unknowns(aff)
797-
@show ps
798797

799798
if aff isa AffectSystem
799+
affsys = system(aff)
800800
aff_map = aff_to_sys(aff)
801801
sys_map = Dict([v => k for (k, v) in aff_map])
802802
build_initializeprob = has_alg_eqs(sys)
@@ -809,11 +809,11 @@ function compile_affect(
809809
push!(pmap, pre_p => pval)
810810
end
811811
guesses = Pair[u => integrator[aff_map[u]] for u in updated_vals(aff)]
812-
affprob = ImplicitDiscreteProblem(system(aff), Pair[], (0, 1), pmap; guesses, build_initializeprob)
812+
affprob = ImplicitDiscreteProblem(affsys, Pair[], (0, 1), pmap; guesses, build_initializeprob)
813813

814814
affsol = init(affprob, SimpleIDSolve())
815815
for u in unknowns(aff)
816-
integrator[u] = affsol[u]
816+
integrator[u] = affsol[sys_map[u]]
817817
end
818818
for p in discretes(aff)
819819
integrator.ps[p] = affsol[sys_map[p]]
@@ -899,9 +899,9 @@ function continuous_events(sys::AbstractSystem)
899899
systems = get_systems(sys)
900900
cbs = [obs;
901901
reduce(vcat,
902-
(map(o -> namespace_callback(o, s), continuous_events(s))
903-
for s in systems),
902+
(map(o -> namespace_callback(o, s), continuous_events(s)) for s in systems),
904903
init = SymbolicContinuousCallback[])]
904+
@show cbs
905905
filter(!isempty, cbs)
906906
end
907907

test/symbolic_events.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ end
644644
dprob = DiscreteProblem(jsys, u0, tspan, p)
645645
jprob = JumpProblem(jsys, dprob, Direct(); kwargs...)
646646
sol = solve(jprob, SSAStepper(); tstops = tstops)
647+
@show sol
647648
@test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1
648649
paramtotest === nothing || (@test sol.ps[paramtotest] == 1.0)
649650
@test sol(40.0)[1] == 0
@@ -654,7 +655,7 @@ end
654655
@variables A(t) B(t)
655656

656657
cond1 = (t == t1)
657-
affect1 = [A ~ Pre(A) + 1]
658+
affect1 = [A ~ A + 1]
658659
cb1 = cond1 => affect1
659660
cond2 = (t == t2)
660661
affect2 = [k ~ 1.0]
@@ -704,7 +705,7 @@ end
704705
testsol(jsys6, u0, p, tspan; tstops = [1.0, 2.0], rng, paramtotest = k)
705706
end
706707

707-
@testset "Oscillator" begin
708+
@testset "Namespacing" begin
708709
function oscillator_ce(k = 1.0; name)
709710
sts = @variables x(t)=1.0 v(t)=0.0 F(t)
710711
ps = @parameters k=k Θ=0.5
@@ -1152,7 +1153,7 @@ end
11521153
f = ModelingToolkit.FunctionalAffect(
11531154
f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = [])
11541155
cb1 = ModelingToolkit.SymbolicContinuousCallback(
1155-
[x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f)
1156+
[x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f)
11561157
@mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1])
11571158
prob = ODEProblem(sys, [x => 1.0], (0.0, 2), [])
11581159
sol = solve(prob, Tsit5(); dtmax = 0.01)

0 commit comments

Comments
 (0)