Skip to content

Commit 8109604

Browse files
test: use new problem construction syntax
1 parent f663b2e commit 8109604

40 files changed

+363
-382
lines changed

test/basic_transformations.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ D = Differential(t)
1414
u0 = [x => 1.0, y => 1.0]
1515
p ==> 1.5, β => 1.0, δ => 3.0, γ => 1.0]
1616
tspan = (0.0, 10.0)
17-
prob = ODEProblem(sys, u0, tspan, p)
17+
prob = ODEProblem(sys, [u0; p], tspan)
1818
sol = solve(prob, Tsit5())
1919

2020
sys2 = liouville_transform(sys)
2121
sys2 = complete(sys2)
2222

2323
u0 = [x => 1.0, y => 1.0, sys2.trJ => 1.0]
24-
prob = ODEProblem(sys2, u0, tspan, p, jac = true)
24+
prob = ODEProblem(sys2, [u0; p], tspan, jac = true)
2525
sol = solve(prob, Tsit5())
2626
@test sol[end, end] 1.0742818931017244
2727
end
@@ -53,8 +53,8 @@ end
5353

5454
M1 = mtkcompile(M1; allow_symbolic = true)
5555
M2 = mtkcompile(M2; allow_symbolic = true)
56-
prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0), [])
57-
prob2 = ODEProblem(M2, [], (1.0, 2.0), [])
56+
prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0))
57+
prob2 = ODEProblem(M2, [], (1.0, 2.0))
5858
sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10)
5959
sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10)
6060
ts = range(0.0, 1.0, length = 50)
@@ -125,7 +125,7 @@ end
125125
Dx = Differential(Mx.x)
126126
u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0]
127127
p = [v => 10.0]
128-
prob = ODEProblem(Mx, u0, (0.0, 20.0), p) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
128+
prob = ODEProblem(Mx, [u0; p], (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
129129
sol = solve(prob, Tsit5(); reltol = 1e-5)
130130
@test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2)
131131
end
@@ -138,7 +138,7 @@ end
138138
Mx = mtkcompile(Mx; allow_symbolic = true)
139139
Dx = Differential(Mx.x)
140140
u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0]
141-
prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
141+
prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
142142
sol = solve(prob, Tsit5(); reltol = 1e-5)
143143
@test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2)
144144
end
@@ -200,7 +200,7 @@ end
200200

201201
_f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1
202202
M2s = mtkcompile(M2; allow_symbolic = true)
203-
prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f])
203+
prob = ODEProblem(M2s, [M2s.y => 0.0, fc => _f, f => _f], (1.0, 4.0))
204204
sol = solve(prob, Tsit5(); abstol = 1e-5)
205205
@test isapprox(sol(4.0, idxs = M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12
206206
end
@@ -227,7 +227,7 @@ end
227227
Mx = mtkcompile(Mx; allow_symbolic = true)
228228
Dx = Differential(Mx.x)
229229
u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0]
230-
prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
230+
prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
231231
sol = solve(prob, Tsit5(); reltol = 1e-5)
232232
# compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2)
233233
@test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10))
@@ -300,7 +300,7 @@ end
300300
new_sys = change_independent_variable(sys, sys.x; add_old_diff = true)
301301
ss_new_sys = mtkcompile(new_sys; allow_symbolic = true)
302302
u0 = [new_sys.y => 0.5, new_sys.t => 0.0]
303-
prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5), [])
303+
prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5))
304304
sol = solve(prob, Tsit5(); reltol = 1e-5)
305305
@test sol[new_sys.y][end] 0.75
306306
end

test/bvproblem.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ daesolvers = [Ascher2, Ascher4, Ascher6]
2121
tspan = (0.0, 10.0)
2222

2323
@mtkcompile lotkavolterra = System(eqs, t)
24-
op = ODEProblem(lotkavolterra, u0map, tspan, parammap)
24+
op = ODEProblem(lotkavolterra, [u0map; parammap], tspan)
2525
osol = solve(op, Vern9())
2626

2727
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
28-
lotkavolterra, u0map, tspan, parammap)
28+
lotkavolterra, [u0map; parammap], tspan)
2929

3030
for solver in solvers
3131
sol = solve(bvp, solver(), dt = 0.01)
@@ -35,7 +35,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6]
3535

3636
# Test out of place
3737
bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(
38-
lotkavolterra, u0map, tspan, parammap)
38+
lotkavolterra, [u0map; parammap], tspan)
3939

4040
for solver in solvers
4141
sol = solve(bvp2, solver(), dt = 0.01)
@@ -58,10 +58,11 @@ end
5858
parammap = [:L => 1.0, :g => 9.81]
5959
tspan = (0.0, 6.0)
6060

61-
op = ODEProblem(pend, u0map, tspan, parammap)
61+
op = ODEProblem(pend, [u0map; parammap], tspan)
6262
osol = solve(op, Vern9())
6363

64-
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap)
64+
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
65+
pend, [u0map; parammap], tspan)
6566
for solver in solvers
6667
sol = solve(bvp, solver(), dt = 0.01)
6768
@test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
@@ -70,7 +71,7 @@ end
7071

