odeint is a Python shell for C code that calls LSODA (a Fortran routine) that calls C code to call my Python callback representing dy / dt. LSODA does not throw a Python exception, and switching from one bit of C code to another is difficult to implement.
I found a satisfactory solution just using ode instead of odeint . I find it ode more difficult to get started, but it behaves correctly when an exception is thrown in Python. The fake_odeint() function below is the beginning to create a function that works like odeint well enough for my purposes so that I can replace it in my existing code. The disadvantage of using ode instead of odeint is that LSODA receives a call once per unit of time; this call occurs in C with odeint and slower in Python in ode .
import numpy as np from scipy.integrate import ode def fake_odeint(func, y0, t, Dfun=None): ig = ode(func, Dfun) ig.set_integrator('lsoda', method='adams') ig.set_initial_value(y0, t=0.) y = [] for tt in t: y.append(ig.integrate(tt)) return np.array(y)
I examined the use of Fortran, C, and Python code so that odeint can interact with LSODA in this SO question .
source share