!----------------------------------------------------------------------
!     Copyright (c) 2022 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (境界値問題, ▽^2φ から φを求める)
!
!履歴  2022/03/29  竹広真一
!
program tee_mpi_module_test_LaplaInvTau

  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 :: zvx_Lapla(:,:,:)          ! 格子データ
  real(8), allocatable :: zvx_Lapla_orig(:,:,:)     ! 格子データ
  real(8), allocatable :: zvx_DData(:,:,:)           ! 格子データ
  real(8), allocatable :: tef_Data(:,:,:)           ! スペクトルデータ
  real(8), allocatable :: tef_Lapla(:,:,:)          ! スペクトルデータ

  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_test_LaplaInvTau', &
       'tee_mpi_module Inverse Laplacian 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_Data(0:km,jc(ip),0:im-1))           ! 格子データ
  allocate(zvx_Data_orig(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_Lapla(0:km,jc(ip),0:im-1))          ! 格子データ
  allocate(zvx_Lapla_orig(0:km,jc(ip),0:im-1))     ! 格子データ
  allocate(zvx_DData(0:km,jc(ip),0:im-1))          ! 格子データ
  allocate(tef_Data(0:nm,-mm:mm,2*lc(ip)))         ! スペクトルデータ
  allocate(tef_Lapla(0:nm,-mm:mm,2*lc(ip)))        ! スペクトルデータ

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

  !-------------------BC-DD(Tau) ----------------------
  zvx_Data_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * cos(pi/2.0D0*zvx_Z)
  zvx_Lapla_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data_orig)))

  tef_Data = tef_zvx(zvx_Data_orig)
  tef_Lapla = tef_Lapla_tef(tef_Data)
  zvx_Data  = zvx_tef(tef_LaplaInvDD_tef(tef_Lapla))

  call AssertEqual(&
       message='tef_LaplaInvTauDD_tef',                           &
       answer = zvx_Data_orig,                                    &
       check = zvx_Data,                                          &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !-------------------BC-NN(Tau) ----------------------
  zvx_Data_orig = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * sin(pi/2.0D0*zvx_Z)
  zvx_Lapla_orig = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data_orig)))

  tef_Data = tef_zvx(zvx_Data_orig)
  tef_Lapla = tef_Lapla_tef(tef_Data)
  zvx_Data  = zvx_tef(tef_LaplaInvNN_tef(tef_Lapla))

  call AssertEqual(&
       message='tef_LaplaInvTauNN_tef',                           &
       answer = zvx_Data_orig,                                    &
       check = zvx_Data,                                          &
       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_test_LaplaInvTau_test', &
       'tee_mpi_module Inverse Laplacian function tests succeeded.')

end program tee_mpi_module_test_LaplaInvTau



