Skip to content

DifferentialEquations.jl does not save fields of custom types after the callback function #117

@ronisbr

Description

@ronisbr

Hi guys!

I am using DifferentialEquations.jl to simulate a satellite with continuous and discrete algorithms. @ChrisRackauckas helped me and the full discussion about it can be found here:

https://discourse.julialang.org/t/get-values-of-symbols-inside-a-function/848

However, I had a problem because I needed to modify custom fields of my new type inside the callback function and use those values in the next integration step. It turns out that DifferentialEquation.jldoes not save those values as I am expecting. Consider the following simplified example:

https://gist.github.com/ronisbr/1cac82902f78cf33ad732df79a5bd369

The dynamic function and callbacks are:

function f(t,u,du)
    du[1] = -0.5*u[1] + u.f1
    du[2] = -0.5*u[2]
end

callback = @ode_callback begin
    if t > 5
        u.f1 = 1.5
    end
    @ode_savevalues
end

Hence, I was expecting that u[1] becomes different of u[2] for t > 5. However, it does not happen as we can see in the following figure:

case_01

@ChrisRackauckas pointed out that in order to make this work, I need modify the callback function as described next:

callback = @ode_callback begin
    if t > 5
        for c in cache
            c.f1 = 1.5
        end
    end
    @ode_savevalues
end

The full code can be seen here:

https://gist.github.com/ronisbr/2883ffd81abcc379a259f13faf2b92ea

Now, the result is:

case_02

It does not seem a bug, as @ChrisRackauckas said, but it really should be documented.

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