!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wq_mpi_module_supack テストプログラム
!
!      磁場トロイダルポテンシャルの境界値問題
!
!履歴  2016/04/01  竹広真一   wq_mpi_module_sjpack_tormagbc_test.f90 を supack 化
!
program wq_mpi_module_supack_tormagbc

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

  implicit none

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

  real(8), allocatable :: xvr_TORMAG(:,:,:)
  real(8), allocatable :: xvr_TORMAG_orig(:,:,:)
  real(8), allocatable :: wq_TORMAG(:,:)
  real(8), allocatable :: wq_TORMAG_orig(:,:)
  real(8), allocatable :: wr_TORMAG(:,:)

  real(8), allocatable :: xvr_Zero(:,:,:)

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

  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','wq_mpi_module_supack_tormagbc_test', &
       'wq_mpi_module_supack wq_TormagBoundary subroutine test')

  call wq_mpi_initial(im,jm,km,nm,lm,ra)

  allocate(xvr_TORMAG(0:im-1,1:jc,km))
  allocate(xvr_TORMAG_orig(0:im-1,1:jc,km))
  allocate(wq_TORMAG(nc,0:lm))
  allocate(wq_TORMAG_orig(nc,0:lm))
  allocate(wr_TORMAG(nc,km))
                
  allocate(xvr_Zero(0:im-1,1:jc,km))

  xvr_Zero = 0.0D0

 !==================== wq_TormagBoundary =========================
  ! P_10
  xvr_TORMAG = sin(xvr_lat) * cos( pi*(xvr_rad-ra)/ra )*xvr_Rad
  ! P_1_1
  !xvr_TORMAG = cos(xvr_lat)*cos(xvr_lon)* cos( pi*(xvr_rad-ri)/(ro-ri) )
  !xvr_TORMAG = 2*sin(xvr_lat)**2 * cos( pi*(xvr_rad-ri)/(ro-ri) )

  wq_TORMAG = wq_xvr(xvr_TORMAG)
  wq_TORMAG_orig = wq_TORMAG

  call wq_TormagBoundary(wq_TORMAG)
  xvr_TORMAG = xvr_wq(wq_TORMAG)

  call AssertEqual(&
       message='wq_TormagBoundaryGrid (Top B.C.)',                   &
       answer = xvr_Zero(:,:,km),                                    &
       check = xvr_TORMAG(:,:,km),                                   &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call AssertEqual(&
       message='wq_TormagBoundary (internal value)',                 &
       answer = wq_Tormag(:,0:lm-2),                                 &
       check = wq_Tormag_orig(:,0:lm-2),                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

 !==================== wq_TormagBoundaryGrid =========================
  ! P_10
  !xvr_TORMAG = sin(xvr_lat) * cos( pi*(xvr_rad-ra)/ra )
  ! P_1_1
  xvr_TORMAG = cos(xvr_lat)*cos(xvr_lon)* cos( pi*(xvr_rad-ra)/ra )*xvr_Rad
  !xvr_TORMAG = 2*sin(xvr_lat)**2 * cos( pi*(xvr_rad-ra)/ra )

  xvr_TORMAG_orig = xvr_TORMAG
  wr_TORMAG = wr_xvr(xvr_TORMAG)
  call wr_TormagBoundaryGrid(wr_TORMAG)
  xvr_TORMAG = xvr_wr(wr_TORMAG)

  call AssertEqual(&
       message='wq_TormagBoundaryGrid (Top B.C.)',                   &
       answer = xvr_Zero(:,:,km),                                    &
       check = xvr_TORMAG(:,:,km),                                   &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call AssertEqual(&
       message='wq_TormagBoundaryGrid (internal value)',             &
       answer = xvr_TORMAG_orig(:,:,1:km-1),                         &
       check = xvr_TORMAG(:,:,1:km-1),                               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )


  call MessageNotify('M','wq_mpi_module_supack_tormagbc_test', &
       'wq_mpi_module_supack wq_TormagBoundary subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wq_mpi_module_supack_tormagbc
