hgrecco / numbakit-ode

Leveraging numba to speed up ODE integration
Other
68 stars 3 forks source link

Error when trying to use BDF solver #24

Closed hfsf closed 3 years ago

hfsf commented 3 years ago

I am currently having trouble to use BDF solver. The numnakit-ode package has version 0.4, installed though pip. When following the tutorial (https://numbakit-ode.readthedocs.io/en/latest/tutorial.html), only replacing the solver name to any of BDF's family solvers, i get the following error output (in the ecample I've used BDF1):

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-9d81f0b04c02> in <module>
      3 solver = nbkode.BDF1(rhs, t0, y0)
      4 ts = np.linspace(0, 10, 100)
----> 5 ts, ys = solver.run(ts)

~/miniconda3/envs/numerical/lib/python3.9/site-packages/nbkode/core.py in run(self, t)
    347         if is_to_interpolate:
    348             t_old = t[to_interpolate]
--> 349             y_old = np.asarray([self.interpolate(_t) for _t in t_old])
    350             t_to_run = t[np.logical_not(to_interpolate)]
    351         else:

~/miniconda3/envs/numerical/lib/python3.9/site-packages/nbkode/core.py in <listcomp>(.0)
    347         if is_to_interpolate:
    348             t_old = t[to_interpolate]
--> 349             y_old = np.asarray([self.interpolate(_t) for _t in t_old])
    350             t_to_run = t[np.logical_not(to_interpolate)]
    351         else:

~/miniconda3/envs/numerical/lib/python3.9/site-packages/nbkode/core.py in interpolate(self, t)
    394             )
    395
--> 396         return self._interpolate(t, *self._step_args)
    397
    398     @staticmethod

~/miniconda3/envs/numerical/lib/python3.9/site-packages/nbkode/multistep/core.py in _step_args(self)
     82     @property
     83     def _step_args(self):
---> 84         return self.rhs, self.cache, self.h
     85
     86

AttributeError: 'BDF1' object has no attribute 'h'

I could reproduce the above error for AdamsMoulton solvers. The Explicit solvers (RK45, DOP853) works fine. Please help.

maurosilber commented 3 years ago

BDF and AdamsMoutlon are implemented as fixed-step solvers, while Runge-Kutta (RK45, DOP853) are implemented as variable-step solvers. Hence, while this works for variable step: nbkode.RK45(rhs, t0, y0) you need to provide a step parameter (named h) to the fixed-step solvers: nbkode.BDF1(rhs, t0, y0, h=0.1)

We have to explain this in the documentation.

hfsf commented 3 years ago

Thanks for the reply. I guess the inclusion of adaptive step size in BDF (as in scipy version) would be a great addition to the software. Again, many thanks for the attention.