Skip to content

Commit d75e2f3

Browse files
test: test CheckInit on Sundials solvers
1 parent e3217f3 commit d75e2f3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

test/downstream/initialization.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using OrdinaryDiffEq, Sundials, SciMLBase, Test
2+
3+
@testset "CheckInit" begin
4+
@testset "Sundials + ODEProblem" begin
5+
function rhs(u, p, t)
6+
return [u[1] * t, u[1]^2 - u[2]^2]
7+
end
8+
function rhs!(du, u, p, t)
9+
du[1] = u[1] * t
10+
du[2] = u[1]^2 - u[2]^2
11+
end
12+
13+
oopfn = ODEFunction{false}(rhs, mass_matrix = [1 0; 0 0])
14+
iipfn = ODEFunction{true}(rhs!, mass_matrix = [1 0; 0 0])
15+
16+
@testset "Inplace = $(SciMLBase.isinplace(f))" for f in [oopfn, iipfn]
17+
prob = ODEProblem(f, [1.0, 1.0], (0.0, 1.0))
18+
integ = init(prob, Sundials.ARKODE())
19+
u0, _, success = SciMLBase.get_initial_values(
20+
prob, integ, f, SciMLBase.CheckInit(), Val(SciMLBase.isinplace(f)))
21+
@test success
22+
@test u0 == prob.u0
23+
24+
integ.u[2] = 2.0
25+
@test_throws SciMLBase.CheckInitFailureError SciMLBase.get_initial_values(
26+
prob, integ, f, SciMLBase.CheckInit(), Val(SciMLBase.isinplace(f)))
27+
end
28+
end
29+
30+
@testset "Sundials + DAEProblem" begin
31+
function daerhs(du, u, p, t)
32+
return [du[1] - u[1] * t - p, u[1]^2 - u[2]^2]
33+
end
34+
function daerhs!(resid, du, u, p, t)
35+
resid[1] = du[1] - u[1] * t - p
36+
resid[2] = u[1]^2 - u[2]^2
37+
end
38+
39+
oopfn = DAEFunction{false}(daerhs)
40+
iipfn = DAEFunction{true}(daerhs!)
41+
42+
@testset "Inplace = $(SciMLBase.isinplace(f))" for f in [oopfn, iipfn]
43+
prob = DAEProblem(f, [1.0, 0.0], [1.0, 1.0], (0.0, 1.0), 1.0)
44+
integ = init(prob, Sundials.IDA())
45+
u0, _, success = SciMLBase.get_initial_values(
46+
prob, integ, f, SciMLBase.CheckInit(), Val(SciMLBase.isinplace(f)))
47+
@test success
48+
@test u0 == prob.u0
49+
50+
integ.u[2] = 2.0
51+
@test_throws SciMLBase.CheckInitFailureError SciMLBase.get_initial_values(
52+
prob, integ, f, SciMLBase.CheckInit(), Val(SciMLBase.isinplace(f)))
53+
54+
integ.u[2] = 1.0
55+
integ.du[1] = 2.0
56+
@test_throws SciMLBase.CheckInitFailureError SciMLBase.get_initial_values(
57+
prob, integ, f, SciMLBase.CheckInit(), Val(SciMLBase.isinplace(f)))
58+
end
59+
end
60+
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ end
121121
@time @safetestset "Tables interface with MTK" begin
122122
include("downstream/tables.jl")
123123
end
124+
@time @safetestset "Initialization" begin
125+
include("downstream/initialization.jl")
126+
end
124127
end
125128

126129
if !is_APPVEYOR && (GROUP == "Downstream" || GROUP == "SymbolicIndexingInterface")

0 commit comments

Comments
 (0)