OrdinaryDiffEq.jl
OrdinaryDiffEq.jl copied to clipboard
Cannot disable Jacobian sparsity pattern checking
I want to solve an ODE with an implicit solver and sparse Jacobian. LinearSolve docs hint at setting check_pattern = false for a speedup if the sparsity pattern is constant. This check allocates and shows up in the solve flamegraph:
using ModelingToolkit, OrdinaryDiffEqRosenbrock, LinearSolve
D, t = ModelingToolkit.D_nounits, ModelingToolkit.t_nounits
@variables x(t) y(t)
@named M = System([D(x) ~ x + y, D(y) ~ x], t)
M = mtkcompile(M)
prob = ODEProblem(M, [x => 1.0, y => 1.0], (0.0, 1.0); jac = true, sparse = true)
@profview sol = solve(prob, Rodas5P(linsolve = KLUFactorization(check_pattern = true)); adaptive = false, dt = 1e-6, save_everystep = false)
Should it then work to solve the ODE as in the last line here?
sol = solve(prob, Rodas5P(linsolve = KLUFactorization(check_pattern = true))) # works
sol = solve(prob, Rodas5P(linsolve = KLUFactorization(check_pattern = false))) # fails
The last line fails with
ERROR: DimensionMismatch:
Stacktrace:
[1] klu!(K::LinearSolveSparseArraysExt.KLU.KLUFactorization{…}, nzval::Vector{…}; check::Bool, allowsingular::Bool)
@ LinearSolveSparseArraysExt.KLU C:\Users\herma\.julia\dev\LinearSolve\src\KLU\klu.jl:651
[2] klu!
@ C:\Users\herma\.julia\dev\LinearSolve\src\KLU\klu.jl:649 [inlined]
[3] solve!(cache::LinearSolve.LinearCache{…}, alg::KLUFactorization; kwargs::@Kwargs{…})
@ LinearSolveSparseArraysExt C:\Users\herma\.julia\dev\LinearSolve\ext\LinearSolveSparseArraysExt.jl:292
[4] solve!
@ C:\Users\herma\.julia\dev\LinearSolve\ext\LinearSolveSparseArraysExt.jl:277 [inlined]
[5] #solve!#21
@ C:\Users\herma\.julia\dev\LinearSolve\src\common.jl:441 [inlined]
[6] solve!
@ C:\Users\herma\.julia\dev\LinearSolve\src\common.jl:440 [inlined]
[7] #dolinsolve#2
@ C:\Users\herma\.julia\packages\OrdinaryDiffEqDifferentiation\TKWRC\src\linsolve_utils.jl:30 [inlined]
[8] dolinsolve
@ C:\Users\herma\.julia\packages\OrdinaryDiffEqDifferentiation\TKWRC\src\linsolve_utils.jl:5 [inlined]
[9] perform_step!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, cache::OrdinaryDiffEqRosenbrock.RosenbrockCache{…}, repeat_step::Bool)
@ OrdinaryDiffEqRosenbrock C:\Users\herma\.julia\packages\OrdinaryDiffEqRosenbrock\L1JuA\src\rosenbrock_perform_step.jl:1363
I suspect what happens is that KLUFactorization stores some "null factorization" at construction, and errors when it encounters the true ODE Jacobian for the first time?
Is this handled?
Yep, forgot to close.