Search code examples
pythonnumerical-methodsdifferential-equations

Using `solve_ivp` and `LSODA` to solve a complex ODE


I am trying to solve a complex system of differential equations. The equations are stiff so I need to need to use a method which can handle both complex ODEs and stiffness switching. I have landed on using odeintw (link) which is able to solve my problem, however I also need to have event handling.

I have tried to solve the problem using solve_ivp and BDF, but the evaluation takes an unreasonable amount of time.

Is there an existing framework, like odeintw which lets me use solve_ivp with the LSODA method to solve complex differential equations? Or is there some way to implement event handling with odeintw?


Solution

  • If you have to rely on a real-only solver, then you have to provide the wrapper yourself. Define functions R2C and C2R that are inverse to each other and enhance or wrap your ODE and event functions.

    def R2C(xy): z = xy[:n]+1j*xy[n:]; return z
    
    def C2R(z): xy = np.concatenate([z.real, z.imag]); return xy
    
    def odefunc(t,xy):
        z = R2C(xy)
        ...
        return C2R(z_dot)
    

    etc.