# How can we save & extract Derivatives from the Solver in Julia?

**URL:** <https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667>\
**Category:** General Usage\
**Created:** [January 11, 2025, 10:23am UTC](https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667 "2025-01-11T10:23:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naceur\_Bouziani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naceur_bouziani/32/35768_2.png) [@Naceur\_Bouziani](https://discourse.julialang.org/u/Naceur_Bouziani)\
**Post date:** [January 11, 2025, 10:23am UTC](https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667/1 "2025-01-11T10:23:50Z")

</div>

Hello everybody and happy new year to all of you,

I’m solving a PDE system using MethodeOfLines tool of Julia. The documentation guided me quite well.

benefiting from [ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas) advise to speed up the non linearity, here is the good setup of the solve line , that works very well for this problem.

```julia
sol = solve(prob, FBDF(linsolve = LinearSolve.KrylovJL_GMRES()), abstol=1e-8, reltol=1e-8)

```

* * *

```julia
pdesys = PDESystem(eqs, bcs, domains, [t, x], [u(t, x), v(t, x), w(t, x)])
prob = discretize(pdesys, discretization)

```

The problem was solved successfully, but very slowly. [ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas) is currently collaborating with Julia’s community to find ways to speed things up.

Now, I want to calculate the derivatives ( Dx(u) ), ( Dx(v) ), and possibly ( Dt(u) ) and ( Dt(v) ) throughout the domain. This would help me better understand the flux in the system.

To retrieve the solutions, I assign them like this:

```julia
solu = sol[u(t,x)]
solv = sol[v(t,x)]
solw = sol[w(t,x)]

```

I thought getting the derivatives would be as simple as retrieving the solutions, for example:

```julia
solDxu = sol[Dx(u)]

```

But this doesn’t work.

Maybe I need to save the derivative information during the solving stage,  
Maybe I should also ask for symbolic discretization because I’m using ModelingToolkit as fellow

```julia
sym_prob = symbolic_discretize(pdesys, discretization)

```

so I can access it later? I’m not sure how to proceed.

If someone understands my question and can help, I’d be really grateful!

If retrieving the derivatives isn’t possible, could you at least guide me on how to extract the boundary values of ( u ) and ( v ) from `solu` and `solv`? That way, I can calculate an average flux in the domain as a function of time.

* * *

Naceur Bouziani

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 12, 2025, 4:19pm UTC](https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667/2 "2025-01-12T16:19:49Z")

</div>

Does this help you or are you looking for something else?

> [@DifferentialEquations and derivatives of solutions?](https://discourse.julialang.org/t/differentialequations-and-derivatives-of-solutions/49995/3):
>
> You just add a second argument with the order of derivative you want: julia\> begin using DifferentialEquations f(u,p,t) = 1.1\*u u0 = 1/2 tspan = (0.0,1.0) prob = ODEProblem(f,u0,tspan) sol = solve(prob, Tsit5(), reltol=1e-10, abstol=1e-10) end; julia\> sol(1.0) # solution at t = 1.0 1.50208301197696 julia\> sol(1.0, Val{1}) ) # First derivative of the solution at t = 1.0 1.6522913131746269 julia\> 1.1 \* sol(1.0) ≈ sol(1.0…

---

<div class="post-metadata">

**Author:** ![Naceur\_Bouziani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naceur_bouziani/32/35768_2.png) [@Naceur\_Bouziani](https://discourse.julialang.org/u/Naceur_Bouziani)\
**Post date:** [January 12, 2025, 7:07pm UTC](https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667/3 "2025-01-12T19:07:23Z")

</div>

Hi [abraemer](https://discourse.julialang.org/u/abraemer)

Thank you so much! I’ll give this a try and let you know how it goes.

I’ve realized that I need to dive deeper into understanding how the integrator works—it’s becoming clear that learning more about it will be super helpful for achieving a lot. That said, I’ll probably need quite a bit of guidance along the way. I hope I won’t end up wearing you all out! 😊

I’ve started reading through [this resource](https://docs.sciml.ai/DiffEqCallbacks/stable/output_saving/) and working through it line by line. It’s not easy, but I know that with time, I’ll learn something new and make progress!

---

<div class="post-metadata">

**Author:** ![Naceur\_Bouziani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naceur_bouziani/32/35768_2.png) [@Naceur\_Bouziani](https://discourse.julialang.org/u/Naceur_Bouziani)\
**Post date:** [January 14, 2025, 5:19pm UTC](https://discourse.julialang.org/t/how-can-we-save-extract-derivatives-from-the-solver-in-julia/124667/4 "2025-01-14T17:19:29Z")

</div>

Hi [abraemer](https://discourse.julialang.org/u/abraemer)

While trying to learn about something (Val(1)) that I didn’t understand in the code you gave me, I found one utility, [DiffEqCallbacks.jl](https://docs.sciml.ai/DiffEqCallbacks/stable/) that seems to save information about the integrator. I’m trying to comprehend how this works, I made some progress. If I can use diff(u)/dx at each time and each x that could give me an estimation of derivatives. And DiffEqCallbacks save them for me, I can plot this and see the flux in my medium and see how this changes vs. time and also vs x. I will come back as soon as I progress
