I am using solve_ivp
with the BDF
method to solve a complex ODE system.
I tested it under different system parameters. It usually takes seconds to get the result. But, for one of the cases, it keeps running with super tiny step sizes (smaller than 1e-8). This may lead to unlimited steps to be taken, and it may take days to weeks to get the results. (t
increases from 0.00017 to 0.00020 in ~20 min, while t_end=100
)
To avoid endless running for such cases, I have attempted to use the timeout function of multiprocessing
. It works for other examples, but failed to terminate solve_ivp
.
Is there any other solution that might work to terminate the ODE integration (with a given time or number of steps)?
You could use a callback function with events
argument to stop the integration after a certain time or number of steps:
from scipy.integrate import solve_ivp
import time
def system(t, y):
dydt = [y[1], -y[0]]
return dydt
y0 =[0, 0]
t_span = [0, 100]
start_time = time.time()
def stop_event(t, y):
return min(time.time() - start_time - 10, len(y) - 1000)
stop_event.terminal = True
solution = solve_ivp(system, t_span, y0, method='BDF', events=stop_event)