# ILU preconditioned GMRES inside TRBDF2 for ODEs generated by NetworkDynamics.jl

**URL:** <https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385>\
**Category:** Performance\
**Tags:** package\
**Created:** [June 22, 2021, 5:07pm UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385 "2021-06-22T17:07:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [June 22, 2021, 5:07pm UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/1 "2021-06-22T17:07:17Z")

</div>

We are solving differential equations defined on a graph using NetworkDynamics.jl and DifferentialEquations.jl. In this context we have the two questions outlined below.

Thx. Domenico Lahaye.

**1/ Switching the automatic Jacobian off in NetworkDynamics.jl**

We would like to convince ourselves that using the Jacobian indeed makes sense by comparing the CPU time of a run with and without the Jacobian. We, however, do not manage to switch off the automatic computation (or use) of the Jacobian. That is, the two runs outlined below give identical CPU time. What are we doing wrong or overseeing?

(ModelingToolkit does not allow to switch off Jacobian computation? Use of Jacobian should be switched off as option for time-integration method? What are solver options for TRBDF2 aside from linsolve= … ?)

RUN 1: prob = ODEProblem(network!, u0, tspan, p, jac=true)

RUN 2: prob = ODEProblem(network!, u0, tspan, p, jac=false)

**2/ Specifying ILU preconditioned GMRES as solver within TRBDF2 in NetworkDynamics.jl**

We managed to run TRBDF2 with unpreconditioned GMRES as Krylov method. Next we would like to switch on ILU as a preconditioner. The use of ILU requires the detection of the sparsity pattern. The sparsity detection conflicts with the definition by NetworkDynamics of ODEFuntion. Details (including the error message) are given at [https://github.com/bszhu/SIR.jl/blob/8a7a9a8bb0fdbcc615030a2da755461a39a1d266/main/preconditioner.jl](https://github.com/bszhu/SIR.jl/blob/8a7a9a8bb0fdbcc615030a2da755461a39a1d266/main/preconditioner.jl)

(network! = network\_dynamics(node, edge, g\_directed) # key constructor network\_dynamics returns an ODEFunction)

(Should we call the function generate\_jacobian(network!,output,input) instead? What does aspreconditioner refer to?)

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [June 24, 2021, 9:09am UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/2 "2021-06-24T09:09:33Z")

</div>

Any suggestions?

---

<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 24, 2021, 10:00am UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/3 "2021-06-24T10:00:42Z")

</div>

> [@ziolai](#):
>
> We would like to convince ourselves that using the Jacobian indeed makes sense by comparing the CPU time of a run with and without the Jacobian. We, however, do not manage to switch off the automatic computation (or use) of the Jacobian. That is, the two runs outlined below give identical CPU time. What are we doing wrong or overseeing?

Stiff ODEs use the Jacobian unless you change the jac\_prototype to be a matrix-free object, which would then require a compatible linear solver. This is covered in the tutorial on stiff ODEs:

[https://diffeq.sciml.ai/stable/tutorials/advanced\_ode\_example/](https://diffeq.sciml.ai/stable/tutorials/advanced_ode_example/)

though admittedly we could (and will) make that a little nicer. So if you’re still using a direct solver, `jac=true` is just generating the analytical solution to the Jacobian and generating the code for that, vs using AD. In some cases that will have similar performance. The advantage vs AD usually is only clear when sparsity is involved, since even sparse automatic differentiation when done optimally has some limitations.

> [@ziolai](#):
>
> We managed to run TRBDF2 with unpreconditioned GMRES as Krylov method. Next we would like to switch on ILU as a preconditioner. The use of ILU requires the detection of the sparsity pattern. The sparsity detection conflicts with the definition by NetworkDynamics of ODEFuntion

Since you have MTK working, you can use its sparsejacobian function to extract the sparse Jacobian, or simply add `sparse=true` in the generation phase and grab the `jac_prototype` (there it would also have the advantage of specializing the Jacobian computation on the sparsity pattern)

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [June 24, 2021, 10:35am UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/4 "2021-06-24T10:35:58Z")

</div>

Sincere thx! We will give it a look and get back here.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [June 24, 2021, 4:38pm UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/5 "2021-06-24T16:38:06Z")

</div>

Spending more time on the documentation proved valuable 😉 Thank you for that suggestion.

The page [ODE Solvers · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/solvers/ode_solve/) list TRBDF2 as an SDIRK method. Is this intentional?

---

<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 24, 2021, 4:44pm UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/6 "2021-06-24T16:44:48Z")

</div>

TRBDF2 is an SDIRK method, yes. In fact, it is in the class of eSDIRK methods.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [June 24, 2021, 4:47pm UTC](https://discourse.julialang.org/t/ilu-preconditioned-gmres-inside-trbdf2-for-odes-generated-by-networkdynamics-jl/63385/7 "2021-06-24T16:47:29Z")

</div>

Will have to read up on this. Thx again.
