Skip to content

Commit 661cafa

Browse files
fix: handle empty idxs in interpolation
1 parent c843174 commit 661cafa

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/solutions/ode_solutions.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ end
245245
function (sol::AbstractODESolution)(t::Number, ::Type{deriv},
246246
idxs::AbstractVector{<:Integer},
247247
continuity) where {deriv}
248+
if isempty(idxs)
249+
return eltype(eltype(sol.u))[]
250+
end
248251
if eltype(sol.u) <: Number
249252
idxs = only(idxs)
250253
end
@@ -259,6 +262,9 @@ end
259262
function (sol::AbstractODESolution)(t::AbstractVector{<:Number}, ::Type{deriv},
260263
idxs::AbstractVector{<:Integer},
261264
continuity) where {deriv}
265+
if isempty(idxs)
266+
return map(_ -> eltype(eltype(sol.u))[], t)
267+
end
262268
if eltype(sol.u) <: Number
263269
idxs = only(idxs)
264270
end
@@ -295,6 +301,9 @@ function (sol::AbstractODESolution)(t::Number, ::Type{deriv}, idxs::AbstractVect
295301
any(isequal(NotSymbolic()), symbolic_type.(idxs))
296302
error("Incorrect specification of `idxs`")
297303
end
304+
if isempty(idxs)
305+
return eltype(eltype(sol.u))[]
306+
end
298307
error_if_observed_derivative(sol, idxs, deriv)
299308
ps = parameter_values(sol)
300309
if is_parameter_timeseries(sol) == Timeseries() && is_discrete_expression(sol, idxs)
@@ -335,6 +344,9 @@ end
335344

336345
function (sol::AbstractODESolution)(t::AbstractVector{<:Number}, ::Type{deriv},
337346
idxs::AbstractVector, continuity) where {deriv}
347+
if isempty(idxs)
348+
return map(_ -> eltype(eltype(sol.u))[], t)
349+
end
338350
error_if_observed_derivative(sol, idxs, deriv)
339351
p = hasproperty(sol.prob, :p) ? sol.prob.p : nothing
340352
getter = getu(sol, idxs)

test/solution_interface.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,17 @@ end
3737
@test_throws ErrorException sol(-0.5)
3838
@test_throws ErrorException sol([0, -0.5, 0])
3939
end
40+
41+
@testset "interpolate with empty idxs" begin
42+
f = (u, p, t) -> u
43+
sol1 = SciMLBase.build_solution(
44+
ODEProblem(f, 1.0, (0.0, 1.0)), :NoAlgorithm, 0.0:0.1:1.0, exp.(0.0:0.1:1.0))
45+
sol2 = SciMLBase.build_solution(ODEProblem(f, [1.0, 2.0], (0.0, 1.0)), :NoAlgorithm,
46+
0.0:0.1:1.0, vcat.(exp.(0.0:0.1:1.0), 2exp.(0.0:0.1:1.0)))
47+
for sol in [sol1, sol2]
48+
@test sol(0.15; idxs = []) == Float64[]
49+
@test sol(0.15; idxs = Int[]) == Float64[]
50+
@test sol([0.15, 0.25]; idxs = []) == [Float64[], Float64[]]
51+
@test sol([0.15, 0.25]; idxs = Int[]) == [Float64[], Float64[]]
52+
end
53+
end

0 commit comments

Comments
 (0)