Skip to content

Commit 6bd3679

Browse files
Merge pull request #3688 from AayushSabharwal/as/mark-broken
test: mark appropriate tests as broken
2 parents 12ef813 + f382bbb commit 6bd3679

File tree

3 files changed

+95
-88
lines changed

3 files changed

+95
-88
lines changed

src/problems/discreteproblem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
add_toterms!(u0map; replace = true)
4848
f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op;
4949
t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression,
50-
kwargs...)
50+
build_initializeprob = false, kwargs...)
5151

5252
if expression == Val{true}
5353
u0 = :(f($u0, p, tspan[1]))

test/discrete_system.jl

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ 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-
reorderer = getu(syss, [S, I, R])
43+
@test_broken getu(syss, [S, I, R])
44+
reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)])
4445
@test reorderer(du) [0.01831563888873422, 0.9816849729159067, 4.999999388195359]
4546

4647
# oop
@@ -57,7 +58,8 @@ prob_map = DiscreteProblem(syss, [u0; p], tspan)
5758
# Solution
5859
using OrdinaryDiffEq
5960
sol_map = solve(prob_map, FunctionMap());
60-
@test sol_map[S] isa Vector
61+
@test_broken sol_map[S] isa Vector
62+
@test sol_map[S(k - 1)] isa Vector
6163

6264
# Using defaults constructor
6365
@parameters c=10.0 nsteps=400 δt=0.1 β=0.05 γ=0.25
@@ -73,18 +75,19 @@ eqs2 = [S ~ S(k - 1) - infection2,
7375
R2 ~ R]
7476

7577
@mtkcompile sys = System(
76-
eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan)
78+
eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ])
7779
@test ModelingToolkit.defaults(sys) != Dict()
7880

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

8285
@test sol_map.u sol_map2.u
8386
for p in parameters(sys)
8487
@test sol_map.prob.ps[p] sol_map2.prob.ps[p]
8588
end
86-
@test_throws Any sol_map2[R2]
87-
@test 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+
@test_broken sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end]
8891
# Direct Implementation
8992

9093
function sir_map!(u_diff, u, p, t)
@@ -100,12 +103,14 @@ function sir_map!(u_diff, u, p, t)
100103
end
101104
nothing
102105
end;
103-
u0 = prob_map2[[S, I, R]];
106+
@test_broken prob_map2[[S, I, R]]
107+
u0 = prob_map2[[S(k - 1), I(k - 1), R(k - 1)]];
104108
p = [0.05, 10.0, 0.25, 0.1];
105109
prob_map = DiscreteProblem(sir_map!, u0, tspan, p);
106110
sol_map2 = solve(prob_map, FunctionMap());
107111

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

110115
# Delayed difference equation
111116
# @variables x(..) y(..) z(t)
@@ -245,79 +250,78 @@ end
245250

246251
@test_nowarn @mtkcompile sys = System(; buffer = ones(10))
247252

248-
# Ensure discrete systems with algebraic equations throw
249-
@variables x(t) y(t)
250-
k = ShiftIndex(t)
251-
@named sys = System([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t)
252-
253253
@testset "Passing `nothing` to `u0`" begin
254-
@variables x(t) = 1
255-
k = ShiftIndex()
256-
@mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t)
257-
prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0))
258-
@test_nowarn solve(prob, FunctionMap())
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
259261
end
260262

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

323327
@testset "Shifted array variables" begin
@@ -335,5 +339,6 @@ end
335339
(0, 4))
336340
@test all(isone, prob.u0)
337341
sol = solve(prob, FunctionMap())
338-
@test sol[[x..., y...], end] == 8ones(4)
342+
@test_broken sol[[x..., y...], end]
343+
@test sol[[x(k - 1)..., y(k - 1)...], end] == 8ones(4)
339344
end

test/extensions/ad.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ ps = [p => zeros(3, 3),
2222
q => 1.0]
2323
tspan = (0.0, 10.0)
2424
@mtkcompile sys = System(eqs, t)
25-
prob = ODEProblem(sys, u0, tspan, ps)
25+
prob = ODEProblem(sys, [u0; ps], tspan)
2626
sol = solve(prob, Tsit5())
2727

2828
mtkparams = parameter_values(prob)
2929
new_p = rand(14)
30-
gs = gradient(new_p) do new_p
31-
new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p)
32-
new_prob = remake(prob, p = new_params)
33-
new_sol = solve(new_prob, Tsit5())
34-
sum(new_sol)
30+
@test_broken begin
31+
gs = gradient(new_p) do new_p
32+
new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p)
33+
new_prob = remake(prob, p = new_params)
34+
new_sol = solve(new_prob, Tsit5())
35+
sum(new_sol)
36+
end
3537
end
3638

3739
@testset "Issue#2997" begin
@@ -50,7 +52,7 @@ end
5052
sys = mtkcompile(sys)
5153

5254
function x_at_0(θ)
53-
prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 1.0), [sys.ργ0 => θ[1], sys.h => θ[2]])
55+
prob = ODEProblem(sys, [sys.x => 1.0, sys.ργ0 => θ[1], sys.h => θ[2]], (0.0, 1.0))
5456
return prob.u0[1]
5557
end
5658

@@ -61,7 +63,7 @@ end
6163
@named sys = System(
6264
Equation[], t, [], [a, b, c, d, e, f, g, h],
6365
continuous_events = [ModelingToolkit.SymbolicContinuousCallback(
64-
[a ~ 0] => [c ~ 0], discrete_parameters = c)])
66+
[a ~ 0] => [c ~ 0], discrete_parameters = c, iv = t)])
6567
sys = complete(sys)
6668

6769
ivs = Dict(c => 3a, b => ones(3), a => 1.0, d => 4, e => [5.0, 6.0, 7.0],
@@ -116,7 +118,7 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals)
116118
sys = mtkcompile(sys)
117119

118120
# Find initial throw velocity that reaches exactly 10 m after 1 s
119-
dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0])
121+
dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0); guesses = [y => 0.0])
120122
function f(ics, _)
121123
dprob = remake(dprob0, u0 = Dict(D(y) => ics[1]))
122124
dsol = solve(dprob, Tsit5())

0 commit comments

Comments
 (0)