Skip to content

Commit

Permalink
Merge pull request #212 from oscardssmith/patch-1
Browse files Browse the repository at this point in the history
remove complication from PresetTimeCallback
  • Loading branch information
ChrisRackauckas authored Mar 28, 2024
2 parents 7d83fc6 + b1436a1 commit 9d65bb5
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions src/preset_time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,43 @@ function PresetTimeCallback(tstops, user_affect!;
initialize = SciMLBase.INITIALIZE_DEFAULT,
filter_tstops = true,
sort_inplace = false, kwargs...)
local tdir
if tstops isa AbstractVector
if sort_inplace
sort!(tstops)
else
tstops = sort(tstops)
end
search_start, search_end = firstindex(tstops), lastindex(tstops)
condition = function (u, t, integrator)
t in @view(tstops[search_start:search_end])
insorted(t, tstops) && (integrator.t - integrator.dt) != integrator.t
end
else
search_start, search_end = 0, 0
elseif tstops isa Number
condition = function (u, t, integrator)
t == tstops
t == tstops && (integrator.t - integrator.dt) != integrator.t
end
else
throw(ArgumentError("tstops must either be a number or a Vector. Was $tstops"))
end

# Call f, update tnext, and make sure we stop at the new tnext
affect! = function (integrator)
user_affect!(integrator)
if integrator.tdir > 0
search_start += 1
else
search_end -= 1
end
nothing
end

# Initialization: first call to `f` should be *before* any time steps have been taken:
initialize_preset = function (c, u, t, integrator)
initialize(c, u, t, integrator)
if tstops isa AbstractVector
search_start, search_end = firstindex(tstops), lastindex(tstops)
else
search_start, search_end = 0, 0
end

if filter_tstops
tdir = integrator.tdir
_tstops = tstops[@.((tdir * tstops >
tdir *
integrator.sol.prob.tspan[1])*(tdir *
tstops <
tdir *
integrator.sol.prob.tspan[2]))]
tspan = integrator.sol.prob.tspan
_tstops = tstops[@. tdir * tspan[1] <= tdir * tstops < tdir * tspan[2]]
add_tstop!.((integrator,), _tstops)
else
add_tstop!.((integrator,), tstops)
end
if t tstops
user_affect!(integrator)
if integrator.tdir > 0
search_start += 1
else
search_end -= 1
end
end
end
DiscreteCallback(condition, affect!; initialize = initialize_preset, kwargs...)
Expand Down

0 comments on commit 9d65bb5

Please sign in to comment.