# How to use PeriodicCallbacks from DiffEqs

**URL:** <https://discourse.julialang.org/t/how-to-use-periodiccallbacks-from-diffeqs/12440>\
**Category:** Numerics\
**Tags:** diffeq\
**Created:** [July 17, 2018, 3:47pm UTC](https://discourse.julialang.org/t/how-to-use-periodiccallbacks-from-diffeqs/12440 "2018-07-17T15:47:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SanAlphaTau](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SanAlphaTau](https://discourse.julialang.org/u/SanAlphaTau)\
**Post date:** [July 17, 2018, 3:47pm UTC](https://discourse.julialang.org/t/how-to-use-periodiccallbacks-from-diffeqs/12440/1 "2018-07-17T15:47:58Z")

</div>

Hey guys I’m having trouble understanding the documentation of periodic callbacks.

Say I have a ODE and want periodically input some number to a value. From reading the documentation I thought periodic callback would do this for me but I can’t get it to work.

as a minimal working example I have the lorenz system with adding 10 to the y variable every 2 units of time.

```
println("loading packages")
using DifferentialEquations
using Plots
gr()

g = @ode_def LorenzExample begin
  dx = σ*(y-x)
  dy = x*(ρ-z) - y
  dz = x*y - β*z
end σ ρ β

u0 = [1.0,0.0,0.0]
tspan = (0.0,10.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(g,u0,tspan,p)

function periodic(integrator)
    integrator.u[2] += 10
end

cb = PeriodicCallback(periodic, 2::Number)

sol = solve(prob, callback = cb)
plot(sol)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 17, 2018, 4:56pm UTC](https://discourse.julialang.org/t/how-to-use-periodiccallbacks-from-diffeqs/12440/2 "2018-07-17T16:56:40Z")

</div>

> [@SanAlphaTau](#):
>
> cb = PeriodicCallback(periodic, 2::Number)

The solution is to change this to a float:

```julia
cb = PeriodicCallback(periodic, 2::Number)

```

We should auto-upconvert here.

> <https://github.com/SciML/DifferentialEquations.jl/issues/313>
>
> \`\`\`julia
> using DifferentialEquations
> using Plots
> gr()
> 
> g = @ode\_def LorenzE…xample begin
> dx = σ\*(y-x)
> dy = x\*(ρ-z) - y
> dz = x\*y - β\*z
> end σ ρ β
> 
> u0 = \[1.0,0.0,0.0\]
> tspan = (0.0,10.0)
> p = \[10.0,28.0,8/3\]
> prob = ODEProblem(g,u0,tspan,p)
> 
> function periodic(integrator)
> integrator.u\[2\] += 10
> end
> 
> cb = PeriodicCallback(periodic, 2::Number)
> 
> sol = solve(prob, callback = cb)
> plot(sol)
> \`\`\`
> 
> fails, but
> 
> \`\`\`julia
> cb = PeriodicCallback(periodic, 2.0)
> 
> sol = solve(prob, Tsit5(), callback = cb)
> plot(sol)
> \`\`\`
> 
> works. It should know to upconvert but it fails in the \`push!\` for the binary heap.
> 
> \`\`\`julia
> \> sol = solve(prob, Tsit5(), callback = cb)
> ERROR: MethodError: no method matching push!(::DataStructures.BinaryHeap{Float64,DataStructures.LessThan}, ::Int64)
> Closest candidates are:
> push!(::Any, ::Any, ::Any) at abstractarray.jl:1940
> push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:1941
> push!(::Array{Any,1}, ::ANY) at array.jl:653
> ...
> Stacktrace:
> \[1\] add\_tstop!(::OrdinaryDiffEq.ODEIntegrator{OrdinaryDiffEq.Tsit5,Array{Float64,1},Float64,Array{Float64,1},Float64,Float64,Float64,Array{Array{Float64,1},1},DiffEqBase.ODESolution{Float64,2,Array{Array{Float64,1},1},Void,Void,Array{Float64,1},Array{Array{Array{Float64,1},1},1},DiffEqBase.ODEProblem{Array{Float64,1},Float64,true,Array{Float64,1},LorenzExample,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem},OrdinaryDiffEq.Tsit5,OrdinaryDiffEq.InterpolationData{LorenzExample,Array{Array{Float64,1},1},Array{Float64,1},Array{Array{Array{Float64,1},1},1},OrdinaryDiffEq.Tsit5Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}}},LorenzExample,Void,OrdinaryDiffEq.Tsit5Cache{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}},OrdinaryDiffEq.DEOptions{Float64,Float64,Float64,Float64,DiffEqBase.#ODE\_DEFAULT\_NORM,DiffEqBase.CallbackSet{Tuple{},Tuple{DiffEqBase.DiscreteCallback{DiffEqCallbacks.##40#43{Base.RefValue{Int64}},DiffEqCallbacks.##41#44{#periodic,Int64,Base.RefValue{Int64}},DiffEqCallbacks.##42#45{DiffEqBase.#INITIALIZE\_DEFAULT,Bool,Int64,Base.RefValue{Int64},DiffEqCallbacks.##41#44{#periodic,Int64,Base.RefValue{Int64}}}}}},DiffEqBase.#ODE\_DEFAULT\_ISOUTOFDOMAIN,DiffEqBase.#ODE\_DEFAULT\_PROG\_MESSAGE,DiffEqBase.#ODE\_DEFAULT\_UNSTABLE\_CHECK,DataStructures.BinaryHeap{Float64,DataStructures.LessThan},DataStructures.BinaryHeap{Float64,DataStructures.LessThan},Void,Void,Int64,Array{Float64,1},Array{Float64,1},Array{Float64,1}},Array{Float64,1}}, ::Int64) at C:\\Users\\Chris\\.julia\\v0.6\\OrdinaryDiffEq\\src\\integrators\\integrator\_interface.jl:73
> \`\`\`
