Use solve_ivp's 'events' to check for convergence

Viewed 338

Problem:

Assume a simple decay process as described by the following ode:

def exponential_decay(t,y):
    return -0.5 * y

This can easily be integrated with the help of scipy's solve_ivp()

t_min = 0; t_max = 25; y0 = 1
sol = solve_ivp(exponential_decay, [t_min, t_max],[y0],dense_output=True)

The resulting solution might look like this:

enter image description here

Question:

I would like to use solve_ivp's "event"-finder to check for convergence to reduce the computational time spent after convergence is reached. However, the designed signature of the event tracker when an event function is provided is:

event(t,y) -> t_event

An event occurs when the return of the event function is equal to zero.

Because event(t,y) only knows the current y(t) it can not be used straightforwardly to implement standard convergence criteria as they all require a series of y.

So to cut this short: Is there a good way to do so, to use the event finder to check for convergence?
Or to make use of any kind of range of y(t) in the convergence tracker? This seems like something that would be helpful in many applications

A (bad way) to do so I found is to pass a global variable in and out of event(t,y) that stores the the differnt (t,y(t). However, this is not only extreamly unelegant, it also offsets the computational efficiency provided by solive_ivp()

0 Answers
Related