Skip to content

Commit 358bdde

Browse files
Merge pull request #929 from SciML/nan_error
Error if NaN is in tspan
2 parents 67a9b64 + 2c2a52e commit 358bdde

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/solve.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ function Base.showerror(io::IO, e::NoTspanError)
169169
println(io, TruncatedStacktraces.VERBOSE_MSG)
170170
end
171171

172+
const NAN_TSPAN_MESSAGE = """
173+
NaN tspan is set in the problem or chosen in the init/solve call.
174+
Note that -Inf and Inf values are allowed in the timespan for solves
175+
which are terminated via callbacks, however NaN values are not allowed
176+
since the direction of time is undetermined.
177+
"""
178+
179+
struct NaNTspanError <: Exception end
180+
181+
function Base.showerror(io::IO, e::NaNTspanError)
182+
print(io, NAN_TSPAN_MESSAGE)
183+
println(io, TruncatedStacktraces.VERBOSE_MSG)
184+
end
185+
186+
172187
const NON_SOLVER_MESSAGE = """
173188
The arguments to solve are incorrect.
174189
The second argument must be a solver choice, `solve(prob,alg)`
@@ -1192,6 +1207,8 @@ function get_concrete_tspan(prob, isadapt, kwargs, p)
11921207

11931208
isadapt && eltype(tspan) <: Integer && (tspan = float.(tspan))
11941209

1210+
any(isnan, tspan) && throw(NaNTspanError())
1211+
11951212
tspan
11961213
end
11971214

test/downstream/solve_error_handling.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ prob = ODEProblem{false}(f, 1.0 + im, tspan)
2828
prob = ODEProblem{false}(f, u0, (nothing, nothing))
2929
@test_throws DiffEqBase.NoTspanError solve(prob, Tsit5())
3030

31+
prob = ODEProblem{false}(f, u0, (NaN, 1.0))
32+
@test_throws DiffEqBase.NaNTspanError solve(prob, Tsit5())
33+
34+
prob = ODEProblem{false}(f, u0, (1.0, NaN))
35+
@test_throws DiffEqBase.NaNTspanError solve(prob, Tsit5())
36+
3137
prob = ODEProblem{false}(f, Any[1.0, 1.0f0], tspan)
3238
@test_throws DiffEqBase.NonConcreteEltypeError solve(prob, Tsit5())
3339

0 commit comments

Comments
 (0)