Solving an ODE using solve_ivp from scipy

I am trying to slove an ordinary differential equation using scipy’s solve_ivp.
I call the right hand side of the differential equation ‘gradient’, so I wrote:
@njit
def gradient(…):

pretty complicated code

return …

result = solve_ivp(gradient…)

I get the error message below:
UnsupportedError: Failed in nopython mode pipeline (step: analyzing bytecode)
An unsupported bytecode sequence has been encountered: op_LIST_EXTEND at the start of a block.

This could be due to the use of a branch in a tuple unpacking statement.

I absolutely do not understand what this may mean.
Any help is greatly appreciated!

Hi Peter,

the error indicates that you are doing something in your code that is not supported by numba.

Without seeing the actual code it is virtually impossible to tell what exactly the problem is (and whether this is something that SHOULD work or is expected to fail). So if you can, I encourage you to share the code, even if it is complex.

If you can’t share your code, the error message gives a hint at a likely issue: You have some kind of conditional branch somewhere, and some operation you do generates some violation. This can for example happen when using the same variable name for something that may end up with different types depending on the branch (although the error does not really look like that). Keep in mind that numba has to be much more rigid than what is possible in pure Python.

I would also advise you to call your gradient function manually with some reasonable inputs, that makes it much easier to reproduce and understand your issues, without having to roll the full solve_ivp machinery and the obfuscated values it pushes into your function.

I will hazard a guess that your code may represent some rather complicated, parameterised model. As I have been in a similar situation in the past, I can relate that those can be tricky to translate into numba compatible Python, especially if you are not dealing with a pure function but try to shoehorn in other stuff in hacky ways.

Once you have everything working well, it could be fun for you to check out this project by some other numba users: GitHub - hgrecco/numbakit-ode: Leveraging numba to speed up ODE integration. They have taken solve_ivp as a starting point for a fully numba-rised ODE solver package. I.e. even the integrator runs in compiled code, not just your RightHandSide Function. This is particularly nice because your RHS will typically be called very often, and every time you switch from Python to compiled numba and back, has a little cost.

Dear Hannes,
Thanks again for your reply!
I do not mind sharing the code, but it is a monster, complicated as you rightly guessed.

I am reasonably o.k. at using sympy.physics.mechanics for simulations - but TOTAL novice at numba!
I will take your advice to try numba on simpler models to learn more about it.

Thanks again!

Peter