# DiffEqCallbacks' StepSizeLimiter and Dual numbers

**URL:** https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197
**Category:** Modelling & Simulations
**Tags:** diffeq
**Created:** [June 1, 2021, 2:47pm UTC](https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197 "2021-06-01T14:47:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [June 1, 2021, 2:47pm UTC](https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197/1 "2021-06-01T14:47:58Z")

</div>

I am trying to use GalacticOptim to run an optimization and I am using GalacticOptim.AutoForwardDiff() to generate the gradients and the run an optimizer over my objective function. My objective function contains a model with differential equations that I am solving with DifferentialEquations.jl. All is fine with it until I am adding a StepsizeLimiter callback from DiffeqCallbacks.jl (meaning that the optimizer correctly runs on my obj. function without the callback present).

With the callback present, I am getting the error:

```julia
 LoadError: TypeError: in setfield!, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 2}

```

Reading the documentation ([GitHub - SciML/DiffEqCallbacks.jl: A library of useful callbacks for hybrid scientific machine learning (SciML) with augmented differential equation solvers](https://github.com/SciML/DiffEqCallbacks.jl#stepsizelimiter)) tells me that this probably has to do with the cached\_dtcache that I have to set to the right type, because my time domain is not Float64 anymore… At least that’s what I think the problem boils down to! (So close to a solution!?) But…what is the specific type to use here?

In other words: What should the “???” be in this line of code:  
`StepsizeLimiter(dtFE, cached_dtcache = ???)`

Any help is greatly appreciated and sorry for not posting a MWE. I thought that this actually boils down to a simple question, but of course I can create a MWE if you think it’s indispensable.

---

<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: [May 11, 2022, 1:41pm UTC](https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197/2 "2022-05-11T13:41:51Z")

</div>

> [@vettert](#):
>
> In other words: What should the “???” be in this line of code:  
> `StepsizeLimiter(dtFE, cached_dtcache = ???)`

just make it match the eltype of parameters in there, so it’s hard without seeing your code, but something like `cached_dtcache = zero(eltype(p))` or whatever is Dual.

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [May 17, 2022, 6:46am UTC](https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197/3 "2022-05-17T06:46:19Z")

</div>

OK, does this imply that I would have to re-generate the callback that I am using from scratch if I swap over from Float parameters to Dual parameters?

I’ll give it a try and if performance is bad for this, I guess I’ll be back 🙂

---

<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: [May 17, 2022, 7:56am UTC](https://discourse.julialang.org/t/diffeqcallbacks-stepsizelimiter-and-dual-numbers/62197/4 "2022-05-17T07:56:00Z")

</div>

> [@vettert](#):
>
> OK, does this imply that I would have to re-generate the callback that I am using from scratch if I swap over from Float parameters to Dual parameters?

Yes. This callback does need to.