7172
# Test out-of-place
7273
bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(
73-
pend, u0map, tspan, parammap)
74+
pend, [u0map; parammap], tspan)
7475

7576
for solver in solvers
7677
sol = solve(bvp2, solver(), dt = 0.01)
@@ -289,8 +290,8 @@ end
289290
@mtkcompile lksys = System(eqs, t; costs, consolidate)
290291

291292
@test_throws ModelingToolkit.SystemCompatibilityError ODEProblem(
292-
lksys, u0map, tspan, parammap)
293-
prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false)
293+
lksys, [u0map; parammap], tspan)
294+
prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false)
294295
sol = solve(prob, Tsit5())
295296
costfn = ModelingToolkit.generate_cost(
296297
lksys; expression = Val{false}, wrap_gfw = Val{true})
@@ -304,7 +305,7 @@ end
304305
@mtkcompile lksys = System(eqs, t; costs, consolidate)
305306
@test t_c Set(parameters(lksys))
306307
push!(parammap, t_c => 0.56)
307-
prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false)
308+
prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false)
308309
sol = solve(prob, Tsit5())
309310
costfn = ModelingToolkit.generate_cost(
310311
lksys; expression = Val{false}, wrap_gfw = Val{true})

test/clock.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@ eqs = [yd ~ Sample(dt)(y)
120120

121121
@test_skip begin
122122
Tf = 1.0
123-
prob = ODEProblem(ss, [x => 0.1], (0.0, Tf),
124-
[kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0])
123+
prob = ODEProblem(
124+
ss, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf))
125125
# create integrator so callback is evaluated at t=0 and we can test correct param values
126126
int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent)
127127
@test sort(vcat(int.p...)) == [0.1, 1.0, 2.1, 2.1, 2.1] # yd, kp, ud(k-1), ud, Hold(ud)
128-
prob = ODEProblem(ss, [x => 0.1], (0.0, Tf),
129-
[kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values
128+
prob = ODEProblem(
129+
ss, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) # recreate problem to empty saved values
130130
sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent)
131131

132132
ss_nosplit = mtkcompile(sys; split = false)
133-
prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf),
134-
[kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0])
133+
prob_nosplit = ODEProblem(
134+
ss_nosplit, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf))
135135
int = init(prob_nosplit, Tsit5(); kwargshandle = KeywordArgSilent)
136136
@test sort(int.p) == [0.1, 1.0, 2.1, 2.1, 2.1] # yd, kp, ud(k-1), ud, Hold(ud)
137-
prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf),
138-
[kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values
137+
prob_nosplit = ODEProblem(
138+
ss_nosplit, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) # recreate problem to empty saved values
139139
sol_nosplit = solve(prob_nosplit, Tsit5(), kwargshandle = KeywordArgSilent)
140140
# For all inputs in parameters, just initialize them to 0.0, and then set them
141141
# in the callback.
@@ -299,8 +299,8 @@ eqs = [yd ~ Sample(dt)(y)
299299
ss_nosplit = mtkcompile(cl; split = false)
300300

301301
if VERSION >= v"1.7"
302-
prob = ODEProblem(ss, [x => 0.0], (0.0, 1.0), [kp => 1.0])
303-
prob_nosplit = ODEProblem(ss_nosplit, [x => 0.0], (0.0, 1.0), [kp => 1.0])
302+
prob = ODEProblem(ss, [x => 0.0, kp => 1.0], (0.0, 1.0))
303+
prob_nosplit = ODEProblem(ss_nosplit, [x => 0.0, kp => 1.0], (0.0, 1.0))
304304
sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent)
305305
sol_nosplit = solve(prob_nosplit, Tsit5(), kwargshandle = KeywordArgSilent)
306306

@@ -535,8 +535,8 @@ eqs = [yd ~ Sample(dt)(y)
535535
@test int.ps[x] == 2.0
536536
@test int.ps[x(k - 1)] == 1.0
537537

538-
@test_throws ErrorException ODEProblem(sys, [], (0.0, 10.0), [x => 2.0])
539-
prob = ODEProblem(sys, [], (0.0, 10.0), [x(k - 1) => 2.0])
538+
@test_throws ErrorException ODEProblem(sys, [x => 2.0], (0.0, 10.0))
539+
prob = ODEProblem(sys, [x(k - 1) => 2.0], (0.0, 10.0))
540540
int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent)
541541
@test int.ps[x] == 3.0
542542
@test int.ps[x(k - 1)] == 2.0

test/code_generation.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959
@variables x(t)
6060
@parameters p[0:2] (f::Function)(..)
6161
@mtkcompile sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t)
62-
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum])
62+
prob = ODEProblem(sys, [x => 1.0, p => [1.0, 2.0, 3.0], f => sum], (0.0, 1.0))
6363
@test prob.ps[p] == [1.0, 2.0, 3.0]
6464
@test prob.ps[p[0]] == 1.0
6565
sol = solve(prob, Tsit5())
@@ -72,8 +72,9 @@ end
7272
[D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t)
7373
sys = mtkcompile(sys, inputs = [x[2]], outputs = [])
7474
@test is_parameter(sys, x[2])
75-
prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0),
76-
[p => ones(2), f => sum, x[2] => 2.0])
75+
prob = ODEProblem(
76+
sys, [x[0] => 1.0, x[1] => 1.0, x[2] => 2.0, p => ones(2), f => sum],
77+
(0.0, 1.0))
7778
sol = solve(prob, Tsit5())
7879
@test SciMLBase.successful_retcode(sol)
7980
end

