!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2006. All rights reserved.
!---------------------------------------------------------------------
!
!= 3 次元 (xyz 方向) 不等間隔交互格子 境界条件モジュール
!
!* 履歴
!  * 2007/07/15 (小高正嗣) : 3D deepconv へ移植, dc_types を Use.
!  * 2006/06/07 (小高正嗣) : 新規作成

module xyz_bc_module
  != 3 次元 (xyz 方向) 不等間隔交互格子 有限差分モデル用 境界条件モジュール
  !
  !== 概要
  !
  ! xyz_bc_module は, 3 次元 (xyz 方向) 不等間隔交互格子を用いた有限差分法に
  ! 基づく数値モデルのための, 境界条件設定 Fortran 90 副プログラムを提供
  ! する.
  !
  ! このモジュールは xyz_module の下位モジュールである. 下請けモジュール
  ! として data_type, xyz_base_module, x_bc_module, y_bc_module, 
  ! z_bc_module モジュールを用いている. 
  !
  !
  !== 手続きの命名法
  !
  ! 境界条件を設定する副プログラムは
  !
  !   Boundary[境界条件を示す文字列][...]_(入力配列の次元情報)
  !
  ! のように命名されている. 境界条件を示す文字列の種類は x_bc_module,
  ! y_bc_module, z_bc_module の場合と同じである.
  ! 
  !
  use dc_types, only : DP
  use gridset,  only : imin, imax, jmin, jmax, kmin, kmax,    &
    &                  xmg, ymg, zmg, nx, ny, nz 
#ifdef LIB_MPI
  use mpi_wrapper, only : myrank, nprocs, &
    &                     MPIWrapperISend, MPIWrapperIRecv, MPIWrapperWait
#else
!  use mpi_wrapper, only : myrank, nprocs
#endif

  implicit none

  private

  public :: BoundaryXSym_xyz, BoundaryXAsym_xyz, BoundaryXCyc_xyz 
  public :: BoundaryXSym_xqz, BoundaryXAsym_xqz, BoundaryXCyc_xqz 
  public :: BoundaryXSym_xyr, BoundaryXAsym_xyr, BoundaryXCyc_xyr 
  public :: BoundaryXSym_pyz, BoundaryXAsym_pyz, BoundaryXCyc_pyz 

  public :: BoundaryYSym_xyz, BoundaryYAsym_xyz, BoundaryYCyc_xyz 
  public :: BoundaryYSym_xqz, BoundaryYAsym_xqz, BoundaryYCyc_xqz 
  public :: BoundaryYSym_xyr, BoundaryYAsym_xyr, BoundaryYCyc_xyr 
  public :: BoundaryYSym_pyz, BoundaryYAsym_pyz, BoundaryYCyc_pyz 

  public :: BoundaryZSym_xyz, BoundaryZAsym_xyz, BoundaryZCyc_xyz 
  public :: BoundaryZSym_xqz, BoundaryZAsym_xqz, BoundaryZCyc_xqz 
  public :: BoundaryZSym_xyr, BoundaryZAsym_xyr, BoundaryZCyc_xyr 
  public :: BoundaryZSym_pyz, BoundaryZAsym_pyz, BoundaryZCyc_pyz 

  interface BoundaryXSym_xyz
    module procedure BoundaryXSym_xaa
  end interface

  interface BoundaryXSym_xqz
    module procedure BoundaryXSym_xaa
  end interface
  
  interface BoundaryXSym_xyr
    module procedure BoundaryXSym_xaa
  end interface

  interface BoundaryXAsym_xyz
    module procedure BoundaryXAsym_xaa
  end interface

  interface BoundaryXAsym_xqz
    module procedure BoundaryXAsym_xaa
  end interface

  interface BoundaryXAsym_xyr
    module procedure BoundaryXAsym_xaa
  end interface

#ifdef LIB_MPI
  interface BoundaryXCyc_xyz
    module procedure MPIBoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_xqz
    module procedure MPIBoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_xyr
    module procedure MPIBoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_pyz
    module procedure MPIBoundaryXCyc_aaa
  end interface
#else
  interface BoundaryXCyc_xyz
    module procedure BoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_xqz
    module procedure BoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_xyr
    module procedure BoundaryXCyc_aaa
  end interface

  interface BoundaryXCyc_pyz
    module procedure BoundaryXCyc_aaa
  end interface
