Speeding up repetitive calls to ODEProblem

You should try to avoid creating a new ODEProblem on every call.

Probably remake is what you are searching for. For example:


using DifferentialEquations

function multiple_odes()
    f(u,p,t) = 1.01*u
    u0 = 1/2
    tspan = (0.0,1.0)
    prob = ODEProblem(f,u0,tspan)
    for i in 1:100
        prob = remake(prob, u0 = rand())
        sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
    end
end

function multiple_odes0()
    f(u,p,t) = 1.01*u
    u0 = 1/2
    tspan = (0.0,1.0)
    for i in 1:100
        u0 = rand()
        prob = ODEProblem(f,u0,tspan)
        sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
    end
end

Gives:

julia> @btime multiple_odes()
  268.775 μs (4104 allocations: 621.91 KiB)

julia> @btime multiple_odes0()
  1.019 ms (14057 allocations: 1.04 MiB)
4 Likes