@@ -89,7 +89,7 @@ function AffectSystem(affect::Vector{Equation}; discrete_parameters = SymbolicT[
8989 if ! haspre (eq) && ! (isconst (eq. lhs) && isconst (eq. rhs))
9090 @invokelatest warn_algebraic_equation (eq)
9191 end
92- collect_vars! (dvs, params, eq, iv; op = Pre)
92+ collect_vars! (dvs, params, eq, iv, Pre)
9393 empty! (_varsbuf)
9494 SU. search_variables! (_varsbuf, eq; is_atomic = OperatorIsAtomic {Pre} ())
9595 filter! (x -> iscall (x) && operation (x) === Pre (), _varsbuf)
@@ -125,20 +125,35 @@ function AffectSystem(affect::Vector{Equation}; discrete_parameters = SymbolicT[
125125 # This `@invokelatest` should not be necessary, but it works around the inference bug
126126 # in https://github.com/JuliaLang/julia/issues/59943. Remove it at your own risk, the
127127 # bug took weeks to reduce to an MWE.
128- affectsys = @invokelatest mtkcompile (affectsys; fully_determined = nothing )
128+ affectsys = ( @invokelatest mtkcompile (affectsys; fully_determined = nothing )) :: System
129129 # get accessed parameters p from Pre(p) in the callback parameters
130130 accessed_params = Vector {SymbolicT} (filter (isparameter, map (unPre, collect (pre_params))))
131131 union! (accessed_params, sys_params)
132132
133133 # add scalarized unknowns to the map.
134134 _obs, _ = unhack_observed (observed (affectsys), equations (affectsys))
135135 _dvs = vcat (unknowns (affectsys), map (eq -> eq. lhs, _obs))
136- _dvs = reduce (vcat, map (safe_vec ∘ scalarize, _dvs), init = SymbolicT[] )
137- _discs = reduce (vcat, map (safe_vec ∘ scalarize, discretes); init = SymbolicT[] )
136+ _dvs = __safe_scalarize_vars ( _dvs)
137+ _discs = __safe_scalarize_vars ( discretes)
138138 setdiff! (_dvs, _discs)
139139 AffectSystem (affectsys, _dvs, accessed_params, discrete_parameters)
140140end
141141
142+ function __safe_scalarize_vars (vars:: Vector{SymbolicT} )
143+ _vars = SymbolicT[]
144+ for v in vars
145+ sh = SU. shape (v):: SU.ShapeVecT
146+ if isempty (sh)
147+ push! (_vars, v)
148+ continue
149+ end
150+ for i in SU. stable_eachindex (v)
151+ push! (_vars, v[i])
152+ end
153+ end
154+ return _vars
155+ end
156+
142157safe_vec (@nospecialize (x)) = x isa SymbolicT ? [x] : vec (x:: Array{SymbolicT} )
143158
144159system (a:: AffectSystem ) = a. system
@@ -1043,13 +1058,13 @@ The `SymbolicDiscreteCallback`s in the returned vector are structs with two fiel
10431058See also `get_discrete_events`, which only returns the events of the top-level system.
10441059"""
10451060function discrete_events (sys:: AbstractSystem )
1046- obs = get_discrete_events (sys)
1061+ cbs = get_discrete_events (sys)
10471062 systems = get_systems (sys)
1048- cbs = [obs;
1049- reduce (vcat,
1050- ( map (cb -> namespace_callback (cb , s), discrete_events (s)) for s in systems),
1051- init = SymbolicDiscreteCallback[])]
1052- cbs
1063+ cbs = copy (cbs)
1064+ for s in systems
1065+ append! (cbs, map (Base . Fix2 (namespace_callback , s), discrete_events (s)))
1066+ end
1067+ return cbs
10531068end
10541069
10551070"""
@@ -1100,15 +1115,13 @@ The `SymbolicContinuousCallback`s in the returned vector are structs with two fi
11001115See also `get_continuous_events`, which only returns the events of the top-level system.
11011116"""
11021117function continuous_events (sys:: AbstractSystem )
1103- obs = get_continuous_events (sys)
1104- filter (! isempty, obs)
1105-
1118+ cbs = get_continuous_events (sys)
11061119 systems = get_systems (sys)
1107- cbs = [obs;
1108- reduce (vcat,
1109- ( map (o -> namespace_callback (o , s), continuous_events (s)) for s in systems),
1110- init = SymbolicContinuousCallback[])]
1111- filter ( ! isempty, cbs)
1120+ cbs = copy (cbs)
1121+ for s in systems
1122+ append! (cbs, map (Base . Fix2 (namespace_callback , s), continuous_events (s)))
1123+ end
1124+ return cbs
11121125end
11131126
11141127"""
0 commit comments