Skip to content

Commit 0facf9b

Browse files
test: update discrete system tests
1 parent 8747976 commit 0facf9b

File tree

2 files changed

+22
-88
lines changed

2 files changed

+22
-88
lines changed

test/discrete_system.jl

Lines changed: 18 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ u = ModelingToolkit.varmap_to_vars(
4040
Dict([S(k - 1) => 1, I(k - 1) => 2, R(k - 1) => 3]), unknowns(syss))
4141
p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5))
4242
df.f(du, u, p, 0)
43-
@test_broken getu(syss, [S, I, R])
4443
reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)])
4544
@test reorderer(du) [0.01831563888873422, 0.9816849729159067, 4.999999388195359]
4645

@@ -49,16 +48,17 @@ reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)])
4948
[0.01831563888873422, 0.9816849729159067, 4.999999388195359]
5049

5150
# Problem
52-
u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0]
51+
u0 = [S => 990.0, I => 10.0, R => 0.0]
5352
p ==> 0.05, c => 10.0, γ => 0.25, δt => 0.1, nsteps => 400]
5453
tspan = (0.0, ModelingToolkit.value(substitute(nsteps, p))) # value function (from Symbolics) is used to convert a Num to Float64
55-
prob_map = DiscreteProblem(syss, [u0; p], tspan)
54+
prob_map = DiscreteProblem(
55+
syss, [u0; p], tspan; guesses = [S(k - 1) => 1.0, I(k - 1) => 1.0, R(k - 1) => 1.0])
5656
@test prob_map.f.sys === syss
5757

5858
# Solution
5959
using OrdinaryDiffEq
6060
sol_map = solve(prob_map, FunctionMap());
61-
@test_broken sol_map[S] isa Vector
61+
@test sol_map[S] isa Vector
6262
@test sol_map[S(k - 1)] isa Vector
6363

6464
# Using defaults constructor
@@ -78,16 +78,16 @@ eqs2 = [S ~ S(k - 1) - infection2,
7878
eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ])
7979
@test ModelingToolkit.defaults(sys) != Dict()
8080

81-
@test_broken DiscreteProblem(sys, [], tspan)
82-
prob_map2 = DiscreteProblem(sys, [S(k - 1) => S, I(k - 1) => I, R(k - 1) => R], tspan)
81+
prob_map2 = DiscreteProblem(sys, [], tspan)
82+
# prob_map2 = DiscreteProblem(sys, [S(k - 1) => S, I(k - 1) => I, R(k - 1) => R], tspan)
8383
sol_map2 = solve(prob_map2, FunctionMap());
8484

8585
@test sol_map.u sol_map2.u
8686
for p in parameters(sys)
8787
@test sol_map.prob.ps[p] sol_map2.prob.ps[p]
8888
end
89-
@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end]
90-
@test_broken sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end]
89+
@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end] ==
90+
sol_map2[R][begin:(end - 1)]
9191
# Direct Implementation
9292

9393
function sir_map!(u_diff, u, p, t)
@@ -103,14 +103,12 @@ function sir_map!(u_diff, u, p, t)
103103
end
104104
nothing
105105
end;
106-
@test_broken prob_map2[[S, I, R]]
107-
u0 = prob_map2[[S(k - 1), I(k - 1), R(k - 1)]];
106+
u0 = sol_map2[[S, I, R], 1];
108107
p = [0.05, 10.0, 0.25, 0.1];
109108
prob_map = DiscreteProblem(sir_map!, u0, tspan, p);
110109
sol_map2 = solve(prob_map, FunctionMap());
111110

112-
@test_broken reduce(hcat, sol_map[[S, I, R]]) Array(sol_map2)
113-
@test reduce(hcat, sol_map[[S(k - 1), I(k - 1), R(k - 1)]]) Array(sol_map2)
111+
@test reduce(hcat, sol_map[[S, I, R]]) Array(sol_map2)
114112

