Skip to content

Commit 32b0634

Browse files
test: account for new @constants behavior in tests
1 parent cd6579e commit 32b0634

12 files changed

+44
-46
lines changed

test/components.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ end
230230
eqs = [
231231
v ~ i * R
232232
]
233-
extend(System(eqs, t, [], []; name = name), oneport)
233+
extend(System(eqs, t, [], [R]; name = name), oneport)
234234
end
235235
capacitor = Capacitor(; name = :c1, C = 1.0)
236236
resistor = FixedResistor(; name = :r1)

test/constants.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ MT = ModelingToolkit
44
UMT = ModelingToolkit.UnitfulUnitCheck
55

66
@constants a = 1
7-
@test_throws MT.ArgumentError @constants b
7+
@test isconstant(a)
8+
@test !istunable(a)
89

910
@independent_variables t
1011
@variables x(t) w(t)
@@ -14,9 +15,6 @@ eqs = [D(x) ~ a]
1415
prob = ODEProblem(complete(sys), [0], [0.0, 1.0], [])
1516
sol = solve(prob, Tsit5())
1617

17-
newsys = MT.eliminate_constants(sys)
18-
@test isequal(equations(newsys), [D(x) ~ 1])
19-
2018
# Test structural_simplify substitutions & observed values
2119
eqs = [D(x) ~ 1,
2220
w ~ a]
@@ -29,24 +27,23 @@ simp = structural_simplify(sys)
2927
@constants β=1 [unit = u"m/s"]
3028
UMT.get_unit(β)
3129
@test MT.isconstant(β)
30+
@test !MT.istunable(β)
3231
@independent_variables t [unit = u"s"]
3332
@variables x(t) [unit = u"m"]
3433
D = Differential(t)
3534
eqs = [D(x) ~ β]
3635
@named sys = System(eqs, t)
3736
simp = structural_simplify(sys)
3837

39-
@test isempty(MT.collect_constants(nothing))
40-
4138
@testset "Issue#3044" begin
42-
@constants h = 1
39+
@constants h
4340
@parameters τ = 0.5 * h
4441
@variables x(MT.t_nounits) = h
4542
eqs = [MT.D_nounits(x) ~ (h - x) / τ]
4643

4744
@mtkbuild fol_model = System(eqs, MT.t_nounits)
4845

49-
prob = ODEProblem(fol_model, [], (0.0, 10.0))
46+
prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1])
5047
@test prob[x] 1
5148
@test prob.ps[τ] 0.5
5249
end

test/discrete_system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ eqs = [S ~ S(k - 1) - infection * h,
3030
R ~ R(k - 1) + recovery]
3131

3232
# System
33-
@named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ])
33+
@named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ, h])
3434
syss = structural_simplify(sys)
3535
@test syss == syss
3636

test/dq_units.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ end
233233
L(t), [unit = u"m"]
234234
L_out(t), [unit = u"1"]
235235
end
236-
@test to_m in ModelingToolkit.vars(ModelingToolkit.fold_constants(Symbolics.unwrap(L_out *
237-
-to_m)))
236+
@test to_m in ModelingToolkit.vars(Symbolics.unwrap(L_out * -to_m))
238237

239238
# test units for registered functions
240239
let

test/funcaffect.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function bb_affect!(integ, u, p, ctx)
282282
integ.u[u.v] = -integ.u[u.v]
283283
end
284284

285-
@named bb_model = System(bb_eqs, t, sts, par,
285+
@named bb_model = System(bb_eqs, t, sts, [par; zr],
286286
continuous_events = [
287287
[y ~ zr] => (bb_affect!, [v], [], [], nothing)
288288
])

test/input_output_handling.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ end
447447
@constants c = 2.0
448448
@variables x(t)
449449
eqs = [D(x) ~ c * x]
450-
@mtkbuild sys = System(eqs, t, [x], [])
450+
@mtkbuild sys = System(eqs, t, [x], [c])
451451

452452
f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys)
453453
@test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) [1.0]

