Skip to content

Commit 0998e07

Browse files
Merge pull request #640 from SciML/downstream
Fix downstream tests for MTK v9
2 parents 875496a + 22fe521 commit 0998e07

File tree

6 files changed

+43
-55
lines changed

6 files changed

+43
-55
lines changed

test/downstream/ensemble_multi_prob.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using ModelingToolkit, OrdinaryDiffEq, Test
2+
using ModelingToolkit: t_nounits as t, D_nounits as D
3+
@variables x(t), y(t)
24

3-
@variables t, x(t), y(t)
4-
D = Differential(t)
5-
6-
@named sys1 = ODESystem([D(x) ~ x,
7-
D(y) ~ -y])
8-
@named sys2 = ODESystem([D(x) ~ 2x,
9-
D(y) ~ -2y])
10-
@named sys3 = ODESystem([D(x) ~ 3x,
11-
D(y) ~ -3y])
5+
@mtkbuild sys1 = ODESystem([D(x) ~ x,
6+
D(y) ~ -y], t)
7+
@mtkbuild sys2 = ODESystem([D(x) ~ 2x,
8+
D(y) ~ -2y], t)
9+
@mtkbuild sys3 = ODESystem([D(x) ~ 3x,
10+
D(y) ~ -3y], t)
1211

1312
prob1 = ODEProblem(sys1, [1.0, 1.0], (0.0, 1.0))
1413
prob2 = ODEProblem(sys2, [2.0, 2.0], (0.0, 1.0))

test/downstream/integrator_indexing.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, StochasticDiffEq,
22
SymbolicIndexingInterface, Test
3-
3+
using ModelingToolkit: t_nounits as t, D_nounits as D
44
### Tests on non-layered model (everything should work). ###
55

6-
@parameters t a b c d
6+
@parameters a b c d
77
@variables s1(t) s2(t)
8-
D = Differential(t)
98

109
eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
1110
D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2]
1211

13-
@named population_model = ODESystem(eqs)
12+
@named population_model = ODESystem(eqs, t)
1413

1514
# Tests on ODEProblem.
1615
u0 = [s1 => 2.0, s2 => 1.0]
@@ -119,23 +118,24 @@ integrator[noisy_population_model.s2] = 10.0
119118
integrator[:s1] = 1.0
120119
@test integrator[s1] == integrator[noisy_population_model.s1] == integrator[:s1] == 1.0
121120

122-
@parameters t σ ρ β
121+
@parameters σ ρ β
123122
@variables x(t) y(t) z(t)
124123
D = Differential(t)
125124

126125
eqs = [D(x) ~ σ * (y - x),
127126
D(y) ~ x *- z) - y,
128127
D(z) ~ x * y - β * z]
129128

130-
@named lorenz1 = ODESystem(eqs)
131-
@named lorenz2 = ODESystem(eqs)
129+
@named lorenz1 = ODESystem(eqs, t)
130+
@named lorenz2 = ODESystem(eqs, t)
132131