test/components.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,16 @@ include("common/serial_inductor.jl")
119119
u0 = unknowns(sys) .=> 0
120120
@test_nowarn ODEProblem(
121121
sys, [], (0, 10.0), guesses = u0, warn_initialize_determined = false)
122-
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, [], (0, 0.5), guesses = u0)
122+
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, (0, 0.5), guesses = u0)
123123
sol = solve(prob, DFBDF())
124124
@test sol.retcode == SciMLBase.ReturnCode.Success
125125

126126
sys2 = mtkcompile(ll2_model)
127127
@test length(equations(sys2)) == 3
128-
u0 = unknowns(sys) .=> 0
128+
u0 = [sys.inductor2.i => 0]
129129
prob = ODEProblem(sys, u0, (0, 10.0))
130-
@test_nowarn sol = solve(prob, FBDF())
130+
sol = solve(prob, FBDF())
131+
@test SciMLBase.successful_retcode(sol)
131132
end
132133

133134
@testset "Compose/extend" begin

test/constants.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ UMT = ModelingToolkit.UnitfulUnitCheck
1212
D = Differential(t)
1313
eqs = [D(x) ~ a]
1414
@named sys = System(eqs, t)
15-
prob = ODEProblem(complete(sys), [0], [0.0, 1.0], [])
15+
prob = ODEProblem(complete(sys), [0], [0.0, 1.0])
1616
sol = solve(prob, Tsit5())
1717

1818
# Test mtkcompile substitutions & observed values
@@ -43,7 +43,7 @@ simp = mtkcompile(sys)
4343

4444
@mtkcompile fol_model = System(eqs, MT.t_nounits)
4545

46-
prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1])
46+
prob = ODEProblem(fol_model, [h => 1], (0.0, 10.0))
4747
@test prob[x] 1
4848
@test prob.ps[τ] 0.5
4949
end

test/dae_jacobian.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ u0 = [u1 => 1.0,
4343

4444
tspan = (0.0, 10.0)
4545

46-
du0 = [0.5, -2.0]
46+
du0 = [D(u1) => 0.5, D(u2) => -2.0]
4747

4848
p = [p1 => 1.5,
4949
p2 => 3.0]
5050

51-
prob = DAEProblem(complete(sys), du0, u0, tspan, p, jac = true, sparse = true)
51+
prob = DAEProblem(complete(sys), [du0; u0; p], tspan, jac = true, sparse = true)
5252
sol = solve(prob, IDA(linear_solver = :KLU))
5353

5454
@test maximum(sol - sol1) < 1e-12

test/discrete_system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ reorderer = getu(syss, [S, I, R])
5151
u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0]
5252
p ==> 0.05, c => 10.0, γ => 0.25, δt => 0.1, nsteps => 400]
5353
tspan = (0.0, ModelingToolkit.value(substitute(nsteps, p))) # value function (from Symbolics) is used to convert a Num to Float64
54-
prob_map = DiscreteProblem(syss, u0, tspan, p)
54+
prob_map = DiscreteProblem(syss, [u0; p], tspan)
5555
@test prob_map.f.sys === syss
5656

5757
# Solution

test/distributed.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ addprocs(2)
1616
@everywhere @named de = System(eqs, t)
1717
@everywhere de = complete(de)
1818

19-
@everywhere u0 = [19.0, 20.0, 50.0]
20-
@everywhere params = [16.0, 45.92, 4]
19+
@everywhere u0 = unknowns(de) .=> [19.0, 20.0, 50.0]
20+
@everywhere params = parameters(de) .=> [16.0, 45.92, 4]
2121

22-
@everywhere ode_prob = ODEProblem(de, u0, (0.0, 10.0), params)
22+
@everywhere ode_prob = ODEProblem(de, [u0; params], (0.0, 10.0))
2323

2424
@everywhere begin
2525
using OrdinaryDiffEq

test/dq_units.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ end
216216
p = [pend.g => 1.0, pend.L => 1.0]
217217
guess = [pend.λ => 0.0]
218218
@test prob = ODEProblem(
219-
pend, u0, (0.0, 1.0), p; guesses = guess, check_units = false) isa Any
219+
pend, [u0; p], (0.0, 1.0); guesses = guess, check_units = false) isa Any
220220
end
221221

222222
@parameters p [unit = u"L/s"] d [unit = u"s^(-1)"]

0 commit comments

Comments
 (0)