Skip to content

Commit 834e38c

Browse files
test: update tests as per new MTKBase mtkcompile
1 parent ba2820f commit 834e38c

File tree

10 files changed

+32
-66
lines changed

10 files changed

+32
-66
lines changed

lib/ModelingToolkitBase/test/analysis_points.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ using Test
66
using ModelingToolkitBase: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem
77
import ModelingToolkitBase as MTK
88
import ControlSystemsBase as CS
9+
using SciCompDSL
10+
using ModelingToolkitStandardLibrary
11+
912
using Symbolics: NAMESPACE_SEPARATOR
1013

1114
@testset "AnalysisPoint is lowered to `connect`" begin

lib/ModelingToolkitBase/test/changeofvariables.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ModelingToolkitBase, OrdinaryDiffEq, StochasticDiffEq
22
using Test, LinearAlgebra
33
import DiffEqNoiseProcess
4+
using Symbolics: unwrap
45

56
common_alg = @isdefined(ModelingToolkit) ? Tsit5() : Rodas5P()
67

@@ -136,7 +137,7 @@ new_sys = change_of_variables(sys, t, forward_subs, backward_subs)
136137
@test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2)
137138
@test equations(new_sys)[2] == (D(w) ~ α^2)
138139
@test equations(new_sys)[3] == (D(v) ~ μ - 1/2*^2 + σ^2))
139-
col1 = @isdefined(ModelingToolkit) ? 1 : 2
140+
col1 = isequal(noise_eqs(new_sys)[1, 1], unwrap(σ))::Bool ? 1 : 2
140141
col2 = 3 - col1
141142
@test value(noise_eqs(new_sys)[1, col1]) === value(σ)
142143
@test value(noise_eqs(new_sys)[1, col2]) === value(0)

lib/ModelingToolkitBase/test/code_generation.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,8 @@ end
7878
return [x, 2x]
7979
end
8080
@mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t)
81-
if @isdefined(ModelingToolkit)
82-
@test length(equations(sys)) == 1
83-
@test length(ModelingToolkitBase.observed(sys)) == 3
84-
else
85-
@test length(equations(sys)) == 3
86-
end
81+
@test length(equations(sys)) == 1
82+
@test length(ModelingToolkitBase.observed(sys)) == 3
8783
prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn2], (0.0, 1.0))
8884
val[] = 0
8985
@test_nowarn prob.f(prob.u0, prob.p, 0.0)

lib/ModelingToolkitBase/test/components.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ include("common/rc_model.jl")
7979
@test !isempty(ModelingToolkitBase.bindings(sys))
8080
u0 = [capacitor.v => 0.0]
8181
prob = ODEProblem(sys, u0, (0, 10.0))
82-
sol = solve(prob, Rodas4())
82+
sol = solve(prob, Rodas4(); abstol = 1e-8, reltol = 1e-8)
8383
check_rc_sol(sol)
8484
end
8585

