0
$\begingroup$

In Scipy's solve_ivp documentation, we see the method argument can be either a string or a user-defined OdeSolver inherited from scipy.integrate._ivp.base (source code). I tried to create my own OdeSolver class and pass it into solve_ivp as follows:

from scipy.integrate._ivp.base import OdeSolver 

### not actually changing anything here
class MyOdeSolver(OdeSolver):
  def __init__(self, fname, lname):
      super().__init__(fname, lname)

sol = scipy.integrate.solve_ivp(ODEs, time_span, 
      initial_conditions,method=MyOdeSolver)

and got the error

553         method = METHODS[method]
    554 
--> 555     solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
    556 
    557     if t_eval is None:

TypeError: __init__() got an unexpected keyword argument 'vectorized'

I have been searching online about how to use a custom-defined OdeSolver with solve_ivp, but couldn't find any example. If anyone can show me an example, it would be great!

$\endgroup$
3
  • $\begingroup$ This looks like it should be a bug report. But does the base class implement a method or is it just abstract? Does it work when you import the RK45 and use that class instead of the string in method? $\endgroup$ Dec 7, 2023 at 7:59
  • $\begingroup$ On reading the documentation, how did you get the idea to use only fname, lname as parameters, what are they anyway? docs.scipy.org/doc/scipy/reference/generated/… gives detailed instructions on how to implement your own solver, I do not see such parameters involved. You need to code a _step_impl and _dense_output_impl and also _call_impl in the dense output class, where the calculation happens. These are stubs with raise NotImplementedError in the base classes. $\endgroup$ Dec 8, 2023 at 9:31
  • $\begingroup$ Ah. Thanks for pointing this out! $\endgroup$
    – Axel Wang
    Dec 8, 2023 at 22:02

1 Answer 1

2
$\begingroup$

This error is entirely normal. You should be giving a OdeSolver subclass (e.g. scipy.integrate.RK45) which implements the correct interface. In particular, the OdeSolver class is not meant to be instantiated directly. It is for example missing a few arguments in the init function, which is why you get this error. The documentation of solve_ivp will tell you how to implement your own solver.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.