Specifying RetCodes in Event when solving ODE

Suppose I want to add a discrete event when solving an ODE using DifferentialEquations.jl, and if the condition is met I want to terminate the solving process. I can do that with the following:

cb1_fun(u,t,integrator) = begin
    # some code which return either true or false

cb1 = DiscreteCallback(cb1_fun, terminate!)
prob = ODEProblem(f, y0, (0.0, tf), par)
@time sol = solve(prob, FBDF(), callback=cb1);

Right now if the event function is triggered, I get sol.retcode = :Terminated. If I want to be more specific and add my own retcode, how can I do it? The documentation says:

terminate!(i::DEIntegrator[, retcode = :Terminated])

Terminates the integrator by emptying tstops. This can be used in events and callbacks to immediately end the solution process. Optionally, retcode may be specified (see: Return Codes (RetCodes)).

But I don’t really understand the syntax of the square bracket. I tried something like cb1 = DiscreteCallback(cb1_fun, terminate!(retcode=:MaxTimeReached)) and it doesn’t work. Would be great if someone can let me know how retcode should be specified.


I the affect! argument to DiscreteCallback needs to be a function that accepts the integrator as an argument:

cb1 = DiscreteCallback(cb1_fun, i->terminate!(i, retcode=:MyRetCode))

I got it work with cb1 = DiscreteCallback(cb1_fun, i->terminate!(i, :MyRetCode)). It complains about having unsupported keyword argument if I have the retcode= part. Thanks.

IIRC it’s a positional not a kwarg.