Nicholaswogan / numbalsoda

Python wrapper of LSODA (solving ODEs) which can be called from within numba functions.
MIT License
91 stars 9 forks source link

Backward in time using numbalsoda #19

Open meysam-motaharfar opened 1 year ago

meysam-motaharfar commented 1 year ago

Hi

I am using numblsoda to solve couples of first order differential equations. I need to solve those equations backward in time for some particular situations. I could not figure out how I can do that. I tried to use decreasing time step but it does not work as it works for solve_ivp and odeint.

for instance in this code how can I go backward in time?

from numbalsoda import lsoda_sig, lsoda, dop853
from numba import njit, cfunc
import numpy as np

@cfunc(lsoda_sig)
def rhs(t, u, du, p):
    du[0] = u[0]-u[0]*u[1]
    du[1] = u[0]*u[1]-u[1]*p[0]

funcptr = rhs.address # address to ODE function
u0 = np.array([5.,0.8]) # Initial conditions
data = np.array([1.0]) # data you want to pass to rhs (data == p in the rhs).
t_eval = np.arrange(0.0,50.0,0.1) # times to evaluate solution

usol, success = lsoda(funcptr, u0, t_eval, data = data)

I tried t_eval = np.arrange(50, 0, -0.1) but it does not work. It would be great if someone could help me to fix this issue.

Nicholaswogan commented 1 year ago

This is a current drawback of the code. It does not permit backwards integration right now, but it is very fixable. I will leave the issue up, and fix it when I have time