test/jumpsystem.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ModelingToolkit, DiffEqBase, JumpProcesses, Test, LinearAlgebra
2+
using SymbolicIndexingInterface
23
using Random, StableRNGs, NonlinearSolve
34
using OrdinaryDiffEq
45
using ModelingToolkit: t_nounits as t, D_nounits as D
@@ -17,7 +18,7 @@ rate₂ = γ * I + t
1718
affect₂ = [I ~ Pre(I) - 1, R ~ Pre(R) + 1]
1819
j₁ = ConstantRateJump(rate₁, affect₁)
1920
j₂ = VariableRateJump(rate₂, affect₂)
20-
@named js = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ])
21+
@named js = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ, h])
2122
unknowntoid = Dict(MT.value(unknown) => i for (i, unknown) in enumerate(unknowns(js)))
2223
mtjump1 = MT.assemble_crj(js, j₁, unknowntoid)
2324
mtjump2 = MT.assemble_vrj(js, j₂, unknowntoid)
@@ -38,7 +39,7 @@ jump2 = VariableRateJump(rate2, affect2!)
3839

3940
# test crjs
4041
u = [100, 9, 5]
41-
p = (0.1 / 1000, 0.01)
42+
p = (0.1 / 1000, 0.01, 1)
4243
tf = 1.0
4344
mutable struct TestInt{U, V, T}
4445
u::U
@@ -62,15 +63,15 @@ jump2.affect!(integrator)
6263
rate₃ = γ * I * h
6364
affect₃ = [I ~ Pre(I) * h - 1, R ~ Pre(R) + 1]
6465
j₃ = ConstantRateJump(rate₃, affect₃)
65-
@named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ])
66+
@named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ, h])
6667
js2 = complete(js2)
6768
u₀ = [999, 1, 0];
68-
p = (0.1 / 1000, 0.01);
6969
tspan = (0.0, 250.0);
7070
u₀map = [S => 999, I => 1, R => 0]
7171
parammap ==> 0.1 / 1000, γ => 0.01]
7272
jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(),
7373
save_positions = (false, false), rng)
74+
p = parameter_values(jprob)
7475
@test jprob.prob isa DiscreteProblem
7576
Nsims = 30000
7677
function getmean(jprob, Nsims; use_stepper = true)
@@ -90,7 +91,7 @@ mb = getmean(jprobb, Nsims; use_stepper = false)
9091

9192
@variables S2(t)
9293
obs = [S2 ~ 2 * S]
93-
@named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ], observed = obs)
94+
@named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ, h], observed = obs)
9495
js2b = complete(js2b)
9596
jprob = JumpProblem(js2b, u₀map, tspan, parammap; aggregator = Direct(),
9697
save_positions = (false, false), rng)
@@ -110,6 +111,8 @@ jump2 = ConstantRateJump(rate2, affect2!)
110111
mtjumps = jprob.discrete_jump_aggregation
111112
@test abs(mtjumps.rates[1](u, p, tf) - jump1.rate(u, p, tf)) < 10 * eps()
112113
@test abs(mtjumps.rates[2](u, p, tf) - jump2.rate(u, p, tf)) < 10 * eps()
114+
115+
ModelingToolkit.@set! mtintegrator.p = (mtintegrator.p, (1,))
113116
mtjumps.affects![1](mtintegrator)
114117
jump1.affect!(integrator)
115118
@test all(integrator.u .== mtintegrator.u)

test/model_parsing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ using ModelingToolkit: getdefault, scalarize
511511

512512
@test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) ==
513513
eval(u"")
514-
@test lastindex(parameters(model_with_component_array)) == 3
514+
@test lastindex(parameters(model_with_component_array)) == 4
515515

516516
# Test the constant `k`. Manually k's value should be kept in sync here
517517
# and the ModelParsingPrecompile.