133132
@parameters γ
134133
@variables a(t) α(t)
135134
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
136135
α ~ 2lorenz1.x + a * γ]
137-
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
138-
sys_simplified = structural_simplify(sys)
136+
@mtkbuild sys_simplified = ODESystem(
137+
connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
138+
sys_simplified = complete(structural_simplify(sys))
139139

140140
u0 = [lorenz1.x => 1.0,
141141
lorenz1.y => 0.0,
@@ -185,7 +185,7 @@ step!(integrator, 100.0, true)
185185
eqs = [D(q[1]) ~ 2q[1]
186186
D(q[2]) ~ 2.0]
187187
@named sys2 = ODESystem(eqs, t, [q...], [])
188-
sys2_simplified = structural_simplify(sys2)
188+
sys2_simplified = complete(structural_simplify(sys2))
189189
prob2 = ODEProblem(sys2, [], (0.0, 5.0))
190190
integrator2 = init(prob2, Tsit5())
191191

@@ -198,7 +198,7 @@ integrator2 = init(prob2, Tsit5())
198198
@variables u(t)
199199
eqs = [D(u) ~ u]
200200

201-
@named sys2 = ODESystem(eqs)
201+
@mtkbuild sys2 = ODESystem(eqs, t)
202202

203203
tspan = (0.0, 5.0)
204204

@@ -327,13 +327,12 @@ plot(sol,idxs=(t,α))
327327
=#
328328

329329
using LinearAlgebra
330-
@variables t
331330
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
332331
ps = @parameters p[1:3] = [1, 2, 3]
333332
D = Differential(t)
334333
eqs = [collect(D.(x) .~ x)
335334
D(y) ~ norm(x) * y - x[1]]
336-
@named sys = ODESystem(eqs, t, [sts...;], [ps...;])
335+
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
337336
prob = ODEProblem(sys, [], (0, 1.0))
338337
integrator = init(prob, Tsit5(), save_everystep = false)
339338
@test integrator[x] isa Vector{Float64}

test/downstream/problem_interface.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using ModelingToolkit, OrdinaryDiffEq, Test
2+
using ModelingToolkit: t_nounits as t, D_nounits as D
23
using SymbolicIndexingInterface
34

45
@parameters σ ρ β
5-
@variables t x(t) y(t) z(t)
6-
D = Differential(t)
6+
@variables x(t) y(t) z(t)
77

88
eqs = [D(D(x)) ~ σ * (y - x),
99
D(y) ~ x *- z) - y,
1010
D(z) ~ x * y - β * z]
1111

12-
@named sys = ODESystem(eqs)
13-
14-
sys = structural_simplify(sys)
12+
@mtkbuild sys = ODESystem(eqs, t)
1513

1614
u0 = [D(x) => 2.0,
1715
x => 1.0,
@@ -181,13 +179,12 @@ set_tuple!(sprob, [10.0, 10.0])
181179
@test get_tuple(sprob) == (10.0, 10.0)
182180

183181
using LinearAlgebra
184-
@variables t
185182
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
186183
ps = @parameters p[1:3] = [1, 2, 3]
187184
D = Differential(t)
188185
eqs = [collect(D.(x) .~ x)
189186
D(y) ~ norm(x) * y - x[1]]
190-
@named sys = ODESystem(eqs, t, [sts...;], [ps...;])
187+
@mtkbuild sys = ODESystem(eqs, t, [sts...;], [ps...;])
191188
prob = ODEProblem(sys, [], (0, 1.0))
192189
@test getp(sys, p)(prob) == prob.ps[p] == [1, 2, 3]
193190
setp(sys, p)(prob, [4, 5, 6])

test/downstream/remake_autodiff.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OrdinaryDiffEq, ModelingToolkit, Zygote, SciMLSensitivity
2+
using ModelingToolkit: t_nounits as t, D_nounits as D
23

3-
@variables t x(t) o(t)
4+
@variables x(t) o(t)
45
D = Differential(t)
56
function lotka_volterra(; name = name)
67
unknowns = @variables x(t)=1.0 y(t)=1.0 o(t)

test/downstream/solution_interface.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, StochasticDiffEq, Test
2-
# compat for MTKv8 and v9
3-
unknowns = isdefined(ModelingToolkit, :states) ? ModelingToolkit.states :
4-
ModelingToolkit.unknowns
2+
using ModelingToolkit: t_nounits as t, D_nounits as D
53

64
### Tests on non-layered model (everything should work). ###
75

8-
@parameters t a b c d
6+
@parameters a b c d
97
@variables s1(t) s2(t)
10-
D = Differential(t)
118

129
eqs = [D(s1) ~ a * s1 / (1 + s1 + s2) - b * s1,
1310
D(s2) ~ +c * s2 / (1 + s1 + s2) - d * s2]
1411

15-
@named population_model = ODESystem(eqs)
12+
@mtkbuild population_model = ODESystem(eqs, t)
1613

1714
# Tests on ODEProblem.
1815
u0 = [s1 => 2.0, s2 => 1.0]
@@ -32,7 +29,7 @@ sol = solve(oprob, Rodas4())
3229
noiseeqs = [0.1 * s1,
3330
0.1 * s2]
3431
@named noisy_population_model = SDESystem(population_model, noiseeqs)
35-
sprob = SDEProblem(noisy_population_model, u0, (0.0, 100.0), p)
32+
sprob = SDEProblem(complete(noisy_population_model), u0, (0.0, 100.0), p)
3633
sol = solve(sprob, ImplicitEM())
3734

3835
@test sol[s1] == sol[noisy_population_model.s1] == sol[:s1]
@@ -42,23 +39,22 @@ sol = solve(sprob, ImplicitEM())
4239
@test_throws Exception sol[:a]
4340
### Tests on layered model (some things should not work). ###
4441

45-
@parameters t σ ρ β
42+
@parameters σ ρ β
4643
@variables x(t) y(t) z(t)
47-
D = Differential(t)
4844

4945
eqs = [D(x) ~ σ * (y - x),
5046
D(y) ~ x *- z) - y,
5147
D(z) ~ x * y - β * z]
5248

53-
@named lorenz1 = ODESystem(eqs)
54-
@named lorenz2 = ODESystem(eqs)
49+
@named lorenz1 = ODESystem(eqs,t)
50+
@named lorenz2 = ODESystem(eqs,t)
5551

5652
@parameters γ
5753
@variables a(t) α(t)
5854
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
5955
α ~ 2lorenz1.x + a * γ]
6056
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
61-
sys_simplified = structural_simplify(sys)
57+
sys_simplified = complete(structural_simplify(sys))
6258

