Skip to content

Commit 540184e

Browse files
Add tests
1 parent bdd8d9b commit 540184e

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

lib/OrdinaryDiffEqCore/src/initialize_dae.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ function _initialize_dae!(integrator, prob::Union{ODEProblem, DAEProblem},
178178
end
179179

180180
## CheckInit
181+
struct CheckInitFailureError <: Exception
182+
normresid
183+
abstol
184+
end
185+
186+
function Base.showerror(io::IO, e::CheckInitFailureError)
187+
print(io, "CheckInit specified but initialization not satisifed. normresid = $(e.normresid) > abstol = $(e.abstol)")
188+
end
181189

182190
function _initialize_dae!(integrator, prob::ODEProblem, alg::CheckInit,
183191
isinplace::Val{true})
@@ -195,7 +203,7 @@ function _initialize_dae!(integrator, prob::ODEProblem, alg::CheckInit,
195203

196204
normresid = integrator.opts.internalnorm(tmp, t)
197205
if normresid > integrator.opts.abstol
198-
error("CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator.opts.abstol )")
206+
throw(CheckInitFailureError(normresid, integrator.opts.abstol))
199207
end
200208
end
201209

@@ -212,9 +220,9 @@ function _initialize_dae!(integrator, prob::ODEProblem, alg::CheckInit,
212220
du = f(u0, p, t)
213221
resid = _vec(du)[algebraic_eqs]
214222

215-
normresid = integrator.opts.internalnorm(tmp, t)
223+
normresid = integrator.opts.internalnorm(resid, t)
216224
if normresid > integrator.opts.abstol
217-
error("CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator.opts.abstol )")
225+
throw(CheckInitFailureError(normresid, integrator.opts.abstol))
218226
end
219227
end
220228

@@ -227,7 +235,7 @@ function _initialize_dae!(integrator, prob::DAEProblem,
227235
f(resid, integrator.du, u0, p, t)
228236
normresid = integrator.opts.internalnorm(resid, t)
229237
if normresid > integrator.opts.abstol
230-
error("CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator.opts.abstol )")
238+
throw(CheckInitFailureError(normresid, integrator.opts.abstol))
231239
end
232240
end
233241

@@ -245,6 +253,6 @@ function _initialize_dae!(integrator, prob::DAEProblem,
245253
resid = f(integrator.du, u0, p, t)
246254
normresid = integrator.opts.internalnorm(resid, t)
247255
if normresid > integrator.opts.abstol
248-
error("CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator.opts.abstol )")
256+
throw(CheckInitFailureError(normresid, integrator.opts.abstol))
249257
end
250258
end

test/interface/checkinit_tests.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using OrdinaryDiffEqBDF, OrdinaryDiffEqRosenbrock, LinearAlgebra, ForwardDiff, Test
2+
using OrdinaryDiffEqCore
3+
4+
function rober(du, u, p, t)
5+
y₁, y₂, y₃ = u
6+
k₁, k₂, k₃ = p
7+
du[1] = -k₁ * y₁ + k₃ * y₂ * y₃
8+
du[2] = k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2
9+
du[3] = y₁ + y₂ + y₃ - 1
10+
nothing
11+
end
12+
function rober(u, p, t)
13+
y₁, y₂, y₃ = u
14+
k₁, k₂, k₃ = p
15+
[-k₁ * y₁ + k₃ * y₂ * y₃,
16+
k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2,
17+
y₁ + y₂ + y₃ - 1]
18+
end
19+
M = [1.0 0 0
20+
0 1.0 0
21+
0 0 0]
22+
roberf = ODEFunction(rober, mass_matrix = M)
23+
roberf_oop = ODEFunction{false}(rober, mass_matrix = M)
24+
prob_mm = ODEProblem(roberf, [1.0, 0.0, 0.2], (0.0, 1e5), (0.04, 3e7, 1e4))
25+
prob_mm_oop = ODEProblem(roberf_oop, [1.0, 0.0, 0.2], (0.0, 1e5), (0.04, 3e7, 1e4))
26+
27+
@test_throws OrdinaryDiffEqCore.CheckInitFailureError solve(prob_mm, Rodas5P(), reltol = 1e-8, abstol = 1e-8, initializealg = SciMLBase.CheckInit())
28+
@test_throws OrdinaryDiffEqCore.CheckInitFailureError solve(prob_mm_oop, Rodas5P(), reltol = 1e-8, abstol = 1e-8, initializealg = SciMLBase.CheckInit())
29+
30+
f_oop = function (du, u, p, t)
31+
out1 = -0.04u[1] + 1e4 * u[2] * u[3] - du[1]
32+
out2 = +0.04u[1] - 3e7 * u[2]^2 - 1e4 * u[2] * u[3] - du[2]
33+
out3 = u[1] + u[2] + u[3] - 1.0
34+
[out1, out2, out3]
35+
end
36+
37+
f = function (resid, du, u, p, t)
38+
resid[1] = -0.04u[1] + 1e4 * u[2] * u[3] - du[1]
39+
resid[2] = +0.04u[1] - 3e7 * u[2]^2 - 1e4 * u[2] * u[3] - du[2]
40+
resid[3] = u[1] + u[2] + u[3] - 1.0
41+
end
42+
43+
u₀ = [1.0, 0, 0.2]
44+
du₀ = [0.0, 0.0, 0.0]
45+
tspan = (0.0, 100000.0)
46+
differential_vars = [true, true, false]
47+
prob = DAEProblem(f, du₀, u₀, tspan, differential_vars = differential_vars)
48+
prob_oop = DAEProblem(f_oop, du₀, u₀, tspan, differential_vars = differential_vars)
49+
@test_throws OrdinaryDiffEqCore.CheckInitFailureError solve(prob, DFBDF(), reltol = 1e-8, abstol = 1e-8, initializealg = SciMLBase.CheckInit())
50+
@test_throws OrdinaryDiffEqCore.CheckInitFailureError solve(prob_oop, DFBDF(), reltol = 1e-8, abstol = 1e-8, initializealg = SciMLBase.CheckInit())

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ end
6565
@time @safetestset "Linear Solver Split ODE Tests" include("interface/linear_solver_split_ode_test.jl")
6666
@time @safetestset "Sparse Diff Tests" include("interface/sparsediff_tests.jl")
6767
@time @safetestset "Enum Tests" include("interface/enums.jl")
68-
@time @safetestset "Enum Tests" include("interface/get_du.jl")
68+
@time @safetestset "CheckInit Tests" include("interface/checkinit_tests.jl")
69+
@time @safetestset "Get du Tests" include("interface/get_du.jl")
6970
@time @safetestset "Mass Matrix Tests" include("interface/mass_matrix_tests.jl")
7071
@time @safetestset "W-Operator prototype tests" include("interface/wprototype_tests.jl")
7172
end

0 commit comments

Comments
 (0)