test/nonlinearsystem.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ end
2626
eqs = [0 ~ σ * (y - x) * h,
2727
0 ~ x *- z) - y,
2828
0 ~ x * y - β * z]
29-
@named ns = System(eqs, [x, y, z], [σ, ρ, β], defaults = Dict(x => 2))
29+
@named ns = System(eqs, [x, y, z], [σ, ρ, β, h], defaults = Dict(x => 2))
3030
@test eval(toexpr(ns)) == ns
31-
test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β))
31+
test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β, h))
3232
@test begin
33-
f = generate_rhs(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2]
33+
f = generate_rhs(ns, [x, y, z], [σ, ρ, β, h], expression = Val{false})[2]
3434
du = [0.0, 0.0, 0.0]
35-
f(du, [1, 2, 3], [1, 2, 3])
35+
f(du, [1, 2, 3], [1, 2, 3, 1])
3636
du [1, -3, -7]
3737
end
3838

@@ -64,9 +64,9 @@ a = y - x
6464
eqs = [0 ~ σ * a * h,
6565
0 ~ x *- z) - y,
6666
0 ~ x * y - β * z]
67-
@named ns = System(eqs, [x, y, z], [σ, ρ, β])
67+
@named ns = System(eqs, [x, y, z], [σ, ρ, β, h])
6868
ns = complete(ns)
69-
nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β])
69+
nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β, h])
7070
nf = NonlinearFunction(ns)
7171
jac = calculate_jacobian(ns)
7272

@@ -99,7 +99,7 @@ eqs1 = [
9999
0 ~ x + y - z - u
100100
]
101101

102-
lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name)
102+
lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β, h], name = name)
103103
lorenz1 = lorenz(:lorenz1)
104104
@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5), zeros(3))
105105
lorenz2 = lorenz(:lorenz2)
@@ -132,7 +132,7 @@ sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7)
132132
eqs = [0 ~ σ * (y - x),
133133
0 ~ x *- z) - y,
134134
0 ~ x * y - β * z * h]
135-
@named ns = System(eqs, [x, y, z], [σ, ρ, β])
135+
@named ns = System(eqs, [x, y, z], [σ, ρ, β, h])
136136
np = NonlinearProblem(
137137
complete(ns), [0, 0, 0], [σ => 1, ρ => 2, β => 3], jac = true, sparse = true)
138138
@test calculate_jacobian(ns, sparse = true) isa SparseMatrixCSC
@@ -214,7 +214,7 @@ testdict = Dict([:test => 1])
214214
eqs = [0 ~ a * (y - x) * h,
215215
0 ~ x * (b - z) - y,
216216
0 ~ x * y - c * z]
217-
@named sys = System(eqs, [x, y, z], [a, b, c], defaults = Dict(x => 2.0))
217+
@named sys = System(eqs, [x, y, z], [a, b, c, h], defaults = Dict(x => 2.0))
218218
sys = complete(sys)
219219
prob = NonlinearProblem(sys, ones(length(unknowns(sys))))
220220

test/odesystem.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ModelingToolkit.toexpr.(eqs)[1]
2727
@named de = System(eqs, t; defaults = Dict(x => 1))
2828
subed = substitute(de, [σ => k])
2929
ssort(eqs) = sort(eqs, by = string)
30-
@test isequal(ssort(parameters(subed)), [k, β, ρ])
30+
@test isequal(ssort(parameters(subed)), [k, β, κ, ρ])
3131
@test isequal(equations(subed),
3232
[D(x) ~ k * (y - x)
3333
D(y) ~- z) * x - y
@@ -47,7 +47,7 @@ function test_diffeq_inference(name, sys, iv, dvs, ps)
4747
end
4848
end
4949

50-
test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β])
50+
test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β, κ])
5151
jac_expr = generate_jacobian(de)
5252
jac = calculate_jacobian(de)
5353
jacfun = eval(jac_expr[2])
@@ -138,11 +138,11 @@ tgrad_iip(du, u, p, t)
138138
eqs = [D(x) ~ σ(t - 1) * (y - x),
139139
D(y) ~ x *- z) - y,
140140
D(z) ~ x * y - β * z * κ]
141-
@named de = System(eqs, t, [x, y, z], [σ, ρ, β])
142-
test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β))
141+
@named de = System(eqs, t, [x, y, z], [σ, ρ, β, κ])
142+
test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β, κ))
143143
f = generate_rhs(de, expression = Val{false}, wrap_gfw = Val{true})
144144
du = [0.0, 0.0, 0.0]
145-
f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0)
145+
f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3, 1], 5.0)
146146
@test du [11, -3, -7]
147147

