Run scipy odeint on first error

Edit: this problem is due to a bug fixed in Scipy 0.15

Since I am developing and testing the code, I can make a simple mistake, for example, NameError. When I use scipy.integrate.odeint, odeint will print an error message, but will continue to integrate with any number of temporary requests that I request, so I get many identical error messages. I assume that it has such a behavior that it can act when arithmetic errors occur (for example, divide by zero), but this is useless behavior for programming errors.

Is there a way to get scipy to stop after the first error message? It would be better if I could make him stop for errors, but not for arithmetic exceptions.

+4
source share
1 answer

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 .

+2
source

Source: https://habr.com/ru/post/1479563/


All Articles