!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_module テストプログラム (境界値問題,Tau)
!
!履歴  2016/09/20  竹広真一
!
program tee_mpi_module_boundariestau_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=10       ! 切断波数の設定(X,Y)

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

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

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

  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_BoundariesTau_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(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(tef_Data_orig(0:nm,-mm:mm,2*lc(ip))) ! スペクトルデータ

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

 !-------------------BC-DD(Tau) ----------------------
  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

  call tef_BoundariesTau(tef_Data)
  zvx_Data = zvx_tef(tef_Data)

  call AssertEqual(&
    message='tef_BoundariesTau [DD,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [DD,Top]',&
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [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)

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

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

  call AssertEqual(&
    message='tef_BoundariesTau [DD,vaules,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [DD,values,Top]',&
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [DD,values,Bottom]', &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-NN(Tau) ----------------------
  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

  call tef_BoundariesTau(tef_Data,cond='NN')
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))

  call AssertEqual(&
    message='tef_BoundariesTau [NN,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [NN,Top]',&
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [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)

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

  call tef_BoundariesTau(tef_Data,values=aef_Boundaries,cond='NN')
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))

  call AssertEqual(&
    message='tef_BoundariesTau [NN,values,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [NN,values,Top]',&
    answer = zvx_Data(0,:,:),                                     &
    check = vx_BoundaryTop,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_BoundariesTau [NN,values,Bottom]', &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-DN(Tau) ----------------------
  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

  call tef_BoundariesTau(tef_Data,cond='DN')

  call AssertEqual(&
    message='tef_BoundariesTau [DN,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesTau [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_BoundariesTau [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)

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

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

  call AssertEqual(&
    message='tef_BoundariesTau [DN,values,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Data)
  call AssertEqual(&
    message='tef_BoundariesTau [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_BoundariesTau [DN,values,Bottom]', &
    answer = zvx_Data(km,:,:),                                    &
    check = vx_BoundaryBtm,                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-ND(Tau) ----------------------
  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  vx_BoundaryTop = 0.0D0
  vx_BoundaryBtm = 0.0D0

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

  call tef_BoundariesTau(tef_Data,cond='ND')

  call AssertEqual(&
    message='tef_BoundariesTau [ND,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesTau [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_BoundariesTau [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)

  tef_Data = tef_zvx(zvx_Data)
  tef_Data_orig = tef_Data

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

  call AssertEqual(&
    message='tef_BoundariesTau [ND,values,internal]',&
    answer = tef_Data_orig(0:nm-2,:,:),                           &
    check = tef_Data(0:nm-2,:,:),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zvx_Data = zvx_tef(tef_Dz_tef(tef_Data))
  call AssertEqual(&
    message='tef_BoundariesTau [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_BoundariesTau [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_boundariesTau_test', &
       'tee_mpi_module boundary condition subroutine tests succeeded!')

end program tee_mpi_module_boundariestau_test

