Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segfault when interpolating on failed solution #476

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&

Check warning on line 148 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L148

Added line #L148 was not covered by tests
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 @@
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 @@
u = id.u
typeof(id) <: HermiteInterpolation && (du = id.du)
tdir = sign(t[end] - t[1])
t[end] == t[1] && tval != t[end] &&

Check warning on line 261 in src/interpolation.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolation.jl#L261

Added line #L261 was not covered by tests
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
Loading