Skip to content

Commit ac75e77

Browse files
committed
Add parent to hierarchical systems and set it in structural_simplify
1 parent c4868e3 commit ac75e77

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

src/systems/abstractsystem.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ for prop in [:eqs
230230
:gui_metadata
231231
:discrete_subsystems
232232
:unknown_states
233-
:split_idxs]
233+
:split_idxs
234+
:parent]
234235
fname1 = Symbol(:get_, prop)
235236
fname2 = Symbol(:has_, prop)
236237
@eval begin
@@ -307,6 +308,9 @@ function Base.propertynames(sys::AbstractSystem; private = false)
307308
end
308309

309310
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys))
311+
if has_parent(sys) && (parent = get_parent(sys); parent !== nothing)
312+
sys = parent
313+
end
310314
wrap(getvar(sys, name; namespace = namespace))
311315
end
312316
function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys))
@@ -1185,7 +1189,7 @@ macro mtkbuild(expr)
11851189
name = named_expr.args[1]
11861190
esc(quote
11871191
$named_expr
1188-
$name = complete(structural_simplify($name))
1192+
$name = $structural_simplify($name)
11891193
end)
11901194
end
11911195

src/systems/diffeqs/odesystem.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ struct ODESystem <: AbstractODESystem
143143
split_idxs: a vector of vectors of indices for the split parameters.
144144
"""
145145
split_idxs::Union{Nothing, Vector{Vector{Int}}}
146+
"""
147+
parent: the hierarchical parent system before simplification.
148+
"""
149+
parent::Any
146150

147151
function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad,
148152
jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults,
@@ -151,7 +155,7 @@ struct ODESystem <: AbstractODESystem
151155
tearing_state = nothing,
152156
substitutions = nothing, complete = false,
153157
discrete_subsystems = nothing, unknown_states = nothing,
154-
split_idxs = nothing; checks::Union{Bool, Int} = true)
158+
split_idxs = nothing, parent = nothing; checks::Union{Bool, Int} = true)
155159
if checks == true || (checks & CheckComponents) > 0
156160
check_variables(dvs, iv)
157161
check_parameters(ps, iv)
@@ -165,7 +169,7 @@ struct ODESystem <: AbstractODESystem
165169
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, torn_matching,
166170
connector_type, preface, cevents, devents, metadata, gui_metadata,
167171
tearing_state, substitutions, complete, discrete_subsystems,
168-
unknown_states, split_idxs)
172+
unknown_states, split_idxs, parent)
169173
end
170174
end
171175

src/systems/diffeqs/sdesystem.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,17 @@ struct SDESystem <: AbstractODESystem
115115
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
116116
"""
117117
complete::Bool
118+
"""
119+
parent: the hierarchical parent system before simplification.
120+
"""
121+
parent::Any
118122

119123
function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed,
120124
tgrad,
121125
jac,
122126
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type,
123127
cevents, devents, metadata = nothing, gui_metadata = nothing,
124-
complete = false;
128+
complete = false, parent = nothing;
125129
checks::Union{Bool, Int} = true)
126130
if checks == true || (checks & CheckComponents) > 0
127131
check_variables(dvs, iv)
@@ -135,7 +139,7 @@ struct SDESystem <: AbstractODESystem
135139
new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac,
136140
ctrl_jac,
137141
Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents,
138-
metadata, gui_metadata, complete)
142+
metadata, gui_metadata, complete, parent)
139143
end
140144
end
141145

src/systems/discrete_system/discrete_system.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
8686
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
8787
"""
8888
complete::Bool
89+
"""
90+
parent: the hierarchical parent system before simplification.
91+
"""
92+
parent::Any
8993

9094
function DiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, ctrls,
9195
observed,
9296
name,
9397
systems, defaults, preface, connector_type,
9498
metadata = nothing, gui_metadata = nothing,
9599
tearing_state = nothing, substitutions = nothing,
96-
complete = false; checks::Union{Bool, Int} = true)
100+
complete = false, parent = nothing; checks::Union{Bool, Int} = true)
97101
if checks == true || (checks & CheckComponents) > 0
98102
check_variables(dvs, iv)
99103
check_parameters(ps, iv)
@@ -105,7 +109,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
105109
systems,
106110
defaults,
107111
preface, connector_type, metadata, gui_metadata,
108-
tearing_state, substitutions, complete)
112+
tearing_state, substitutions, complete, parent)
109113
end
110114
end
111115

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,23 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem
7575
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
7676
"""
7777
complete::Bool
78+
"""
79+
parent: the hierarchical parent system before simplification.
80+
"""
81+
parent::Any
7882

7983
function NonlinearSystem(tag, eqs, states, ps, var_to_name, observed, jac, name,
8084
systems,
8185
defaults, connector_type, metadata = nothing,
8286
gui_metadata = nothing,
8387
tearing_state = nothing, substitutions = nothing,
84-
complete = false; checks::Union{Bool, Int} = true)
88+
complete = false, parent = nothing; checks::Union{Bool, Int} = true)
8589
if checks == true || (checks & CheckUnits) > 0
8690
all_dimensionless([states; ps]) || check_units(eqs)
8791
end
8892
new(tag, eqs, states, ps, var_to_name, observed, jac, name, systems, defaults,
89-
connector_type, metadata, gui_metadata, tearing_state, substitutions, complete)
93+
connector_type, metadata, gui_metadata, tearing_state, substitutions, complete,
94+
parent)
9095
end
9196
end
9297

src/systems/optimization/optimizationsystem.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,23 @@ struct OptimizationSystem <: AbstractOptimizationSystem
5656
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
5757
"""
5858
complete::Bool
59+
"""
60+
parent: the hierarchical parent system before simplification.
61+
"""
62+
parent::Any
5963

6064
function OptimizationSystem(tag, op, states, ps, var_to_name, observed,
6165
constraints, name, systems, defaults, metadata = nothing,
62-
gui_metadata = nothing, complete = false;
66+
gui_metadata = nothing, complete = false, parent = nothing;
6367
checks::Union{Bool, Int} = true)
6468
if checks == true || (checks & CheckUnits) > 0
6569
unwrap(op) isa Symbolic && check_units(op)
6670
check_units(observed)
6771
all_dimensionless([states; ps]) || check_units(constraints)
6872
end
6973
new(tag, op, states, ps, var_to_name, observed,
70-
constraints, name, systems, defaults, metadata, gui_metadata, complete)
74+
constraints, name, systems, defaults, metadata, gui_metadata, complete,
75+
parent)
7176
end
7277
end
7378

src/systems/systems.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ This will convert all `inputs` to parameters and allow them to be unconnected, i
1717
simplification will allow models where `n_states = n_equations - n_inputs`.
1818
"""
1919
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
20+
kwargs...)
21+
newsys = __structural_simplify(sys, io; simplify, kwargs...)
22+
@set! newsys.parent = complete(sys)
23+
return complete(newsys)
24+
end
25+
function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
2026
kwargs...)
2127
sys = expand_connections(sys)
2228
sys isa DiscreteSystem && return sys

0 commit comments

Comments
 (0)