Error using Sundials and JuMP

First I ve defined a function that uses Sundials solver.

function rheo(czz0)

prob = DAEProblem(filament,du₀,u₀,tspan,differential_vars=differential_vars)
sol = Sundials.solve(prob, IDA(), reltol=1e-8, abstol=1e-8)
Dr=sol[4,end]

return Dr
end

The function works well. Thus, I tried to use JuMP.jl to optimization.

Somethink like

model = Model(GLPK.Optimizer)
@variable(model, 1 <= x <= 3.6)
@objective(model, Min, abs(10-rheo(x)))
optimize!(model)

Thus, I obtained the error

ERROR: MethodError: no method matching Sundials.FunJac(::Sundials.var"#82#86"{DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}},Tuple{Int64},Tuple{Int64}}, ::Nothing, ::Nothing, ::DiffEqBase.NullParameters, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Array{GenericAffExpr{Float64,VariableRef},1}, ::Array{Float64,1}, ::Array{Float64,1})
Closest candidates are:
Sundials.FunJac(::F, ::F2, ::J, ::P, ::M, ::J2, ::Prec, ::PS, ::uType, ::uType, ::uType2) where {F, F2, J, P, M, J2, uType, uType2, Prec, PS} at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\function_types.jl:3
Sundials.FunJac(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\function_types.jl:16
Sundials.FunJac(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\function_types.jl:15
Stacktrace:
[1] Sundials.FunJac(::Function, ::Nothing, ::DiffEqBase.NullParameters, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Array{GenericAffExpr{Float64,VariableRef},1}, ::Array{Float64,1}, ::Array{Float64,1}) at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\function_types.jl:16
[2] __init(::DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}}, ::IDA{:Dense,Nothing,Nothing}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}; verbose::Bool, dt::Nothing, dtmax::Float64, save_on::Bool, save_start::Bool, callback::Nothing, abstol::Float64, reltol::Float64, saveat::Array{Float64,1}, tstops::Array{Float64,1}, maxiters::Int64, timeseries_errors::Bool, dense_errors::Bool, save_everystep::Bool, dense::Bool, save_timeseries::Nothing, save_end::Bool, progress::Bool, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), advance_to_tstop::Bool, stop_at_next_tstop::Bool, userdata::Nothing, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\solve.jl:897
[3] init_call(::DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}}, ::IDA{:Dense,Nothing,Nothing}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Float64,Tuple{Symbol,Symbol},NamedTuple{(:reltol, :abstol),Tuple{Float64,Float64}}}) at C:\Users\jorgem63.julia\packages\DiffEqBase\smkNe\src\solve.jl:14
[4] init(::DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}}, ::IDA{:Dense,Nothing,Nothing}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Float64,Tuple{Symbol,Symbol},NamedTuple{(:reltol, :abstol),Tuple{Float64,Float64}}}) at C:\Users\jorgem63.julia\packages\DiffEqBase\smkNe\src\solve.jl:33
[5] __solve(::DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}}, ::IDA{:Dense,Nothing,Nothing}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Type{Val{true}}; kwargs::Base.Iterators.Pairs{Symbol,Float64,Tuple{Symbol,Symbol},NamedTuple{(:reltol, :abstol),Tuple{Float64,Float64}}}) at C:\Users\jorgem63.julia\packages\Sundials\LzbrB\src\common_interface\solve.jl:10
[6] solve_call(::DAEProblem{Array{GenericAffExpr{Float64,VariableRef},1},Array{Float64,1},Tuple{Float64,Float64},true,DiffEqBase.NullParameters,DAEFunction{true,var"#filament#3"{Float64,Float64,Float64,Float64,Float64,Float64,Float64,Int64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},Array{Bool,1}}, ::IDA{:Dense,Nothing,Nothing}; merge_callbacks::Bool, kwargs::Base.Iterators.Pairs{Symbol,Float64,Tuple{Symbol,Symbol},NamedTuple{(:reltol, :abstol),Tuple{Float64,Float64}}}) at C:\Users\jorgem63.julia\packages\DiffEqBase\smkNe\src\solve.jl:60
[7] #solve_up#444 at C:\Users\jorgem63.julia\packages\DiffEqBase\smkNe\src\solve.jl:85 [inlined]
[8] #solve#443 at C:\Users\jorgem63.julia\packages\DiffEqBase\smkNe\src\solve.jl:69 [inlined]
[9] rheotens(::VariableRef) at C:\Users\jorgem63\rheotens2.jl:79
[10] top-level scope at C:\Users\jorgem63.julia\packages\MutableArithmetics\NuiNA\src\rewrite.jl:227
[11] top-level scope at C:\Users\jorgem63.julia\packages\JuMP\YXK4e\src\macros.jl:800

Any idea about this?

JuMP assumes differentiability, and the Sundials solvers are not differentiable. You’ll want to use DiffEqFlux for this kind of thing, or directly define the gradient of the ODE via https://docs.sciml.ai/latest/analysis/sensitivity/

2 Likes