# Save ODE solution for specific values of one state variable

**URL:** <https://discourse.julialang.org/t/save-ode-solution-for-specific-values-of-one-state-variable/126168>\
**Category:** Modelling & Simulations\
**Tags:** differentialequation, ordinarydiffeq\
**Created:** [February 21, 2025, 9:40pm UTC](https://discourse.julialang.org/t/save-ode-solution-for-specific-values-of-one-state-variable/126168 "2025-02-21T21:40:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [February 21, 2025, 9:40pm UTC](https://discourse.julialang.org/t/save-ode-solution-for-specific-values-of-one-state-variable/126168/1 "2025-02-21T21:40:02Z")

</div>

Hello,

I am using `DifferentialEquations.jl` to solve a system of ordinary differential equations (an `ODEProblem`) of the form `du = f(u, p, t)` where `u` is a vector.

One component of `u`, say `u[1]`, is monotonically increasing over the integration time span `(0, tf)`, with known initial and final values `u1_0`, `u1_f`.

How can I store the solution to the ODE on a uniform grid over the values of `u[1]`, i.e. for `u1_0:u1_step:u1_f`, where `u1_step` is given in input?

Note that this will result in a non-uniform time grid due to the nonlinearity in the ODEs, and I am interested in extracting this time grid together with the values of the other components of `u`.

I thought about using a `ContinuousCallback` to store the values on the fly, or to store the dense solution and then search for the `tk` such that `sol(tk)[1] % u1_step == 0`.

Which would be the preferred approach to follow?

---

<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:** [March 2, 2025, 11:12pm UTC](https://discourse.julialang.org/t/save-ode-solution-for-specific-values-of-one-state-variable/126168/2 "2025-03-02T23:12:53Z")

</div>

> [@afossa](#):
>
> I thought about using a `ContinuousCallback` to store the values on the fly, or to store the dense solution and then search for the `tk` such that `sol(tk)[1] % u1_step == 0`.
> 
> Which would be the preferred approach to follow?

Yes a continuous callback with `sol(tk)[1] % u1_step`. Just come up with a function that is continuous with the same zeros.

Though if you’re just saving, a specialized discrete callback would be more efficient.

---

<div class="post-metadata">

**Author:** ![afossa](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@afossa](https://discourse.julialang.org/u/afossa)\
**Post date:** [March 27, 2025, 6:40pm UTC](https://discourse.julialang.org/t/save-ode-solution-for-specific-values-of-one-state-variable/126168/3 "2025-03-27T18:40:54Z")

</div>

Thank you.

As I am not interested in the actual value of `u[1]`, but only in its increment between saved steps, I ended up defining the callback function as `u[1] -u1_step == 0` and resetting `u[1]` to zero after every occurrence of the event.

But how would you implement a discrete callback? My understanding is that this only checks the condition at the end of each time step, and does not perform root finding within one integration step.
