@@ -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)
274268end
275269
@@ -494,16 +488,22 @@ function namespace_affect(affect::FunctionalAffect, s)
494488 context (affect))
495489end
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
500500function 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)
508508end
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)
906906end
907907
0 commit comments