Skip to content

Commit 2bc1930

Browse files
committed
Use structural_simplify in reduction tests
1 parent 76cc57a commit 2bc1930

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

test/reduction.jl

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ eqs = [
3939

4040
lorenz1 = ODESystem(eqs,t,name=:lorenz1)
4141

42-
lorenz1_aliased = alias_elimination(lorenz1)
42+
lorenz1_aliased = structural_simplify(lorenz1)
4343
io = IOBuffer(); show(io, lorenz1_aliased); str = String(take!(io))
44-
@test all(s->occursin(s, str), ["lorenz1", "States (3)", "Parameters (3)"])
44+
@test all(s->occursin(s, str), ["lorenz1", "States (2)", "Parameters (3)"])
4545
reduced_eqs = [
4646
D(x) ~ σ*(y - x)
47-
D(y) ~ β + x*- z) - y
48-
0 ~ y + z - x
47+
D(y) ~ β + x*- (x - y)) - y
4948
]
5049
test_equal.(equations(lorenz1_aliased), reduced_eqs)
5150
@test isempty(setdiff(states(lorenz1_aliased), [x, y, z]))
5251
test_equal.(observed(lorenz1_aliased), [
53-
u ~ 0,
54-
a ~ -z,
52+
u ~ 0
53+
a ~ -z
54+
z ~ x - y
5555
])
5656

5757
# Multi-System Reduction
@@ -77,7 +77,7 @@ connected = ODESystem([s ~ a + lorenz1.x
7777

7878
# Reduced Flattened System
7979

80-
reduced_system = alias_elimination(connected)
80+
reduced_system = structural_simplify(connected)
8181

8282
@test setdiff(states(reduced_system), [
8383
s
@@ -102,23 +102,23 @@ reduced_system = alias_elimination(connected)
102102
]) |> isempty
103103

104104
reduced_eqs = [
105-
0 ~ a + lorenz1.x - (s)
106-
0 ~ s - (lorenz2.y)
107-
D(lorenz1.x) ~ lorenz2.u + lorenz1.σ*((lorenz1.y) - (lorenz1.x))
108-
D(lorenz1.y) ~ lorenz1.x*(lorenz1.ρ - (lorenz1.z)) - (lorenz1.u)
105+
D(lorenz1.x) ~ lorenz1.σ*((lorenz1.y) - (lorenz1.x)) - ((lorenz2.z) - (lorenz2.x) - (lorenz2.y))
106+
D(lorenz1.y) ~ lorenz1.z + lorenz1.x*(lorenz1.ρ - (lorenz1.z)) - (lorenz1.x) - (lorenz1.y)
109107
D(lorenz1.z) ~ lorenz1.x*lorenz1.y - (lorenz1.β*(lorenz1.z))
110-
0 ~ lorenz1.x + lorenz1.y - (lorenz1.u) - (lorenz1.z)
111-
D(lorenz2.x) ~ lorenz1.u + lorenz2.σ*((lorenz2.y) - (lorenz2.x))
112-
D(lorenz2.y) ~ lorenz2.x*(lorenz2.ρ - (lorenz2.z)) - (lorenz2.u)
108+
D(lorenz2.x) ~ lorenz2.σ*((lorenz2.y) - (lorenz2.x)) - ((lorenz1.z) - (lorenz1.x) - (lorenz1.y))
109+
D(lorenz2.y) ~ lorenz2.z + lorenz2.x*(lorenz2.ρ - (lorenz2.z)) - (lorenz2.x) - (lorenz2.y)
113110
D(lorenz2.z) ~ lorenz2.x*lorenz2.y - (lorenz2.β*(lorenz2.z))
114-
0 ~ lorenz2.x + lorenz2.y - (lorenz2.u) - (lorenz2.z)
115111
]
116112

117113
test_equal.(equations(reduced_system), reduced_eqs)
118114

119115
observed_eqs = [
120116
lorenz2.F ~ lorenz1.u
121117
lorenz1.F ~ lorenz2.u
118+
s ~ lorenz2.y
119+
a ~ s - (lorenz1.x)
120+
lorenz2.u ~ -((lorenz2.z) - (lorenz2.x) - (lorenz2.y))
121+
lorenz1.u ~ -((lorenz1.z) - (lorenz1.x) - (lorenz1.y))
122122
]
123123
test_equal.(observed(reduced_system), observed_eqs)
124124

@@ -131,16 +131,12 @@ pp = [
131131
lorenz2.β => 8/3
132132
]
133133
u0 = [
134-
a => 1.0
135-
s => 1.0
136134
lorenz1.x => 1.0
137135
lorenz1.y => 0.0
138136
lorenz1.z => 0.0
139-
lorenz1.u => 0.0
140137
lorenz2.x => 1.0
141138
lorenz2.y => 0.0
142139
lorenz2.z => 0.0
143-
lorenz2.u => 0.0
144140
]
145141
prob1 = ODEProblem(reduced_system, u0, (0.0, 100.0), pp)
146142
solve(prob1, Rodas5())
@@ -149,10 +145,10 @@ solve(prob1, Rodas5())
149145
let
150146
@parameters t
151147
D = Differential(t)
152-
@variables x(t), u(t), y(t)
153-
@parameters a, b, c, d
148+
@variables x(t) u(t) y(t)
149+
@parameters a b c d
154150
ol = ODESystem([D(x) ~ a * x + b * u; y ~ c * x + d * u], t, name=:ol)
155-
@variables u_c(t), y_c(t)
151+
@variables u_c(t) y_c(t)
156152
@parameters k_P
157153
pc = ODESystem(Equation[u_c ~ k_P * y_c], t, name=:pc)
158154
connections = [
@@ -161,11 +157,10 @@ let
161157
]
162158
connected = ODESystem(connections, t, systems=[ol, pc])
163159
@test equations(connected) isa Vector{Equation}
164-
reduced_sys = alias_elimination(connected)
160+
reduced_sys = structural_simplify(connected)
165161
ref_eqs = [
166162
D(ol.x) ~ ol.a*ol.x + ol.b*pc.u_c
167-
0 ~ ol.c*ol.x + ol.d*pc.u_c - ol.y
168-
0 ~ pc.k_P*ol.y - pc.u_c
163+
0 ~ -pc.u_c - (pc.k_P*((-ol.c*(ol.x)) - (ol.d*(pc.u_c))))
169164
]
170165
@test ref_eqs == equations(reduced_sys)
171166
end
@@ -180,8 +175,8 @@ eqs = [
180175
u3 ~ hypot(u1, u2) * p
181176
]
182177
sys = NonlinearSystem(eqs, [u1, u2, u3], [p])
183-
reducedsys = alias_elimination(sys)
184-
@test observed(reducedsys) == [u1 ~ u2]
178+
reducedsys = structural_simplify(sys)
179+
@test observed(reducedsys) == [u1 ~ u2; u2 ~ 0.5(u3 - p)]
185180

186181
u0 = [
187182
u1 => 1

0 commit comments

Comments
 (0)