@@ -596,6 +596,22 @@ function isinitial(p)
596596 operation (p) === getindex && isinitial (arguments (p)[1 ]))
597597end
598598
599+ """
600+ $(TYPEDSIGNATURES)
601+
602+ Find [`GlobalScope`](@ref)d variables in `sys` and add them to the unknowns/parameters.
603+ """
604+ function discover_globalscoped (sys:: AbstractSystem )
605+ newunknowns = OrderedSet ()
606+ newparams = OrderedSet ()
607+ iv = has_iv (sys) ? get_iv (sys) : nothing
608+ collect_scoped_vars! (newunknowns, newparams, sys, iv; depth = - 1 )
609+ setdiff! (newunknowns, observables (sys))
610+ @set! sys. ps = unique! (vcat (get_ps (sys), collect (newparams)))
611+ @set! sys. unknowns = unique! (vcat (get_unknowns (sys), collect (newunknowns)))
612+ return sys
613+ end
614+
599615"""
600616$(TYPEDSIGNATURES)
601617
@@ -612,13 +628,7 @@ using [`toggle_namespacing`](@ref).
612628"""
613629function complete (
614630 sys:: AbstractSystem ; split = true , flatten = true , add_initial_parameters = true )
615- newunknowns = OrderedSet ()
616- newparams = OrderedSet ()
617- iv = has_iv (sys) ? get_iv (sys) : nothing
618- collect_scoped_vars! (newunknowns, newparams, sys, iv; depth = - 1 )
619- # don't update unknowns to not disturb `mtkcompile` order
620- # `GlobalScope`d unknowns will be picked up and added there
621- @set! sys. ps = unique! (vcat (get_ps (sys), collect (newparams)))
631+ sys = discover_globalscoped (sys)
622632
623633 if flatten
624634 eqs = equations (sys)
0 commit comments