Skip to content

Commit e4866d4

Browse files
refactor: remove DelayParentScope
1 parent df470ef commit e4866d4

File tree

6 files changed

+53
-131
lines changed

6 files changed

+53
-131
lines changed

docs/src/basics/Composition.md

+6-14
Original file line numberDiff line numberDiff line change
@@ -135,42 +135,34 @@ sys.y = u * 1.1
135135
In a hierarchical system, variables of the subsystem get namespaced by the name of the system they are in. This prevents naming clashes, but also enforces that every unknown and parameter is local to the subsystem it is used in. In some cases it might be desirable to have variables and parameters that are shared between subsystems, or even global. This can be accomplished as follows.
136136

137137
```julia
138-
@parameters a b c d e f
138+
@parameters a b c d
139139

140140
# a is a local variable
141141
b = ParentScope(b) # b is a variable that belongs to one level up in the hierarchy
142142
c = ParentScope(ParentScope(c)) # ParentScope can be nested
143-
d = DelayParentScope(d) # skips one level before applying ParentScope
144-
e = DelayParentScope(e, 2) # second argument allows skipping N levels
145-
f = GlobalScope(f)
143+
d = GlobalScope(d)
146144

147-
p = [a, b, c, d, e, f]
145+
p = [a, b, c, d]
148146

149147
level0 = ODESystem(Equation[], t, [], p; name = :level0)
150148
level1 = ODESystem(Equation[], t, [], []; name = :level1) level0
151149
parameters(level1)
152150
#level0₊a
153151
#b
154152
#c
155-
#level0₊d
156-
#level0₊e
157-
#f
153+
#d
158154
level2 = ODESystem(Equation[], t, [], []; name = :level2) level1
159155
parameters(level2)
160156
#level1₊level0₊a
161157
#level1₊b
162158
#c
163-
#level0₊d
164-
#level1₊level0₊e
165-
#f
159+
#d
166160
level3 = ODESystem(Equation[], t, [], []; name = :level3) level2
167161
parameters(level3)
168162
#level2₊level1₊level0₊a
169163
#level2₊level1₊b
170164
#level2₊c
171-
#level2₊level0₊d
172-
#level1₊level0₊e
173-
#f
165+
#d
174166
```
175167

176168
## Structural Simplify

src/ModelingToolkit.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export PDESystem
296296
export Differential, expand_derivatives, @derivatives
297297
export Equation, ConstrainedEquation
298298
export Term, Sym
299-
export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope
299+
export SymScope, LocalScope, ParentScope, GlobalScope
300300
export independent_variable, equations, controls, observed, full_equations
301301
export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy
302302
export structural_simplify, expand_connections, linearize, linearization_function,

src/systems/abstractsystem.jl

-58
Original file line numberDiff line numberDiff line change
@@ -1189,55 +1189,6 @@ function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}})
11891189
end
11901190
end
11911191

