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

make the saveat and tstop handling match OrdinaryDiffEq more closely #436

Merged
merged 2 commits into from
Nov 21, 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
9 changes: 4 additions & 5 deletions src/common_interface/integrator_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
uType = typeof(integrator.sol.prob.u0)
# The call to first is an overload of Base.first implemented in DataStructures
while !isempty(integrator.opts.saveat) &&
integrator.tdir * first(integrator.opts.saveat) < integrator.tdir * integrator.t
first(integrator.opts.saveat) <= integrator.tdir * integrator.t
saved = true
curt = pop!(integrator.opts.saveat)

curt = integrator.tdir * pop!(integrator.opts.saveat)
tmp = integrator(curt)
save_value!(integrator.sol.u, tmp, uType,
integrator.opts.save_idxs, false)
Expand Down Expand Up @@ -123,13 +122,13 @@
function DiffEqBase.add_tstop!(integrator::AbstractSundialsIntegrator, t)
integrator.tdir * (t - integrator.t) < 0 &&
error("Tried to add a tstop that is behind the current time. This is strictly forbidden")
push!(integrator.opts.tstops, t)
push!(integrator.opts.tstops, integrator.tdir * t)

Check warning on line 125 in src/common_interface/integrator_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/common_interface/integrator_utils.jl#L125

Added line #L125 was not covered by tests
end

function DiffEqBase.add_saveat!(integrator::AbstractSundialsIntegrator, t)
integrator.tdir * (t - integrator.t) < 0 &&
error("Tried to add a saveat that is behind the current time. This is strictly forbidden")
push!(integrator.opts.saveat, t)
push!(integrator.opts.saveat, integrator.tdir * t)

Check warning on line 131 in src/common_interface/integrator_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/common_interface/integrator_utils.jl#L131

Added line #L131 was not covered by tests
end

DiffEqBase.get_tmp_cache(integrator::AbstractSundialsIntegrator) = (integrator.tmp,)
Expand Down
60 changes: 21 additions & 39 deletions src/common_interface/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -936,42 +936,25 @@
end # function solve

function tstop_saveat_disc_handling(tstops, saveat, tdir, tspan, tType)
if isempty(tstops) # TODO: Specialize more
tstops_vec = [tspan[2]]
else
tstops_vec = vec(collect(tType,
Iterators.filter(x -> tdir * tspan[1] < tdir * x ≤
tdir * tspan[end],
Iterators.flatten((tstops, tspan[end])))))
end
tstops_internal = DataStructures.BinaryHeap{tType}(DataStructures.FasterForward())
saveat_internal = DataStructures.BinaryHeap{tType}(DataStructures.FasterForward())

if tdir > 0
tstops_internal = DataStructures.BinaryMinHeap(tstops_vec)
else
tstops_internal = DataStructures.BinaryMaxHeap(tstops_vec)
t0, tf = tspan
tdir_t0 = tdir * t0
tdir_tf = tdir * tf

for t in tstops
tdir_t = tdir * t
tdir_t0 < tdir_t ≤ tdir_tf && push!(tstops_internal, tdir_t)
end
push!(tstops_internal, tdir_tf)

if saveat isa Number
if (tspan[1]:saveat:tspan[end])[end] == tspan[end]
saveat_vec = convert(Vector{tType},
collect(tType, (tspan[1] + saveat):saveat:tspan[end]))
else
saveat_vec = convert(Vector{tType},
collect(tType,
(tspan[1] + saveat):saveat:(tspan[end] - saveat)))
end
elseif isempty(saveat)
saveat_vec = saveat
else
saveat_vec = vec(collect(tType,
Iterators.filter(x -> tdir * tspan[1] < tdir * x <
tdir * tspan[end], saveat)))
saveat = (t0:tdir*abs(saveat):tf)[2:end]
end

if tdir > 0
saveat_internal = DataStructures.BinaryMinHeap(saveat_vec)
else
saveat_internal = DataStructures.BinaryMaxHeap(saveat_vec)
for t in saveat
tdir_t = tdir * t
tdir_t0 < tdir_t ≤ tdir_tf && push!(saveat_internal, tdir_t)
end

tstops_internal, saveat_internal
Expand Down Expand Up @@ -1409,11 +1392,9 @@
uType = eltype(integrator.sol.u)
iters = Ref(Clong(-1))
while !isempty(integrator.opts.tstops)
# Sundials can have floating point issues approaching a tstop if
# there is a modifying event each
# The call to first is an overload of Base.first implemented in DataStructures
while integrator.tdir * (integrator.t - first(integrator.opts.tstops)) < -1e6eps()
tstop = first(integrator.opts.tstops)
while integrator.tdir * integrator.t < first(integrator.opts.tstops)
tstop = integrator.tdir * first(integrator.opts.tstops)
set_stop_time(integrator, tstop)
integrator.tprev = integrator.t
if !(integrator.opts.callback.continuous_callbacks isa Tuple{})
Expand Down Expand Up @@ -1488,12 +1469,13 @@

function handle_tstop!(integrator::AbstractSundialsIntegrator)
tstops = integrator.opts.tstops
if !isempty(tstops)
if integrator.tdir * (integrator.t - first(integrator.opts.tstops)) > -1e6eps()
if !isempty(tstops) && integrator.tdir * integrator.t >= first(tstops)
pop!(tstops)
# If we passed multiple tstops at once (possible if Sundials ignores us or we had redundant tstops)
while !isempty(tstops) && integrator.tdir * integrator.t >= first(tstops)
pop!(tstops)
t = integrator.t
integrator.just_hit_tstop = true
end
integrator.just_hit_tstop = true

Check warning on line 1478 in src/common_interface/solve.jl

View check run for this annotation

Codecov / codecov/patch

src/common_interface/solve.jl#L1478

Added line #L1478 was not covered by tests
end
end

Expand Down
Loading