Hello,
I want to supply the Jacobian to the ODE solver, it seems to me that on supplying the Jacobian the time and memory used by the solver is more than if I hadn’t supplied.
I am using sparse matrices for memory efficiency (for the example given below tri-diagonal would be better).
using DifferentialEquations,Plots, SparseArrays, LinearAlgebra
function my_fun!(du,u,p,t)
for i = 2:length(u)-1
du[i] = u[i+1] - 2*u[i] + u[i-1]
end
du[1] = 0
du[end] = - 2*u[end] + u[end-1]
nothing
end
function jacobian_!(J,u,p,t)
for i = 2:length(u)-1
J[i,i-1] = 1
J[i,i] = -2
J[i,i+1] = 1
end
J[1,1] = 0
J[1,2] = 0
J[end,end] = -2
J[end,end-1] = 1
nothing
end
N = 1000;
u0 = zeros(N)
u0[1] =1.0
tspan = (0.0,1000.0)
p = []
abc = spzeros(N,N)
f! = ODEFunction(my_fun!,jac = jacobian_!,jac_prototype = jacobian_!(abc,u0,p,0))
prob_jac = ODEProblem(f!,u0,tspan,p)
println("Solving with Analytical Jacobian")
@time sol_jac = solve(prob_jac,TRBDF2());
nothing
println("Solving without Analytical Jacobian")
prob = ODEProblem(my_fun!,u0,tspan,p)
@time sol_no_jac = solve(prob,TRBDF2());
nothing
Solving with Analytical Jacobian
1.067418 seconds (1.22 M allocations: 81.478 MiB, 6.94% gc time, 56.08% compilation time: 100% of which was recompilation)
Solving without Analytical Jacobian
0.708462 seconds (32.45 k allocations: 26.289 MiB, 40.18% gc time, 4.56% compilation time: 100% of which was recompilation)
What is it that I might be doing wrong?
Note: The condition number is very high for this example
julia> cond(Array(abc),2)
8.207431449069317e15