1192-
"""
1193-
$(TYPEDEF)
1194-
1195-
Denotes that a variable belongs to a system that is at least `N + 1` levels up in the
1196-
hierarchy from the system whose equations it is involved in. It is namespaced by the
1197-
first `N` parents and not namespaced by the `N+1`th parent in the hierarchy. The scope
1198-
of the variable after this point is given by `parent`.
1199-
1200-
In other words, this scope delays applying `ParentScope` by `N` levels, and applies
1201-
`LocalScope` in the meantime.
1202-
1203-
# Fields
1204-
1205-
$(TYPEDFIELDS)
1206-
"""
1207-
struct DelayParentScope <: SymScope
1208-
parent::SymScope
1209-
N::Int
1210-
end
1211-
1212-
"""
1213-
$(TYPEDSIGNATURES)
1214-
1215-
Apply `DelayParentScope` to `sym`, with a delay of `N` and `parent` being `LocalScope`.
1216-
"""
1217-
function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N)
1218-
Base.depwarn(
1219-
"`DelayParentScope` is deprecated and will be removed soon", :DelayParentScope)
1220-
apply_to_variables(sym) do sym
1221-
if iscall(sym) && operation(sym) == getindex
1222-
args = arguments(sym)
1223-
a1 = setmetadata(args[1], SymScope,
1224-
DelayParentScope(getmetadata(value(args[1]), SymScope, LocalScope()), N))
1225-
maketerm(typeof(sym), operation(sym), [a1, args[2:end]...],
1226-
metadata(sym))
1227-
else
1228-
setmetadata(sym, SymScope,
1229-
DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N))
1230-
end
1231-
end
1232-
end
1233-
1234-
"""
1235-
$(TYPEDSIGNATURES)
1236-
1237-
Apply `DelayParentScope` to `sym`, with a delay of `1` and `parent` being `LocalScope`.
1238-
"""
1239-
DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) = DelayParentScope(sym, 1)
1240-
12411192
"""
12421193
$(TYPEDEF)
12431194
@@ -1290,15 +1241,6 @@ function renamespace(sys, x)
12901241
rename(x, renamespace(getname(sys), getname(x)))::T
12911242
elseif scope isa ParentScope
12921243
setmetadata(x, SymScope, scope.parent)::T
1293-
elseif scope isa DelayParentScope
1294-
if scope.N > 0
1295-
x = setmetadata(x, SymScope,
1296-
DelayParentScope(scope.parent, scope.N - 1))
1297-
rename(x, renamespace(getname(sys), getname(x)))::T
1298-
else
1299-
#rename(x, renamespace(getname(sys), getname(x)))
1300-
setmetadata(x, SymScope, scope.parent)::T
1301-
end
13021244
else # GlobalScope
13031245
x::T
13041246
end

src/utils.jl

-2
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,6 @@ function check_scope_depth(scope, depth)
632632
return depth == 0
633633
elseif scope isa ParentScope
634634
return depth > 0 && check_scope_depth(scope.parent, depth - 1)
635-
elseif scope isa DelayParentScope
636-
return depth >= scope.N && check_scope_depth(scope.parent, depth - scope.N - 1)
637635
elseif scope isa GlobalScope
638636
return depth == -1
639637
end

test/jumpsystem.jl

+10-12
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,20 @@ end
378378

379379
# scoping tests
380380
let
381-
@variables x1(t) x2(t) x3(t) x4(t) x5(t)
381+
@variables x1(t) x2(t) x3(t) x4(t)
382382
x2 = ParentScope(x2)
383383
x3 = ParentScope(ParentScope(x3))
384-
x4 = DelayParentScope(x4)
385-
x5 = GlobalScope(x5)
386-
@parameters p1 p2 p3 p4 p5
384+
x4 = GlobalScope(x4)
385+
@parameters p1 p2 p3 p4
387386
p2 = ParentScope(p2)
388387
p3 = ParentScope(ParentScope(p3))
389-
p4 = DelayParentScope(p4)
390-
p5 = GlobalScope(p5)
388+
p4 = GlobalScope(p4)
391389

392390
j1 = ConstantRateJump(p1, [x1 ~ x1 + 1])
393391
j2 = MassActionJump(p2, [x2 => 1], [x3 => -1])
394392
j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1])
395-
j4 = MassActionJump(p4 * p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1])
396-
@named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4, x5], [p1, p2, p3, p4, p5])
393+
j4 = MassActionJump(p4 * p4, [x1 => 1, x4 => 1], [x1 => -1, x4 => -1, x2 => 1])
394+
@named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4], [p1, p2, p3, p4])
397395

398396
us = Set()
399397
ps = Set()
@@ -414,13 +412,13 @@ let
414412

415413
empty!.((us, ps))
416414
MT.collect_scoped_vars!(us, ps, js, iv; depth = 2)
417-
@test issetequal(us, [x3, x4])
418-
@test issetequal(ps, [p3, p4])
415+
@test issetequal(us, [x3])
416+
@test issetequal(ps, [p3])
419417

420418
empty!.((us, ps))
421419
MT.collect_scoped_vars!(us, ps, js, iv; depth = -1)
422-
@test issetequal(us, [x5])
423-
@test issetequal(ps, [p5])
420+
@test issetequal(us, [x4])
421+
@test issetequal(ps, [p4])
424422
end
425423

426424
# PDMP test

test/variable_scope.jl

+36-44
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,11 @@ end
5151
@test renamed([:foo :bar :baz], c) == Symbol("foo₊c")
5252
@test renamed([:foo :bar :baz], d) == :d
5353

54-
@parameters a b c d e f
54+
@parameters a b c d
5555
p = [a
5656
ParentScope(b)
5757
ParentScope(ParentScope(c))
58-
DelayParentScope(d)
59-
DelayParentScope(e, 2)
60-
GlobalScope(f)]
58+
GlobalScope(d)]
6159

6260
level0 = ODESystem(Equation[], t, [], p; name = :level0)
6361
level1 = ODESystem(Equation[], t, [], []; name = :level1) level0
@@ -69,9 +67,7 @@ ps = ModelingToolkit.getname.(parameters(level3))
6967
@test isequal(ps[1], :level2₊level1₊level0₊a)
7068
@test isequal(ps[2], :level2₊level1₊b)
7169
@test isequal(ps[3], :level2₊c)
72-
@test isequal(ps[4], :level2₊level0₊d)
73-
@test isequal(ps[5], :level1₊level0₊e)
74-
@test isequal(ps[6], :f)
70+
@test isequal(ps[4], :d)
7571

7672
# Issue@2252
7773
# Tests from PR#2354
@@ -102,40 +98,36 @@ defs = ModelingToolkit.defaults(bar)
10298
@test defs[bar.p] == 2
10399
@test isequal(defs[bar.foo.p], bar.p)
104100

105-
# Issue#3101
106-
@variables x1(t) x2(t) x3(t) x4(t) x5(t)
107-
x2 = ParentScope(x2)
108-
x3 = ParentScope(ParentScope(x3))
109-
x4 = DelayParentScope(x4)
110-
x5 = GlobalScope(x5)
111-
@parameters p1 p2 p3 p4 p5
112-
p2 = ParentScope(p2)
113-
p3 = ParentScope(ParentScope(p3))
114-
p4 = DelayParentScope(p4)
115-
p5 = GlobalScope(p5)
116-
117-
@named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4, D(x5) ~ p5], t)
118-
@test isequal(x1, only(unknowns(sys1)))
119-
@test isequal(p1, only(parameters(sys1)))
120-
@named sys2 = ODESystem(Equation[], t; systems = [sys1])
121-
@test length(unknowns(sys2)) == 2
122-
@test any(isequal(x2), unknowns(sys2))
123-
@test length(parameters(sys2)) == 2
124-
@test any(isequal(p2), parameters(sys2))
125-
@named sys3 = ODESystem(Equation[], t)
126-
sys3 = sys3 sys2
127-
@test length(unknowns(sys3)) == 4
128-
@test any(isequal(x3), unknowns(sys3))
129-
@test any(isequal(ModelingToolkit.renamespace(sys1, x4)), unknowns(sys3))
130-
@test length(parameters(sys3)) == 4
131-
@test any(isequal(p3), parameters(sys3))
132-
@test any(isequal(ModelingToolkit.renamespace(sys1, p4)), parameters(sys3))
133-
sys4 = complete(sys3)
134-
@test length(unknowns(sys3)) == 4
135-
@test length(parameters(sys4)) == 5
136-
@test any(isequal(p5), parameters(sys4))
137-
sys5 = structural_simplify(sys3)
138-
@test length(unknowns(sys5)) == 5
139-
@test any(isequal(x5), unknowns(sys5))
140-
@test length(parameters(sys5)) == 5
141-
@test any(isequal(p5), parameters(sys5))
101+
@testset "Issue#3101" begin
102+
@variables x1(t) x2(t) x3(t) x4(t)
103+
x2 = ParentScope(x2)
104+
x3 = ParentScope(ParentScope(x3))
105+
x4 = GlobalScope(x4)
106+
@parameters p1 p2 p3 p4
107+
p2 = ParentScope(p2)
108+
p3 = ParentScope(ParentScope(p3))
109+
p4 = GlobalScope(p4)
110+
111+
@named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4], t)
112+
@test isequal(x1, only(unknowns(sys1)))
113+
@test isequal(p1, only(parameters(sys1)))
114+
@named sys2 = ODESystem(Equation[], t; systems = [sys1])
115+
@test length(unknowns(sys2)) == 2
116+
@test any(isequal(x2), unknowns(sys2))
117+
@test length(parameters(sys2)) == 2
118+
@test any(isequal(p2), parameters(sys2))
119+
@named sys3 = ODESystem(Equation[], t)
120+
sys3 = sys3 sys2
121+
@test length(unknowns(sys3)) == 3
122+
@test any(isequal(x3), unknowns(sys3))
123+
@test length(parameters(sys3)) == 3
124+
@test any(isequal(p3), parameters(sys3))
125+
sys4 = complete(sys3)
126+
@test length(unknowns(sys4)) == 3
127+
@test length(parameters(sys4)) == 4
128+
sys5 = structural_simplify(sys3)
129+
@test length(unknowns(sys5)) == 4
130+
@test any(isequal(x4), unknowns(sys5))
131+
@test length(parameters(sys5)) == 4
132+
@test any(isequal(p4), parameters(sys5))
133+
end

0 commit comments

Comments
 (0)