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

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

  implicit none

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

  real(8), dimension(im,jm,0:kmo)           :: xyz_POLMAG
  real(8), dimension((nm+1)*(nm+1),0:lmo)   :: wt_POLMAG
  real(8), dimension(im,jm,kmi)             :: xyr_POLMAG
  real(8), dimension((nm+1)*(nm+1),0:lmi)   :: wq_POLMAG
  real(8), dimension(im,jm,0:kmo)           :: xyz_DPOLDR
  real(8), dimension(im,jm,kmi)             :: xyr_DPOLDR

  real(8), dimension((nm+1)*(nm+1),0:kmo)   :: wz_TopBoundary
  real(8), dimension((nm+1)*(nm+1),0:kmo)   :: wz_n   ! 全波数
  real(8), dimension((nm+1)*(nm+1))         :: w_Null=0.0D0

  real(8), parameter  :: pi=3.1415926535897932385D0
  ! 判定誤差設定
  integer, parameter :: check_digits = 11
  integer, parameter :: ignore = -12

  integer :: n,k, 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_polmagbctau_test', &
       'wtq_mpi_module  wtq_TormagBoundariesTau subroutine test')

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

  do k=0,kmo
     do n=1,(nm+1)**2
        nn=nm_l(n)
        wz_n(n,k) = nn(1)
     enddo
  enddo

  ! P_10

  xyz_POLMAG = sin(xyz_lat) * cos( pi*(xyz_rad-ri)/(ro-ri) )
  xyr_POLMAG = sin(xyr_lat) * cos( pi*(xyr_rad-ri)/ri ) * xyr_Rad

  wt_POLMAG = wt_xyz(xyz_POLMAG)
  wq_POLMAG = wq_xyr(xyr_POLMAG)
  call wtq_PolmagBoundariesTau(wt_POLMAG,wq_POLMAG)
  xyz_POLMAG = xyz_wt(wt_POLMAG)
  xyr_POLMAG = xyr_wq(wq_POLMAG)

  xyz_DPOLDR = xyz_wt(wt_DRad_wt(wt_POLMAG))
  xyr_DPOLDR = xyr_wq(wq_RadDRad_wq(wq_POLMAG))/xyr_Rad
  wz_TopBoundary = wz_wt(wt_DRad_wt(wt_POLMAG)) &
                     + (wz_n +1)*wz_wt(wt_POLMAG)/wz_RAD

  call checkresults

  ! P_1_1
  xyz_POLMAG = cos(xyz_lat)*cos(xyz_lon)* cos( pi*(xyz_rad-ri)/(ro-ri) )
  xyr_POLMAG = cos(xyr_lat)*cos(xyr_lon)* cos( pi*(xyr_rad-ri)/ri )*xyr_Rad

  wt_POLMAG = wt_xyz(xyz_POLMAG)
  wq_POLMAG = wq_xyr(xyr_POLMAG)
  call wtq_PolmagBoundariesTau(wt_POLMAG,wq_POLMAG)
  xyz_POLMAG = xyz_wt(wt_POLMAG)
  xyr_POLMAG = xyr_wq(wq_POLMAG)

  xyz_DPOLDR = xyz_wt(wt_DRad_wt(wt_POLMAG))
  xyr_DPOLDR = xyr_wq(wq_RadDRad_wq(wq_POLMAG))/xyr_Rad

  call checkresults

  call MessageNotify('M','wtq_test_polmagbctau', &
       'wtq_module  wtq_PolmagBoundariesTau subroutine test succeded')

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

  call MPI_FINALIZE(IERR)

contains
  subroutine checkresults

     call AssertEqual(&
       message='Top B.C',                                            &
       answer = wz_TopBoundary(:,0),                                 &
       check = w_Null,                                               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

     call AssertEqual(&
       message='Inner B.C (value conituity) ',                       &
       answer = xyz_POLMAG(:,:,kmo),                                 &
       check = xyr_POLMAG(:,:,kmi),                                  &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

     call AssertEqual(&
       message='Inner B.C (derivative conituity) ',                  &
       answer = xyz_DPOLDR(:,:,kmo),                                 &
       check = xyr_DPOLDR(:,:,kmi),                                  &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
   end subroutine checkresults

end program wtq_mpi_module_polmagbctau