Skip to content

Commit 61470f6

Browse files
committed
feat: InitialFailure if initialization fails
1 parent 1fbb4c0 commit 61470f6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/ImplicitDiscreteSolve/src/solve.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function _initialize_dae!(integrator, prob::ImplicitDiscreteProblem,
5656
NonlinearProblem{isinplace(f)}(_f, u, initstate)
5757
end
5858
sol = solve(prob, SimpleNewtonRaphson())
59-
integrator.u = sol
59+
if sol.retcode == ReturnCode.Success
60+
integrator.u = sol
61+
else
62+
integrator.sol = SciMLBase.solution_new_retcode(integrator.sol, ReturnCode.InitialFailure)
63+
end
6064
end
6165
end

lib/ImplicitDiscreteSolve/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Test
33
using ImplicitDiscreteSolve
44
using OrdinaryDiffEqCore
55
using OrdinaryDiffEqSDIRK
6+
using SciMLBase
67

78
# Test implicit Euler using ImplicitDiscreteProblem
89
@testset "Implicit Euler" begin
@@ -105,3 +106,16 @@ end
105106
integ = init(idprob, IDSolve())
106107
@test integ.cache.prob isa NonlinearProblem
107108
end
109+
110+
@testset "InitialFailure thrown" begin
111+
function bad(u_next, u, p, t)
112+
[u_next[1] - u_next[2], u_next[1] - 3, u_next[2] - 4]
113+
end
114+
115+
u0 = [3., 4.]
116+
idprob = ImplicitDiscreteProblem(bad, u0, (0, 0), [])
117+
integ = init(idprob, IDSolve())
118+
@test check_error(integ) == ReturnCode.InitialFailure
119+
sol = solve(idprob, IDSolve())
120+
@test length(sol.u) == 1
121+
end

0 commit comments

Comments
 (0)