# Solving DAEs with known sparsity pattern and IDA

**URL:** https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318
**Category:** General Usage
**Tags:** sparse, differentialequation, sundials
**Created:** [May 15, 2024, 6:59pm UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318 "2024-05-15T18:59:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ataideneto](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@ataideneto](https://discourse.julialang.org/u/ataideneto)
#### Post date: [May 15, 2024, 6:59pm UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318/1 "2024-05-15T18:59:47Z")

</div>

Hi everyone!

Can someone provide a minimal example of how to use sparsity patterns (jac\_prototype) when solving DAEs with IDA solver? I manage to use sparsity patterns with ODEs and the julia solvers…

Here are some examples that I am trying to solve:

ODE Version

n = 1000  
L = 0.5 .+ rand(n)

function odefun(yp, y, p, t)  
yp[:] = -L.\*y  
end

jac\_pattern = spdiagm(ones(n))  
colorvec = matrix\_colors(jac\_pattern)  
tspan = (0,2)  
y0 = ones(n)  
ode = ODEFunction(odefun)  
ode\_sparse = ODEFunction(odefun, jac\_prototype=jac\_pattern, colorvec=colorvec)

prob = ODEProblem(ode, y0, tspan)  
prob\_sparse = ODEProblem(ode\_sparse, y0, tspan)

@btime solve(prob, TRBDF2()) # solves in 85 ms  
@btime solve(prob\_sparse, TRBDF2()) # solves in 1 ms

However the DAE doesn’t work:

n = 1000  
L = 0.5 .+ rand(n)

function residuals(res, yp, y, p, t)  
res[:] = yp + L.\*y  
end

jac\_pattern = spdiagm(ones(n))  
colorvec = matrix\_colors(jac\_pattern)  
tspan = (0,2)  
y0 = ones(n)  
yp0 = -L

dae = DAEFunction(residuals)  
dae\_sparse = DAEFunction(residuals, jac\_prototype=jac\_pattern, colorvec=colorvec)

prob = DAEProblem(dae, yp0, y0, tspan, differential\_vars=trues(n))  
prob\_sparse = DAEProblem(dae\_sparse, yp0, y0, tspan, differential\_vars=trues(n))

@btime solve(prob, IDA()) # solves in 52 ms  
@btime solve(prob\_sparse, IDA()) # solves in 49 ms

I would expect that the sparse problem would have a huge decrease in time as in the ODE case.

Thanks for the support!

---

<div class="post-metadata">

### Author: ![ataideneto](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@ataideneto](https://discourse.julialang.org/u/ataideneto)
#### Post date: [May 15, 2024, 8:02pm UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318/2 "2024-05-15T20:02:01Z")

</div>

The same problem occurs when using ODE formulation with CVODE\_BDF also from sundials:

n = 1000  
L = 0.5 .+ rand(n)

function odefun(yp, y, p, t)  
yp[:] = -L.\*y  
end

jac\_pattern = spdiagm(ones(n))  
colorvec = matrix\_colors(jac\_pattern)  
tspan = (0,2)  
y0 = ones(n)

ode = ODEFunction(odefun)  
ode\_sparse = ODEFunction(odefun, jac\_prototype=jac\_pattern, colorvec=colorvec)  
prob = ODEProblem(ode, y0, tspan)  
prob\_sparse = ODEProblem(ode\_sparse, y0, tspan)

@btime solve(prob, CVODE\_BDF()) # solves in 27 ms  
@btime solve(prob\_sparse, CVODE\_BDF()) # solves in 27ms

---

<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 16, 2024, 8:10am UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318/3 "2024-05-16T08:10:43Z")

</div>

With Sundials you have to tell it to use a sparse linear solver, i.e. `CVODE_BDF(linear_solver=:KLU)`

---

<div class="post-metadata">

### Author: ![ataideneto](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@ataideneto](https://discourse.julialang.org/u/ataideneto)
#### Post date: [May 16, 2024, 4:16pm UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318/4 "2024-05-16T16:16:44Z")

</div>

Thank you for the answer, Cris!

However, for the KLU you have to pass the actual jacobian…

---

<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 16, 2024, 10:51pm UTC](https://discourse.julialang.org/t/solving-daes-with-known-sparsity-pattern-and-ida/114318/5 "2024-05-16T22:51:55Z")

</div>

Oh yes, Sundials cannot use sparse differentiation.
