question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in Solve_ivp with BDF and Radau solvers and numpy arrays

See original GitHub issue

It seems Scipy integration module solve_ivp when used with BDF and Radau solvers appends rounded numbers to the array. This will then cause an error of inconsistent dimensional further on in my code. I wrote a simple example of a system with (2,1)-dimensional state (i.e. a second order system), which prints the state y as it runs in the function f(t,y), at one point, it appends the rounded number and gave me

[[ 13.67999979  13.67999999]
 [-15.99       -15.98999976]]

The second column is unnecessary and these added numbers will cause error in my original code.

Reproducing code example:

from numpy import array, matmul
from scipy.integrate import solve_ivp
from numpy import size
def sys(t,x,A,xeq,matmul,size):
    if size(x)==4:
        print(x)
    xdot=matmul(A,x-xeq)
    return xdot

T=10
A=array([[-1,2],[-2,-6]])
x0=array([13.67999999,-15.989999999])
xeq=array([[0.99999999],[2.999999999]])
fun=lambda t, x: sys(t,x,A,xeq,matmul,size)
sol=solve_ivp(fun,[0,T],x0,method='BDF',vectorized=True)

The output is:

[[ 13.67999979  13.67999999]
 [-15.99       -15.98999976]]

Scipy/Numpy/Python version information:

1.1.0 1.14.4 sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewDMarquiscommented, Jul 15, 2019

I believe this bug is bigger than appending rounded numbers to the array. I’m working with a very large system of ODEs (1000+ equations), and have found that when I use the vectorized=True option for solve_ivp the solver will attempt to pass a (n,n) matrix into the right-hand-side function, where n is the number of variables (n=1180 for the ODE system I’m working with).

I also agree that the documentation for the “vectorized” option is entirely unclear.

1reaction
TUCMathcommented, Jun 12, 2018

Why should there be a difference in the way BDF and Radau implemented with that of RK45 and RK23. Users should be able to easily change the method to observe speed, convergence, and accuracy of each method. Nonetheless, if there is difference, it should be explicitly stated. I will try to design a simple system of ODEs for which switching from RK45 to BDF yields an error of inconsistent dimension.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange behaviour in scipy.solve_ivp when using an implicit ...
It appears that the Radau and BDF methods do not handle single-valued RHS functions. Making the function f above output a 1-D list...
Read more >
scipy.integrate.solve_ivp — SciPy v1.9.3 Manual
If it makes unusually many iterations, diverges, or fails, your problem is likely to be stiff and you should use 'Radau' or 'BDF'....
Read more >
Ordinary differential equation solvers in Python
Scipy. Scipy uses the scipy.integrate.solve_ivp function to numerically solve an ordinary first order differential equation with initial value.
Read more >
A Comparison Between Differential Equation Solver Suites In ...
We still note that its array of available methods is small and it offers radau which is great for high accuracy ODEs and...
Read more >
Scipy integrate solve ivp - Carlino Edilizia
The individual solvers (RK23, RK45, Radau, BDF and LSODA) can also be used ... this using solve_ivp function in scipy. integrate import solve_ivp...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found