!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (境界値問題, ポロイダル磁場, 選点法)
!
!履歴  2016/09/20  竹広真一
!
program tee_mpi_module_polmagbcgrid_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use tee_mpi_module
  use mpi
  implicit none

 !---- 空間解像度設定 ----
  integer, parameter :: im=64, jm=64, km=16       ! 格子点の設定(X,Y)
  integer, parameter :: lm=21, mm=21, nm=16       ! 切断波数の設定(X,Y)

 !---- 変数 ----
  real(8), allocatable :: zvx_Polmag(:,:,:)       ! 格子データ
  real(8), allocatable :: zvx_Polmag_orig(:,:,:)  ! 格子データ

  real(8), allocatable :: tef_Polmag(:,:,:)       ! スペクトルデータ

  real(8), allocatable :: tef_Kh(:,:,:)           ! 水平全波数

  real(8), allocatable :: zef_Boundary(:,:,:)     ! 境界条件
  real(8), allocatable :: ef_Null(:,:)            ! 0 

  real(8)            :: rl, rm
  integer            :: l=2, m=5
  integer            :: l1, m1
  integer            :: np, ip, ierr
  
 !---- 座標変数など ----
  real(8), parameter :: xmin = 0.0d0, xmax=2.0d0
  real(8), parameter :: ymin = 0.0d0, ymax=2.0d0
  real(8), parameter :: zmin = -1.0d0, zmax=1.0d0

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

  ! 判定誤差設定
  integer, parameter :: check_digits = 11
  integer, parameter :: ignore = -12

  call MessageNotify('M','tee_mpi_module_PolmagbcGrid_test', &
       'tee_mpi_module poloidal mag. field B.C. subruoutine tests')

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IP,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)
   
 !---------------- 座標値の設定 ---------------------
  ! スペクトル初期化
  call tee_mpi_initial(im,jm,km,lm,mm,nm,xmin,xmax,ymin,ymax,zmin,zmax) 

  allocate(zvx_Polmag(0:km,jc(ip),0:im-1))        ! 格子データ
  allocate(zvx_Polmag_orig(0:km,jc(ip),0:im-1))   ! 格子データ  

  allocate(tef_Polmag(0:nm,-mm:mm,2*lc(ip)))      ! スペクトルデータ

  allocate(tef_Kh(0:nm,-mm:mm,2*lc(ip)))          ! 水平全波数

  allocate(zef_Boundary(0:km,-mm:mm,2*lc(ip)))    ! 境界条件
  allocate(ef_Null(-mm:mm,2*lc(ip)))              ! 0
  ef_Null = 0.0D0

  do m1=-mm,mm
     do l1=-lm,lm
        if ( lf_l(l1) > 0 ) then
           rl = 2*PI/(xmax-xmin)*l1 ; rm = 2*PI/(ymax-ymin)*m1
           tef_Kh(:,m1,lf_l(l1)) = sqrt(rl**2+rm**2)
        end if
     enddo
  enddo

 !-------------------BC-DD(Grid) ----------------------
  zvx_Polmag = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  zvx_Polmag_orig = zvx_Polmag

  tef_Polmag = tef_zvx(zvx_Polmag)

  call tef_PolmagBoundariesGrid(tef_Polmag)
  zvx_Polmag = zvx_tef(tef_Polmag)

  call AssertEqual(&
    message='tef_PolmagBoundariesGrid [internal]',&
    answer = zvx_Polmag_orig(1:km-1,:,:),                         &
    check = zvx_Polmag(1:km-1,:,:),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zef_Boundary = zef_tef(tef_Dz_tef(tef_Polmag) + tef_Kh * tef_Polmag)
  call AssertEqual(&
    message='tef_PolmagBoundariesGrid [Top]',&
    answer = ef_Null,                                             &
    check = zef_Boundary(0,:,:),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  zef_Boundary = zef_tef(tef_Dz_tef(tef_Polmag) - tef_Kh * tef_Polmag)
  call AssertEqual(&
    message='tef_PolmagBoundariesGrid [Bottom]',&
    answer = ef_Null,                                             &
    check = zef_Boundary(km,:,:),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','tee_mpi_module_PolmagbcGrid_test', &
       'tee_mpi_module poloidal mag. field B.C. subruoutine tests succeeded!')

end program tee_mpi_module_polmagbcgrid_test
