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

  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=10         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

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

  real(8), allocatable :: xvh_TorVel0(:,:,:)
  real(8), allocatable :: xvh_TorVel(:,:,:)
  real(8), allocatable :: ut_TorVel(:,:)
  real(8), allocatable :: xvh_Zero(:,:,:)
  real(8), allocatable :: xvh_True(:,:,:)

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


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

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

  integer :: l

  !---------------- 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_torvelbc_test', &
       'ut_mpi_module wt_TorgBoundariesTau subroutine test')

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

  allocate(xvh_TorVel0(0:im-1,jc,kc))
  allocate(xvh_TorVel(0:im-1,jc,kc))
  allocate(ut_TorVel(nmc,0:lm))
  allocate(xvh_Zero(0:im-1,jc,kc))
  allocate(xvh_True(0:im-1,jc,kc))

  xvh_Zero = 0.0D0

  do l=1,4

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

     select case (BCOND(l))
     case ('FF')
        xvh_TorVel = sin(xvh_Lat) * xvh_Rad * (xvh_Rad-ri)**2 * (xvh_Rad-ro)**2
     case('FR')
        xvh_TorVel = cos(xvh_lat)*cos(xvh_lon) * xvh_Rad * (xvh_Rad-ri) * (xvh_Rad-ro)**2
     case('RF')
        xvh_TorVel = 2*sin(xvh_lat)**2 * xvh_Rad * (xvh_Rad-ri)**2 * (xvh_Rad-ro)
     case('RR')
        xvh_TorVel = sin(xvh_Lat) * xvh_Rad * (xvh_Rad-ri) * (xvh_Rad-ro)
     end select

     xvh_TorVel0 = xvh_TorVel
     ut_TorVel = ut_xvh(xvh_TorVel)
     call ut_TorBoundariesTau(ut_TorVel,cond=BCond(l),new=.true.)
     xvh_TorVel = xvh_ut(ut_TorVel)

     ! 内部チェック
     call AssertEqual(&
          message='ut_TorBoundariesTau_ut (intenal value)',            &
          answer = xvh_Torvel(:,:,2:kc-1),                              &
          check = xvh_Torvel0(:,:,2:kc-1),                              &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     ! 上端境界チェック
     if( BCond(l)(1:1) == 'F' ) then
        xvh_True = xvh_ut(ut_DRad_ut(ut_xvh(xvh_TorVel/xvh_Rad)))
     else
        xvh_True = xvh_TorVel
     endif

     if ( ks == 0 ) then
        call AssertEqual(&
             message='ut_TorvelBoundariesTau_ut ('//BCond(l)//'-Top B.C)',&
             answer = xvh_Zero(:,:,1),                                     &
             check = xvh_True(:,:,1),                                      &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     else
        call AssertEqual(&
             message='ut_TorvelBoundariesTau_ut ('//BCond(l)//'-Top B.C)',&
             answer = xvh_Torvel(:,:,1),                                   &
             check = xvh_Torvel0(:,:,1),                                   &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     endif

     ! 下端境界チェック
     if( BCond(l)(2:2) == 'F' ) then
        xvh_True = xvh_ut(ut_DRad_ut(ut_xvh(xvh_TorVel/xvh_Rad)))
     else
        xvh_True = xvh_TorVel
     endif

     if ( ke == km ) then
        call AssertEqual(&
             message='ut_TorvelBoundariesTau_ut ('//BCond(l)//'-Bottom B.C)',&
             answer = xvh_Zero(:,:,kc),                                    &
             check = xvh_True(:,:,kc),                                     &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     else
        call AssertEqual(&
             message='ut_TorvelBoundariesTau_ut ('//BCond(l)//'-Bottom B.C)',&
             answer = xvh_Torvel(:,:,kc),                                  &
             check = xvh_Torvel0(:,:,kc),                                  &
             significant_digits = check_digits, ignore_digits = ignore     &
             )
     endif
  enddo

  call MessageNotify('M','ut_mpi_module_torvelbc_test', &
       'ut_mpi_module  ut_TorvelBoundariesTau subroutine test succeeded!')

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

end program ut_mpi_module_test_torvelbc_tau
