!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (境界値問題,Grid)
!
!履歴  2016/09/20  竹広真一
!
program tee_mpi_module_boundariesgrid_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use tee_mpi_module
  use mpi
  implicit none

 !---- 空間解像度設定 ----
  integer, parameter :: im=32, jm=32, km=16       ! 格子点の設定(X,Y)
  integer, parameter :: lm=10, mm=10, nm=16       ! 切断波数の設定(X,Y)

 !---- 変数 ----
  real(8), allocatable :: zvx_Data(:,:,:)         ! 格子データ
  real(8), allocatable :: zvx_Data_orig(:,:,:)    ! 格子データ

  real(8), allocatable :: vx_BoundaryTop(:,:)     ! 格子データ
  real(8), allocatable :: vx_BoundaryBtm(:,:)     ! 格子データ

  real(8), allocatable :: tef_Data(:,:,:)      ! スペクトルデータ

  real(8), allocatable :: aef_Boundaries(:,:,:)   ! スペクトルデータ

  integer            :: l=2, m=5
  integer            :: np, ip, ierr

 !---- 座標変数など ----
  real(8), parameter :: xmin = 0.0d0, xmax=2.0d0
  real(8), parameter :: ymin = 0.0d0, ymax=2.0d0
  real(8), parameter :: zmin = -1.0d0, zmax=1.0d0

  real(8), parameter :: pi=3.1415926535897932385D0

  ! 判定誤差設定
  integer, parameter :: check_digits = 11
  integer, parameter :: ignore = -12

  call MessageNotify('M','tee_mpi_module_BoundariesGrid_test', &
       'tee_mpi_module boundary condition subruoutine tests')

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IP,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)
   
 !---------------- 座標値の設定 ---------------------
  ! スペクトル初期化
  call tee_mpi_initial(im,jm,km,lm,mm,nm,xmin,xmax,ymin,ymax,zmin,zmax) 

  allocate(zvx_Data(0:km,jc(ip),0:im-1))        ! 格子データ
  allocate(zvx_Data_orig(0:km,jc(ip),0:im-1))   ! 格子データ  

  allocate(vx_BoundaryTop(jc(ip),0:im-1))       ! 格子データ
  allocate(vx_BoundaryBtm(jc(ip),0:im-1))       ! 格子データ

  allocate(tef_Data(0:nm,-mm:mm,2*lc(ip)))      ! スペクトルデータ

  allocate(aef_Boundaries(2,-mm:mm,2*lc(ip)))   ! スペクトルデータ

 !-------------------BC-DD(Grid) ----------------------
  zvx_Data = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data)
  zvx_Data = zvx_tef(tef_Data)

  call AssertEqual(&
    message='tef_BoundariesGrid [DD,internal]',                   &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [DD,Top]',                        &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [DD,Bottom]',                     &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = sin(m*pi*vx_X) * cos(l*pi*vx_Y)
  vx_BoundaryBtm = cos(m*pi*vx_X) * sin(l*pi*vx_Y)
  aef_Boundaries(1,:,:)=ef_vx(vx_Boundarytop)
  aef_Boundaries(2,:,:)=ef_vx(vx_BoundaryBtm)

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,values=aef_Boundaries)
  zvx_Data = zvx_tef(tef_Data)

  call AssertEqual(&
    message='tef_BoundariesGrid [DD,vaules,internal]',            &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [DD,values,Top]',                 &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [DD,values,Bottom]',              &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-NN(Grid) ----------------------
  zvx_Data = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='NN')

  call AssertEqual(&
    message='tef_BoundariesGrid [NN,internal]',                   &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [NN,Top]',                        &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [NN,Bottom]',                     &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = sin(m*pi*vx_X) * cos(l*pi*vx_Y)
  vx_BoundaryBtm = cos(m*pi*vx_X) * sin(l*pi*vx_Y)
  aef_Boundaries(1,:,:)=ef_vx(vx_Boundarytop)
  aef_Boundaries(2,:,:)=ef_vx(vx_BoundaryBtm)

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='NN',values=aef_Boundaries)

  call AssertEqual(&
    message='tef_BoundariesGrid [NN,vaules,internal]',            &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [NN,values,Top]',                 &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesGrid [NN,values,Bottom]',              &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-DN(Grid) ----------------------
  zvx_Data = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='DN')

  call AssertEqual(&
    message='tef_BoundariesGrid [DN,internal]',                   &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesGrid [DN,Top]',                        &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [DN,Bottom]',                     &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = sin(m*pi*vx_X) * cos(l*pi*vx_Y)
  vx_BoundaryBtm = cos(m*pi*vx_X) * sin(l*pi*vx_Y)
  aef_Boundaries(1,:,:)=ef_vx(vx_Boundarytop)
  aef_Boundaries(2,:,:)=ef_vx(vx_BoundaryBtm)

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='DN',values=aef_Boundaries)

  call AssertEqual(&
    message='tef_BoundariesGrid [DN,vaules,internal]',            &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesGrid [DN,values,Top]',                 &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [DN,values,Bottom]',              &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-ND(Grid) ----------------------
  zvx_Data = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='ND')

  call AssertEqual(&
    message='tef_BoundariesGrid [ND,internal]',                   &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [ND,Top]',                        &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesGrid [ND,Bottom]',                     &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = sin(m*pi*vx_X) * cos(l*pi*vx_Y)
  vx_BoundaryBtm = cos(m*pi*vx_X) * sin(l*pi*vx_Y)
  aef_Boundaries(1,:,:)=ef_vx(vx_Boundarytop)
  aef_Boundaries(2,:,:)=ef_vx(vx_BoundaryBtm)

  zvx_Data_orig = zvx_Data
  tef_Data = tef_zvx(zvx_Data)

  call tef_BoundariesGrid(tef_Data,cond='ND',values=aef_Boundaries)

  call AssertEqual(&
    message='tef_BoundariesGrid [ND,vaules,internal]',            &
    answer = zvx_Data_orig(1:km-1,:,:),                           &
    check = zvx_Data(1:km-1,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesGrid [ND,values,Top]',                 &
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesGrid [ND,values,Bottom]',              &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','tee_mpi_module_BoundariesGrid_test', &
       'tee_mpi_module boundary condition subroutine tests succeeded!')

end program tee_mpi_module_boundariesgrid_test
