module RKG_2D
  use size_time_resol
  implicit none
contains
  subroutine Runge_Kutta_2D(Gdata, Gdata_dt, WR, WQ, grd_x, grd_z, step)
    real(8), intent(inout) :: Gdata(-1:,0:), WR(:,:),WQ(:,:)
    real(8), intent(in)    :: Gdata_dt(:,:)
    ! Runge Kutta Gill のステップ数(step=1,2,3,4)
    integer, intent(in)    :: grd_x, grd_z, step
    ! Runge Kutta Gill の変数
    integer :: i, j
    
    ! 各ステップの定数
    real(8) :: CX, CQ
    real(8) :: WX(1:grd_x, 1:grd_z)
!    CX = dble(0)
!    CQ = dble(0)
    ! 各ステップの係数を設定
    if (step == 1) then
       CX = 1.0d+0
       CQ = 1.0d+0
    else if (step == 2) then
       CX = 1.0D+0-dsqrt(0.5D+0)
       CQ = 1.0D+0-dsqrt(0.5D+0)
    else if (step == 3) then
       CX = 1.0D+0+dsqrt(0.5D+0)
       CQ = 1.0D+0+dsqrt(0.5D+0)
    else if (step == 4) then
       CX = dble(1/3)
       CQ = 1.0d+0 
    endif
    do j = 1, grd_z
       do i=1, grd_x
          WX(i,j)    = Gdata(i,j)+CX*(Gdata_dt(i,j)-WQ(i,j))
          WR(i,j)    = WX(i,j)-Gdata(i,j)
          WQ(i,j)    = WQ(i,j)+3.0D+0*WR(i,j)-CQ*Gdata_dt(i,j)
          Gdata(i,j) = WX(i,j)
       end do
    end do
  end subroutine Runge_Kutta_2D

end module RKG_2D
