!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wt_mpi_module_supack テストプログラム
!
!      磁場トロイダルポテンシャルの境界値問題
!
!履歴  2016/03/27  竹広真一   wt_module_nmomp_test_torvelbc.f90 より改造!
!
program wt_mpi_module_supack_tormagbc_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_TORMAG(:,:,:)
  real(8), allocatable :: wt_TORMAG(:,:)
  real(8), allocatable :: xvz_Zero(:,:,:)

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

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

  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_tormagbc_test', &
       'wt_mpi_module_supack wt_TormagBoundaries subroutine test')

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

  allocate(xvz_TORMAG(0:im-1,jc,0:km))
  allocate(wt_TORMAG(nc,0:lm))
  allocate(xvz_Zero(0:im-1,jc,0:km))

  xvz_Zero = 0.0D0

 !==================== wt_TormagBoundaries =========================
  ! P_10
  xvz_TORMAG = sin(xvz_lat) * cos( pi*(xvz_rad-ri)/(ro-ri) )
  ! P_1_1
  !xvz_TORMAG = cos(xvz_lat)*cos(xvz_lon)* cos( pi*(xvz_rad-ri)/(ro-ri) )
  !xvz_TORMAG = 2*sin(xvz_lat)**2 * cos( pi*(xvz_rad-ri)/(ro-ri) )

  wt_TORMAG = wt_xvz(xvz_TORMAG)
  call wt_TormagBoundaries(wt_TORMAG)
  xvz_TORMAG = xvz_wt(wt_TORMAG)

  call AssertEqual(&
       message='wt_TormagBoundaries (Top B.C.)',                     &
       answer = xvz_Zero(:,:,0),                                     &
       check = xvz_TORMAG(:,:,0),                                    &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='wt_TormagBoundaries (Bottom B.C.)',                  &
       answer = xvz_Zero(:,:,km),                                    &
       check = xvz_TORMAG(:,:,km),                                   &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

 !==================== wt_TormagBoundariesGrid =========================
  ! P_10
  !xvz_TORMAG = sin(xvz_lat) * cos( pi*(xvz_rad-ri)/(ro-ri) )
  ! P_1_1
  xvz_TORMAG = cos(xvz_lat)*cos(xvz_lon)* cos( pi*(xvz_rad-ri)/(ro-ri) )
  !xvz_TORMAG = 2*sin(xvz_lat)**2 * cos( pi*(xvz_rad-ri)/(ro-ri) )

  wt_TORMAG = wt_xvz(xvz_TORMAG)
  call wt_TormagBoundariesGrid(wt_TORMAG)
  xvz_TORMAG = xvz_wt(wt_TORMAG)


  call AssertEqual(&
       message='wt_TormagBoundariesGrid (Top B.C.)',                 &
       answer = xvz_Zero(:,:,0),                                     &
       check = xvz_TORMAG(:,:,0),                                    &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='wt_TormagBoundariesGrid (Bottom B.C.)',              &
       answer = xvz_Zero(:,:,km),                                    &
       check = xvz_TORMAG(:,:,km),                                   &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','wt_mpi_module_supack_tormagbc_test', &
       'wt_mpi_module_supack wt_TormagBoundaries subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wt_mpi_module_supack_tormagbc_test