115113
# Delayed difference equation
116114
# @variables x(..) y(..) z(t)
@@ -217,7 +215,7 @@ eqs = [u ~ 1
217215
prob = DiscreteProblem(de, [x(k - 1) => 0.0], (0, 10))
218216
sol = solve(prob, FunctionMap())
219217

220-
@test reduce(vcat, sol.u) == 1:11
218+
@test sol[x] == 1:11
221219

222220
# Issue#2585
223221
getdata(buffer, t) = buffer[mod1(Int(t), length(buffer))]
@@ -251,77 +249,12 @@ end
251249
@test_nowarn @mtkcompile sys = System(; buffer = ones(10))
252250

253251
@testset "Passing `nothing` to `u0`" begin
254-
@test_broken begin
255-
@variables x(t) = 1
256-
k = ShiftIndex()
257-
@mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t)
258-
prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0))
259-
@test_nowarn solve(prob, FunctionMap())
260-
end
261-
end
262-
263-
@testset "Initialization" begin
264-
@test_broken begin
265-
# test that default values apply to the entire history
266-
@variables x(t) = 1.0
267-
@mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t)
268-
prob = DiscreteProblem(de, [], (0, 10))
269-
@test prob[x] == 2.0
270-
@test prob[x(k - 1)] == 1.0
271-
272-
# must provide initial conditions for history
273-
@test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10))
274-
@test_throws ErrorException DiscreteProblem(de, [x(k + 1) => 2.0], (0, 10))
275-
276-
# initial values only affect _that timestep_, not the entire history
277-
prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10))
278-
@test prob[x] == 3.0
279-
@test prob[x(k - 1)] == 2.0
280-
@variables xₜ₋₁(t)
281-
@test prob[xₜ₋₁] == 2.0
282-
283-
# Test initial assignment with lowered variable
284-
prob = DiscreteProblem(de, [xₜ₋₁(k - 1) => 4.0], (0, 10))
285-
@test prob[x(k - 1)] == prob[xₜ₋₁] == 1.0
286-
@test prob[x] == 5.0
287-
288-
# Test missing initial throws error
289-
@variables x(t)
290-
@mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t)
291-
@test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10))
292-
@test_throws ErrorException prob=DiscreteProblem(
293-
de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10))
294-
295-
# Test non-assigned initials are given default value
296-
@variables x(t) = 2.0
297-
@mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t)
298-
prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10))
299-
@test prob[x] == 26.0
300-
@test prob[x(k - 1)] == 2.0
301-
@test prob[x(k - 2)] == 2.0
302-
303-
# Elaborate test
304-
@variables xₜ₋₂(t) zₜ₋₁(t) z(t)
305-
eqs = [x ~ x(k - 1) + z(k - 2),
306-
z ~ x(k - 2) * x(k - 3) - z(k - 1)^2]
307-
@mtkcompile de = System(eqs, t)
308-
u0 = [x(k - 1) => 3,
309-
xₜ₋₂(k - 1) => 4,
310-
x(k - 2) => 1,
311-
z(k - 1) => 5,
312-
zₜ₋₁(k - 1) => 12]
313-
prob = DiscreteProblem(de, u0, (0, 10))
314-
@test prob[x] == 15
315-
@test prob[z] == -21
316-
317-
import ModelingToolkit: shift2term
318-
# unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z
319-
vars = sort(ModelingToolkit.value.(unknowns(de)); by = string)
320-
@test isequal(shift2term(Shift(t, 1)(vars[2])), vars[1])
321-
@test isequal(shift2term(Shift(t, 1)(vars[3])), vars[2])
322-
@test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5])
323-
@test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3])
324-
end
252+
@variables x(t) = 1
253+
k = ShiftIndex()
254+
@mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t)
255+
prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0))
256+
sol = solve(prob, FunctionMap())
257+
@test SciMLBase.successful_retcode(sol)
325258
end
326259

327260
@testset "Shifted array variables" begin
@@ -339,6 +272,5 @@ end
339272
(0, 4))
340273
@test all(isone, prob.u0)
341274
sol = solve(prob, FunctionMap())
342-
@test_broken sol[[x..., y...], end]
343-
@test sol[[x(k - 1)..., y(k - 1)...], end] == 8ones(4)
275+
@test sol[[x..., y...], end] == 8ones(4)
344276
end

test/implicit_discrete_system.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ModelingToolkit, Test
1+
using ModelingToolkit, SymbolicIndexingInterface, Test
22
using ModelingToolkit: t_nounits as t
33
using StableRNGs
44

@@ -45,6 +45,8 @@ end
4545
1 - (u_next[1] + u_next[2])^2 - u_next[3]^2]
4646
end
4747

48+
reorderer = getu(sys, [x(k - 2), x(k - 1), y])
49+
4850
for _ in 1:10
4951
u_next = rand(rng, 3)
5052
u = rand(rng, 3)
@@ -73,6 +75,6 @@ end
7375
y(k) ~ x(k - 1) + x(k - 2),
7476
z(k) * x(k) ~ 3]
7577
@mtkcompile sys = System(eqs, t)
76-
@test occursin("var\"Shift(t, 1)(z(t))\"",
78+
@test occursin("var\"Shift(t, 1)(x(t))\"",
7779
string(ImplicitDiscreteFunction(sys; expression = Val{true})))
7880
end

0 commit comments

Comments
 (0)