!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_module テストプログラム (境界値問題, トロイダル速度ポテンシャル)
!
!履歴  2016/09/21  竹広真一
!
program tee_mpi_module_torvelbcTau_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_Torvel(:,:,:)      ! 格子データ
  real(8), allocatable :: zvx_DTorvel(:,:,:)     ! 格子データ
  real(8), allocatable :: tef_Torvel(:,:,:)      ! スペクトルデータ
  real(8), allocatable :: tef_Torvel_orig(:,:,:) ! スペクトルデータ

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

  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_TorvelbcTau_test', &
       'tee_mpi_module toroidal velocity potential B.C. function 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_Torvel(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_DTorvel(0:km,jc(ip),0:im-1))     ! 格子データ
  allocate(tef_Torvel(0:nm,-mm:mm,2*lc(ip)))      ! スペクトルデータ
  allocate(tef_Torvel_orig(0:nm,-mm:mm,2*lc(ip)))      ! スペクトルデータ  

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

 !-------------------BC-FF(Tau) ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Torvel_orig = tef_Torvel
 
  call tef_TorBoundariesTau(tef_Torvel,cond='FF',new=.true.)
  zvx_Torvel = zvx_tef(tef_Torvel)

  call AssertEqual(&
    message='tef_TorBoundariesTau [FF,internal]',                &
    answer = tef_Torvel_orig(0:nm-2,:,:),                         &
    check = tef_Torvel(0:nm-2,:,:),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Dz_tef(tef_Torvel))
  call AssertEqual(&
    message='tef_TorBoundariesTau [FF,Top]',                     &
    answer = zvx_DTorvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_TorBoundariesTau [FF,Bottom]',                  &
    answer = zvx_DTorvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-RR(Tau) ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Torvel_orig = tef_Torvel

  call tef_TorBoundariesTau(tef_Torvel,cond='RR',new=.true.)
  zvx_Torvel = zvx_tef(tef_Torvel)

  call AssertEqual(&
    message='tef_TorBoundariesTau [RR,internal]',                &
    answer = tef_Torvel_orig(0:nm-2,:,:),                         &
    check = tef_Torvel(0:nm-2,:,:),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Torvel)
  call AssertEqual(&
    message='tef_TorBoundariesTau [RR,Top]',                     &
    answer = zvx_DTorvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='tef_TorBoundariesTau [RR,Bottom]',                  &
    answer = zvx_DTorvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-RF(Tau) ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Torvel_orig = tef_Torvel

  call tef_TorBoundariesTau(tef_Torvel,cond='RF',new=.true.)
  zvx_Torvel = zvx_tef(tef_Torvel)

  call AssertEqual(&
    message='tef_TorBoundariesTau [RF,internal]',                &
    answer = tef_Torvel_orig(0:nm-2,:,:),                         &
    check = tef_Torvel(0:nm-2,:,:),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Torvel)
  call AssertEqual(&
    message='tef_TorBoundariesTau [RF,Top]',                     &
    answer = zvx_DTorvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Dz_tef(tef_Torvel))
  call AssertEqual(&
    message='tef_TorBoundariesTau [RF,Bottom]',                  &
    answer = zvx_DTorvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-FR(Tau) ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Torvel_orig = tef_Torvel

  call tef_TorBoundariesTau(tef_Torvel,cond='FR',new=.true.)
  zvx_Torvel = zvx_tef(tef_Torvel)

  call AssertEqual(&
    message='tef_TorBoundariesTau [FR,internal]',                &
    answer = tef_Torvel_orig(0:nm-2,:,:),                         &
    check = tef_Torvel(0:nm-2,:,:),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Dz_tef(tef_Torvel))
  call AssertEqual(&
    message='tef_TorBoundariesTau [FR,Top]',                     &
    answer = zvx_DTorvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DTorvel = zvx_tef(tef_Torvel)
  call AssertEqual(&
    message='tef_TorBoundariesTau [RF,Bottom]',                  &
    answer = zvx_DTorvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','tee_mpi_module_TorvelbcTau_test', &
       'tee_mpi_module toroidal velocity potential B.C. function tests succeeded!')

end program tee_mpi_module_torvelbcTau_test

