!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module_supack テストプログラム
!
!      トロイダルポテンシャルの境界値問題
!
!履歴  2016/08/04  竹広真一   wt_mpi_module_supack_test_torvelbc.f90 より改造!
!
program ut_mpi_module_svpack_torvelbc_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_TorVel0(:,:,:)
  real(8), allocatable :: xyh_TorVel(:,:,:)
  real(8), allocatable :: ut_TorVel(:,:)
  real(8), allocatable :: xyh_Zero(:,:,:)
  real(8), allocatable :: xyh_True(:,:,:)

  character(len=2), dimension(4),parameter :: BCond=(/'FF','FR','RF','RR'/)


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

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

  integer :: l

  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_supack_torvelbc_test', &
       'ut_mpi_module_supack ut_TorgBoundariesGrid subroutine test')

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

  allocate(xyh_TorVel0(0:im-1,jm,kc))
  allocate(xyh_TorVel(0:im-1,jm,kc))
  allocate(ut_TorVel(nc,0:lm))
  allocate(xyh_Zero(0:im-1,jm,kc))
  allocate(xyh_True(0:im-1,jm,kc))

  xyh_Zero = 0.0D0

  do l=1,4

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

     xyh_TorVel0 = xyh_TorVel
     ut_TorVel = ut_xyh(xyh_TorVel)
     call ut_TorBoundariesGrid(ut_TorVel,cond=BCond(l),new=.true.)
     xyh_TorVel = xyh_ut(ut_TorVel)

     ! 内部チェック
     if ( kh_kz(0) /= 0 .and. kh_kz(km) /= 0 )then
        call AssertEqual(&
             message='ut_TorBoundariesGrid_ut (intenal value)',            &
             answer = xyh_Torvel(:,:,2:kc-1),                                &
             check = xyh_Torvel0(:,:,2:kc-1),                                &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     else if ( kh_kz(0) == 0 .and. kh_kz(km) /= 0 ) then
        call AssertEqual(&
             message='ut_TorBoundariesGrid_ut (intenal value)',            &
             answer = xyh_Torvel(:,:,1:kc-1),                              &
             check = xyh_Torvel0(:,:,1:kc-1),                              &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     else if ( kh_kz(0) /= 0 .and. kh_kz(km) == 0 ) then
        call AssertEqual(&
             message='ut_TorBoundariesGrid_ut (intenal value)',            &
             answer = xyh_Torvel(:,:,kh_kz(0)+1:kc),                       &
             check = xyh_Torvel0(:,:,kh_kz(0)+1:kc),                       &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     else 
        call AssertEqual(&
             message='ut_TorBoundariesGrid_ut (intenal value)',            &
             answer = xyh_Torvel,                                          &
             check = xyh_Torvel0,                                          &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     endif

     ! 上端境界チェック
     if( BCond(l)(1:1) == 'F' ) then
        xyh_True = xyh_ut(ut_DRad_ut(ut_xyh(xyh_TorVel/xyh_Rad)))
     else
        xyh_True = xyh_TorVel
     endif

     if ( kh_kz(0) /= 0 )then
        call AssertEqual(&
             message='ut_TorvelBoundariesGrid_ut ('//BCond(l)//'-Top B.C)',&
             answer = xyh_Zero(:,:,kh_kz(0)),                              &
             check = xyh_True(:,:,kh_kz(0)),                               &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     endif
        
     ! 下端境界チェック
     if( BCond(l)(2:2) == 'F' ) then
        xyh_True = xyh_ut(ut_DRad_ut(ut_xyh(xyh_TorVel/xyh_Rad)))
     else
        xyh_True = xyh_TorVel
     endif

     if ( kh_kz(km) /= 0 )then
        call AssertEqual(&
             message='ut_TorvelBoundariesGrid_ut ('//BCond(l)//'-Bottom B.C)',&
             answer = xyh_Zero(:,:,kh_kz(km)),                             &
             check = xyh_True(:,:,kh_kz(km)),                              &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     endif
     
  enddo
  
  call MessageNotify('M','ut_mpi_module_svpack_torvelbc_test', &
       'ut_mpi_module_svpack  ut_TorvelBoundariesGrid subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program ut_mpi_module_svpack_torvelbc_test
