# Sundials KLU Linear solver

**URL:** https://discourse.julialang.org/t/sundials-klu-linear-solver/112954
**Category:** Modelling & Simulations
**Tags:** ode, sundials
**Created:** [April 15, 2024, 9:57am UTC](https://discourse.julialang.org/t/sundials-klu-linear-solver/112954 "2024-04-15T09:57:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jespfra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jespfra/32/202556_2.png) [@jespfra](https://discourse.julialang.org/u/jespfra)
#### Post date: [April 15, 2024, 9:57am UTC](https://discourse.julialang.org/t/sundials-klu-linear-solver/112954/1 "2024-04-15T09:57:53Z")

</div>

Hi everyone,

I am trying to use the KLU method as linear solver for solving an ODE problem using Sundials.  
I have tried to apply it to the CVODE\_BDF solver for a simple problem, the rober equation. However, when I provide the sparsity pattern determined from the Jacobian function, that I also provide to the ODEFunction, it says:

> “Sparsity Pattern in receiving SUNMatrix doesn’t match sending SparseMatrix”

The code is:

> using Sundials  
> using IncompleteLU  
> using SparseArrays  
> using Plots  
> using LinearAlgebra
> 
> ################## Solving ODE ##################
> 
> #ode repressentation  
> function rober!(du, u, p, t)  
> y₁, y₂, y₃ = u  
> k₁, k₂, k₃ = p  
> du[1] = -k₁ \* y₁ + k₃ \* y₂ \* y₃  
> du[2] = k₁ \* y₁ - k₂ \* y₂^2 - k₃ \* y₂ \* y₃  
> du[3] = k₂ \* y₂^2  
> nothing  
> end
> 
> u0 = [1.0; 0.0; 0.0]  
> tspan = (0.0, 1e4)  
> p = [0.04, 3e7, 1e4]  
> prob = ODEProblem(rober!, u0, tspan,p)  
> sol = solve(prob,CVODE\_BDF())  
> p1 = plot();  
> plot!(p1,sol,label=“ODE”);
> 
> ################## Solving ODE With Jacobian ##################  
> #Jacobian  
> function rober\_jac!(J, u, p, t)  
> y₁, y₂, y₃ = u  
> k₁, k₂, k₃ = p  
> J[1, 1] = k₁ \* -1  
> J[2, 1] = k₁  
> J[3, 1] = 0  
> J[1, 2] = y₃ \* k₃  
> J[2, 2] = y₂ \* k₂ \* -2 + y₃ \* k₃ \* -1  
> J[3, 2] = y₂ \* 2 \* k₂  
> J[1, 3] = k₃ \* y₂  
> J[2, 3] = k₃ \* y₂ \* -1  
> J[3, 3] = 0  
> nothing  
> end
> 
> u0 = [1.0, 0, 0]  
> tspan = (0.0, 1e4)  
> p = [0.04, 3e7, 1e4]  
> jac\_proto = zeros(length(u0),length(u0))  
> rober\_jac!(jac\_proto, u0 .+ 1e-6, p, 0.0)  
> fun = ODEFunction(rober!; jac = rober\_jac!, jac\_prototype = sparse(jac\_proto))  
> prob = ODEProblem(fun, u0, tspan,p)  
> sol = solve(prob,CVODE\_BDF(; linear\_solver = :KLU))  
> p1 = plot();  
> plot!(p1,sol,label=“ODE”);

As seen from the ODE definition and the Jacobian, the sparsity patters is indeed:

> x x x  
> x x x  
> ⋅ x ⋅

However, if I instead set the sparsity patters as a dense 3x3 matrix, it solves the problem without any warning.

> fun = ODEFunction(rober!; jac = rober\_jac!, jac\_prototype = sparse(ones(3,3)))  
> prob = ODEProblem(fun, u0, tspan,p)  
> sol = solve(prob,CVODE\_BDF(; linear\_solver = :KLU))  
> p1 = plot();  
> plot!(p1,sol,label=“ODE”);

To me, that seems a bit odd as the elements [3,1] and [3,3] in the jacobian prototype are indeed zeros as also seen from the ODE definition (Rober!) and the Jacobian function (Rober\_jac!).  
Did I specify anything incorrectly?  
I also looked at the examples from [Sundials.jl/test/common\_interface/jacobians.jl at master · SciML/Sundials.jl (github.com)](https://github.com/SciML/Sundials.jl/blob/master/test/common_interface/jacobians.jl), however, the Jacobian for the Lotka problem is in this case a dense 2x2 matrix.  
Did some of you guys try to use the KLU linear solver in a Sundials solver, either ODE or DAE, and specify a sparsity pattern that is not a dense matrix?

---

<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: [April 15, 2024, 10:55am UTC](https://discourse.julialang.org/t/sundials-klu-linear-solver/112954/2 "2024-04-15T10:55:33Z")

</div>

You have to add the diagonal to it since it’s I - gamma\*J
