Skip to content

Commit 070de8c

Browse files
test: fix odesystem tests
1 parent 1476779 commit 070de8c

File tree

1 file changed

+33
-65
lines changed

1 file changed

+33
-65
lines changed

test/odesystem.jl

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ ssort(eqs) = sort(eqs, by = string)
3737
@test eval(toexpr(de)) == de
3838
@test hash(deepcopy(de)) == hash(de)
3939

40-
generate_function(de)
41-
4240
function test_diffeq_inference(name, sys, iv, dvs, ps)
4341
@testset "System construction: $name" begin
4442
@test isequal(independent_variables(sys)[1], value(iv))
@@ -49,13 +47,12 @@ function test_diffeq_inference(name, sys, iv, dvs, ps)
4947
end
5048

5149
test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β])
52-
generate_function(de, [x, y, z], [σ, ρ, β])
5350
jac_expr = generate_jacobian(de)
5451
jac = calculate_jacobian(de)
5552
jacfun = eval(jac_expr[2])
5653

5754
de = complete(de)
58-
f = ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true)
55+
f = ODEFunction(de, tgrad = true, jac = true)
5956
# system
6057
@test f.sys === de
6158

@@ -86,40 +83,14 @@ f.jac(J, u, p, t)
8683
@test J == f.jac(u, p, t)
8784

8885
#check iip_config
89-
f = ODEFunction(de, [x, y, z], [σ, ρ, β], iip_config = (false, true))
86+
f = ODEFunction(de; iip_config = (false, true))
9087
du = zeros(3)
9188
u = collect(1:3)
9289
p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0)
9390
f.f(du, u, p, 0.1)
9491
@test du == [4, 0, -16]
9592
@test_throws ArgumentError f.f(u, p, 0.1)
9693

97-
#check iip
98-
f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β]))
99-
f2 = ODEFunction(de, [x, y, z], [σ, ρ, β])
100-
@test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2)
101-
@test SciMLBase.specialization(f) === SciMLBase.specialization(f2)
102-
for iip in (true, false)
103-
f = eval(ODEFunctionExpr{iip}(de, [x, y, z], [σ, ρ, β]))
104-
f2 = ODEFunction{iip}(de, [x, y, z], [σ, ρ, β])
105-
@test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip
106-
@test SciMLBase.specialization(f) === SciMLBase.specialization(f2)
107-
108-
for specialize in (SciMLBase.AutoSpecialize, SciMLBase.FullSpecialize)
109-
f = eval(ODEFunctionExpr{iip, specialize}(de, [x, y, z], [σ, ρ, β]))
110-
f2 = ODEFunction{iip, specialize}(de, [x, y, z], [σ, ρ, β])
111-
@test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip
112-
@test SciMLBase.specialization(f) === SciMLBase.specialization(f2) === specialize
113-
end
114-
end
115-
116-
#check sparsity
117-
f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = true))
118-
@test f.sparsity == ModelingToolkit.jacobian_sparsity(de)
119-
120-
f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = false))
121-
@test isnothing(f.sparsity)
122-
12394
eqs = [D(x) ~ σ * (y - x),
12495
D(y) ~ x *- z) - y * t,
12596
D(z) ~ x * y - β * z * κ]
@@ -142,45 +113,44 @@ eqs = [D(x) ~ σ(t - 1) * (y - x),
142113
D(z) ~ x * y - β * z * κ]
143114
@named de = System(eqs, t)
144115
test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β))
145-
f = generate_function(de, [x, y, z], [σ, ρ, β], expression = Val{false})[2]
116+
f = generate_rhs(de, [x, y, z], [σ, ρ, β], expression = Val{false})
146117
du = [0.0, 0.0, 0.0]
147118
f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0)
148119
@test du [11, -3, -7]
149120

150121
eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)]
151122
@named de = System(eqs, t)
152123
test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,))
153-
f = generate_function(de, [x], [σ], expression = Val{false})[2]
124+
f = generate_rhs(de, [x], [σ], expression = Val{false})
154125
du = [0.0]
155126
f(du, [1.0], [t -> t + 2], 5.0)
156127
@test du [27561]
157128

