Continuous Callback - terminate on second zero crossing?

How do I set up the condition and affect functions such that ODE solver terminates on second zero crossing?

Currently, I have the following setup for terminating on the first zero crossing.

function condition(z,t,integrator)
    z[2]
end

function affect!(integrator)
    terminate!(integrator)
end

cb = ContinuousCallback(condition,affect!) 
 prob = ODEProblem(EOM,ic,tspan,p)

The easiest way would be something like

NUM_TIMES_EFFECT_HIT::Int = 0
function affect!(integrator)
    if NUM_TIMES_EFFECT_HIT < 2
       NUM_TIMES_EFFECT_HIT += 1 
    else
        terminate!(integrator)
    end
end
2 Likes