# Julia DifferentialEquations: add delay into ODE

**URL:** <https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337>\
**Category:** Numerics\
**Tags:** question, package, diffeq\
**Created:** [November 17, 2020, 10:20pm UTC](https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337 "2020-11-17T22:20:53Z")\
**Posts on this page:** 4\
**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:** [November 17, 2020, 10:20pm UTC](https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337/1 "2020-11-17T22:20:54Z")

</div>

Hello,  
I have a set of ODE to model the growth of bacteria nad phages:

```julia
function multiInfect!(du, u, p, t)
    μ, ν, κ, φ, ψ, ω, β, Β, ρ, τ, η, Η = p
    #=
    du[1] = resident susceptible
    du[2] = resident infected
    du[3] = resident phage
    du[4] = invader susceptible
    du[5] = invader infected
    du[6] = invader phage
    =#
    TOT = u[1]+u[2]+u[4]+u[5]
    du[1] = (μ * u[1]) * (1 - TOT/κ) - (φ * 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])

    du[4] = (ν * u[4]) * (1 - TOT/κ) - (ψ * u[4] * u[6]) - (ω * u[4])
    du[5] = (ψ * u[4] * u[6]) - (Η * u[5]) - (ω * u[5])
    du[6] = (Β * Η * u[2]) - (ρ * ψ * u[4] * u[6]) - (ω * u[6])
end

# parameters
mu = 0.47 # maximum growth rate susceptible (B. longum)
nu = 0.72 # maximum growth rate resistant (E. coli)
kappa = 2.2*10^7 # maximum population density
phi = 10.0^-9 # adsorption rate
psi = 10.0^-9 # adsorption rate alternative
omega = 0.05 # outflow
eta = 1.0 # lyse rate
Eta = 1.0 # lyse rate alternative
beta = 50.0 # burst size
Beta = 50.0 # burst size alternative
rho = 0.6 # reinfection rate
tau = 3.62 # latency time
tmax = 4000.0 # time span 0-tmax
s0 = 50000.0 # initial susceptible population
i0 = 1.0e-9 # initial infected population
v0 = 80.0 # initial phage population
r_s0 = 50000.0 # initial resident susceptible population
r_i0 = 1.0e-9 # initial resident infected population
r_v0 = 80.0 # initial resident phage population
i_s0 = 500.0 # initial invader susceptible population
i_i0 = 1.0e-9 # initial invader infected population
i_v0 = 80.0 # initial invader phage population
# implement
tspan = (0.0, tmax)
u0 = [r_s0, r_i0, r_v0, i_s0, i_i0, i_v0]
parms = [mu, nu, kappa, phi, psi, omega, beta, Beta, rho, tau, eta, Eta]
prob = ODEProblem(multiInfect!, u0, tspan, parms)
soln = solve(prob, AutoVern7(Rodas5()) )

```

which gives me this figure

 ![Scenario_3](https://global.discourse-cdn.com/julialang/original/3X/0/9/0913d6aede25cea495ace0b88183ec52af4a77ad.png)  
I would like to introduce the invading bacteria after a certain time lag, let’s say 1000 hours, so I added this modificaion:

```julia
condition(u, t, integrator) = t==1000
affect!(integrator) = integrator.u[4] += i_s0 
cb = DiscreteCallback(condition,affect!)
prob = ODEProblem(multiInfect!, u0, tspan, parms)
soln = solve(prob, AutoVern7(Rodas5()), callback=cb, tstops=[1000])

```

but the solution is the same and it is evident that the invader was present from the beginning.  
How can I properly set the delay?  
Thank you

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 17, 2020, 10:46pm UTC](https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337/2 "2020-11-17T22:46:29Z")

</div>

You are setting that population to 500 initially in `u0` (so it is not starting at zero), and adding 500 again at `t==1000`, but by then the population appears to be so large that 500 would not make a visible difference (if I’m lining up the state variables to the plot lines correctly).

---

<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:** [November 18, 2020, 6:59am UTC](https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337/3 "2020-11-18T06:59:32Z")

</div>

I see… the initial inoculum must be zero, of course. Thanks.  
I changed i\_s0 to 1e-9 (I was told it is better to have very small numbers than zero altogether) and

```julia
condition(u, t, integrator) = t==1000 # time of inoculum
affect!(integrator) = integrator.u[4] += 500 # amount of inoculum

```

but again the inoculum does not happen at t=1000 but at the beginning:

 ![Scenario_3](https://global.discourse-cdn.com/julialang/original/3X/3/2/32253278a41c29ba06eb1196e7c60acac06cf824.png)  
What am I missing?  
Thank you

---

<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:** [November 18, 2020, 7:03am UTC](https://discourse.julialang.org/t/julia-differentialequations-add-delay-into-ode/50337/4 "2020-11-18T07:03:01Z")

</div>

Actually, if I use zero instead of the suggested `1e-9`, the system works:

 ![Scenario_3](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae710ef56afc961fca83cc0e93a357c1c9c22fe6.png)  
I think this solves the problem!  
Thank you
