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

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

  real(8), dimension(0:im-1,jm,0:kmo)           :: xyz_TORMAG
  real(8), dimension((nm+1)*(nm+1),0:lmo)       :: wt_TORMAG
  real(8), dimension(0:im-1,jm,kmi)             :: xyr_TORMAG
  real(8), dimension((nm+1)*(nm+1),0:lmi)       :: wq_TORMAG
  real(8), dimension(0:im-1,jm,0:kmo)           :: xyz_DTORDR
  real(8), dimension(0:im-1,jm,kmi)             :: xyr_DTORDR

  real(8), dimension(0:im-1,jm)                 :: xy_Null=0.0D0 

  real(8), parameter  :: Pmo = 1.0           ! 球殻の磁気プランドル数
  real(8), parameter  :: Pmi = 2.0           ! 内球の磁気プランドル数

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

  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_tormagbctau_test', &
       'wq_mpi_module  wtq_TormagBoundariesTau subroutine test')

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

 !==================== wtq_TormagBoundariesTau =========================
  ! P_10

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

  wt_TORMAG = wt_xyz(xyz_TORMAG)
  wq_TORMAG = wq_xyr(xyr_TORMAG)
  call wtq_TormagBoundariesTau(wt_TORMAG,wq_TORMAG,Pmo,Pmi)
  xyz_TORMAG = xyz_wt(wt_TORMAG)
  xyr_TORMAG = xyr_wq(wq_TORMAG)

  xyz_DTORDR = xyz_wt(wt_DRad_wt(wt_TORMAG))
  xyr_DTORDR = xyr_wq(wq_RadDRad_wq(wq_TORMAG))/xyr_Rad

  call checkresults

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

  wt_TORMAG = wt_xyz(xyz_TORMAG)
  wq_TORMAG = wq_xyr(xyr_TORMAG)
  call wtq_TormagBoundariesTau(wt_TORMAG,wq_TORMAG,Pmo,Pmi)
  xyz_TORMAG = xyz_wt(wt_TORMAG)
  xyr_TORMAG = xyr_wq(wq_TORMAG)

  xyz_DTORDR = xyz_wt(wt_DRad_wt(wt_TORMAG))
  xyr_DTORDR = xyr_wq(wq_RadDRad_wq(wq_TORMAG))/xyr_Rad

  call checkresults

  call MessageNotify('M','wtq_mpi_module_tormagbctau_test', &
       'wtq_mpi_module  wtq_TormagBoundariesTau subroutine test succeded')

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

  call MPI_FINALIZE(IERR)

contains

  subroutine checkresults

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

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

    call AssertEqual(&
      message='Inner B.C (derivative conituity) ',                  &
      answer = Pmo*xyz_DTORDR(:,:,kmo),                             &
      check = Pmi*xyr_DTORDR(:,:,kmi),                              &
      significant_digits = check_digits, ignore_digits = ignore     &
      )
  end subroutine checkresults

end program wtq_mpi_module_tormagbctau
