!------------------------------------------------------------------------
! Copyright (c) 2016 SPMODEL Development Group. All rights reserved.
!------------------------------------------------------------------------
!
!表題  ee_mpi_module テストプログラム (正逆変換)
!
!履歴  2016/08/07  竹広真一
!
program ee_mpi_module_base_test

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

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

 !---- 変数 ----
  real(8), allocatable  :: vx_Data(:,:)         ! 格子データ
  real(8), allocatable  :: ef_Data(:,:)         ! スペクトルデータ
  real(8), allocatable  :: ef_Data1(:,:)        ! スペクトルデータ

  integer            :: k=2,l=5
  integer            :: np, ip, ierr

 !---- 座標変数など ----
  real(8), parameter :: xmin = 0.0d0, xmax=2.0d0
  real(8), parameter :: ymin = 0.0d0, ymax=1.0d0

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

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

  
  call MessageNotify('M','ee_mpi_module_base_test', &
       'ee_mpi_module basic function 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 ee_mpi_initial(im,jm,km,lm,xmin,xmax,ymin,ymax)    ! スペクトル初期化

  allocate(vx_Data(js(ip):je(ip),0:im-1))
  allocate(ef_Data(-lm:lm,2*kc(ip)))
  allocate(ef_Data1(-lm:lm,2*kc(ip)))

 !------------------- 初期値設定 ----------------------
  vx_Data = sin(k*pi*vx_X) * sin(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ef_Data(-l,kf_k(k)) = 0.25  ; ef_Data(l,kf_k(k)) = -0.25
  endif
  call check(ef_vx(vx_Data), ef_Data, &
       'Transform sin(k*pi*X)*sin(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, &
       'Inverse transform sin(k*pi*X)*sin(2*l*pi*Y)')

  vx_Data = cos(k*pi*vx_X) * cos(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ef_Data(l,kf_k(k)) = 0.25  ; ef_Data(-l,kf_k(k)) = 0.25
  endif
  call check(ef_vx(vx_Data), ef_Data, &
       'Transform cos(k*pi*X)*cos(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, &
       'Inverse transoform cos(k*pi*X)*cos(2*l*pi*Y)')

  vx_Data = sin(k*pi*vx_X) * cos(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ef_Data(-l,kf_k(-k)) = -0.25  ; ef_Data(l,kf_k(-k)) = - 0.25
  endif
  call check(ef_vx(vx_Data), ef_Data, &
       'Transform sin(k*pi*X)*cos(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, &
       'Inverse transform sin(k*pi*X)*cos(2*l*pi*Y)')

  vx_Data = cos(k*pi*vx_X) * sin(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ef_Data(-l,kf_k(-k)) = -0.25  ; ef_Data(l,kf_k(-k)) = 0.25
  endif
  call check(ef_vx(vx_Data), ef_Data, &
       'Transform cos(k*pi*X)*sin(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, &
       'Inverse transform cos(k*pi*X)*sin(2*l*pi*Y)')

  !------ l=0 モード ------
  vx_Data = sin(k*pi*vx_X)
  ef_Data = 0.0
  if ( kf_k(-k) > 0 ) then
     ef_Data(0,kf_k(-k)) = -0.5
  endif
  call check(ef_vx(vx_Data), ef_Data, 'Transform sin(k*pi*X)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, 'Inverse transform sin(k*pi*X)')

  vx_Data = cos(k*pi*vx_X)
  ef_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ef_Data(0,kf_k(k)) = 0.5
  endif
  call check(ef_vx(vx_Data), ef_Data, 'Transform cos(k*pi*X)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, 'Inverse transform cos(k*pi*X)')
  
  !------ k=0 モード ------
  vx_Data = sin(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ef_Data(-l,kf_k(0)) = -0.5
  endif
  call check(ef_vx(vx_Data), ef_Data, 'Transform sin(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, 'Inverse transform sin(2*l*pi*Y)')

  vx_Data = cos(2*l*pi*vx_Y)
  ef_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ef_Data(l,kf_k(0)) = 0.5
  endif
  ef_Data1 = ef_vx(vx_Data)

  call check(ef_vx(vx_Data), ef_Data, 'Transform cos(2*l*pi*Y)')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, 'Inverse transform cos(2*l*pi*Y)')

  !------ k=l=0 モード ------
  vx_Data = 1.0D0
  ef_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ef_Data(0,kf_k(0)) = 1.0
  endif
  call check(ef_vx(vx_Data), ef_Data, 'Transform constatn field')
  call check(vx_ef(ef_vx(vx_Data)), vx_Data, 'Inverse transform constant field')


  call MPI_FINALIZE(IERR)
 
  call MessageNotify('M','ee_test_transform', &
       'ee_module transform function tests succeeded!')
contains

  subroutine check(var,sol,funcname)
    real(8) :: var(:,:)                ! 判定する配列
    real(8) :: sol(:,:)                ! 誤差
    character(len=*) :: funcname

    call AssertEqual(     &
      message = funcname, &
      answer = sol,       &
      check = var,        &
      significant_digits = check_digits, ignore_digits = ignore     )

  end subroutine check

end program ee_mpi_module_base_test
