!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module テストプログラム
!
!      磁場トロイダルポテンシャルの境界値問題
!
!履歴  2019/05/29  竹広真一
!
program ut_mpi_module_test_tormagbc

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use ut_mpi_module
  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      ! 内外半径

  integer :: npv = 2
  integer :: iproc, np, ierr

  real(8), allocatable :: xvh_TORMAG(:,:,:)
  real(8), allocatable :: xvh_TORMAG0(:,:,:)
  real(8), allocatable :: ut_TORMAG(:,:)
  real(8), allocatable :: xvh_Zero(:,:,:)

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

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

  !---------------- MPI スタート ---------------------
  !call get_ENV_INT('NPV',npv)

  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_test_tormagbc', &
       'ut_mpi_module ut_TormagBoundaries subroutine test')

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

  allocate(xvh_TORMAG(0:im-1,jc,kc))
  allocate(xvh_TORMAG0(0:im-1,jc,kc))
  allocate(ut_TORMAG(nmc,0:lm))
  allocate(xvh_Zero(0:im-1,jc,kc))

  xvh_Zero = 0.0D0

  !==================== ut_TormagBoundaries =========================
  ! P_10
  xvh_TORMAG = sin(xvh_lat) * (xvh_Rad-ri) * (xvh_Rad-ro)
  ! P_1_1
  !xvh_TORMAG = cos(xvh_lat)*cos(xvh_lon)* cos( pi*(xvh_rad-ri)/(ro-ri) )
  !xvh_TORMAG = 2*sin(xvh_lat)**2 * cos( pi*(xvh_rad-ri)/(ro-ri) )

  xvh_TORMAG0 = xvh_TORMAG
  ut_TORMAG = ut_xvh(xvh_TORMAG)
  call ut_TormagBoundaries(ut_TORMAG)
  xvh_TORMAG = xvh_ut(ut_TORMAG)

  call AssertEqual(&
       message='ut_TormagBoundariesTau (internal value)',            &
       answer = xvh_TORMAG0(:,:,2:kc-1),                             &
       check = xvh_TORMAG(:,:,2:kc-1),                               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  if ( ks == 0 ) then
     call AssertEqual(&
          message='ut_TormagBoundaries (Top B.C.)',                     &
          answer = xvh_Zero(:,:,1),                                     &
          check = xvh_TORMAG(:,:,1),                                    &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  else
     call AssertEqual(&
          message='ut_TormagBoundariesTau (internal value)',            &
          answer = xvh_TORMAG0(:,:,1),                                  &
          check = xvh_TORMAG(:,:,1),                                    &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif

  if ( ke == km ) then
     call AssertEqual(&
          message='ut_TormagBoundaries (Bottom B.C.)',                  &
          answer = xvh_Zero(:,:,kc),                                    &
          check = xvh_TORMAG(:,:,kc),                                   &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  else
     call AssertEqual(&
          message='ut_TormagBoundariesTau (internal value)',            &
          answer = xvh_TORMAG0(:,:,kc),                                 &
          check = xvh_TORMAG(:,:,kc),                                   &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif

  !==================== ut_TormagBoundariesGrid =========================
  ! P_10
  !xvh_TORMAG = sin(xvh_lat) * cos( pi*(xvh_rad-ri)/(ro-ri) )
  ! P_1_1
  xvh_TORMAG = cos(xvh_lat)*cos(xvh_lon)* cos( pi*(xvh_rad-ri)/(ro-ri) )
  !xvh_TORMAG = 2*sin(xvh_lat)**2 * cos( pi*(xvh_rad-ri)/(ro-ri) )

  xvh_TORMAG0 = xvh_TORMAG
  ut_TORMAG = ut_xvh(xvh_TORMAG)
  call ut_TormagBoundariesGrid(ut_TORMAG)
  xvh_TORMAG = xvh_ut(ut_TORMAG)

  call AssertEqual(&
       message='ut_TormagBoundariesGrid (internal value)',           &
       answer = xvh_TORMAG0(:,:,2:kc-1),                             &
       check = xvh_TORMAG(:,:,2:kc-1),                               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  if ( ks == 1 ) then
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (Top B.C.)',                 &
          answer = xvh_Zero(:,:,1),                                     &
          check = xvh_TORMAG(:,:,1),                                    &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  else
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (internal value)',           &
          answer = xvh_TORMAG0(:,:,1),                                  &
          check = xvh_TORMAG(:,:,1),                                    &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif

  if ( ke == km+1 ) then
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (Bottom B.C.)',              &
          answer = xvh_Zero(:,:,kc),                                    &
          check = xvh_TORMAG(:,:,kc),                                   &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  else
     call AssertEqual(&
          message='ut_TormagBoundariesGrid (internal value)',           &
          answer = xvh_TORMAG0(:,:,kc),                                 &
          check = xvh_TORMAG(:,:,kc),                                   &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  endif

  call MessageNotify('M','ut_mpi_module_test_tormagbc', &
       'ut_mpi_module ut_TormagBoundaries subroutine test succeeded!')

  !------ MPIの終了 ------
  call MPI_FINALIZE(IERR)

end program ut_mpi_module_test_tormagbc
