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

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use ut_mpi_module_svpack
  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 :: xyh_TORMAG(:,:,:)
  real(8), allocatable :: ut_TORMAG(:,:)
  real(8), allocatable :: xyh_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','ut_mpi_module_svpack_tormagbc_test', &
       'ut_mpi_module_svpack ut_TormagBoundaries subroutine test')

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

  allocate(xyh_TORMAG(0:im-1,jm,kc))
  allocate(ut_TORMAG(nc,0:lm))
  allocate(xyh_Zero(0:im-1,jm,kc))

  xyh_Zero = 0.0D0

 !==================== ut_TormagBoundaries =========================
  ! P_10
  xyh_TORMAG = sin(xyh_lat) * cos( pi*(xyh_rad-ri)/(ro-ri) )
  ! P_1_1
  !xyh_TORMAG = cos(xyh_lat)*cos(xyh_lon)* cos( pi*(xyh_rad-ri)/(ro-ri) )
  !xyh_TORMAG = 2*sin(xyh_lat)**2 * cos( pi*(xyh_rad-ri)/(ro-ri) )

  ut_TORMAG = ut_xyh(xyh_TORMAG)
  call ut_TormagBoundaries(ut_TORMAG)
  xyh_TORMAG = xyh_ut(ut_TORMAG)

  if ( kh_kz(0) /= 0 )then
     call AssertEqual(&
          message='ut_TormagBoundaries (Top B.C.)',                     &
          answer = xyh_Zero(:,:,kh_kz(0)),                              &
          check = xyh_TORMAG(:,:,kh_kz(0)),                             &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif
  if ( kh_kz(km) /= 0 )then
     call AssertEqual(&
          message='ut_TormagBoundaries (Bottom B.C.)',                  &
          answer = xyh_Zero(:,:,kh_kz(km)),                             &
          check = xyh_TORMAG(:,:,kh_kz(km)),                            &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif
     
 !==================== ut_TormagBoundariesGrid =========================
  ! P_10
  !xyh_TORMAG = sin(xyh_lat) * cos( pi*(xyh_rad-ri)/(ro-ri) )
  ! P_1_1
  xyh_TORMAG = cos(xyh_lat)*cos(xyh_lon)* cos( pi*(xyh_rad-ri)/(ro-ri) )
  !xyh_TORMAG = 2*sin(xyh_lat)**2 * cos( pi*(xyh_rad-ri)/(ro-ri) )

  ut_TORMAG = ut_xyh(xyh_TORMAG)
  call ut_TormagBoundariesGrid(ut_TORMAG)
  xyh_TORMAG = xyh_ut(ut_TORMAG)


  if ( kh_kz(0) /= 0 )then
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (Top B.C.)',                 &
          answer = xyh_Zero(:,:, kh_kz(0)),                             &
          check = xyh_TORMAG(:,:, kh_kz(0)),                            &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif
  if ( kh_kz(km) /= 0 )then
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (Bottom B.C.)',              &
          answer = xyh_Zero(:,:, kh_kz(km)),                            &
          check = xyh_TORMAG(:,:, kh_kz(km)),                           &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif
  
  call MessageNotify('M','ut_mpi_module_svpack_tormagbc_test', &
       'ut_mpi_module_svpack ut_TormagBoundaries subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program ut_mpi_module_svpack_tormagbc_test
