# Integrator.sol.retcode does not update after reinit!(integrator)

**URL:** <https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417>\
**Category:** General Usage\
**Tags:** differentialequation\
**Created:** [May 3, 2022, 10:59am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417 "2022-05-03T10:59:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 3, 2022, 10:59am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/1 "2022-05-03T10:59:48Z")

</div>

Hi all,

In my code I need to check the status of my integrator, and if it got terminated (because of my own callback) I want to know about that.  
But I have some difficulty with the MWE down below. It seems like the retcode does not change after `reinit!`.

In this code I check for u if it is positive, and then terminate. But then I change the initial condition and the retcode is still `:Terminated` (although the solution is perfectly good).

This may be an issue?

```julia
using DifferentialEquations
f(u,p,t) = 2*u 
# the solution is positive or negative depends on the initial condition
u_positive(u,t,integrator) = u > 0
terminate_if_u_pos = DiscreteCallback(u_positive, terminate!)

prob = ODEProblem(f, 1.0, (0.0, 1.0)) # positive initial condition > positive u > :Terminated
integrator = init(prob, Tsit5(), callback=terminate_if_u_pos)
sol1 = solve!(integrator)
@show sol1.retcode # should be :Terminated
# julia> sol1.retcode = :Terminated

reinit!(integrator, -1.0) # negative initial condition > negative u > :Success!
sol2 = solve!(integrator)
@show sol2.retcode
# julia> sol2.retcode = :Terminated
# but should be :Success like as to following code:

prob = ODEProblem(f, -1.0,(0.0, 1.0)) # negative initial condition > negative u > :Success!
integrator = init(prob, Tsit5(), callback=terminate_if_u_pos)
sol3 = solve!(integrator)
@show sol3.retcode
# julia> sol3.retcode = :Success

```

Thanks a lot for the great community!

---

<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:** [May 3, 2022, 11:07am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/2 "2022-05-03T11:07:13Z")

</div>

yeah that’s an issue. Open an issue on DifferentialEquations.jl

---

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 3, 2022, 11:17am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/3 "2022-05-03T11:17:50Z")

</div>

Ok, thanks!  
Here is the [issue](https://github.com/SciML/DifferentialEquations.jl/issues/861).

Any workaround for the meantime? Since it is immutable I couldn’t change the `retcode` manually.

---

<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:** [May 3, 2022, 11:28am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/4 "2022-05-03T11:28:45Z")

</div>

SetField.jl to swap it out is a fine solution. There’s also an old function specifically for swapping retcodes in SciMLBase or DiffEqBase, but SetField.jl is the modern way to do it.

---

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 3, 2022, 11:31am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/5 "2022-05-03T11:31:24Z")

</div>

That’s nice! I’ll check it out.

---

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 3, 2022, 2:31pm UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/6 "2022-05-03T14:31:06Z")

</div>

I’ll just mention here that I was not able to make [Setfield.jl](https://github.com/jw3126/Setfield.jl) package work with `ODESolution`. To my understanding, the Setfield.jl package does not find the right constructor for the object.

---

<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:** [May 3, 2022, 2:39pm UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/7 "2022-05-03T14:39:03Z")

</div>

For now just use [SciMLBase.jl/ode\_solutions.jl at master · SciML/SciMLBase.jl · GitHub](https://github.com/SciML/SciMLBase.jl/blob/master/src/solutions/ode_solutions.jl#L151) then.

---

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 3, 2022, 4:59pm UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/8 "2022-05-03T16:59:14Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![Elyco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elyco/32/18418_2.png) [@Elyco](https://discourse.julialang.org/u/Elyco)\
**Post date:** [May 15, 2022, 7:46am UTC](https://discourse.julialang.org/t/integrator-sol-retcode-does-not-update-after-reinit-integrator/80417/9 "2022-05-15T07:46:52Z")

</div>

The issue is closed, thanks.
