Skip to content

Commit a276947

Browse files
Merge pull request #2630 from vyudu/handle_u0_nothing
ImplicitDiscreteSolve: handle case where u is nothing
2 parents 050653e + c25f062 commit a276947

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

lib/ImplicitDiscreteSolve/src/cache.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mutable struct ImplicitDiscreteState{uType, pType, tType}
2-
u::Vector{uType}
2+
u::uType
33
p::pType
44
t_next::tType
55
end
@@ -16,7 +16,7 @@ function alg_cache(alg::SimpleIDSolve, u, rate_prototype, ::Type{uEltypeNoUnits}
1616
dt, reltol, p, calck,
1717
::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
1818

19-
state = ImplicitDiscreteState(similar(u), p, t)
19+
state = ImplicitDiscreteState(isnothing(u) ? nothing : zero(u), p, t)
2020
SimpleIDSolveCache(u, uprev, state, nothing)
2121
end
2222

@@ -31,7 +31,7 @@ function alg_cache(alg::SimpleIDSolve, u, rate_prototype, ::Type{uEltypeNoUnits}
3131
dt, reltol, p, calck,
3232
::Val{false}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
3333

34-
state = ImplicitDiscreteState(similar(u), p, t)
34+
state = ImplicitDiscreteState(isnothing(u) ? nothing : zero(u), p, t)
3535
SimpleIDSolveCache(u, uprev, state, nothing)
3636
end
3737

lib/ImplicitDiscreteSolve/src/solve.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function perform_step!(integrator, cache::SimpleIDSolveCache, repeat_step = fals
1212
end
1313

1414
function initialize!(integrator, cache::SimpleIDSolveCache)
15-
cache.state.u .= integrator.u
15+
integrator.u isa AbstractVector && (cache.state.u .= integrator.u)
1616
cache.state.p = integrator.p
1717
cache.state.t_next = integrator.t
1818
f = integrator.f
@@ -33,6 +33,7 @@ end
3333

3434
function _initialize_dae!(integrator, prob::ImplicitDiscreteProblem,
3535
alg::DefaultInit, x::Union{Val{true}, Val{false}})
36+
isnothing(prob.u0) && return
3637
atol = one(eltype(prob.u0)) * 1e-12
3738
if SciMLBase.has_initializeprob(prob.f)
3839
_initialize_dae!(integrator, prob,

lib/ImplicitDiscreteSolve/test/runtests.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ end
6565

6666
for ts in 1:tsteps
6767
step!(integ)
68-
@show integ.u
6968
@test integ.u[1]^2 + integ.u[2]^2 16
7069
end
7170
end
71+
72+
@testset "Handle nothing in u0" begin
73+
function empty(u_next, u, p, t)
74+
nothing
75+
end
76+
77+
tsteps = 5
78+
u0 = nothing
79+
idprob = ImplicitDiscreteProblem(empty, u0, (0, tsteps), [])
80+
@test_nowarn integ = init(idprob, SimpleIDSolve())
81+
end

0 commit comments

Comments
 (0)