Skip to content

Commit b72a0ed

Browse files
authored
Merge pull request #1863 from SciML/myb/complete
Define `complete` for hierarchical systems
2 parents 67d9559 + 5c31d08 commit b72a0ed

File tree

11 files changed

+65
-21
lines changed

11 files changed

+65
-21
lines changed

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function parameters end
111111

112112
# this has to be included early to deal with depency issues
113113
include("structural_transformation/bareiss.jl")
114+
function complete end
114115
include("bipartite_graph.jl")
115116
using .BipartiteGraphs
116117

@@ -210,7 +211,7 @@ export simplify, substitute
210211
export build_function
211212
export modelingtoolkitize
212213
export @variables, @parameters
213-
export @named, @nonamespace, @namespace, extend, compose
214+
export @named, @nonamespace, @namespace, extend, compose, complete
214215
export debug_system
215216

216217
end # module

src/bipartite_graph.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module BipartiteGraphs
22

3+
import ModelingToolkit: complete
4+
35
export BipartiteEdge, BipartiteGraph, DiCMOBiGraph, Unassigned, unassigned,
46
Matching, ResidualCMOGraph, InducedCondensationGraph, maximal_matching,
57
construct_augmenting_path!, MatchedCondensationGraph
68

79
export 𝑠vertices, 𝑑vertices, has_𝑠vertex, has_𝑑vertex, 𝑠neighbors, 𝑑neighbors,
810
𝑠edges, 𝑑edges, nsrcs, ndsts, SRC, DST, set_neighbors!, invview,
9-
complete, delete_srcs!, delete_dsts!
11+
delete_srcs!, delete_dsts!
1012

1113
using DocStringExtensions
1214
using UnPack

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di
2525
AliasGraph, filter_kwargs, lower_varname, setio
2626

2727
using ModelingToolkit.BipartiteGraphs
28-
import .BipartiteGraphs: invview
28+
import .BipartiteGraphs: invview, complete
2929
using Graphs
3030
using ModelingToolkit.SystemStructures
3131
using ModelingToolkit.SystemStructures: algeqs, EquationsView

src/systems/abstractsystem.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ independent_variables(sys::AbstractTimeDependentSystem) = [getfield(sys, :iv)]
162162
independent_variables(sys::AbstractTimeIndependentSystem) = []
163163
independent_variables(sys::AbstractMultivariateSystem) = getfield(sys, :ivs)
164164

165+
iscomplete(sys::AbstractSystem) = isdefined(sys, :complete) && getfield(sys, :complete)
166+
167+
"""
168+
$(TYPEDSIGNATURES)
169+
170+
Mark a system as completed. If a system is complete, the system will no longer
171+
namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
172+
"""
173+
function complete(sys::AbstractSystem)
174+
isdefined(sys, :complete) ? (@set! sys.complete = true) : sys
175+
end
176+
165177
for prop in [:eqs
166178
:noiseeqs
167179
:iv
@@ -266,10 +278,10 @@ function Base.propertynames(sys::AbstractSystem; private = false)
266278
end
267279
end
268280

269-
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace = true)
281+
function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys))
270282
wrap(getvar(sys, name; namespace = namespace))
271283
end
272-
function getvar(sys::AbstractSystem, name::Symbol; namespace = false)
284+
function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys))
273285
systems = get_systems(sys)
274286
if isdefined(sys, name)
275287
Base.depwarn("`sys.name` like `sys.$name` is deprecated. Use getters like `get_$name` instead.",

src/systems/diffeqs/odesystem.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,16 @@ struct ODESystem <: AbstractODESystem
115115
substitutions: substitutions generated by tearing.
116116
"""
117117
substitutions::Any
118+
"""
119+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
120+
"""
121+
complete::Bool
118122

119123
function ODESystem(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad,
120124
jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults,
121125
torn_matching, connector_type, preface, cevents,
122126
devents, metadata = nothing, tearing_state = nothing,
123-
substitutions = nothing;
127+
substitutions = nothing, complete = false;
124128
checks::Union{Bool, Int} = true)
125129
if checks == true || (checks & CheckComponents) > 0
126130
check_variables(dvs, iv)
@@ -134,7 +138,7 @@ struct ODESystem <: AbstractODESystem
134138
new(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac,
135139
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, torn_matching,
136140
connector_type, preface, cevents, devents, metadata, tearing_state,
137-
substitutions)
141+
substitutions, complete)
138142
end
139143
end
140144

src/systems/diffeqs/sdesystem.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ struct SDESystem <: AbstractODESystem
100100
metadata: metadata for the system, to be used by downstream packages.
101101
"""
102102
metadata::Any
103+
"""
104+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
105+
"""
106+
complete::Bool
107+
103108
function SDESystem(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac,
104109
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type,
105-
cevents, devents, metadata = nothing;
110+
cevents, devents, metadata = nothing, complete = false;
106111
checks::Union{Bool, Int} = true)
107112
if checks == true || (checks & CheckComponents) > 0
108113
check_variables(dvs, iv)
@@ -115,7 +120,7 @@ struct SDESystem <: AbstractODESystem
115120
end
116121
new(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac,
117122
Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents,
118-
metadata)
123+
metadata, complete)
119124
end
120125
end
121126

