Skip to content

Commit 3e07adf

Browse files
committed
create separate _toplevel function versions isntead
1 parent 7867202 commit 3e07adf

File tree

2 files changed

+79
-46
lines changed

2 files changed

+79
-46
lines changed

src/systems/abstractsystem.jl

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,15 +1292,11 @@ $(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.
12981295
"""
1299-
function unknowns(sys::AbstractSystem; toplevel = false)
1300-
toplevel && (sys = recursive_get_parent(sys))
1296+
function unknowns(sys::AbstractSystem)
13011297
sts = get_unknowns(sys)
13021298
systems = get_systems(sys)
1303-
nonunique_unknowns = if toplevel || isempty(systems)
1299+
nonunique_unknowns = if isempty(systems)
13041300
sts
13051301
else
13061302
system_unknowns = reduce(vcat, namespace_variables.(systems))
@@ -1317,18 +1313,28 @@ function unknowns(sys::AbstractSystem; toplevel = false)
13171313
unique(nonunique_unknowns)
13181314
end
13191315

1316+
"""
1317+
unknowns_toplevel(sys::AbstractSystem)
1318+
1319+
Replicates the behaviour of `unknowns`, but ignores unknowns of subsystems.
1320+
"""
1321+
function unknowns_toplevel(sys::AbstractSystem)
1322+
if has_parent(sys) && (parent = get_parent(sys)) !== nothing
1323+
return unknowns_toplevel(parent)
1324+
end
1325+
return get_unknowns(sys)
1326+
end
1327+
1328+
13201329
"""
13211330
$(TYPEDSIGNATURES)
13221331
13231332
Get the parameters of the system `sys` and its subsystems.
13241333
13251334
See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref).
13261335
1327-
Arguments:
1328-
- `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
13291336
"""
1330-
function parameters(sys::AbstractSystem; initial_parameters = false, toplevel = false)
1331-
toplevel && (sys = recursive_get_parent(sys))
1337+
function parameters(sys::AbstractSystem; initial_parameters = false)
13321338
ps = get_ps(sys)
13331339
if ps == SciMLBase.NullParameters()
13341340
return []
@@ -1337,7 +1343,7 @@ function parameters(sys::AbstractSystem; initial_parameters = false, toplevel =
13371343
ps = first.(ps)
13381344
end
13391345
systems = get_systems(sys)
1340-
result = unique(toplevel || isempty(systems) ?
1346+
result = unique(isempty(systems) ?
13411347
ps : [ps; reduce(vcat, namespace_parameters.(systems))])
13421348
if !initial_parameters
13431349
if is_time_dependent(sys)
@@ -1362,19 +1368,15 @@ function dependent_parameters(sys::AbstractSystem)
13621368
end
13631369

13641370
"""
1365-
recursive_get_parent(sys::AbstractSystem)
1371+
parameters_toplevel(sys::AbstractSystem)
13661372
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.
1373+
Replicates the behaviour of `parameters`, but ignores parameters of subsystems.
13721374
"""
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)
1375+
function parameters_toplevel(sys::AbstractSystem)
1376+
if has_parent(sys) && (parent = get_parent(sys)) !== nothing
1377+
return parameters_toplevel(parent)
13761378
end
1377-
return sys
1379+
return get_ps(sys)
13781380
end
13791381

13801382
"""
@@ -1514,10 +1516,8 @@ function controls(sys::AbstractSystem)
15141516
isempty(systems) ? ctrls : [ctrls; reduce(vcat, namespace_controls.(systems))]
15151517
end
15161518

1517-
function observed(sys::AbstractSystem; toplevel = false)
1518-
toplevel && (sys = recursive_get_parent(sys))
1519+
function observed(sys::AbstractSystem)
15191520
obs = get_observed(sys)
1520-
toplevel && return obs
15211521
systems = get_systems(sys)
15221522
[obs;
15231523
reduce(vcat,
@@ -1536,7 +1536,7 @@ If they are not explicitly provided, variables and parameters are initialized to
15361536
15371537
See also [`initialization_equations`](@ref), [`parameter_dependencies`](@ref) and [`ModelingToolkit.get_defaults`](@ref).
15381538
"""
1539-
function defaults(sys::AbstractSystem; toplevel = false)
1539+
function defaults(sys::AbstractSystem)
15401540
systems = get_systems(sys)
15411541
defs = get_defaults(sys)
15421542
# `mapfoldr` is really important!!! We should prefer the base model for
@@ -1545,8 +1545,7 @@ function defaults(sys::AbstractSystem; toplevel = false)
15451545
# `compose(ODESystem(...; defaults=defs), ...)`
15461546
#
15471547
# Thus, right associativity is required and crucial for correctness.
1548-
(toplevel || isempty(systems)) ?
1549-
defs : mapfoldr(namespace_defaults, merge, systems; init = defs)
1548+
isempty(systems) ? defs : mapfoldr(namespace_defaults, merge, systems; init = defs)
15501549
end
15511550

15521551
function defaults_and_guesses(sys::AbstractSystem)
@@ -1576,14 +1575,11 @@ It is often the most useful way to inspect the equations of a system.
15761575
15771576
See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref).
15781577
1579-
Arguments:
1580-
- `toplevel = false`: if set to true, do not return the continuous events of the subsystems.
15811578
"""
1582-
function equations(sys::AbstractSystem; toplevel = false)
1583-
toplevel && (sys = recursive_get_parent(sys))
1579+
function equations(sys::AbstractSystem)
15841580
eqs = get_eqs(sys)
15851581
systems = get_systems(sys)
1586-
if toplevel || isempty(systems)
1582+
if isempty(systems)
15871583
return eqs
15881584
else
15891585
eqs = Equation[eqs;
@@ -1594,6 +1590,23 @@ function equations(sys::AbstractSystem; toplevel = false)
15941590
end
15951591
end
15961592

1593+
1594+
"""
1595+
equations_toplevel(sys::AbstractSystem)
1596+
1597+
Replicates the behaviour of `equations`, but ignores equations of subsystems.
1598+
1599+
Notes:
1600+
- Cannot be applied to non-complete systems.
1601+
"""
1602+
function equations_toplevel(sys::AbstractSystem)
1603+
iscomplete(sys) && error("Cannot apply `equations_toplevel` to complete systems.")
1604+
if has_parent(sys) && (parent = get_parent(sys)) !== nothing
1605+
return equations_toplevel(parent)
1606+
end
1607+
return get_eqs(sys)
1608+
end
1609+
15971610
"""
15981611
$(TYPEDSIGNATURES)
15991612

src/systems/callbacks.jl

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

322322
"""
323-
continuous_events(sys::AbstractSystem; toplevel = false)::Vector{SymbolicContinuousCallback}
323+
continuous_events(sys::AbstractSystem)::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.
332329
"""
333-
function continuous_events(sys::AbstractSystem; toplevel = false)
334-
toplevel && (sys = recursive_get_parent(sys))
330+
function continuous_events(sys::AbstractSystem)
335331
cbs = get_continuous_events(sys)
336332
filter(!isempty, cbs)
337-
toplevel && return cbs
338333

339334
systems = get_systems(sys)
340335
cbs = [cbs;
@@ -361,6 +356,21 @@ function vars!(vars, cb::SymbolicContinuousCallback; op = Differential)
361356
return vars
362357
end
363358

359+
"""
360+
continuous_events_toplevel(sys::AbstractSystem)
361+
362+
Replicates the behaviour of `continuous_events`, but ignores events of subsystems.
363+
364+
Notes:
365+
- Cannot be applied to non-complete systems.
366+
"""
367+
function continuous_events_toplevel(sys::AbstractSystem)
368+
if has_parent(sys) && (parent = get_parent(sys)) !== nothing
369+
return continuous_events_toplevel(parent)
370+
end
371+
return get_continuous_events(sys)
372+
end
373+
364374
#################################### discrete events #####################################
365375

366376
struct SymbolicDiscreteCallback
@@ -480,20 +490,15 @@ SymbolicDiscreteCallbacks(cbs::Vector{<:SymbolicDiscreteCallback}) = cbs
480490
SymbolicDiscreteCallbacks(::Nothing) = SymbolicDiscreteCallback[]
481491

482492
"""
483-
discrete_events(sys::AbstractSystem; toplevel = false) :: Vector{SymbolicDiscreteCallback}
493+
discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback}
484494
485495
Returns a vector of all the `discrete_events` in an abstract system and its component subsystems.
486496
The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and
487497
`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e.
488498
`condition => affect`.
489-
490-
Arguments:
491-
- `toplevel = false`: if set to true, do not return the discrete events of the subsystems.
492499
"""
493-
function discrete_events(sys::AbstractSystem; toplevel = false)
494-
toplevel && (sys = recursive_get_parent(sys))
500+
function discrete_events(sys::AbstractSystem)
495501
cbs = get_discrete_events(sys)
496-
toplevel && return cbs
497502
systems = get_systems(sys)
498503
cbs = [cbs;
499504
reduce(vcat,
@@ -524,6 +529,21 @@ function vars!(vars, cb::SymbolicDiscreteCallback; op = Differential)
524529
return vars
525530
end
526531

532+
"""
533+
discrete_events_toplevel(sys::AbstractSystem)
534+
535+
Replicates the behaviour of `discrete_events`, but ignores events of subsystems.
536+
537+
Notes:
538+
- Cannot be applied to non-complete systems.
539+
"""
540+
function discrete_events_toplevel(sys::AbstractSystem)
541+
if has_parent(sys) && (parent = get_parent(sys)) !== nothing
542+
return discrete_events_toplevel(parent)
543+
end
544+
return get_discrete_events(sys)
545+
end
546+
527547
################################# compilation functions ####################################
528548

529549
# handles ensuring that affect! functions work with integrator arguments

0 commit comments

Comments
 (0)