!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (境界値問題, ▽^2φ から φを求める)
!
!履歴  2016/09/21  竹広真一
!
program tee_mpi_module_polvelbcGrid_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_Polvel(:,:,:)           ! 格子データ
  real(8), allocatable :: zvx_Polvel_orig(:,:,:)      ! 格子データ
  real(8), allocatable :: zvx_LaplaPolvel(:,:,:)      ! 格子データ
  real(8), allocatable :: zvx_LaplaPolvel_orig(:,:,:) ! 格子データ
  real(8), allocatable :: zvx_DPolvel(:,:,:)          ! 格子データ
  real(8), allocatable :: tef_Polvel(:,:,:)           ! スペクトルデータ

  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_PolvelbcGrid_test', &
       'tee_mpi_module poloidal 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_Polvel(0:km,jc(ip),0:im-1))           ! 格子データ
  allocate(zvx_Polvel_orig(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_LaplaPolvel(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_LaplaPolvel_orig(0:km,jc(ip),0:im-1)) ! 格子データ
  allocate(zvx_DPolvel(0:km,jc(ip),0:im-1))          ! 格子データ
  allocate(tef_Polvel(0:nm,-mm:mm,2*lc(ip)))         ! スペクトルデータ

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

 !-------------------BC-FF(Grid) ----------------------
  zvx_LaplaPolvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_LaplaPolvel_orig = zvx_LaplaPolvel

  tef_Polvel = tef_zef(zef_LaplaPol2Pol_zef(zef_zvx(zvx_LaplaPolvel),&
       cond='FF',new=.true.))

  zvx_Polvel = zvx_tef(tef_Polvel)
  zvx_LaplaPolvel = zvx_tef(tef_Lapla_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FF,internal]',                 &
    answer = zvx_LaplaPolvel_orig(2:km-2,:,:),                    &
    check = zvx_LaplaPolvel(2:km-2,:,:),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FF,Top]',                      &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FF,Bottom]',                   &
    answer = zvx_Polvel(km,:,:),                                  &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Dz_tef(tef_Polvel)))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FF,Top,deriv]',                &
    answer = zvx_DPolvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FF,Bottom,deriv]',             &
    answer = zvx_DPolvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-RR(Grid) ----------------------
  zvx_LaplaPolvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_LaplaPolvel_orig = zvx_LaplaPolvel

  tef_Polvel = tef_zef(zef_LaplaPol2Pol_zef(zef_zvx(zvx_LaplaPolvel),&
       cond='RR',new=.true.))

  zvx_Polvel = zvx_tef(tef_Polvel)
  zvx_LaplaPolvel = zvx_tef(tef_Lapla_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RR,internal]',                 &
    answer = zvx_LaplaPolvel_orig(2:km-2,:,:),                    &
    check = zvx_LaplaPolvel(2:km-2,:,:),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RR,Top]',                      &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RR,Bottom]',                   &
    answer = zvx_Polvel(km,:,:),                                  &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RR,Top,deriv]',                &
    answer = zvx_DPolvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

 !-------------------BC-RF(Grid) ----------------------
  zvx_LaplaPolvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_LaplaPolvel_orig = zvx_LaplaPolvel

  tef_Polvel = tef_zef(zef_LaplaPol2Pol_zef(zef_zvx(zvx_LaplaPolvel),&
       cond='RF',new=.true.))

  zvx_Polvel = zvx_tef(tef_Polvel)
  zvx_LaplaPolvel = zvx_tef(tef_Lapla_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,internal]',                 &
    answer = zvx_LaplaPolvel_orig(2:km-2,:,:),                    &
    check = zvx_LaplaPolvel(2:km-2,:,:),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,Top]',                      &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,Bottom]',                   &
    answer = zvx_Polvel(km,:,:),                                  &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,Top,deriv]',                &
    answer = zvx_DPolvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Dz_tef(tef_Polvel)))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,Bottom,deriv]',             &
    answer = zvx_DPolvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-FR(Grid) ----------------------
  zvx_LaplaPolvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_LaplaPolvel_orig = zvx_LaplaPolvel

  tef_Polvel = tef_zef(zef_LaplaPol2Pol_zef(zef_zvx(zvx_LaplaPolvel),&
       cond='FR',new=.true.))

  zvx_Polvel = zvx_tef(tef_Polvel)
  zvx_LaplaPolvel = zvx_tef(tef_Lapla_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FR,internal]',                 &
    answer = zvx_LaplaPolvel_orig(2:km-2,:,:),                    &
    check = zvx_LaplaPolvel(2:km-2,:,:),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FR,Top]',                      &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FR,Bottom]',                   &
    answer = zvx_Polvel(km,:,:),                                  &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Dz_tef(tef_Polvel)))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [FR,Top,deriv]',                &
    answer = zvx_DPolvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_DPolvel = zvx_tef(tef_Dz_tef(tef_Polvel))

  call AssertEqual(&
    message='zef_LaplaPol2Pol_zef [RF,Bottom,deriv]',             &
    answer = zvx_DPolvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)

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

end program tee_mpi_module_polvelbcGrid_test
