!------------------------------------------------------------------------
! Copyright (c) 2016 SPMODEL Development Group. All rights reserved.
!------------------------------------------------------------------------
!
!表題  ee_mpi_module テストプログラム (パワースペクトル計算)
!
!履歴  2016/08/08  竹広真一
!
!
program ee_mpi_module_spectrum_test

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

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

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

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

  real(8) :: EnergyTMP, ToTEnergy
  real(8) :: EnstrophyTMP, ToTEnstrophy
  
 !---- 座標変数など ----
  real(8), parameter :: xmin = -1.0d0, xmax=1.0d0
  real(8), parameter :: ymin = -1.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_spectrum_test', &
       'ee_mpi_module spectrum 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(jc(ip),0:im-1))     ! 格子データ
  allocate(ef_Data(-lm:lm,2*kc(ip)))   ! スペクトルデータ
  
  !------------------- 初期値設定 ----------------------
  vx_Data = sin(k*pi*vx_X) * sin(l*pi*vx_Y)
  ef_Data = 0.0d0
  if ( kf_k(k) > 0 ) then
     ef_Data(l,kf_k(k))   = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
     ef_Data(-l,kf_k(k))  = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
     ef_Data(l,kf_k(-k))  = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
     ef_Data(-l,kf_k(-k)) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  endif
     
  call AssertEqual(                                      &
    message = 'Energy spectrum sin(k*pi*X)*sin(l*pi*Y)', &
    answer = ef_Data,                                    &
    check = ef_EnergyFromStreamfunc_ef(ef_vx(vx_Data)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  EnergyTmp = sum(ef_EnergyFromStreamfunc_ef(ef_vx(vx_Data))) &
            *(xmax-xmin)*(ymax-ymin)

  CALL MPI_ALLREDUCE(EnergyTmp,ToTEnergy,1,MPI_REAL8, &
                     MPI_SUM,MPI_COMM_WORLD,IERR)
  
  call AssertEqual(                                         &
    message = 'total energy sin(k*pi*X)*sin(l*pi*Y)',       &
    answer = ((k*pi)**2 + (l*pi)**2)/2,                     &
    check = TotEnergy,                                      &
    significant_digits = check_digits, ignore_digits = ignore     )

  ef_Data = 0.0d0
  if ( kf_k(k) > 0 ) then
     ef_Data(l,kf_k(k))   = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
     ef_Data(-l,kf_k(k))  = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
     ef_Data(l,kf_k(-k))  = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
     ef_Data(-l,kf_k(-k)) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  endif
  
  call AssertEqual(                                         &
    message = 'Enstrophy spectrum sin(k*pi*X)*sin(l*pi*Y)', &
    answer = ef_Data,                                       &
    check = ef_EnstrophyFromStreamfunc_ef(ef_vx(vx_Data)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  EnstrophyTMP = sum(ef_EnstrophyFromStreamfunc_ef(ef_vx(vx_Data))) &
            *(xmax-xmin)*(ymax-ymin)

  CALL MPI_ALLREDUCE(EnstrophyTmp,ToTEnstrophy,1,MPI_REAL8, &
                     MPI_SUM,MPI_COMM_WORLD,IERR)

  call AssertEqual(                                            &
    message = 'total enstrophy sin(k*pi*X)*sin(l*pi*Y)',       &
    answer = ((k*pi)**2 + (l*pi)**2)**2/2,                     &
    check = ToTEnstrophy,                                      &
    significant_digits = check_digits, ignore_digits = ignore     )

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','ee_mpi_module_spectrum_test', &
       'ee_mpi_module spectrum function tests succeeded!')

end program ee_mpi_module_spectrum_test

