The DAEFunction was misspecified and a sparse linear solver is required. Working code is:
# generating the sparse jacobian using ModelingtoolKit
using ModelingToolkit, SparseArrays, Sundials
@variables u[1:3] du[1:3] γ
dIC=[0.8,2]
IC =[1.0,1.0,2.0]
tspan=(0.0,10.0)
diff_vars = [true,true, false]
p=[-2,1.2,-1.0]
function f!(res,du,u,p,t)
res[1] = du[1] - p[1] * u[1] + p[2] * u[1]*u[2]
res[2] = du[2] -p[3] * u[2] - u[1]*u[2]
res[3]= u[2] + u[1] - u[3]
end
res = similar(u)
t = 0.0
f!(res, du, u, p, t)
# need to combine d(res)/du + γ.*d(res)/d(du)
J_u = ModelingToolkit.jacobian(res, u)
J_du = γ.*ModelingToolkit.jacobian(res, du)
J = similar(J_u)
for i in 1:length(J_u)
J[i] = simplify(J_u[i] + J_du[i])
end
J_func! = eval(build_function(sparse(J), u, γ)[2])
function gnew!(J,du,u,p,gamma,t)
J_func!(J, u, gamma)
nothing
end
jac_prototype = sparse([1.0 1 0; 1 1 0; 1 1 1])
# solving the DAE
fg2 = DAEFunction(f!;jac=gnew!,jac_prototype = jac_prototype)
probMTK = DAEProblem(fg2,dIC,IC,tspan,p,differential_vars=diff_vars)
solMTK = solve(probMTK,IDA(linear_solver=:KLU))