Skip to content

Commit 1d872e0

Browse files
committed
update with aayush code to handle flattened systems
1 parent 186a7d3 commit 1d872e0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/systems/abstractsystem.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,12 @@ $(TYPEDSIGNATURES)
12921292
Get the unknown variables of the system `sys` and its subsystems.
12931293
12941294
See 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
"""
12961299
function 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)
13191323
Get the parameters of the system `sys` and its subsystems.
13201324
13211325
See 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
"""
13231330
function 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))
13541362
end
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)
13581382
Get the parameter dependencies of the system `sys` and its subsystems.
@@ -1491,6 +1515,7 @@ function controls(sys::AbstractSystem)
14911515
end
14921516

14931517
function 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.
15501575
It is often the most useful way to inspect the equations of a system.
15511576
15521577
See 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
"""
15541582
function 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)

src/systems/callbacks.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,18 @@ function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuo
320320
end
321321

322322
"""
323-
continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback}
323+
continuous_events(sys::AbstractSystem; toplevel = false)::Vector{SymbolicContinuousCallback}
324324
325325
Returns a vector of all the `continuous_events` in an abstract system and its component subsystems.
326326
The `SymbolicContinuousCallback`s in the returned vector are structs with two fields: `eqs` and
327327
`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e.
328328
`eqs => affect`.
329+
330+
Arguments:
331+
- `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
329332
"""
330333
function continuous_events(sys::AbstractSystem; toplevel = false)
334+
toplevel && (sys = recursive_get_parent(sys))
331335
cbs = get_continuous_events(sys)
332336
filter(!isempty, cbs)
333337
toplevel && return cbs
@@ -476,14 +480,18 @@ SymbolicDiscreteCallbacks(cbs::Vector{<:SymbolicDiscreteCallback}) = cbs
476480
SymbolicDiscreteCallbacks(::Nothing) = SymbolicDiscreteCallback[]
477481

478482
"""
479-
discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback}
483+
discrete_events(sys::AbstractSystem; toplevel = false) :: Vector{SymbolicDiscreteCallback}
480484
481485
Returns a vector of all the `discrete_events` in an abstract system and its component subsystems.
482486
The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and
483487
`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e.
484488
`condition => affect`.
489+
490+
Arguments:
491+
- `toplevel = false`: if set to true, do not return the discrete events of the subsystems.
485492
"""
486493
function discrete_events(sys::AbstractSystem; toplevel = false)
494+
toplevel && (sys = recursive_get_parent(sys))
487495
cbs = get_discrete_events(sys)
488496
toplevel && return cbs
489497
systems = get_systems(sys)

0 commit comments

Comments
 (0)