#endif

  interface BoundaryYSym_xyz
    module procedure BoundaryYSym_aya
  end interface

  interface BoundaryYSym_pyz
    module procedure BoundaryYSym_aya
  end interface

  interface BoundaryYSym_xyr
    module procedure BoundaryYSym_aya
  end interface

  interface BoundaryYAsym_xyz
    module procedure BoundaryYAsym_aya
  end interface

  interface BoundaryYAsym_pyz
    module procedure BoundaryYAsym_aya
  end interface

  interface BoundaryYAsym_xyr
    module procedure BoundaryYAsym_aya
  end interface

  interface BoundaryYCyc_xyz
    module procedure BoundaryYCyc_aaa
  end interface

  interface BoundaryYCyc_pyz
    module procedure BoundaryYCyc_aaa
  end interface

  interface BoundaryYCyc_xyr
    module procedure BoundaryYCyc_aaa
  end interface

  interface BoundaryYCyc_xqz
    module procedure BoundaryYCyc_aaa
  end interface

  interface BoundaryZSym_xyz
    module procedure BoundaryZSym_aaz
  end interface

  interface BoundaryZSym_pyz
    module procedure BoundaryZSym_aaz
  end interface

  interface BoundaryZSym_xqz
    module procedure BoundaryZSym_aaz
  end interface

  interface BoundaryZAsym_xyz
    module procedure BoundaryZAsym_aaz
  end interface

  interface BoundaryZAsym_pyz
    module procedure BoundaryZAsym_aaz
  end interface

  interface BoundaryZAsym_xqz
    module procedure BoundaryZAsym_aaz
  end interface

  interface BoundaryZCyc_xyz
    module procedure BoundaryZCyc_aaa
  end interface

  interface BoundaryZCyc_pyz
    module procedure BoundaryZCyc_aaa
  end interface

  interface BoundaryZCyc_xqz
    module procedure BoundaryZCyc_aaa
  end interface

  interface BoundaryZCyc_xyr
    module procedure BoundaryZCyc_aaa
  end interface

  integer :: ix, jy, kz
  
contains
  
!--------------------------------------------------------------------
  subroutine BoundaryXSym_xaa(xaa_Var)
    real(DP),intent(inout) :: xaa_Var(imin:imax,jmin:jmax,kmin:kmax) 

    ! x 方向に周期境界条件を適用する
    
    do ix = 1, xmg
      xaa_Var(1-ix,:,:)  = xaa_Var(ix,:,:)
      xaa_Var(nx+ix,:,:) = xaa_Var(nx+1-ix,:,:)
    end do
    
  end subroutine BoundaryXSym_xaa
!--------------------------------------------------------------------
  subroutine BoundaryXSym_pyz(pyz_Var)
    real(DP),intent(inout) :: pyz_Var(imin:imax,jmin:jmax,kmin:kmax) 
    
    ! x 方向に対称境界条件を適用する

    do ix = 1, xmg-1
      pyz_Var(-ix,:,:)   = pyz_Var(ix,:,:)      
    end do
    
    do ix = 1, xmg
      pyz_Var(nx+ix,:,:) = pyz_Var(nx+1-ix,:,:)
    end do
    
    
  end subroutine BoundaryXSym_pyz
!--------------------------------------------------------------------
  subroutine BoundaryXAsym_xaa(xaa_Var)
    real(DP),intent(inout) :: xaa_Var(imin:imax,jmin:jmax,kmin:kmax) 

    ! x 方向に反対称境界条件を適用する

    do ix = 1, xmg
      xaa_Var(1-ix,:,:)  = - xaa_Var(ix,:,:)
      xaa_Var(nx+ix,:,:) = - xaa_Var(nx+1-ix,:,:)
    end do
        
  end subroutine BoundaryXAsym_xaa
!--------------------------------------------------------------------
  subroutine BoundaryXAsym_pyz(pyz_Var)
    real(DP),intent(inout) :: pyz_Var(imin:imax,jmin:jmax,kmin:kmax) 
    
    ! x 方向に反対称境界条件を適用する
    pyz_Var(0,:,:) = 0.0d0
    pyz_Var(nx,:,:) = 0.0d0
          
    do ix = 1, xmg-1
      pyz_Var(-ix,:,:) = - pyz_Var(ix,:,:)
    end do
    
    do ix = 1, xmg
      pyz_Var(nx+ix,:,:) = - pyz_Var(nx-ix,:,:)
    end do
    
  end subroutine BoundaryXAsym_pyz
