DifferentialEquations.jl does not save fields of custom types after the callback function #117
I am using
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
The dynamic function and callbacks are:
function f(t,u,du) du = -0.5*u + u.f1 du = -0.5*u end callback = @ode_callback begin if t > 5 u.f1 = 1.5 end @ode_savevalues end
Hence, I was expecting that
@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:
Now, the result is:
It does not seem a bug, as @ChrisRackauckas said, but it really should be documented.
The text was updated successfully, but these errors were encountered:
I don't know if that plot if from a different problem (the time goes from 0 to 30 and the discontinuity is at a different spot?), but I ran your code and saw the same wobblyness in the plot. This problem occured when there's a value which was both in
Another thing that I had to handle though is the fact that discontinuities in the derivative mess with the interpolation. The fix is to save both before and after applying a discontinuous change. I fixed up the code to do this here:
which gives the solution:
(with the patch applied, using
That makes me think I led you astray. I'm sorry! These are details that the event handling framework takes care of for you, so maybe I should have had you define an event instead! Here's the event-handling way.
For events, you define a function which is
const tstop =  function event_f(t,u) # Event when event_f(t,u) == 0 t ∉ tstop end
Then you say what you want. This is the discontinuous change you defined before:
function apply_event!(u,cache) for c in cache c.f1 = 1.5 end end
const rootfind_event_loc = false const interp_points = 0 callback = @ode_callback begin @ode_event event_f apply_event! rootfind_event_loc interp_points end
The first part
The code using events in its entirety can be found here:
and produces the following plot:
I'll add this example to DiffEqTutorials.jl.
Thank you very much! Don't need to apologize, it was my fault since I did not describe the problem deeply enough.
I will port the code to use those events. My only question is: inside the
Not yet, but that's in the plan:
(Feel free to comment there on the kinds of things you need in events and callbacks!)
If you need it right now, I can help you get it going. It should be as simple as making