Skip to content

Commit f074df6

Browse files
test: update tests to account for new tearing
1 parent 0affb03 commit f074df6

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed

test/bvproblem.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6]
3030
for solver in solvers
3131
sol = solve(bvp, solver(), dt = 0.01)
3232
@test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
33-
@test sol.u[1] == [1.0, 2.0]
33+
@test sol[[x, y], 1] == [1.0, 2.0]
3434
end
3535

3636
# Test out of place
@@ -40,7 +40,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6]
4040
for solver in solvers
4141
sol = solve(bvp2, solver(), dt = 0.01)
4242
@test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
43-
@test sol.u[1] == [1.0, 2.0]
43+
@test sol[[x, y], 1] == [1.0, 2.0]
4444
end
4545
end
4646

@@ -129,7 +129,9 @@ end
129129
sol2 = solve(bvpi2, MIRK4(), dt = 0.01)
130130
sol3 = solve(bvpi3, MIRK4(), dt = 0.01)
131131
sol4 = solve(bvpi4, MIRK4(), dt = 0.01)
132-
@test sol1 sol2 sol3 sol4 # don't get true equality here, not sure why
132+
@test sol1.t sol2.t sol3.t sol4.t
133+
@test sol1.u sol2.u sol3[[x(t), y(t)]] sol4[[x(t), y(t)]]
134+
# @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why
133135
end
134136

135137
function test_solvers(
@@ -296,7 +298,7 @@ end
296298
costfn = ModelingToolkit.generate_cost(
297299
lksys; expression = Val{false}, wrap_gfw = Val{true})
298300
_t = tspan[2]
299-
@test costfn(sol, prob.p, _t) (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2
301+
@test costfn(sol, prob.p, _t) (sol(0.6; idxs = x(t)) + 3)^2 + sol(0.3; idxs = x(t))^2
300302

301303
### With a parameter
302304
@parameters t_c
@@ -309,5 +311,6 @@ end
309311
sol = solve(prob, Tsit5())
310312
costfn = ModelingToolkit.generate_cost(
311313
lksys; expression = Val{false}, wrap_gfw = Val{true})
312-
@test costfn(sol, prob.p, _t) log(sol(0.56)[2] + sol(0.0)[1]) - sol(0.4)[1]^2
314+
@test costfn(sol, prob.p, _t)
315+
log(sol(0.56; idxs = y(t)) + sol(0.0; idxs = x(t))) - sol(0.4; idxs = x(t))^2
313316
end

test/dde.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ prob = DDEProblem(sys,
4646
tspan,
4747
constant_lags = [tau])
4848
sol_mtk = solve(prob, alg, reltol = 1e-7, abstol = 1e-10)
49-
@test sol_mtk.u[end] sol.u[end]
49+
@test sol_mtk[[x₀, x₁, x₂(t)]][end] sol.u[end]
5050
prob2 = DDEProblem(sys,
5151
[x₀ => 1.0 - t * q1 * 10, x₁ => 1.0 - t * q1 * 10, x₂(t) => 1.0 - t * q1 * 10],
5252
tspan,
5353
constant_lags = [tau])
5454
sol2_mtk = solve(prob2, alg, reltol = 1e-7, abstol = 1e-10)
55-
@test sol2_mtk.u[end] sol2.u[end]
55+
@test sol2_mtk[[x₀, x₁, x₂(t)]][end] sol2.u[end]
5656
@test_nowarn sol2_mtk[[x₀, x₁, x₂(t)]]
5757
@test_nowarn sol2_mtk[[x₀, x₁, x₂(t - 0.1)]]
5858

test/initial_values.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ using SymbolicIndexingInterface
88
@variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2]
99

1010
@mtkcompile sys=System([D(x) ~ t * x], t) simplify=false
11-
@test get_u0(sys, []) == [1.0, 2.0, 3.0]
12-
@test get_u0(sys, [x => [2.0, 3.0, 4.0]]) == [2.0, 3.0, 4.0]
13-
@test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0]) == [2.0, 3.0, 4.0]
11+
reorderer = getsym(sys, x)
12+
@test reorderer(get_u0(sys, [])) == [1.0, 2.0, 3.0]
13+
@test reorderer(get_u0(sys, [x => [2.0, 3.0, 4.0]])) == [2.0, 3.0, 4.0]
14+
@test reorderer(get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])) == [2.0, 3.0, 4.0]
1415
@test get_u0(sys, [2.0, 3.0, 4.0]) == [2.0, 3.0, 4.0]
1516

1617
@mtkcompile sys=System([
@@ -182,7 +183,7 @@ end
182183
prob = ODEProblem(sys, [], (0.0, 1.0))
183184
sol = solve(prob)
184185
@test SciMLBase.successful_retcode(sol)
185-
@test sol.u[1] [1.0, 1.0, 0.5, 0.5]
186+
@test sol[x, 1] [1.0, 1.0, 0.5, 0.5]
186187
end
187188

188189
@testset "Missing/cyclic guesses throws error" begin

test/initializationsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ sol = solve(prob, Tsit5())
459459

460460
prob = ODEProblem(simpsys, [z => 1.0, y => 1.0], tspan, guesses = [x => 2.0])
461461
sol = solve(prob, Tsit5())
462-
@test sol.u[1] == [0.0, 1.0]
462+
@test sol[[x, y], 1] == [0.0, 1.0]
463463

464464
# This should warn, but logging tests can't be marked as broken
465465
@test_logs prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0])

