@@ -1292,8 +1292,12 @@ $(TYPEDSIGNATURES)
12921292Get the unknown variables of the system `sys` and its subsystems.
12931293
12941294See also [`ModelingToolkit.get_unknowns`](@ref).
1295+
1296+ Arguments:
1297+ - `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
12951298"""
12961299function unknowns (sys:: AbstractSystem ; toplevel = false )
1300+ toplevel && (sys = recursive_get_parent (sys))
12971301 sts = get_unknowns (sys)
12981302 systems = get_systems (sys)
12991303 nonunique_unknowns = if toplevel || isempty (systems)
@@ -1319,8 +1323,12 @@ $(TYPEDSIGNATURES)
13191323Get the parameters of the system `sys` and its subsystems.
13201324
13211325See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref).
1326+
1327+ Arguments:
1328+ - `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
13221329"""
13231330function parameters (sys:: AbstractSystem ; initial_parameters = false , toplevel = false )
1331+ toplevel && (sys = recursive_get_parent (sys))
13241332 ps = get_ps (sys)
13251333 if ps == SciMLBase. NullParameters ()
13261334 return []
@@ -1353,6 +1361,22 @@ function dependent_parameters(sys::AbstractSystem)
13531361 return map (eq -> eq. lhs, parameter_dependencies (sys))
13541362end
13551363
1364+ """
1365+ recursive_get_parent(sys::AbstractSystem)
1366+
1367+ Loops through parent systems to find the original parent system.
1368+
1369+ Warning:
1370+ - Curently only used (and tested) in the context of accessor functions (e.g. `parameters`),
1371+ specifically in the context of the `toplevel` keyword argument.
1372+ """
1373+ function recursive_get_parent (sys:: AbstractSystem )
1374+ if ModelingToolkit. has_parent (sys) && (p = ModelingToolkit. get_parent (sys)) != = nothing
1375+ return recursive_get_parent (p)
1376+ end
1377+ return sys
1378+ end
1379+
13561380"""
13571381$(TYPEDSIGNATURES)
13581382Get the parameter dependencies of the system `sys` and its subsystems.
@@ -1491,6 +1515,7 @@ function controls(sys::AbstractSystem)
14911515end
14921516
14931517function observed (sys:: AbstractSystem ; toplevel = false )
1518+ toplevel && (sys = recursive_get_parent (sys))
14941519 obs = get_observed (sys)
14951520 toplevel && return obs
14961521 systems = get_systems (sys)
@@ -1550,8 +1575,12 @@ It may include some abbreviations and aliases of observables.
15501575It is often the most useful way to inspect the equations of a system.
15511576
15521577See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref).
1578+
1579+ Arguments:
1580+ - `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
15531581"""
15541582function equations (sys:: AbstractSystem ; toplevel = false )
1583+ toplevel && (sys = recursive_get_parent (sys))
15551584 eqs = get_eqs (sys)
15561585 systems = get_systems (sys)
15571586 if toplevel || isempty (systems)
0 commit comments