148148
eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)]
@@ -1254,7 +1254,8 @@ end
12541254
prob = ODEProblem(sys, u0, (0.0, 1.0), p)
12551255

12561256
# evaluate
1257-
u0_v, p_v, _ = ModelingToolkit.get_u0_p(sys, u0, p)
1257+
u0_v = prob.u0
1258+
p_v = prob.p
12581259
@test prob.f(u0_v, p_v, 0.0) == [c_b, c_a]
12591260
end
12601261

test/parameter_dependencies.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ end
296296
j₁ = ConstantRateJump(rate₁, affect₁)
297297
j₃ = ConstantRateJump(rate₃, affect₃)
298298
@named js2 = JumpSystem(
299-
[j₃], t, [S, I, R], [γ]; parameter_dependencies ==> 0.01γ])
300-
@test isequal(only(parameters(js2)), γ)
301-
@test Set(full_parameters(js2)) == Set([γ, β])
299+
[j₃], t, [S, I, R], [γ, h]; parameter_dependencies ==> 0.01γ])
300+
@test issetequal(parameters(js2), [γ, h])
301+
@test Set(full_parameters(js2)) == Set([γ, β, h])
302302
js2 = complete(js2)
303303
tspan = (0.0, 250.0)
304304
u₀map = [S => 999, I => 1, R => 0]
@@ -310,7 +310,7 @@ end
310310
@test_nowarn solve(jprob, SSAStepper())
311311

312312
@named js2 = JumpSystem(
313-
[j₁, j₃], t, [S, I, R], [γ]; parameter_dependencies ==> 0.01γ],
313+
[j₁, j₃], t, [S, I, R], [γ, h]; parameter_dependencies ==> 0.01γ],
314314
discrete_events = [SymbolicDiscreteCallback(
315315
[10.0] =>~ 0.02], discrete_parameters = [γ])])
316316
js2 = complete(js2)

test/structural_transformation/tearing.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ eqs = [
1818
0 ~ u4 - hypot(u2, u3),
1919
0 ~ u5 - hypot(u4, u1)
2020
]
21-
@named sys = System(eqs, [u1, u2, u3, u4, u5], [])
21+
@named sys = System(eqs, [u1, u2, u3, u4, u5], [h])
2222
state = TearingState(sys)
2323
StructuralTransformations.find_solvables!(state)
2424

@@ -149,19 +149,17 @@ eqs = [D(x) ~ z * h
149149
0 ~ sin(z) + y - p * t]
150150
@named daesys = System(eqs, t)
151151
newdaesys = structural_simplify(daesys)
152-
@test equations(newdaesys) == [D(x) ~ z; 0 ~ y + sin(z) - p * t]
153-
@test equations(tearing_substitution(newdaesys)) == [D(x) ~ z; 0 ~ x + sin(z) - p * t]
152+
@test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t]
153+
@test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t]
154154
@test isequal(unknowns(newdaesys), [x, z])
155-
@test isequal(unknowns(newdaesys), [x, z])
156-
@test_deprecated ODAEProblem(newdaesys, [x => 1.0, z => -0.5π], (0, 1.0), [p => 0.2])
157155
prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π], (0, 1.0), [p => 0.2])
158156
du = [0.0, 0.0];
159157
u = [1.0, -0.5π];
160-
pr = 0.2;
158+
pr = prob.p;
161159
tt = 0.1;
162160
@test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0
163161
prob.f(du, u, pr, tt)
164-
@test du[u[2], u[1] + sin(u[2]) - pr * tt] atol=1e-5
162+
@test du[u[2], u[1] + sin(u[2]) - prob.ps[p] * tt] atol=1e-5
165163

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

0 commit comments

Comments
 (0)