!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wt_mpi_module_supack テストプログラム
!
!      ポロイダルポテンシャルの境界値問題
!
!履歴  2016/03/27  竹広真一   wt_module_nmomp_test_polvelbc.f90 より改造
!
program wt_mpi_module_supack_polvelbc_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wt_mpi_module_supack
  use mpi

  implicit none

  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: nm=10, lm=16         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5      ! 内外半径

  real(8), allocatable :: xvz_Poloidal(:,:,:)
  real(8), allocatable :: xvz_LaplaPol(:,:,:)
  real(8), allocatable :: xvz_LaplaPol1(:,:,:)
  real(8), allocatable :: xvz_True(:,:,:)
  real(8), allocatable :: xvz_Zero(:,:,:)
  character(len=2), dimension(4), parameter :: BCond=(/'FF','FR','RF','RR'/)

  ! 判定誤差設定
  integer, parameter :: check_digits = 9
  integer, parameter :: ignore = -10

  real(8), parameter  :: pi=3.1415926535897932385D0

  integer :: l

  integer :: iproc, np, ierr

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  call MessageNotify('M','wt_mpi_module_supack_polvelbc_test', &
       'wt_mpi_module_supack wt_LaplaPol2polGrid_wt function tests')

  call wt_mpi_initial(im,jm,km,nm,lm,ri,ro)

  allocate(xvz_Poloidal(0:im-1,jc,0:km))
  allocate(xvz_LaplaPol(0:im-1,jc,0:km))
  allocate(xvz_LaplaPol1(0:im-1,jc,0:km))
  allocate(xvz_True(0:im-1,jc,0:km))
  allocate(xvz_Zero(0:im-1,jc,0:km))

  do l=1,4

     ! P_10
     xvz_Poloidal = sin(xvz_Lat) * sin( pi*(xvz_Rad-ri)/(ro-ri) )
     xvz_LaplaPol = xvz_wt(wt_Lapla_wt(wt_xvz(xvz_Poloidal)))
     !xvz_LaplaPol = sin(xvz_Lat) * sin( pi*(xvz_Rad-ri)/(ro-ri) )
     ! P_1_1
     !xvz_LaplaPol = cos(xvz_Lat)*cos(xvz_Lon)* sin( pi*(xvz_Rad-ri)/(ro-ri) )
     !xvz_LaplaPol = 2*sin(xvz_Lat)**2 * sin( pi*(xvz_Rad-ri)/(ro-ri) )

     !xvz_Poloidal = xvz_wz(wz_LaplaPol2pol_wz(wz_xvz(xvz_LaplaPol),BCond(l)))
     !xvz_Poloidal = xvz_wt(wt_LaplaPol2PolTau_wt(wt_xvz(xvz_LaplaPol),BCond(l)))

     xvz_Poloidal = xvz_wt(wt_LaplaPol2PolGrid_wt(wt_xvz(xvz_LaplaPol),BCond(l),new=.true.))

     xvz_LaplaPol1 = xvz_wt(wt_Lapla_wt(wt_xvz(xvz_Poloidal)))
     xvz_Zero = 0.0D0

     !---------------- 内部チェック -----------------------
     call AssertEqual(&
          message='wt_LaplaPol2polGrid_wt (intenal value)',             &
          answer = xvz_LaplaPol1(:,:,2:km-2),                           &
          check = xvz_LaplaPol(:,:,2:km-2),                             &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !--------- 上端境界チェック ----------

     !----- Φ=0 at the top ---------
     call AssertEqual(&
          message='wt_LaplaPol2polGrid_wt (Top B.C. Φ=0)',             &
          answer = xvz_Zero(:,:,0),                                     &
          check = xvz_Poloidal(:,:,0),                                  &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !----- dΦ/dr=0, d^2Φ/dr^2 at the top ---------
     if( BCond(l)(1:1) == 'F' ) then
        xvz_True = xvz_wt(wt_DRad_wt(wt_DRad_wt(wt_xvz(xvz_Poloidal))))
     else
        xvz_True = xvz_wt(wt_DRad_wt(wt_xvz(xvz_Poloidal)))
     endif

     call AssertEqual(&
          message='wt_LaplaPol2polGrid_wt ('//BCond(l)//'-Top B.C)',    &
          answer = xvz_Zero(:,:,0),                                     &
          check = xvz_True(:,:,0),                                      &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !--------- 下端境界チェック ----------

     !----- Φ=0 at the bottom ---------
     call AssertEqual(&
          message='wt_LaplaPol2polGrid_wt (Bottom B.C. Φ=0)',          &
          answer = xvz_Zero(:,:,km),                                    &
          check = xvz_Poloidal(:,:,km),                                 &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !----- dΦ/dr=0, d^2Φ/dr^2 at the bottom ---------
     if( BCond(l)(2:2) == 'F' ) then
        xvz_True = xvz_wt(wt_DRad_wt(wt_DRad_wt(wt_xvz(xvz_Poloidal))))
     else
        xvz_True = xvz_wt(wt_DRad_wt(wt_xvz(xvz_Poloidal)))
     endif

     call AssertEqual(&
          message='wt_LaplaPol2polGrid_wt ('//BCond(l)//'-Bottom B.C)', &
          answer = xvz_Zero(:,:,km),                                    &
          check = xvz_True(:,:,km),                                     &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

  end do

  call MessageNotify('M','wt_mpi_module_supack_polvelbc_test', &
       'wt_mpi_module_supack wt_LaplaPol2polGrid_wt function tests suceeded!')

 !------ MPIの終了 ------

  call MPI_FINALIZE(IERR)

end program wt_mpi_module_supack_polvelbc_test
