Skip to content

get_du cause accessing undefined reference #785

@shipengcheng1230

Description

@shipengcheng1230

Hello, I am trying to retrieve the derivatives and save them to elsewhere and find a problem. Consider the following naive example:

using DifferentialEquations

function foo(du, u, p, t)
    du.x[1] .= u.x[1] / 10
    du.x[2] .= u.x[2] / 100
    du.x[3] .= u.x[3] / 1000
end
u0 = ArrayPartition(rand(2, 3), rand(5), rand(3, 2))
tspan = (0.0, 1000.0)
prob = ODEProblem(foo, u0, tspan)
cb1 = FunctionCallingCallback((u, t, integrator) -> integrator.fsallast)
cb2 = FunctionCallingCallback((u, t, integrator) -> integrator.cache.tmp)
cb3 = FunctionCallingCallback((u, t, integrator) -> get_du(integrator))

solve(prob, Tsit5(); callback=cb1) # ERROR: UndefRefError: access to undefined reference
solve(prob, Tsit5(); callback=cb2) # OK
solve(prob, Tsit5(); callback=cb3) # ERROR: UndefRefError: access to undefined reference

Accessing field fsallast of integrator would trigger this error. However, the following seems works without a glitch:

integrator = init(prob, Tsit5())
for i = 1: 100
    step!(integrator)
    x = get_du(integrator)
    y = integrator.cache.tmp
    @show x.x[1][1, 1], y.x[1][1, 1]
end # seems no error

But the derivatives are different (which one should I use?). How do I reliably retrieve the derivatives in such case? I am all on the latest Julia and packages.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions