Skip to content

Commit

Permalink
Merge pull request #229 from isaacsas/tstop_valtree_fix
Browse files Browse the repository at this point in the history
add dispatch hooks for internal integrator fields
  • Loading branch information
ChrisRackauckas authored Aug 31, 2024
2 parents 67a0a32 + 0f84179 commit 33aab21
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Aqua = "0.8"
DataInterpolations = "4.6"
DataStructures = "0.18.13"
DiffEqBase = "6.146"
DiffEqBase = "6.154"
ForwardDiff = "0.10.36"
Functors = "0.4"
LinearAlgebra = "1.10"
Markdown = "1.10"
NonlinearSolve = "3.7.2"
ODEProblemLibrary = "0.1.5"
OrdinaryDiffEq = "6.68"
OrdinaryDiffEq = "6.88"
Parameters = "0.12"
QuadGK = "2.9"
RecipesBase = "1.3.4"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
[compat]
DiffEqCallbacks = "3"
Documenter = "1"
OrdinaryDiffEq = "6.31"
OrdinaryDiffEq = "6.88"
Plots = "1.36"
2 changes: 2 additions & 0 deletions src/DiffEqCallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ using Parameters: @unpack

import SciMLBase

using DiffEqBase: get_tstops, get_tstops_array, get_tstops_max

include("functor_helpers.jl")
include("autoabstol.jl")
include("manifold.jl")
Expand Down
19 changes: 9 additions & 10 deletions src/iterative_and_periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ function IterativeCallback(time_choice, user_affect!, tType = Float64;
# Schedule next call to `f` using `add_tstops!`, but be careful not to keep integrating forever
tnew = time_choice(integrator)
tnew === nothing && (tnext[] = tnew; return)
tstops = integrator.opts.tstops
tstops = get_tstops(integrator)
#=
Okay yeah, this is nasty
the comparer is always less than for type stability, so in order
for this to actually check the correct direction we multiply by
tdir
=#
tdir_tnew = integrator.tdir * tnew
tstops_array = get_tstops_array(integrator)
for i in length(tstops):-1:1 # reverse iterate to encounter large elements earlier
if tdir_tnew < tstops.valtree[i] # TODO: relying on implementation details
if tdir_tnew < tstops_array[i]
tnext[] = tnew
add_tstop!(integrator, tnew)
break
elseif tdir_tnew == tstops.valtree[i]
elseif tdir_tnew == tstops_array[i]
# If it's already a tstop, no need to re-add! This is for the final point
tnext[] = tnew
end
Expand Down Expand Up @@ -92,7 +93,6 @@ function add_next_tstop!(integrator, S)

# Schedule next call to `f` using `add_tstops!`, but be careful not to keep integrating forever
tnew = t0[] + (index[] + 1) * Δt
tstops = integrator.opts.tstops
#=
Okay yeah, this is nasty
the comparer is always less than for type stability, so in order
Expand All @@ -101,7 +101,7 @@ function add_next_tstop!(integrator, S)
=#
tdir_tnew = integrator.tdir * tnew
index[] += 1
if tdir_tnew < maximum(tstops.valtree)
if tdir_tnew < get_tstops_max(integrator)
add_tstop!(integrator, tnew)
end
end
Expand All @@ -117,7 +117,7 @@ PeriodicCallback(f, Δt::Number; phase = 0, initial_affect = false,
integration time (as opposed to wall time), i.e. at `t = tspan[1]`, `t = tspan[1] + Δt`,
`t = tspan[1] + 2Δt`, and so on.
If a non-zero `phase` is provided, the invocations of the callback will be shifted by
If a non-zero `phase` is provided, the invocations of the callback will be shifted by
`phase` time units, i.e., the calls will occur at
`t = tspan[1] + phase`, `t = tspan[1] + phase + Δt`,
`t = tspan[1] + phase + 2Δt`, and so on.
Expand All @@ -129,9 +129,9 @@ discrete-time controller for a continuous-time system, running at a fixed rate.
- `f` the `affect!(integrator)` function to be called periodically
- `Δt` is the period
## Keyword Arguments
- `phase` is a phase offset
- `initial_affect` is whether to apply the affect at the initial time, which defaults to `false`
- `final_affect` is whether to apply the affect at the final time, which defaults to `false`
Expand Down Expand Up @@ -177,8 +177,7 @@ end
# Checking for floating point equality is OK here as `DifferentialEquations.jl`
# sets the time exactly to the final time in the last iteration
return integrator.t == last(integrator.sol.prob.tspan) ||
isempty(integrator.opts.tstops) ||
integrator.iter == integrator.opts.maxiters
(hasfield(typeof(integrator), :iter) && (integrator.iter == integrator.opts.maxiters))
end

export PeriodicCallback

0 comments on commit 33aab21

Please sign in to comment.