src/systems/discrete_system/discrete_system.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
7171
substitutions: substitutions generated by tearing.
7272
"""
7373
substitutions::Any
74+
"""
75+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
76+
"""
77+
complete::Bool
7478

7579
function DiscreteSystem(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name,
7680
systems, defaults, preface, connector_type,
7781
metadata = nothing,
78-
tearing_state = nothing, substitutions = nothing;
79-
checks::Union{Bool, Int} = true)
82+
tearing_state = nothing, substitutions = nothing,
83+
complete = false; checks::Union{Bool, Int} = true)
8084
if checks == true || (checks & CheckComponents) > 0
8185
check_variables(dvs, iv)
8286
check_parameters(ps, iv)
@@ -85,7 +89,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
8589
all_dimensionless([dvs; ps; iv; ctrls]) || check_units(discreteEqs)
8690
end
8791
new(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name, systems, defaults,
88-
preface, connector_type, metadata, tearing_state, substitutions)
92+
preface, connector_type, metadata, tearing_state, substitutions, complete)
8993
end
9094
end
9195

src/systems/jumps/jumpsystem.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
8787
metadata: metadata for the system, to be used by downstream packages.
8888
"""
8989
metadata::Any
90+
"""
91+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
92+
"""
93+
complete::Bool
94+
9095
function JumpSystem{U}(ap::U, iv, states, ps, var_to_name, observed, name, systems,
9196
defaults, connector_type, devents,
92-
metadata = nothing;
97+
metadata = nothing, complete = false;
9398
checks::Union{Bool, Int} = true) where {U <: ArrayPartition}
9499
if checks == true || (checks & CheckComponents) > 0
95100
check_variables(states, iv)
@@ -99,7 +104,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
99104
all_dimensionless([states; ps; iv]) || check_units(ap, iv)
100105
end
101106
new{U}(ap, iv, states, ps, var_to_name, observed, name, systems, defaults,
102-
connector_type, devents, metadata)
107+
connector_type, devents, metadata, complete)
103108
end
104109
end
105110

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem
6262
substitutions: substitutions generated by tearing.
6363
"""
6464
substitutions::Any
65+
"""
66+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
67+
"""
68+
complete::Bool
6569

6670
function NonlinearSystem(eqs, states, ps, var_to_name, observed, jac, name, systems,
6771
defaults, connector_type, metadata = nothing,
68-
tearing_state = nothing, substitutions = nothing;
69-
checks::Union{Bool, Int} = true)
72+
tearing_state = nothing, substitutions = nothing,
73+
complete = false; checks::Union{Bool, Int} = true)
7074
if checks == true || (checks & CheckUnits) > 0
7175
all_dimensionless([states; ps]) || check_units(eqs)
7276
end
7377
new(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults,
74-
connector_type, metadata, tearing_state, substitutions)
78+
connector_type, metadata, tearing_state, substitutions, complete)
7579
end
7680
end
7781

src/systems/optimization/optimizationsystem.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ struct OptimizationSystem <: AbstractTimeIndependentSystem
4141
metadata: metadata for the system, to be used by downstream packages.
4242
"""
4343
metadata::Any
44+
"""
45+
complete: if a model `sys` is complete, then `sys.x` no longer performs namespacing.
46+
"""
47+
complete::Bool
48+
4449
function OptimizationSystem(op, states, ps, var_to_name, observed,
45-
constraints, name, systems, defaults, metadata = nothing;
46-
checks::Union{Bool, Int} = true)
50+
constraints, name, systems, defaults, metadata = nothing,
51+
complete = false; checks::Union{Bool, Int} = true)
4752
if checks == true || (checks & CheckUnits) > 0
4853
unwrap(op) isa Symbolic && check_units(op)
4954
check_units(observed)
5055
all_dimensionless([states; ps]) || check_units(constraints)
5156
end
5257
new(op, states, ps, var_to_name, observed,
53-
constraints, name, systems, defaults, metadata)
58+
constraints, name, systems, defaults, metadata, complete)
5459
end
5560
end
5661

@@ -143,7 +148,7 @@ namespace_constraint(eq::Equation, sys) = namespace_equation(eq, sys)
143148
# _lhs = namespace_expr(ineq.lhs, sys, n)
144149
# _rhs = namespace_expr(ineq.rhs, sys, n)
145150
# Inequality(
146-
# namespace_expr(_lhs, sys, n),
151+
# namespace_expr(_lhs, sys, n),
147152
# namespace_expr(_rhs, sys, n),
148153
# ineq.relational_op,
149154
# )

0 commit comments

Comments
 (0)