# Set parameter dependent time steps using OrdinaryDiffEq

**URL:** <https://discourse.julialang.org/t/set-parameter-dependent-time-steps-using-ordinarydiffeq/104759>\
**Category:** Numerics\
**Tags:** diffeq\
**Created:** [October 9, 2023, 4:58pm UTC](https://discourse.julialang.org/t/set-parameter-dependent-time-steps-using-ordinarydiffeq/104759 "2023-10-09T16:58:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![henry2004y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henry2004y/32/9284_2.png) [@henry2004y](https://discourse.julialang.org/u/henry2004y)\
**Post date:** [October 9, 2023, 4:58pm UTC](https://discourse.julialang.org/t/set-parameter-dependent-time-steps-using-ordinarydiffeq/104759/1 "2023-10-09T16:58:08Z")

</div>

Hi,

I have a question about how to set time-dependent time steps (or maximum time steps) for solving an ODE. For a quick demo, say we are dealing with this simple ODE:

```julia
using OrdinaryDiffEq

f(u, p, t) = 1.01 * p * u
u0 = 1 / 2
p = 1.0 # ODE parameter
tspan = (0.0, 1.0)
prob = ODEProblem(f, u0, tspan, p)
sol = solve(prob, Tsit5(), reltol = 1e-8, abstol = 1e-8)

```

`Tsit5()` allows adaptive time steps, and it takes 17 steps to reach `t=1.0`. I can instead enforce a fixed timestep like

```julia
sol = solve(prob, Tsit5(), adaptive=false, dt = 0.01, reltol = 1e-8, abstol = 1e-8)

```

and as expected, it takes 101 steps.

Now my question is, is it possible to define a customized time step `dt`(or a maximum-allowed time step `dtmax`) which is dependent on the ODE parameters `prob.p`?

I guess this can be achieved via [Step Control Callbacks · DiffEqCallbacks.jl (sciml.ai)](https://docs.sciml.ai/DiffEqCallbacks/stable/step_control/). While continuing to go over its usage, there’s no harm posting my original question here.

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [October 9, 2023, 6:09pm UTC](https://discourse.julialang.org/t/set-parameter-dependent-time-steps-using-ordinarydiffeq/104759/2 "2023-10-09T18:09:07Z")

</div>

You could implement it as a [`DiscreteCallback`](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/#SciMLBase.DiscreteCallback) and make it determine the maximum step size in each time step. We use this in Trixi.jl to implement our [`StepsizeCallback`](https://github.com/trixi-framework/Trixi.jl/blob/ae3a0530d2edcd6c22fd3292319d771bd53e18c1/src/callbacks_step/stepsize.jl)

---

<div class="post-metadata">

**Author:** ![henry2004y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henry2004y/32/9284_2.png) [@henry2004y](https://discourse.julialang.org/u/henry2004y)\
**Post date:** [October 9, 2023, 6:09pm UTC](https://discourse.julialang.org/t/set-parameter-dependent-time-steps-using-ordinarydiffeq/104759/3 "2023-10-09T18:09:24Z")

</div>

Ah I figured it out:

```julia
using OrdinaryDiffEq
using DiffEqCallbacks

f(u, p, t) = 1.01 * p * u
u0 = 1 / 2
p = 1.0 # ODE parameter
tspan = (0.0, 1.0)
prob = ODEProblem(f, u0, tspan, p)
dtFE(u, p, t) = 0.1 * p
cb = StepsizeLimiter(dtFE; safety_factor=1, max_step=true)

sol = solve(prob, Tsit5(); callback=cb, adaptive=false, dt=0.1) # dt has a is dummy value

```
