rk4 Subroutine

private subroutine rk4(me, t, x, h, xf)

Take one Runge Kutta 4 integration step: t -> t+h (x -> xf)

Type Bound

rk4_class

Arguments

Type IntentOptional Attributes Name
class(rk4_class), intent(inout) :: me
real(kind=wp), intent(in) :: t

initial time

real(kind=wp), intent(in), dimension(me%n) :: x

initial state

real(kind=wp), intent(in) :: h

time step

real(kind=wp), intent(out), dimension(me%n) :: xf

state at time t+h


Source Code

    subroutine rk4(me,t,x,h,xf)

    implicit none

    class(rk4_class),intent(inout)       :: me
    real(wp),intent(in)                  :: t   !! initial time
    real(wp),dimension(me%n),intent(in)  :: x   !! initial state
    real(wp),intent(in)                  :: h   !! time step
    real(wp),dimension(me%n),intent(out) :: xf  !! state at time `t+h`

    !local variables:
    real(wp),dimension(me%n) :: f1,f2,f3,f4
    real(wp) :: h2

    !parameters:
    real(wp),parameter :: half = 0.5_wp
    real(wp),parameter :: six  = 6.0_wp

    h2 = half*h

    call me%f(t,x,f1)
    call me%f(t+h2,x+h2*f1,f2)
    call me%f(t+h2,x+h2*f2,f3)
    call me%f(t+h,x+h*f3,f4)

    xf = x + h*(f1+f2+f2+f3+f3+f4)/six

    end subroutine rk4