!--------------------------------------------------------------------
   subroutine BoundaryXCyc_aaa(aaa_Var)
      real(DP),intent(inout) :: aaa_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! x 方向に周期境界条件を適用する

      do ix = 1, xmg
        aaa_Var(1-ix,:,:)  = aaa_Var(nx+1-ix,:,:)
        aaa_Var(nx+ix,:,:) = aaa_Var(ix,:,:)
      end do
 
   end subroutine BoundaryXCyc_aaa


!--------------------------------------------------------------------
    subroutine BoundaryYSym_aya(aya_Var)
      real(DP),intent(inout) :: aya_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! y 方向に対称境界条件を適用する

      if (ymg == 0) return 

      do jy = 1, ymg
        aya_Var(:,1-jy,:)  = aya_Var(:,jy,:)
        aya_Var(:,ny+jy,:) = aya_Var(:,ny+1-jy,:)
      end do

    end subroutine BoundaryYsym_aya
!--------------------------------------------------------------------
    subroutine BoundaryYSym_xqz(xqz_Var)
      real(DP),intent(inout) :: xqz_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! y 方向に対称境界条件を適用する

      if (ymg == 0) return 

      do jy = 1, ymg-1
        xqz_Var(:,-jy,:)   = xqz_Var(:,jy,:)      
      end do

      do jy = 1, ymg
        xqz_Var(:,ny+jy,:) = xqz_Var(:,ny+1-jy,:)
      end do

    end subroutine BoundaryYsym_xqz
!--------------------------------------------------------------------
    subroutine BoundaryYASym_aya(aya_Var)
      real(DP),intent(inout) :: aya_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! y 方向に反対称境界条件を適用する

      if (ymg == 0) return 

      do jy = 1, ymg
        aya_Var(:,1-jy,:)  = - aya_Var(:,jy,:)
        aya_Var(:,ny+jy,:) = - aya_Var(:,ny+1-jy,:)
      end do

    end subroutine BoundaryYAsym_aya
!--------------------------------------------------------------------
    subroutine BoundaryYASym_xqz(xqz_Var)
      real(DP),intent(inout) :: xqz_Var(imin:imax,jmin:jmax,kmin:kmax) 

      ! y 方向に反対称境界条件を適用する

      if (ymg == 0) return 

      xqz_Var(:,0,:) = 0.0d0
      xqz_Var(:,ny,:) = 0.0d0
          
      do jy = 1, ymg-1
        xqz_Var(:,-jy,:) = - xqz_Var(:,jy,:)
      end do

      do jy = 1, ymg
        xqz_Var(:,ny+jy,:) = - xqz_Var(:,ny-jy,:)
      end do

    end subroutine BoundaryYAsym_xqz
!--------------------------------------------------------------------
    subroutine BoundaryYCyc_aaa(aaa_Var)
      real(DP),intent(inout) :: aaa_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! y 方向に周期境界条件を適用する

      if (ymg == 0) return 

      do jy = 1, ymg
        aaa_Var(:,1-jy,:)  = aaa_Var(:,ny+1-jy,:)
        aaa_Var(:,ny+jy,:) = aaa_Var(:,jy,:)
      end do

    end subroutine BoundaryYCyc_aaa
!--------------------------------------------------------------------
    subroutine BoundaryZSym_aaz(aaz_Var)
      real(DP),intent(inout) :: aaz_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! z 方向に対称境界条件を適用する

      do kz = 1, zmg
        aaz_Var(:,:,1-kz)  = aaz_Var(:,:,kz)
        aaz_Var(:,:,nz+kz) = aaz_Var(:,:,nz+1-kz)
      end do


    end subroutine BoundaryZSym_aaz
!--------------------------------------------------------------------
    subroutine BoundaryZSym_xyr(xyr_Var)
      real(DP),intent(inout) :: xyr_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! z 方向に対称境界条件を適用する

      do kz = 1, zmg-1
        xyr_Var(:,:,-kz)   = xyr_Var(:,:,kz)      
      end do

      do kz = 1, zmg
        xyr_Var(:,:,nz+kz) = xyr_Var(:,:,nz+1-kz)
      end do

    end subroutine BoundaryZSym_xyr
!--------------------------------------------------------------------
    subroutine BoundaryZAsym_aaz(aaz_Var)
      real(DP),intent(inout) :: aaz_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! z 方向に反対称境界条件を適用する

      do kz = 1, zmg
        aaz_Var(:,:,1-kz)  = - aaz_Var(:,:,kz)
        aaz_Var(:,:,nz+kz) = - aaz_Var(:,:,nz+1-kz)
      end do

    end subroutine BoundaryZAsym_aaz
