Skip to content

Commit 47d72df

Browse files
test: update discrete system tests
1 parent 8747976 commit 47d72df

File tree

2 files changed

+10
-76
lines changed

2 files changed

+10
-76
lines changed

test/discrete_system.jl

Lines changed: 6 additions & 74 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

@@ -58,7 +57,7 @@ prob_map = DiscreteProblem(syss, [u0; p], tspan)
5857
# Solution
5958
using OrdinaryDiffEq
6059
sol_map = solve(prob_map, FunctionMap());
61-
@test_broken sol_map[S] isa Vector
60+
@test sol_map[S] isa Vector
6261
@test sol_map[S(k - 1)] isa Vector
6362

6463
# Using defaults constructor
@@ -86,8 +85,8 @@ sol_map2 = solve(prob_map2, FunctionMap());
8685
for p in parameters(sys)
8786
@test sol_map.prob.ps[p] sol_map2.prob.ps[p]
8887
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]
88+
@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end] ==
89+
sol_map2[R][begin:(end - 1)]
9190
# Direct Implementation
9291

9392
function sir_map!(u_diff, u, p, t)
@@ -103,14 +102,12 @@ function sir_map!(u_diff, u, p, t)
103102
end
104103
nothing
105104
end;
106-
@test_broken prob_map2[[S, I, R]]
107-
u0 = prob_map2[[S(k - 1), I(k - 1), R(k - 1)]];
105+
u0 = prob_map2[[S, I, R]];
108106
p = [0.05, 10.0, 0.25, 0.1];
109107
prob_map = DiscreteProblem(sir_map!, u0, tspan, p);
110108
sol_map2 = solve(prob_map, FunctionMap());
111109

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)
110+
@test reduce(hcat, sol_map[[S, I, R]]) Array(sol_map2)
114111

115112
# Delayed difference equation
116113
# @variables x(..) y(..) z(t)
@@ -260,70 +257,6 @@ end
260257
end
261258
end
262259

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
325-
end
326-
327260
@testset "Shifted array variables" begin
328261
@variables x(t)[1:2] y(t)[1:2]
329262
k = ShiftIndex(t)
@@ -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)