# Specifying RetCodes in Event when solving ODE

**URL:** <https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145>\
**Category:** General Usage\
**Tags:** differentialequation\
**Created:** [June 2, 2022, 4:02pm UTC](https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145 "2022-06-02T16:02:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [June 2, 2022, 4:02pm UTC](https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145/1 "2022-06-02T16:02:49Z")

</div>

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:

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

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:

```julia
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)](https://diffeq.sciml.ai/stable/basics/solution/#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.

Thanks.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 2, 2022, 4:08pm UTC](https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145/2 "2022-06-02T16:08:09Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [June 2, 2022, 4:17pm UTC](https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145/3 "2022-06-02T16:17:59Z")

</div>

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.

---

<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 2, 2022, 4:55pm UTC](https://discourse.julialang.org/t/specifying-retcodes-in-event-when-solving-ode/82145/4 "2022-06-02T16:55:32Z")

</div>

IIRC it’s a positional not a kwarg.
