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

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

  implicit none

  integer,parameter  :: im=32, jm=16         ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: kmo=16, kmi=10       ! 格子点の設定(球殻動径, 球動径)
  integer,parameter  :: nm=10                ! 切断波数の設定(水平)
  integer,parameter  :: lmo=16, lmi=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','wtq_mpi_module_supack_tormagbc_test', &
       'wtq_mpi_module_supack wt_TormagBoundaries subroutine test')

  call wtq_mpi_Initial(im,jm,kmi,kmo,nm,lmi,lmo,ri,ro)
 
  allocate(xvz_TORMAG(0:im-1,jc,0:kmo))
  allocate(wt_TORMAG(nc,0:lmo))
  allocate(xvz_Zero(0:im-1,jc,0:kmo))

  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(:,:,kmo),                                   &
       check = xvz_TORMAG(:,:,kmo),                                  &
       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(:,:,kmo),                                   &
       check = xvz_TORMAG(:,:,kmo),                                  &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','wtq_mpi_module_supack_tormagbc_test', &
       'wtq_mpi_module_supack wt_TormagBoundaries subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wtq_mpi_module_supack_tormagbc_test
