!----------------------------------------------------------------------
!     Copyright (c) 2016--2021 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム 
!      (トロイダルポロイダルポテンシャル, スペクトル)
!
!履歴  2016/09/21  竹広真一
!      2021/02/18  竹広真一
!
program tee_mpi_module_spectrum_test

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

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

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

  real(8), allocatable :: zvx_VX(:,:,:)      ! 格子データ
  real(8), allocatable :: zvx_VY(:,:,:)      ! 格子データ
  real(8), allocatable :: zvx_VZ(:,:,:)      ! 格子データ

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

  real(8)              :: Ans
  real(8)              :: Ek, EkTmp
  
  integer              :: l=2, m=5
  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 = 9
  integer, parameter :: ignore = -10

  call MessageNotify('M','tee_mpi_module_spectrum_test', &
       'tee_mpi_module toroidal poloidal potential spectrum functions 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_Torvel(0:km,jc(ip),0:im-1))  ! 格子データ
  allocate(zvx_Polvel(0:km,jc(ip),0:im-1))  ! 格子データ

  allocate(zvx_VX(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_VY(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_VZ(0:km,jc(ip),0:im-1))      ! 格子データ

  allocate(zef_Spectrum(0:km,-mm:mm,2*lc(ip))) ! スペクトルデータ
  allocate(zef_Ans(0:km,-mm:mm,2*lc(ip)))      ! スペクトルデータ
  
 !------------------- zef_ToroidalEnergySpectrum_tef ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_Polvel = 0.0D0

  call tef_Potential2Vector(zvx_VX,zvx_VY,zvx_VZ,&
       tef_zvx(zvx_Torvel),tef_zvx(zvx_Polvel))

  zef_Ans= 0.0D0
  if ( lf_l(l) > 0 ) then
!!$     write(6,*) lf_l(l),lf_l(-l)
     zef_Ans(:,m,lf_l(l))   = ((l*pi)**2+(m*pi)**2)*((2*z_Z**2 -1)*0.25)**2/2
     zef_Ans(:,-m,lf_l(-l)) = zef_Ans(:,m,lf_l(l))
     zef_Ans(:,-m,lf_l(l))  = ((l*pi)**2+(m*pi)**2)*((2*z_Z**2 -1)*0.25)**2/2
     zef_Ans(:,m,lf_l(-l))  = zef_Ans(:,-m,lf_l(l))
  end if
     
  Ans = 0.5*IntZYX_zvx(zvx_VX**2 + zvx_VY**2 + zvx_VZ**2)

  zef_Spectrum=zef_ToroidalEnergySpectrum_tef(tef_zvx(zvx_Torvel))

  call AssertEqual(&
    message='zef_ToroidalEnergySpectrum_tef',                     &
    answer = zef_Ans,                                             &
    check = zef_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  EkTmp = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zef_Spectrum,3),2))
  CALL MPI_ALLREDUCE(EkTmp,Ek,1,MPI_REAL8,MPI_SUM,MPI_COMM_WORLD,IERR)
  
  call AssertEqual(&
    message='zef_ToroidalEnergySpectrum_tef(total)',              &
    answer = Ans,                                                 &
    check = Ek,                                                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !------------------- zef_PoloidalEnergySpectrum_tef ----------------------
  zvx_Torvel = 0.0D0
  zvx_Polvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  call tef_Potential2Vector(zvx_VX,zvx_VY,zvx_VZ,&
       tef_zvx(zvx_Torvel),tef_zvx(zvx_Polvel))

  zef_Ans= 0.0D0
  if ( lf_l(l) > 0 ) then
    zef_Ans(:,m,lf_l(l)) = 0.5*( ((l*pi)**2+(m*pi)**2)*(4*z_Z*0.25)**2 &
                          +((l*pi)**2+(m*pi)**2)**2*((2*z_Z**2 -1)*0.25)**2 )
    zef_Ans(:,-m,lf_l(-l)) = zef_Ans(:,m,lf_l(l))

    zef_Ans(:,-m,lf_l(l))= 0.5*( ((l*pi)**2+(m*pi)**2)*(4*z_Z*0.25)**2 &
                          +((l*pi)**2+(m*pi)**2)**2*((2*z_Z**2 -1)*0.25)**2 )
    zef_Ans(:,m,lf_l(-l)) = zef_Ans(:,-m,lf_l(l))
  endif

  Ans = 0.5*IntZYX_zvx(zvx_VX**2 + zvx_VY**2 + zvx_VZ**2)

  zef_Spectrum=zef_PoloidalEnergySpectrum_tef(tef_zvx(zvx_Polvel))

  call AssertEqual(&
    message='zef_PoloidalEnergySpectrum_tef',                     &
    answer = zef_Ans,                                             &
    check = zef_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  EkTmp = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zef_Spectrum,3),2))
  CALL MPI_ALLREDUCE(EkTmp,Ek,1,MPI_REAL8,MPI_SUM,MPI_COMM_WORLD,IERR)

  call AssertEqual(&
    message='zef_PoloidalEnergySpectrum_tef(total)',              &
    answer = Ans,                                                 &
    check = Ek,                                                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !------------------- zef_ToroidalEnstrophySpectrum_tef ----------------------
  zvx_Torvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  zvx_Polvel = 0.0D0

  call tef_Potential2Rotation(zvx_VX,zvx_VY,zvx_VZ,&
       tef_zvx(zvx_Torvel),tef_zvx(zvx_Polvel))

  zef_Ans= 0.0D0
  if ( lf_l(l) > 0 ) then
    zef_Ans(:,m,lf_l(l)) = 0.5*( ((l*pi)**2+(m*pi)**2)*(4*z_Z*0.25)**2 &
                          +((l*pi)**2+(m*pi)**2)**2*((2*z_Z**2 -1)*0.25)**2 )
    zef_Ans(:,-m,lf_l(-l)) = zef_Ans(:,m,lf_l(l))

    zef_Ans(:,-m,lf_l(l))= 0.5*( ((l*pi)**2+(m*pi)**2)*(4*z_Z*0.25)**2 &
                          +((l*pi)**2+(m*pi)**2)**2*((2*z_Z**2 -1)*0.25)**2 )
    zef_Ans(:,m,lf_l(-l)) = zef_Ans(:,-m,lf_l(l))
  endif

  Ans = 0.5*IntZYX_zvx(zvx_VX**2 + zvx_VY**2 + zvx_VZ**2)

  zef_Spectrum=zef_ToroidalEnstrophySpectrum_tef(tef_zvx(zvx_Torvel))

  call AssertEqual(&
    message='zef_ToroidalEnstrophySpectrum_tef',                  &
    answer = zef_Ans,                                             &
    check = zef_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  EkTmp = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zef_Spectrum,3),2))
  CALL MPI_ALLREDUCE(EkTmp,Ek,1,MPI_REAL8,MPI_SUM,MPI_COMM_WORLD,IERR)

  call AssertEqual(&
    message='zef_ToroidalEnstrophySpectrum_tef(total)',           &
    answer = Ans,                                                 &
    check = Ek,                                                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 !------------------- zef_PoloidalEnstrophySpectrum_tef ----------------------
  zvx_Torvel = 0.0D0
  zvx_Polvel = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * sin(pi*zvx_Z) 

  call tef_Potential2Rotation(zvx_VX,zvx_VY,zvx_VZ,&
       tef_zvx(zvx_Torvel),tef_zvx(zvx_Polvel))

  zef_Ans= 0.0D0
  if ( lf_l(l) > 0 ) then
!!$     write(6,*) lf_l(l),lf_l(-l)
     zef_Ans(:,m,lf_l(l)) &
          = ((l*pi)**2+(m*pi)**2)*((l*pi)**2+(m*pi)**2+pi**2)**2*(sin(pi*z_Z)*0.25)**2/2
     zef_Ans(:,-m,lf_l(-l)) = zef_Ans(:,m,lf_l(l))
     zef_Ans(:,-m,lf_l(l)) &
          = ((l*pi)**2+(m*pi)**2)*((l*pi)**2+(m*pi)**2+pi**2)**2*(sin(pi*z_Z)*0.25)**2/2
     zef_Ans(:,m,lf_l(-l))  = zef_Ans(:,-m,lf_l(l))
  end if
     
  Ans = 0.5*IntZYX_zvx(zvx_VX**2 + zvx_VY**2 + zvx_VZ**2)

  zef_Spectrum=zef_PoloidalEnstrophySpectrum_tef(tef_zvx(zvx_Polvel))
  
  call AssertEqual(&
    message='zef_PoloidalEnstrophySpectrum_tef',                     &
    answer = zef_Ans,                                             &
    check = zef_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  EkTmp = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zef_Spectrum,3),2))
  CALL MPI_ALLREDUCE(EkTmp,Ek,1,MPI_REAL8,MPI_SUM,MPI_COMM_WORLD,IERR)
  
  call AssertEqual(&
    message='zef_PoloidalEnstrophySpectrum_tef(total)',              &
    answer = Ans,                                                 &
    check = Ek,                                                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)
  
  call MessageNotify('M','tee_mpi_module_spectrum_test', &
       'tee_mpi_module toroidal poloidal potential spectrum functions tests suceeded!')

end program tee_mpi_module_spectrum_test

