Handling events in DifferentialEquations.jl that take place over a time interval

Hello,
I am wondering what is the best way to implement events that happen during a time interval that affect an element of the derivative. Currently my code is of the form:

intervals = [1.0 2.0; 5.0 6.0; 9.0 10.0] # every row is a 1-sec interval in which the event takes place

function dydt!(dy,y,p,t)
    @. dy = .....
    # event handling:
    idx = findfirst([intervals[i,1] <= t < intervals[i,2] for i in 1:size(intervals,1)])
    if idx != nothing
        dy[idx] += ..... #add something to that component of the derivative for 1 sec
    end

end

What I was worried about is that the solver won’t actually start and end the effect of the events at the correct times, but rather depending on the time steps. One option is making the solver stop at the start and end points with tstops=intervals[:], but I wasn’t sure if that would be the correct way to handle that.
I thought I should use PresetTimeCallback to handle these events, however it seems mostly appropriate for instantaneous events.
Thanks!

That would be the correct way.

1 Like