ForwardDiff with Rosenbrock in DifferentialEquations.jl

Hello all. I am having an issue using Rosenbrock methods in DifferentialEquations.jl. Below is a MWE. My general problem is that I am trying to optimize parameters of a stiff system of ODEs. This is a relatively small problem so something like Rodas4P with ForwardDiff is suitable. However I am running into issues computing gradients with respect to parameters. When I attempt to do so, I get the following error: type LinearProblem has no field f (see example below). The error tracks back to the solve line. I’m not seeing any dual number errors in the stack trace and I did attempt to type the initial condition (if it is necessary) to ensure the solver inferred the dual type for du.

If I replace Rodas4P with Tsit5(), everything is fine. If I use Rodas4P(autodiff = false), I get the error once again. So it is something about the linear solve in the Rosenbrock method that is causing the issue.

Any thoughts? A search on this specific error doesn’t seem to pull anything up.

function ODE_tmp!(du,u,p,t)

    a = p[1]
    b = p[2]

    du[1] = a - u[1]
    du[2] = a + b - u[2]

    return nothing
end

function run_sim(x_change, odeprob_in)

    u0 = eltype(x_change[1]).(odeprob_in.u0)

    println("u0 = ", u0)
    println("x_change = ", x_change[1])
    println(typeof(x_change[1]))

    odeprob_new = remake(odeprob_in , p = x_change, u0 = u0);
    sol = solve(odeprob_new, Rodas4P(), reltol=1e-4, abstol=1e-4);

    tend = odeprob_new.tspan[2];
    return_val = sol(tend)[1] + sol(tend)[2];

    return return_val
end


u0 = [0.0, 0.0];
tspan = (0.0, 10.0);
p = [2.0,3.0];

odeprob = ODEProblem(ODE_tmp!, u0, tspan, p);


run_sim(p,odeprob) # Runs ok

ForwardDiff.gradient(x -> run_sim(x, odeprob), p) # Error with Rodas4P but not Tsit5()

What package versions are you using? Have you tried updating them?

Also as a side note, Rodas5P is usually faster than Rodas4P. It’s probably worth switching (although it won’t change anything here).

Thanks for the response. The proverbial turn it off / on trick! My environment and all packages were about a year old. Updating corrected the issue.

Thank you for taking the time. I really appreciate that you all respond to these!

2 Likes