!----------------------------------------------------------------------
!     Copyright (c) 2010--2011 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wtq_mpi_module テストプログラム
!
!      トロイダルポテンシャルの境界値問題
!
!履歴  2010/04/18  竹広真一   wt_module_sjpack_test_torvelbc.f90 より改造
!      2011/09/14  竹広真一   MPI 並列化
!      2011/09/14  竹広真一   wtq_mpi_module 用に改造
!
program wtq_mpi_module_torvelbc_wt_test

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

  implicit none

  integer,parameter  :: im=32, jm=16         ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: kmo=16, kmi=10       ! 格子点の設定(球殻動径, 球動径)
  integer,parameter  :: nm=10                ! 切断波数の設定(水平)
  integer,parameter  :: lmo=16, lmi=16       ! 切断波数の設定(球殻動径, 球動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

  real(8), dimension(0:im-1,1:jm,0:kmo)           :: xyz_TorVel0
  real(8), dimension(0:im-1,1:jm,0:kmo)           :: xyz_TorVel
  real(8), dimension((nm+1)*(nm+1),0:lmo)   :: wt_TorVel
  real(8), dimension(0:im-1,1:jm,0:kmo)     :: xyz_Zero = 0.0D0
  character(len=2), dimension(4),parameter :: BCond=(/'FF','FR','RF','RR'/)

  real(8), dimension(0:im-1,1:jm,0:kmo)           :: xyz_True

  ! 判定誤差設定
  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','wtq_mpi_module_torvelbc_wt_test', &
       'wtq_mpi_module wt_TorgBoundariesGrid subroutine test')

  call wtq_mpi_Initial(im,jm,kmi,kmo,nm,lmi,lmo,ri,ro)

  do l=1,4

     ! P_10
     !xyz_TorVel = sin(xyz_lat) * cos( pi*(xyz_rad-ri)/(ro-ri) )
     ! P_1_1
     !xyz_TorVel = cos(xyz_lat)*cos(xyz_lon)* cos( pi*(xyz_rad-ri)/(ro-ri) )
     xyz_TorVel = 2*sin(xyz_lat)**2 * cos( pi*(xyz_rad-ri)/(ro-ri) )

     xyz_TorVel0 = xyz_TorVel
     wt_TorVel = wt_xyz(xyz_TorVel)
     call wt_TorBoundariesGrid(wt_TorVel,cond=BCond(l),new=.true.)
     xyz_TorVel = xyz_wt(wt_TorVel)

     ! 内部チェック
     call AssertEqual(&
          message='wt_TorBoundariesGrid_wt (intenal value)',            &
          answer = xyz_Torvel(:,:,1:kmo-1),                              &
          check = xyz_Torvel0(:,:,1:kmo-1),                              &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     ! 上端境界チェック
     if( BCond(l)(1:1) == 'F' ) then
        xyz_True = xyz_wt(wt_DRad_wt(wt_xyz(xyz_TorVel/xyz_Rad)))
     else
        xyz_True = xyz_TorVel
     endif

     call AssertEqual(&
          message='wt_TorvelBoundariesGrid_wt ('//BCond(l)//'-Top B.C)',&
          answer = xyz_Zero(:,:,0),                                     &
          check = xyz_True(:,:,0),                                      &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     ! 下端境界チェック
     if( BCond(l)(2:2) == 'F' ) then
        xyz_True = xyz_wt(wt_DRad_wt(wt_xyz(xyz_TorVel/xyz_Rad)))
     else
        xyz_True = xyz_TorVel
     endif

     call AssertEqual(&
          message='wt_TorvelBoundariesGrid_wt ('//BCond(l)//'-Bottom B.C)',&
          answer = xyz_Zero(:,:,kmo),                                    &
          check = xyz_True(:,:,kmo),                                     &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  enddo

  call MessageNotify('M','wtq_mpi_module_torvelbc_wt_test', &
       'wtq_mpi_module wt_TorvelBoundariesGrid subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wtq_mpi_module_torvelbc_wt_test
