janerikhy/Wave-Model

RK4 integration

Closed this issue · 0 comments

What?

Add Runge-Kutta 45 integration method to the vessel model. This can be used as an alternative to the forward Euler integration method currently implemented.

How?

In MCSimPython.simulator.vessel we find the abstract base class Vessel. This class contains the method integration(). This integration method is the implementation of the forward euler integration. This method should perhaps be renamed to forward_euler when we implement other integration methods such as RK4.

The RK4 integration method should simply be defined something like:

----------
vessel.py
----------

def RK4(fun, t, x):
    """fun is the differential equation to be integrated. 
    t: time instance
    x: state vector
    """
    k1 = fun(t_n, x_n)
    k2 = fun(t_n + dt/2, x_n + dt*k1/2)
    k3 = fun(t_n + dt/2, x_n + dt*k2/2)
    k4 = fun(t_n + dt, x_n + dt * k3)
 
    x_new = x_n + 1/6 * (k1 + 2*k2 + 2*k3 + k4)*dt

Further, by renaming integration() to forward_euler, we can select the integration method to be used by adding method as an argument to the instantiation of the Vessel object.

---------
vessel.py
---------

class Vessel(ABC):

    def __init__(self, *args, method="Euler"):
        int_methdos = {'Euler': self.forward_euler, 'RK4': self.RK4}
        self.integration = int_methods[method]

Note

This is just a suggestion as to how the problem could be formulated - it is not necessarily the best way to do it.