!--------------------------------------------------------------------
    subroutine BoundaryZAsym_xyr(xyr_Var)
      real(DP),intent(inout) :: xyr_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! z 方向に反対称境界条件を適用する

      xyr_Var(:,:,0) = 0.0d0
      xyr_Var(:,:,nz) = 0.0d0
          
      do kz = 1, zmg-1
        xyr_Var(:,:,-kz) = - xyr_Var(:,:,kz)
      end do

      do kz = 1, zmg
        xyr_Var(:,:,nz+kz) = - xyr_Var(:,:,nz-kz)
      end do

    end subroutine BoundaryZAsym_xyr
!--------------------------------------------------------------------
    subroutine BoundaryZCyc_aaa(aaa_Var)
      real(DP),intent(inout) :: aaa_Var(imin:imax,jmin:jmax,kmin:kmax) 
      
      ! z 方向に周期境界条件を適用する

      do kz = 1, zmg
        aaa_Var(:,:,1-kz)  = aaa_Var(:,:,nz+1-kz)
        aaa_Var(:,:,nz+kz) = aaa_Var(:,:,kz)
      end do

    end subroutine BoundaryZCyc_aaa
!--------------------------------------------------------------------

#ifdef LIB_MPI
  subroutine MPIBoundaryXCyc_aaa(aaa_Var)
    
    implicit none
    
    !変数定義
    real(8), intent(inout)  :: aaa_Var(imin:imax, jmin:jmax, kmin:kmax)
    integer            :: idest_a, idep_a, idest_b, idep_b
    integer, parameter :: nvars = 1
    real(8)            :: sbuf_a( Xmg, ny, nz )
    real(8)            :: rbuf_a( Xmg, ny, nz )
    real(8)            :: sbuf_b( Xmg, ny, nz )
    real(8)            :: rbuf_b( Xmg, ny, nz )
    integer            :: ireqs_a, ireqr_a, ireqs_b, ireqr_b
    integer            :: i, j, k
    
    !-------------------------------
    ! 配列の右側を, ノード間で通信する.
    ! 送信する部分配列(sbuf_a)を用意する
    do k = 1, nz
      do j = 1, ny
        do i = 1, Xmg
          sbuf_a( i, j, k ) = aaa_var( nx + 1 - i , j, k ) 
        end do
      end do
    end do

    idest_a = mod(( myrank+1 )       , nprocs)   !送信先
    idep_a  = mod(( myrank-1 )+nprocs, nprocs)   !受信元

    call MPIWrapperISend( idest_a, xmg, ny, nz, sbuf_a, ireqs_a ) !送信
    call MPIWrapperIRecv( idep_a , xmg, ny, nz, rbuf_a, ireqr_a ) !受信
    
    !-------------------------------
    ! 配列の左側を, ノード間で通信する.
    ! 送信する部分配列(sbuf_b)を用意する
    do k = 1, nz
      do j = 1, ny
        do i = 1, Xmg
          sbuf_b( i, j, k ) = aaa_var( i, j, k ) 
        end do
      end do
    end do
    
    idest_b = mod(( myrank-1 )+nprocs, nprocs)    !送信先
    idep_b  = mod(( myrank+1 )       , nprocs)    !受信元

    call MPIWrapperISend( idest_b, xmg, ny, nz, sbuf_b, ireqs_b ) !送信
    call MPIWrapperIRecv( idep_b , xmg, ny, nz, rbuf_b, ireqr_b ) !受信

    !-------------------------------
    ! 配列の右側を, ノード間で通信する.
    ! 受信した部分配列(rbuf_a)を代入する.
    call MPIWrapperWait( ireqs_a )
    call MPIWrapperWait( ireqr_a )
    
    do k = 1, nz
      do j = 1, ny
        do i = 1, Xmg
          aaa_var( 1-i , j, k ) = rbuf_a( i, j, k ) 
        end do
      end do
    end do
    
    !-------------------------------
    ! 配列の左側を, ノード間で通信する.
    ! 受信した部分配列(rbuf_b)を代入する.
    call MPIWrapperWait( ireqs_b )
    call MPIWrapperWait( ireqr_b )
    
    do k = 1, nz
      do j = 1, ny
        do i = 1, Xmg
          aaa_var(nx + i , j, k ) = rbuf_b( i, j, k ) 
        end do
      end do
    end do
    
  end subroutine MPIBoundaryXCyc_aaa
#endif

end module xyz_bc_module