lib/ModelingToolkitBase/test/constants.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ eqs = [D(x) ~ 1,
2020
@named sys = System(eqs, t)
2121
# Now eliminate the constants first
2222
simp = mtkcompile(sys)
23-
if @isdefined(ModelingToolkit)
24-
@test equations(simp) == [D(x) ~ 1.0]
25-
else
26-
@test equations(simp) == [D(x) ~ 1.0, 0 ~ a-w]
27-
end
23+
@test equations(simp) == [D(x) ~ 1.0]
2824

2925
#Constant with units
3026
@constants β=1 [unit = u"m/s"]

lib/ModelingToolkitBase/test/initializationsystem.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ if @isdefined(ModelingToolkit)
273273
@test SciMLBase.successful_retcode(initsol)
274274
@test maximum(abs.(initsol[conditions])) < 5e-14
275275
else
276-
@test length(initprob.u0) == 63
276+
@test length(initprob.u0) == 8
277277
initsol = solve(initprob, reltol = 1e-12, abstol = 1e-12)
278278
@test SciMLBase.successful_retcode(initsol)
279279
@test maximum(abs.(initsol[conditions])) < 5e-13
@@ -508,11 +508,7 @@ sol = solve(prob, Tsit5())
508508

509509
unsimp = generate_initializesystem(pend; op = [x => 1], initialization_eqs = [y ~ 1])
510510
sys = mtkcompile(unsimp; fully_determined = false)
511-
if @isdefined(ModelingToolkit)
512-
@test length(equations(sys)) in (3, 4, 5) # depending on tearing
513-
else
514-
@test length(equations(sys)) == 7
515-
end
511+
@test length(equations(sys)) in (3, 4, 5) # depending on tearing
516512
end
517513

518514
@testset "Extend two systems with initialization equations and guesses" begin
@@ -592,9 +588,9 @@ end
592588
@parameters k1 k2 ω
593589
@variables X(t) Y(t)
594590
eqs_1st_order = [D(Y) ~ ω - Y,
595-
X + k1 ~ Y + k2]
591+
Y ~ X + k1 - k2]
596592
eqs_2nd_order = [D(D(Y)) ~ -2ω * D(Y) -^2) * Y,
597-
X + k1 ~ Y + k2]
593+
Y ~ X + k1 - k2]
598594
@mtkcompile sys_1st_order = System(eqs_1st_order, t)
599595
@mtkcompile sys_2nd_order = System(eqs_2nd_order, t)
600596

@@ -612,7 +608,7 @@ oprob_2nd_order_2 = ODEProblem(sys_2nd_order, [u0_2nd_order_2; ps], tspan)
612608

613609
@test solve(oprob_1st_order_1, Rosenbrock23()).retcode ==
614610
SciMLBase.ReturnCode.InitialFailure
615-
@test solve(oprob_1st_order_2, Rosenbrock23())[Y][1] == 2.0
611+
@test solve(oprob_1st_order_2, Rosenbrock23())[Y][1] 2.0
616612
@test solve(oprob_2nd_order_1, Rosenbrock23()).retcode ==
617613
SciMLBase.ReturnCode.InitialFailure
618614
sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
@@ -624,7 +620,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
624620
@named sys = System([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x])
625621
sys = mtkcompile(sys)
626622
prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0))
627-
sol = solve(prob, Tsit5(), reltol = 1e-8)
623+
sol = solve(prob, Tsit5(); abstol = 1e-8, reltol = 1e-8)
628624
@test sol(1.0; idxs = sys.x) fill(exp(1), 5) atol=1e-6
629625
@test sol(1.0; idxs = sys.y) fill(-exp(1), 5) atol=1e-6
630626
end
@@ -683,7 +679,7 @@ end
683679
# Solve for either
684680
@mtkcompile sys = System([D(x) ~ p * x + rhss[1], D(y) ~ q * y + rhss[2]], t;
685681
bindings = [p => missing, q => missing],
686-
initialization_eqs = [p ~ 3 * q^2], guesses = [q => 10.0])
682+
initialization_eqs = [p ~ 3 * q^2], guesses = [q => 10.0, p => 1.0])
687683
# Specify `p`
688684
prob = Problem(sys, [x => 1.0, y => 1.0, p => 12.0], (0.0, 1.0); u0_constructor, p_constructor)
689685
if !@isdefined(ModelingToolkit)
@@ -942,10 +938,10 @@ end
942938
end
943939
sys = complete(sys)
944940
prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0))
945-
@test init(prob, alg).ps[p] 2.0
941+
@test init(prob, alg; abstol = 1e-6, reltol = 1e-6).ps[p] 2.0 atol=1e-4
946942
# nonsensical value for y just to test that equations work
947943
prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(x)])
948-
@test init(prob2, alg).ps[p] 3 + exp(1)
944+
@test init(prob2, alg; abstol = 1e-6, reltol = 1e-6).ps[p] 3 + exp(1) atol=1e-4
949945
# solve for `x` given `p` and `y`
950946
prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(y)])
951947
@test init(prob3, alg; abstol=1e-6, reltol=1e-6)[x] 1 - exp(1) atol=1e-6
@@ -954,7 +950,7 @@ end
954950
prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0])
955951
@test solve(prob4, alg).retcode == ReturnCode.InitialFailure
956952
prob5 = remake(prob)
957-
@test init(prob, alg).ps[p] 2.0
953+
@test init(prob, alg; abstol = 1e-6, reltol = 1e-6).ps[p] 2.0 atol=1e-4
958954
end
959955
end
960956