test/input_output_handling.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1
369369
m_inputs = [u[1], u[2]]
370370
m_outputs = [y₂]
371371
sys_simp = mtkcompile(sys, inputs = m_inputs, outputs = m_outputs)
372-
@test isequal(unknowns(sys_simp), collect(x[1:2]))
372+
@test issetequal(unknowns(sys_simp), collect(x[1:2]))
373373
@test length(inputs(sys_simp)) == 2
374374

375375
# https://github.com/SciML/ModelingToolkit.jl/issues/1577
@@ -417,7 +417,7 @@ matrices, ssys = linearize(augmented_sys,
417417
], outs;
418418
op = [augmented_sys.u => 0.0, augmented_sys.input.u[2] => 0.0, augmented_sys.d => 0.0])
419419
matrices = ModelingToolkit.reorder_unknowns(
420-
matrices, unknowns(ssys), [ssys.x[2], ssys.integrator.x[1], ssys.x[1]])
420+
matrices, unknowns(ssys), [ssys.x[1], ssys.x[2], ssys.integrator.x[1]])
421421
@test matrices.A [A [1; 0]; zeros(1, 2) -0.001]
422422
@test matrices.B == I
423423
@test matrices.C == [C zeros(2)]

test/odesystem.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,10 @@ let
568568
@named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0))
569569
sys = mtkcompile(sys)
570570

571-
u0 = unknowns(sys) .=> [0.5, 0]
572-
du0 = D.(unknowns(sys)) .=> 0.0
571+
u0 = x .=> [0.5, 0]
572+
du0 = D.(x) .=> 0.0
573573
prob = DAEProblem(sys, [du0; u0], (0, 50))
574-
@test prob.u0 [0.5, 0.0]
574+
@test prob[x] [0.5, 0.0]
575575
@test prob.du0 [0.0, 0.0]
576576
@test prob.p isa MTKParameters
577577
@test prob.ps[k] 1
@@ -581,7 +581,8 @@ let
581581

582582
prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5],
583583
(0, 50))
584-
@test prob.u0 [0.5, 0]
584+
585+
@test prob[x] [0.5, 0]
585586
@test prob.du0 [0, 0]
586587
@test prob.p isa MTKParameters
587588
@test prob.ps[k] 1
@@ -590,7 +591,7 @@ let
590591

591592
prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5, k => 2],
592593
(0, 50))
593-
@test prob.u0 [0.5, 0]
594+
@test prob[x] [0.5, 0]
594595
@test prob.du0 [0, 0]
595596
@test prob.p isa MTKParameters
596597
@test prob.ps[k] 2
@@ -769,7 +770,7 @@ let
769770
@named sys4 = System([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t)
770771
sys4s = mtkcompile(sys4)
771772
prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0))
772-
@test string.(unknowns(prob.f.sys)) == ["x(t)", "y(t)"]
773+
@test issetequal(string.(unknowns(prob.f.sys)), ["x(t)", "y(t)"])
773774
@test string.(parameters(prob.f.sys)) == ["pp"]
774775
@test string.(independent_variables(prob.f.sys)) == ["t"]
775776
end

test/reduction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ eqs = [D(x) ~ x
245245
sys = mtkcompile(model)
246246
Js = ModelingToolkit.jacobian_sparsity(sys)
247247
@test size(Js) == (3, 3)
248-
@test Js == Diagonal([1, 1, 0])
248+
@test Js == Diagonal([0, 1, 1])
249249

250250
# MWE for #1722
251251
vars = @variables a(t) w(t) phi(t)

test/structural_transformation/tearing.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using ModelingToolkit.StructuralTransformations: SystemStructure, find_solvables
55
using NonlinearSolve
66
using LinearAlgebra
77
using UnPack
8+
using SymbolicIndexingInterface
89
using ModelingToolkit: t_nounits as t, D_nounits as D
910
###
1011
### Nonlinear system
@@ -147,17 +148,21 @@ eqs = [D(x) ~ z * h
147148
0 ~ sin(z) + y - p * t]
148149
@named daesys = System(eqs, t)
149150
newdaesys = mtkcompile(daesys)
150-
@test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t]
151-
@test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t]
152-
@test isequal(unknowns(newdaesys), [x, z])
151+
@test issetequal(equations(newdaesys), [D(x) ~ h * z; 0 ~ y + sin(z) - p * t])
152+
@test issetequal(
153+
equations(tearing_substitution(newdaesys)), [D(x) ~ h * z; 0 ~ x + sin(z) - p * t])
154+
@test issetequal(unknowns(newdaesys), [x, z])
153155
prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π, p => 0.2], (0, 1.0))
154156
du = [0.0, 0.0];
155157
u = [1.0, -0.5π];
156158
pr = prob.p;
157159
tt = 0.1;
158160
@test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0
159161
prob.f(du, u, pr, tt)
160-
@test du[u[2], u[1] + sin(u[2]) - prob.ps[p] * tt] atol=1e-5
162+
xgetter = getsym(prob, x)
163+
zgetter = getsym(prob, z)
164+
@test xgetter(du)zgetter(u) atol=1e-5
165+
@test zgetter(du)xgetter(u) + sin(zgetter(u)) - prob.ps[p] * tt atol=1e-5
161166

162167
# test the initial guess is respected
163168
@named sys = System(eqs, t, defaults = Dict(z => NaN))

0 commit comments

Comments
 (0)