# How to modulate the resolution of Julia DifferentialEquations solver

**URL:** <https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561>\
**Category:** Numerics\
**Tags:** question, ode\
**Created:** [December 9, 2020, 8:51pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561 "2020-12-09T20:51:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 9, 2020, 8:51pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/1 "2020-12-09T20:51:26Z")

</div>

Hello,  
I have this model with delayed differential equations:

```julia
using DifferentialEquations
# function
function baseInfect!(du, u, p, t)
    μ, κ, φ, ω, η, β = p
    du[1] = ((μ * u[1]) * (1 - ((u[1]+u[2])/κ))) - (φ * u[1] * u[3]) - (ω * u[1])
    du[2] = (φ * u[1] * u[3]) - (η * u[2]) - (ω * u[2])
    du[3] = (β * η * u[2]) - (φ * u[1] * u[3]) - (ω * u[3])
end
# parameters
mu = 0.47 # maximum growth rate susceptible (B. longum)
kappa = 2.2*10^7 # maximum population density
phi = 10.0^-9 # adsorption rate
omega = 0.05 # outflow
eta = 1.0 # lyse rate
beta = 50.0 # burst size
tmax = 4000.0 # time span 0-tmax
s0 = 50000.0 # initial susceptible population
i0 = 0.0 # initial infected population
v0 = 0.0 # initial phage population
v_in = 80.0 # amount of injection

t_in = 1000 # injection time
gran = 1000 # granularity of the ODE

# instantiate solver
u0 = [s0, i0, v0]
parms = [mu, kappa, phi, omega, eta, beta]
tspan = (0.0, tmax)
# delay infection
condition(u, t, integrator) = t==t_in # time of inoculum
affect!(integrator) = integrator.u[3] += v_in # amount of inoculum
cb = DiscreteCallback(condition,affect!)
# instantiate model
prob = ODEProblem(baseInfect!, u0, tspan, parms)
soln = solve(prob, AutoVern7(Rodas5()), callback=cb, tstops=[gran])

```

I have set `t_in` to determine the time of modification of the equations and `gran` to determine the “granularity” of the model via `tstops`. In fact, the model works only if `t_in` and `gran` are the same. I reckon the problem is that if the solver has a too large “granularity” (I don’t know the exact term), it might not kick on the modification. But the model does not work even if `gran` goes to 1.  
In other words, I am having this sort of model:

 ![ModelTherapy](https://global.discourse-cdn.com/julialang/original/3X/5/7/5742fc8c9798ab8dc829e94f25086def06846787.png)

only when t==t\_in is the same as tstops=[gran], otherwise I get this:

 ![ModelTherapy](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a12ae4ae726516e739670022c11ff011a828eff.png)

How can I module the solver?  
Thank you

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [December 9, 2020, 9:29pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/2 "2020-12-09T21:29:33Z")

</div>

Actually it is a similar thing in [here](https://discourse.julialang.org/t/julia-differentialequations-add-two-delays-into-ode/50362/4):

> - Simply use `ContinuousCallback` 's and forget about `tstops`

---

<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:** [December 10, 2020, 4:50am UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/3 "2020-12-10T04:50:49Z")

</div>

Of course you need a tstop at a point if a DiscreteCallback is asking if a value is hit exactly. I don’t quite get the question. You can use a `PresetTimeCallback` if you want that part to be done automatically.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 10, 2020, 6:47am UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/4 "2020-12-10T06:47:35Z")

</div>

I ran it without `tstops` but in this case I get the second graph even with t=1000…  
But according to [this post](https://discourse.julialang.org/t/how-to-debug-ode/50576/13) just using `tstops=t_in`, works just fine. (The present post was a special case of that post…)

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 10, 2020, 6:56pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/5 "2020-12-10T18:56:36Z")

</div>

Here, we are saying that if the discrete callback condition cheks equality namely time then this preset time should be in `tstops` so that solver steps into this time exactly. What purpose `gran` has is another thing. I advise you to make it continuos callback (like `t-t_in` in condition) as @MatFi suggests and you don’t need to add the time, `t_in`, in `tsops`.

---

<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:** [December 10, 2020, 7:45pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/6 "2020-12-10T19:45:31Z")

</div>

> [@tomaklutfu](#):
>
> I advise you to make it continuos callback (like `t-t_in` in condition) as @MatFi suggests and you don’t need to add the time, `t_in` , in `tsops` .

But if you can specify the time explicitly, do that. It’s more efficient than a continuous callback and a lot less involved.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [December 10, 2020, 8:00pm UTC](https://discourse.julialang.org/t/how-to-modulate-the-resolution-of-julia-differentialequations-solver/51561/7 "2020-12-10T20:00:21Z")

</div>

Thank you!
