Skip to content

Commit

Permalink
Merge pull request #476 from oscardssmith/fix-interpolation-bounds
Browse files Browse the repository at this point in the history
fix segfault when interpolating on failed solution
  • Loading branch information
ChrisRackauckas authored Aug 10, 2023
2 parents f549934 + 308e0a9 commit 0bffe3e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ end
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&
error("Solution interpolation cannot extrapolate from a single timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
idx = sortperm(tvals, rev = tdir < 0)
i = 2 # Start the search thinking it's between t[1] and t[2]
tdir * tvals[idx[end]] > tdir * t[end] &&
Expand Down Expand Up @@ -143,6 +145,8 @@ times t (sorted), with values u and derivatives ks
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&
error("Solution interpolation cannot extrapolate from a single timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
idx = sortperm(tvals, rev = tdir < 0)
i = 2 # Start the search thinking it's between t[1] and t[2]
tdir * tvals[idx[end]] > tdir * t[end] &&
Expand Down Expand Up @@ -204,6 +208,8 @@ times t (sorted), with values u and derivatives ks
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&
error("Solution interpolation cannot extrapolate from a single timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
tdir * tval > tdir * t[end] &&
error("Solution interpolation cannot extrapolate past the final timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
tdir * tval < tdir * t[1] &&
Expand Down Expand Up @@ -252,6 +258,8 @@ times t (sorted), with values u and derivatives ks
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&
error("Solution interpolation cannot extrapolate from a single timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
tdir * tval > tdir * t[end] &&
error("Solution interpolation cannot extrapolate past the final timepoint. Either solve on a longer timespan or use the local extrapolation from the integrator interface.")
tdir * tval < tdir * t[1] &&
Expand Down
10 changes: 10 additions & 0 deletions test/solution_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ end
nothing) # strs
@test plot_vecs[2][:, 2] @. exp(-plot_vecs[1][:, 2])
end

@testset "interpolate empty ODE solution" begin
f = (u, p, t) -> -u
ode = ODEProblem(f, 1.0, (0.0, 1.0))
sol = SciMLBase.build_solution(ode, :NoAlgorithm, [ode.tspan[begin]], [ode.u0])
@test sol(0.0) == 1.0
# test that indexing out of bounds doesn't segfault
@test_throws ErrorException sol(1.0)
@test_throws ErrorException sol(-0.5)
end

0 comments on commit 0bffe3e

Please sign in to comment.