158-
# Conversion to first-order ODEs #17
159-
D3 = D^3
160-
D2 = D^2
161-
@variables u(t) uˍtt(t) uˍt(t) xˍt(t)
162-
eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1
163-
D2(x) ~ D(x) + 2]
164-
@named de = System(eqs, t)
165-
de1 = ode_order_lowering(de)
166-
lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1
167-
D(xˍt) ~ xˍt + 2
168-
D(uˍt) ~ uˍtt
169-
D(u) ~ uˍt
170-
D(x) ~ xˍt]
171-
172-
#@test de1 == System(lowered_eqs)
173-
174-
# issue #219
175-
@test all(isequal.(
176-
[ModelingToolkit.var_from_nested_derivative(eq.lhs)[1]
177-
for eq in equations(de1)],
178-
unknowns(@named lowered = System(lowered_eqs, t))))
179-
180-
test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], [])
181-
du = zeros(5)
182-
ODEFunction(complete(de1), [uˍtt, xˍt, uˍt, u, x], [])(du, ones(5), nothing, 0.1)
183-
@test du == [5.0, 3.0, 1.0, 1.0, 1.0]
129+
@testset "Issue#17: Conversion to first order ODEs" begin
130+
D3 = D^3
131+
D2 = D^2
132+
@variables u(t) uˍtt(t) uˍt(t) xˍt(t)
133+
eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1
134+
D2(x) ~ D(x) + 2]
135+
@named de = System(eqs, t)
136+
de1 = ode_order_lowering(de)
137+
138+
@testset "Issue#219: Ordering of equations in `ode_order_lowering`" begin
139+
lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1
140+
D(xˍt) ~ xˍt + 2
141+
D(uˍt) ~ uˍtt
142+
D(u) ~ uˍt
143+
D(x) ~ xˍt]
144+
@test isequal(
145+
[ModelingToolkit.var_from_nested_derivative(eq.lhs)[1] for eq in equations(de1)],
146+
unknowns(@named lowered = System(lowered_eqs, t)))
147+
end
148+
149+
test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], [])
150+
du = zeros(5)
151+
ODEFunction(complete(de1))(du, ones(5), nothing, 0.1)
152+
@test du == [5.0, 3.0, 1.0, 1.0, 1.0]
153+
end
184154

185155
# Internal calculations
186156
@parameters σ
@@ -189,12 +159,11 @@ eqs = [D(x) ~ σ * a,
189159
D(y) ~ x *- z) - y,
190160
D(z) ~ x * y - β * z * κ]
191161
@named de = System(eqs, t)
192-
generate_function(de, [x, y, z], [σ, ρ, β])
193162
jac = calculate_jacobian(de)
194163
@test ModelingToolkit.jacobian_sparsity(de).colptr == sparse(jac).colptr
195164
@test ModelingToolkit.jacobian_sparsity(de).rowval == sparse(jac).rowval
196165

197-
f = ODEFunction(complete(de), [x, y, z], [σ, ρ, β])
166+
f = ODEFunction(complete(de))
198167

199168
@parameters A B C
200169
_x = y / C
@@ -203,11 +172,10 @@ eqs = [D(x) ~ -A * x,
203172
@named de = System(eqs, t)
204173
@test begin
205174
local f, du
206-
f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[2]
175+
f = generate_rhs(de, [x, y], [A, B, C], expression = Val{false})
207176
du = [0.0, 0.0]
208177
f(du, [1.0, 2.0], [1, 2, 3], 0.0)
209178
du [-1, -1 / 3]
210-
f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[1]
211179
du f([1.0, 2.0], [1, 2, 3], 0.0)
212180
end
213181

@@ -1286,11 +1254,11 @@ end
12861254
[D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x],
12871255
t, [u..., x..., o...], [p...])
12881256
sys1, = structural_simplify(sys, ([x...], []))
1289-
fn1, = ModelingToolkit.generate_function(sys1; expression = Val{false})
1257+
fn1, = ModelingToolkit.generate_rhs(sys1; expression = Val{false})
12901258
ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)])
12911259
@test_nowarn fn1(ones(4), ps, 4.0)
12921260
sys2, = structural_simplify(sys, ([x...], []); split = false)
1293-
fn2, = ModelingToolkit.generate_function(sys2; expression = Val{false})
1261+
fn2, = ModelingToolkit.generate_rhs(sys2; expression = Val{false})
12941262
ps = zeros(8)
12951263
setp(sys2, x)(ps, 2ones(2))
12961264
setp(sys2, p)(ps, 2ones(2, 2))

0 commit comments

Comments
 (0)