@@ -1660,7 +1656,7 @@ end
16601656

16611657
@mtkcompile sys = System(eqs, t)
16621658
prob = ODEProblem(sys, [], (0.0, 1.0))
1663-
sol = solve(prob, @isdefined(ModelingToolkit) ? Tsit5() : Rodas5P())
1659+
sol = solve(prob, Tsit5())
16641660
@test SciMLBase.successful_retcode(sol)
16651661
end
16661662

lib/ModelingToolkitBase/test/input_output_handling.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ eqs = [connect_sd(sd, mass1, mass2)
324324

325325
f, dvs, ps, io_sys = ModelingToolkitBase.generate_control_function(
326326
model, [u]; simplify = true)
327-
@test length(dvs) == (@isdefined(ModelingToolkit) ? 4 : 8)
327+
@test length(dvs) == 4
328328
p = MTKParameters(io_sys, [io_sys.u => NaN])
329329
x = ModelingToolkitBase.varmap_to_vars(
330330
merge(ModelingToolkitBase.initial_conditions(model),

lib/ModelingToolkitBase/test/namespacing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ nsys = toggle_namespacing(sys, false)
3232
@named inner = System([D(x) ~ x, y ~ 2x + 1], t)
3333
@test issetequal(unknowns(inner), [x, y])
3434
ss = mtkcompile(inner)
35-
@test issetequal(unknowns(ss), [x, y])
35+
@test issetequal(unknowns(ss), [x])
3636

3737
@named sys = System(Equation[], t; systems = [inner])
3838
xx, yy = let sys = inner
@@ -42,5 +42,5 @@ nsys = toggle_namespacing(sys, false)
4242
end
4343
@test issetequal(unknowns(sys), [xx, yy])
4444
ss = mtkcompile(sys)
45-
@test isequal(unknowns(ss), [xx, yy])
45+
@test isequal(unknowns(ss), [xx])
4646
end

lib/ModelingToolkitBase/test/odesystem.jl

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,6 @@ let
572572

573573
u0 = x .=> [0.5, 0]
574574
du0 = D.(x) .=> 0.0
575-
if !@isdefined(ModelingToolkit)
576-
push!(du0, D(y) => 0.0)
577-
end
578575
prob = DAEProblem(sys, du0, (0, 50); guesses = u0)
579576
@test prob.du0 zeros(length(unknowns(sys)))
580577
@test prob.p isa MTKParameters
@@ -583,27 +580,21 @@ let
583580
@test sol[y] 0.9 * sol[x[1]] + sol[x[2]]
584581
@test isapprox(sol[x[1]][end], 1, atol = 1e-3)
585582

586-
prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0], (0, 50); guesses = u0)
583+
prob = DAEProblem(sys, [D(x[1]) => 0, D(x[2]) => 0], (0, 50); guesses = u0)
587584
@test prob.du0 zeros(length(unknowns(sys)))
588585
@test prob.p isa MTKParameters
589586
@test prob.ps[k] 1
590587
sol = solve(prob, IDA())
591588
@test isapprox(sol[x[1]][end], 1, atol = 1e-3)
592589

593-
prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, k => 2],
590+
prob = DAEProblem(sys, [D(x[1]) => 0, D(x[2]) => 0, k => 2],
594591
(0, 50); guesses = u0)
595592
@test prob.du0 zeros(length(unknowns(sys)))
596593
@test prob.p isa MTKParameters
597594
@test prob.ps[k] 2
598595
sol = solve(prob, IDA())
599596
@test isapprox(sol[x[1]][end], 2, atol = 1e-3)
600597

