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

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

  implicit none

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

  real(8), allocatable  :: xyh_POLMAG(:,:,:)
  real(8), allocatable  :: ut_POLMAG(:,:)

  real(8), allocatable  :: uz_TopBoundary(:,:)
  real(8), allocatable  :: uz_BottomBoundary(:,:)

  real(8), allocatable  :: uz_Zero(:,:)

  real(8), allocatable  :: uz_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','ut_mpi_module_svpack_polmagbc_test', &
       'ut_mpi_module_svpack ut_PolmagBoundaries subroutine test')

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

  allocate(xyh_POLMAG(0:im-1,jm,kc))
  allocate(ut_POLMAG(nc,0:lm))

  allocate(uz_TopBoundary(nc,0:km))
  allocate(uz_BottomBoundary(nc,0:km))

  allocate(uz_Zero(nc,0:km))

  allocate(uz_n(nc,0:km))


  !=================== ut_PolmagBoundaries =======================
  ! P_10
  xyh_POLMAG = sin(xyh_lat) * sin( pi*(xyh_rad-ri)/(ro-ri) )

  ! P_1_1
  !xyh_POLMAG = cos(xyh_lat)*cos(xyh_lon)* sin( pi*(xyh_rad-ri)/(ro-ri) )
  !xyh_POLMAG = 2*sin(xyh_lat)**2 * sin( pi*(xyh_rad-ri)/(ro-ri) )

  ut_POLMAG = ut_xyh(xyh_POLMAG)
  call ut_PolmagBoundaries(ut_POLMAG)
  xyh_POLMAG = xyh_ut(ut_POLMAG)

  do k=0,km
     do n=1,nc
        nn=nm_l(n)
        uz_n(n,k) = nn(1)
     enddo
  enddo

  uz_Zero = 0.0D0
  uz_TopBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     + (uz_n +1)*uz_ut(ut_POLMAG)/uz_RAD
  uz_BottomBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     - uz_n * uz_ut(ut_POLMAG)/uz_RAD 
  call AssertEqual(&
       message='ut_PolmagBoundaries (Top B.C.)',                 &
       answer = u_cleanup_u(uz_Zero(:,0)),                       &
       check = u_cleanup_u(uz_TopBoundary(:,0)),                 &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='ut_PolmagBoundaries (Bottom B.C.)',              &
       answer = u_cleanup_u(uz_Zero(:,km)),                      &
       check = u_cleanup_u(uz_BottomBoundary(:,km)),             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

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

  ! P_1_1
  !xyh_POLMAG = cos(xyh_lat)*cos(xyh_lon)* sin( pi*(xyh_rad-ri)/(ro-ri) )
  xyh_POLMAG = 2*sin(xyh_lat)**2 * sin( pi*(xyh_rad-ri)/(ro-ri) )

  ut_POLMAG = ut_xyh(xyh_POLMAG)
  call ut_PolmagBoundariesGrid(ut_POLMAG)
  xyh_POLMAG = xyh_ut(ut_POLMAG)

  do k=0,km
     do n=1,nc
        nn=nm_l(n)
        uz_n(n,k) = nn(1)
     enddo
  enddo

  uz_TopBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     + (uz_n +1)*uz_ut(ut_POLMAG)/uz_RAD
  uz_BottomBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     - uz_n * uz_ut(ut_POLMAG)/uz_RAD 

  uz_Zero = 0.0D0
  uz_TopBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     + (uz_n +1)*uz_ut(ut_POLMAG)/uz_RAD
  uz_BottomBoundary = uz_ut(ut_DRad_ut(ut_POLMAG)) &
                     - uz_n * uz_ut(ut_POLMAG)/uz_RAD 
  call AssertEqual(&
       message='ut_PolmagBoundariesTau (Top B.C.)',                  &
       answer = u_cleanup_u(uz_Zero(:,0)),                           &
       check = u_cleanup_u(uz_TopBoundary(:,0)),                     &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='ut_PolmagBoundariesTau (Bottom B.C.)',               &
       answer = u_cleanup_u(uz_Zero(:,km)),                          &
       check = u_cleanup_u(uz_BottomBoundary(:,km)),                 &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','ut_mpi_module_svpack_polmagbc_test', &
       'ut_mpi_module_svpack ut_PolmagBoundaries subroutine test succeeded!')

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

  call MPI_FINALIZE(IERR)

contains

  function u_cleanup_u(u_data)
    real(8), intent(IN) :: u_data(nc)
    real(8)             :: u_cleanup_u(nc)

    integer l,nmary(2)

    u_cleanup_u = u_data

    do l=1,nc
       nmary = nm_l(l)
       if ( nmary(1) .lt. 0 ) u_cleanup_u(l) = 0.0D0
    end do

 end function u_cleanup_u

end program ut_mpi_module_svpack_polmagbc_test
