# DifferentialEquations.jl: Problems with event detection

**URL:** https://discourse.julialang.org/t/differentialequations-jl-problems-with-event-detection/4462
**Category:** Numerics
**Tags:** question, diffeq
**Created:** [June 26, 2017, 6:33am UTC](https://discourse.julialang.org/t/differentialequations-jl-problems-with-event-detection/4462 "2017-06-26T06:33:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)
#### Post date: [June 26, 2017, 6:33am UTC](https://discourse.julialang.org/t/differentialequations-jl-problems-with-event-detection/4462/1 "2017-06-26T06:33:14Z")

</div>

Hi folks,

I am having problems getting event detection to work properly in DifferentialEquations.jl. I have reduced the code as far as possible:

```julia
using DifferentialEquations
using PyPlot

function evaluate!(t, y, δy)
    rm = norm(y[1:3])
    δy[1:3] = y[4:6]
    δy[4:6] = -mu .* y[1:3] ./ rm^3
end

function condition(t, y, integrator)
    r = y[1:3]
    v = y[4:6]
    rm = norm(r)
    vm = norm(v)
    ecc = norm(((vm^2 - mu/rm)*r - v * (r ⋅ v))/mu)
    sma = -mu/(2*(vm^2/2 - mu/rm))
    ese = (r ⋅ v) / sqrt(mu * sma)
    ece = rm * vm^2 / mu - 1.0
    E = atan2(ese, ece)
    ano = 2*atan2(sqrt(1 + ecc)*sin(E/2), sqrt(1 - ecc)*cos(E/2))
    push!(ts, t)
    push!(as, ano)
    ano
end

function affect!(integrator)
    push!(te, integrator.t)
end

const mu = 398600.4418

ts = Vector()
as = Vector()
te = Vector()

t1 = 5562.095583019713

r = [
6068279.27,
-1692843.94,
-2516619.18,
]/1000

v = [
-660.415582,
5495.938726,
-5303.093233,
]/1000

y0 = [r; v]

prob = ODEProblem(
    evaluate!,
    y0, (0.0, t1),
    callback=ContinuousCallback(condition, affect!),
)
res = solve(prob, Vern9())

idx = sortperm(ts)
t = ts[idx]
ano = as[idx]
plot(t, ano)
for t in te
    plot([te, te], [-pi, pi], "r")
end
grid()

```

This is the output:  
 ![](https://global.discourse-cdn.com/julialang/original/2X/d/d7b218ce180dd38cbbff8bb960ff8066e3425340.png)

The blue line is the output of the `condition` function. I would expect the callback to only be triggered at the upcrossing between 4000 and 5000 seconds. But somehow it also gets triggered at the downcrossing. Am I doing something wrong?

---

<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: [June 26, 2017, 7:11am UTC](https://discourse.julialang.org/t/differentialequations-jl-problems-with-event-detection/4462/2 "2017-06-26T07:11:39Z")

</div>

> [@helgee](#):
>
> callback=ContinuousCallback(condition, affect!)

This means it will trigger at upcrossings and downcrossings.

```julia
cb = ContinuousCallback(condition,affect!) # Both upcrossings and downcrossings, the default
cb = ContinuousCallback(condition,affect!,nothing) # Only upcrossings
cb = ContinuousCallback(condition,nothing,affect!) # Only downcrossings

```

It is mentioned further down the page:

> Notice that passing only one affect! is the same as ContinuousCallback(condition,affect!,affect!), i.e. both upcrossings and downcrossings will activate the event. Using ContinuousCallback(condition,affect!,nothing)will thus be the same as above because the first event is an upcrossing.

but reading the top, this seems to be a bit more confusing than I remember it being. Would you mind doing a PR for the docs to make it clearer?

---

<div class="post-metadata">

### Author: ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)
#### Post date: [June 26, 2017, 8:25am UTC](https://discourse.julialang.org/t/differentialequations-jl-problems-with-event-detection/4462/3 "2017-06-26T08:25:56Z")

</div>

Thanks!

Here is the PR: [https://github.com/JuliaDiffEq/DiffEqDocs.jl/pull/31](https://github.com/JuliaDiffEq/DiffEqDocs.jl/pull/31)

There is a sarcastic German proverb that roughly translates to:  
“The person who is able to read always has the upper hand.” 😂
