!----------------------------------------------------------------------
!     Copyright (c) 2009--2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (境界値問題, ▽^2φ から φを求める)
!
!履歴  2016/09/20  竹広真一
!
program tee_mpi_module_polvelbcTau_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 = 10
  integer, parameter :: ignore = -11

  call MessageNotify('M','tee_mpi_module_PolvelbcTau_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(Tau) ----------------------
  zvx_Polvel_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (zvx_Z-zmin)**3 * (zvx_Z-zmax)**3
  zvx_LaplaPolvel_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Polvel_orig)))

  tef_Polvel = tef_LaplaPol2PolTau_tef(tef_zvx(zvx_LaplaPolvel_orig),&
       cond='FF',new=.true.)

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

  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FF,poloidal]',             &
    answer = zvx_Polvel_orig,                                    &
    check = zvx_Polvel,                                          &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FF,laplapol]',             &
    answer = zvx_LaplaPolvel_orig,                               &
    check = zvx_LaplaPolvel,                                     &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FF,Top]',                  &
    answer = zvx_Polvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [FF,Top,deriv]',            &
    answer = zvx_DPolvel(0,:,:),                                 &
    check = zvx_Null(0,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore    &
    )

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

 !-------------------BC-RR(Tau) ----------------------
  zvx_Polvel_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (zvx_Z-zmin)**2 * (zvx_Z-zmax)**2
  zvx_LaplaPolvel_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Polvel_orig)))

  tef_Polvel = tef_LaplaPol2PolTau_tef(tef_zvx(zvx_LaplaPolvel_orig),&
       cond='RR',new=.true.)

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

  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RR,poloidal]',              &
    answer = zvx_Polvel_orig,                                     &
    check = zvx_Polvel,                                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RR,laplapol]',              &
    answer = zvx_LaplaPolvel_orig,                                &
    check = zvx_LaplaPolvel,                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RR,Top]',                   &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [RR,Top,deriv]',             &
    answer = zvx_DPolvel(0,:,:),                                  &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

 !-------------------BC-RF(Tau) ----------------------
  zvx_Polvel_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (zvx_Z-zmin)**3 * (zvx_Z-zmax)**2
  zvx_LaplaPolvel_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Polvel_orig)))

  tef_Polvel = tef_LaplaPol2PolTau_tef(tef_zvx(zvx_LaplaPolvel_orig),&
       cond='RF',new=.true.)

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

  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RF,poloidal]',              &
    answer = zvx_Polvel_orig,                                     &
    check = zvx_Polvel,                                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RF,laplapol]',              &
    answer = zvx_LaplaPolvel_orig,                                &
    check = zvx_LaplaPolvel,                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [RF,Top]',                   &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [RF,Bottom,deriv]',          &
    answer = zvx_DPolvel(km,:,:),                                 &
    check = zvx_Null(km,:,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !-------------------BC-FR(Tau) ----------------------
  zvx_Polvel_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (zvx_Z-zmin)**2 * (zvx_Z-zmax)**3
  zvx_LaplaPolvel_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Polvel_orig)))

  tef_Polvel = tef_LaplaPol2PolTau_tef(tef_zvx(zvx_LaplaPolvel_orig),&
       cond='FR',new=.true.)

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

  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FR,poloidal]',              &
    answer = zvx_Polvel_orig,                                     &
    check = zvx_Polvel,                                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FR,laplapol]',              &
    answer = zvx_LaplaPolvel_orig,                                &
    check = zvx_LaplaPolvel,                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [FR,Top]',                   &
    answer = zvx_Polvel(0,:,:),                                   &
    check = zvx_Null(0,:,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [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='tef_LaplaPol2PolTau_tef [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_PolvelbcTau_test', &
       'tee_mpi_module poloidal velocity potential B.C. function tests succeeded!')

end program tee_mpi_module_polvelbcTau_test


