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

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wtq_mpi_module_supack
  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), allocatable  :: xvz_POLMAG(:,:,:)
  real(8), allocatable  :: wt_POLMAG(:,:)

  real(8), allocatable  :: wz_TopBoundary(:,:)
  real(8), allocatable  :: wz_BottomBoundary(:,:)

  real(8), allocatable  :: wz_Zero(:,:)

  real(8), allocatable  :: wz_n(:,:)   ! 全波数

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

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

  integer :: k, n, nn(2)

  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_supack_polmagbc_test', &
       'wtq_mpi_module_supack wt_PolmagBoundaries subroutine test')

  call wtq_mpi_Initial(im,jm,kmi,kmo,nm,lmi,lmo,ri,ro)
 
  allocate(xvz_POLMAG(0:im-1,jc,0:kmo))
  allocate(wt_POLMAG(nc,0:lmo))

  allocate(wz_TopBoundary(nc,0:kmo))
  allocate(wz_BottomBoundary(nc,0:kmo))

  allocate(wz_Zero(nc,0:kmo))

  allocate(wz_n(nc,0:kmo))


  !=================== wt_PolmagBoundaries =======================
  ! P_10
  xvz_POLMAG = sin(xvz_lat) * sin( pi*(xvz_rad-ri)/(ro-ri) )

  ! P_1_1
  !xvz_POLMAG = cos(xvz_lat)*cos(xvz_lon)* sin( pi*(xvz_rad-ri)/(ro-ri) )
  !xvz_POLMAG = 2*sin(xvz_lat)**2 * sin( pi*(xvz_rad-ri)/(ro-ri) )

  wt_POLMAG = wt_xvz(xvz_POLMAG)
  call wt_PolmagBoundaries(wt_POLMAG)
  xvz_POLMAG = xvz_wt(wt_POLMAG)

  do k=0,kmo
     do n=1,nc
        nn=nm_l(n)
        wz_n(n,k) = nn(1)
     enddo
  enddo

  wz_Zero = 0.0D0
  wz_TopBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     + (wz_n +1)*wz_wt(wt_POLMAG)/wz_RAD
  wz_BottomBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     - wz_n * wz_wt(wt_POLMAG)/wz_RAD 
  call AssertEqual(&
       message='wt_PolmagBoundaries (Top B.C.)',                     &
       answer = wz_Zero(:,0),                                        &
       check = wz_TopBoundary(:,0),                                  &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='wt_PolmagBoundaries (Bottom B.C.)',                  &
       answer = wz_Zero(:,kmo),                                      &
       check = wz_BottomBoundary(:,kmo),                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  !=================== wt_PolmagBoundariesGrid =======================
  ! P_10
  !xyz_POLMAG = sin(xyz_lat) * sin( pi*(xyz_rad-ri)/(ro-ri) )

  ! P_1_1
  !xvz_POLMAG = cos(xvz_lat)*cos(xvz_lon)* sin( pi*(xvz_rad-ri)/(ro-ri) )
  xvz_POLMAG = 2*sin(xvz_lat)**2 * sin( pi*(xvz_rad-ri)/(ro-ri) )

  wt_POLMAG = wt_xvz(xvz_POLMAG)
  call wt_PolmagBoundariesGrid(wt_POLMAG)
  xvz_POLMAG = xvz_wt(wt_POLMAG)

  do k=0,kmo
     do n=1,nc
        nn=nm_l(n)
        wz_n(n,k) = nn(1)
     enddo
  enddo

  wz_TopBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     + (wz_n +1)*wz_wt(wt_POLMAG)/wz_RAD
  wz_BottomBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     - wz_n * wz_wt(wt_POLMAG)/wz_RAD 

  wz_Zero = 0.0D0
  wz_TopBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     + (wz_n +1)*wz_wt(wt_POLMAG)/wz_RAD
  wz_BottomBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     - wz_n * wz_wt(wt_POLMAG)/wz_RAD 
  call AssertEqual(&
       message='wt_PolmagBoundariesTau (Top B.C.)',                  &
       answer = wz_Zero(:,0),                                        &
       check = wz_TopBoundary(:,0),                                  &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='wt_PolmagBoundariesTau (Bottom B.C.)',               &
       answer = wz_Zero(:,kmo),                                      &
       check = wz_BottomBoundary(:,kmo),                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','wtq_mpi_module_supack_polmagbc_test', &
       'wtq_mpi_module_supack wt_PolmagBoundaries subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wtq_mpi_module_supack_polmagbc_test
