# Setting really small values in ODE to zero

**URL:** <https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [August 28, 2020, 2:00pm UTC](https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693 "2020-08-28T14:00:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [August 28, 2020, 2:00pm UTC](https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693/1 "2020-08-28T14:00:50Z")

</div>

In modelling biological communities with ODEs, given that individuals are somewhat finite entities, I am trying to avoid infinitesimal densities which are mathematically correct but make no sense. More specifically, I want to set an extinction threshold at a certain small value (eg 1e-12), to ensure that extinct species are actually extinct and do not come back from the dead later. Currently doing this with a callback (see below). It works but I was wondering if there is a more elegant or efficient way of setting to zero values of the solution under a certain threshold?

My current approach (simplified, this is of course the drug concentration decay example with a large threshold)

```julia
function f(du,u,p,t)
    du[1] = -u[1]
    du[2] = -u[2]
end
threshold = 0.2
condition(u,t,integrator) = any( (u .< threshold) .& (u.>0))
function affect!(integrator)
  integrator.u[(integrator.u .< threshold) .& (integrator.u .> 0)] .= 0
end
cb = DiscreteCallback(condition,affect!)
u0 = [1, 0.5]
prob = ODEProblem(f,u0,(0.0,10.0))
sol = solve(prob,Tsit5(),callback=cb,saveat = 0.1)
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:** [August 28, 2020, 2:11pm UTC](https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693/2 "2020-08-28T14:11:08Z")

</div>

I think this is a fine way to do this kind of modeling. Using Gillespie-type models are another way to have “true zeros” in the model.

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [August 28, 2020, 2:23pm UTC](https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693/3 "2020-08-28T14:23:22Z")

</div>

Thank you for the reply 🙂 I tried discrete-individuals stochastic models (Gillespie algorithm, tau-leaping), but they need very large populations and very small mutation rates to approach the analytical solution. Also the added stochasticity was more an annoyance than anything, so I went back to ODEs.

---

<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:** [August 28, 2020, 2:31pm UTC](https://discourse.julialang.org/t/setting-really-small-values-in-ode-to-zero/45693/4 "2020-08-28T14:31:03Z")

</div>

makes sense