6359
u0 = [lorenz1.x => 1.0,
6460
lorenz1.y => 0.0,

test/downstream/symbol_indexing.jl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using ModelingToolkit, OrdinaryDiffEq, RecursiveArrayTools, SymbolicIndexingInterface, Test
22
using Optimization, OptimizationOptimJL
3-
# compat for MTKv8 and v9
4-
unknowns = isdefined(ModelingToolkit, :states) ? ModelingToolkit.states :
5-
ModelingToolkit.unknowns
3+
using ModelingToolkit: t_nounits as t, D_nounits as D
64

7-
@parameters t σ ρ β
5+
@parameters σ ρ β
86
@variables x(t) y(t) z(t)
97
D = Differential(t)
108

@@ -19,8 +17,7 @@ eqs = [D(x) ~ σ * (y - x),
1917
@variables a(t) α(t)
2018
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ,
2119
α ~ 2lorenz1.x + a * γ]
22-
@named sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
23-
sys_simplified = structural_simplify(sys)
20+
@mtkbuild sys = ODESystem(connections, t, [a, α], [γ], systems = [lorenz1, lorenz2])
2421

2522
u0 = [lorenz1.x => 1.0,
2623
lorenz1.y => 0.0,
@@ -39,7 +36,7 @@ p = [lorenz1.σ => 10.0,
3936
γ => 2.0]
4037

4138
tspan = (0.0, 100.0)
42-
prob = ODEProblem(sys_simplified, u0, tspan, p)
39+
prob = ODEProblem(sys, u0, tspan, p)
4340
integ = init(prob, Rodas4())
4441
sol = solve(prob, Rodas4())
4542

@@ -135,7 +132,7 @@ sol1 = sol(0.0:1.0:10.0)
135132

136133
sol2 = sol(0.1)
137134
@test sol2 isa Vector
138-
@test length(sol2) == length(unknowns(sys_simplified))
135+
@test length(sol2) == length(unknowns(sys))
139136
@test first(sol2) isa Real
140137

141138
sol3 = sol(0.0:1.0:10.0, idxs = [lorenz1.x, lorenz2.x])
@@ -191,9 +188,9 @@ sol10 = sol(0.1, idxs = 2)
191188
@test sol10 isa Real
192189

193190
@test is_timeseries(sol) == Timeseries()
194-
getx = getu(sys_simplified, lorenz1.x)
195-
get_arr = getu(sys_simplified, [lorenz1.x, lorenz2.x])
196-
get_tuple = getu(sys_simplified, (lorenz1.x, lorenz2.x))
191+
getx = getu(sys, lorenz1.x)
192+
get_arr = getu(sys, [lorenz1.x, lorenz2.x])
193+
get_tuple = getu(sys, (lorenz1.x, lorenz2.x))
197194
get_obs = getu(sol, lorenz1.x + lorenz2.x) # can't use sys for observed
198195
get_obs_arr = getu(sol, [lorenz1.x + lorenz2.x, lorenz1.y + lorenz2.y])
199196
l1x_idx = variable_index(sol, lorenz1.x)
@@ -218,7 +215,6 @@ plot(sol,idxs=(t,α))
218215
=#
219216

220217
using LinearAlgebra
221-
@variables t
222218
sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0
223219
ps = @parameters p[1:3] = [1, 2, 3]
224220
D = Differential(t)
@@ -342,7 +338,7 @@ for (sym, oldval, newval, check_inference) in [
342338
end
343339

344340
# accessing parameters
345-
@variables t x(t)
341+
@variables x(t)
346342
@parameters tau
347343
D = Differential(t)
348344

0 commit comments

Comments
 (0)