601-
if !@isdefined(ModelingToolkit)
602-
# no initial conditions for D(x[1]) and D(x[2]) provided
603-
@test_throws ModelingToolkitBase.MissingVariablesError prob=DAEProblem(
604-
sys, Pair[], (0, 50); guesses = u0)
605-
end
606-
607598
prob = ODEProblem(sys, Pair[x[1] => 0], (0, 50))
608599
sol = solve(prob, Rosenbrock23())
609600
@test isapprox(sol[x[1]][end], 1, atol = 1e-3)
@@ -760,13 +751,9 @@ end
760751
eqs = [D(Q) ~ 1 / sin(P), D(P) ~ log(-cos(Q))]
761752
@named sys = System(eqs, t, [P, Q], [])
762753
sys = complete(debug_system(sys))
763-
if @isdefined(ModelingToolkit)
764-
prob = ODEProblem(sys, [], (0.0, 1.0))
765-
@test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0)
766-
@test_throws "/(1, sin(P(t))) output non-finite value" prob.f([0, 2], prob.p, 0.0)
767-
else
768-
@test_throws "/(1, sin(P(t))) output non-finite value" ODEProblem(sys, [], (0.0, 1.0))
769-
end
754+
prob = ODEProblem(sys, [], (0.0, 1.0))
755+
@test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0)
756+
@test_throws "/(1, sin(P(t))) output non-finite value" prob.f([0, 2], prob.p, 0.0)
770757

771758
let
772759
@variables x(t) = 1
@@ -996,11 +983,7 @@ end
996983
@variables x(t)[0:1] # 0-indexed variable array
997984
@named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], [])
998985
sys = @test_nowarn mtkcompile(sys)
999-
if @isdefined(ModelingToolkit)
1000-
@test full_equations(sys) == [D(x[1]) ~ 0.0]
1001-
else
1002-
@test issetequal(full_equations(sys), [D(x[1]) ~ x[0], 0 ~ -x[0]])
1003-
end
986+
@test full_equations(sys) == [D(x[1]) ~ 0.0]
1004987
end
1005988

1006989
# Namespacing of array variables

lib/ModelingToolkitBase/test/symbolic_events.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,7 @@ end
322322
@test length(ModelingToolkitBase.continuous_events(ball)) == 1
323323
cev = only(continuous_events(ball))
324324
@test isequal(only(equations(cev)), x ~ 0)
325-
if @isdefined(ModelingToolkit)
326-
@test isequal(only(observed(cev.affect.system)), v ~ -Pre(v))
327-
else
328-
@test isequal(only(equations(cev.affect.system)), 0 ~ -Pre(v) - v)
329-
end
325+
@test isequal(only(observed(cev.affect.system)), v ~ -Pre(v))
330326

331327
tspan = (0.0, 5.0)
332328
prob = ODEProblem(ball, Pair[], tspan)
@@ -358,13 +354,8 @@ end
358354
_cevs = getfield(ball, :continuous_events)
359355
@test isequal(only(equations(_cevs[1])), x ~ 0)
360356
@test issetequal(equations(_cevs[2]), [y ~ -1.5, y ~ 1.5])
361-
if @isdefined(ModelingToolkit)
362-
@test isequal(only(observed(_cevs[1].affect.system)), vx ~ -Pre(vx))
363-
@test isequal(only(observed(_cevs[2].affect.system)), vy ~ -Pre(vy))
364-
else
365-
@test isequal(only(equations(_cevs[1].affect.system)), 0 ~ -Pre(vx) - vx)
366-
@test isequal(only(equations(_cevs[2].affect.system)), 0 ~ -Pre(vy) - vy)
367-
end
357+
@test isequal(only(observed(_cevs[1].affect.system)), vx ~ -Pre(vx))
358+
@test isequal(only(observed(_cevs[2].affect.system)), vy ~ -Pre(vy))
368359
cond = cb.condition
369360
out = [0.0, 0.0, 0.0]
370361
p0 = 0.0

0 commit comments

Comments
 (0)