!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  scee_module_fftj テストプログラム 
!      (トロイダルポロイダルポテンシャル, スペクトル)
!
!履歴  2017/05/28  竹広真一
!
program scee_module_fftj_spectrum_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use scee_module_fftj
  implicit none

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

  !---- 変数 ----
  real(8)            :: zyx_Torvel(0:km,0:jm-1,0:im-1)  ! 格子データ
  real(8)            :: zyx_Polvel(0:km,0:jm-1,0:im-1)  ! 格子データ

  real(8)            :: zyx_VX(0:km,0:jm-1,0:im-1)      ! 格子データ
  real(8)            :: zyx_VY(0:km,0:jm-1,0:im-1)      ! 格子データ
  real(8)            :: zyx_VZ(0:km,0:jm-1,0:im-1)      ! 格子データ

  real(8)            :: zee_Spectrum(0:km,-mm:mm,-lm:lm)! スペクトルデータ
  real(8)            :: zee_Ans(0:km,-mm:mm,-lm:lm)     ! スペクトルデータ

  real(8)            :: Ans

  integer            :: l=2, m=5

  !---- 座標変数など ----
  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

  real(8)            :: zyx_Xi(0:km,0:jm-1,0:im-1)    ! 格子データ  

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

  call MessageNotify('M','scee_module_fftj_spectrum_test', &
       'scee_module_fftj toroidal poloidal potential spectrum functions tests')

  !---------------- 座標値の設定 ---------------------
  ! スペクトル初期化
  call scee_initial(im,jm,km,lm,mm,nm,xmin,xmax,ymin,ymax,zmin,zmax)

  zyx_Xi = (zyx_Z - zmin)/(zmax-zmin)

  !------------------- zee_ToroidalEnergySpectrum_cee ----------------------
  zyx_Torvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  zyx_Polvel = 0.0D0

  call scee_Potential2Vector_cee_see(zyx_VX,zyx_VY,zyx_VZ,&
       cee_zyx(zyx_Torvel),see_zyx(zyx_Polvel))

  zee_Ans= 0.0D0
  zee_Ans(:,m,l) = ((l*pi)**2+(m*pi)**2)*(cos(2*PI*zyx_Xi(:,0,0))*0.25)**2/2
  zee_Ans(:,-m,-l) = zee_Ans(:,m,l)
  zee_Ans(:,-m,l) = ((l*pi)**2+(m*pi)**2)*(cos(2*PI*zyx_Xi(:,0,0))*0.25)**2/2
  zee_Ans(:,m,-l) = zee_Ans(:,-m,l)

  Ans = 0.5*IntZYX_zyx(zyx_VX**2 + zyx_VY**2 + zyx_VZ**2)

  zee_Spectrum=zee_ToroidalEnergySpectrum_cee(cee_zyx(zyx_Torvel))

  call AssertEqual(&
    message='zee_ToroidalEnergySpectrum_cee',                     &
    answer = zee_Ans,                                             &
    check = zee_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='zee_ToroidalEnergySpectrum_cee(total)',              &
    answer = Ans,                                                 &
    check = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zee_Spectrum,3),2)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- zee_ToroidalEnergySpectrum_see ----------------------
  zyx_Torvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(3*PI*zyx_Xi)
  zyx_Polvel = 0.0D0

  call scee_Potential2Vector_see_cee(zyx_VX,zyx_VY,zyx_VZ,&
       see_zyx(zyx_Torvel),cee_zyx(zyx_Polvel))

  zee_Ans= 0.0D0
  zee_Ans(:,m,l) = ((l*pi)**2+(m*pi)**2)*(sin(3*PI*zyx_Xi(:,0,0))*0.25)**2/2
  zee_Ans(:,-m,-l) = zee_Ans(:,m,l)
  zee_Ans(:,-m,l) = ((l*pi)**2+(m*pi)**2)*(sin(3*PI*zyx_Xi(:,0,0))*0.25)**2/2
  zee_Ans(:,m,-l) = zee_Ans(:,-m,l)

  Ans = 0.5*IntZYX_zyx(zyx_VX**2 + zyx_VY**2 + zyx_VZ**2)

  zee_Spectrum=zee_ToroidalEnergySpectrum_see(see_zyx(zyx_Torvel))

  call AssertEqual(&
    message='zee_ToroidalEnergySpectrum_see',                     &
    answer = zee_Ans,                                             &
    check = zee_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='zee_ToroidalEnergySpectrum_see(total)',              &
    answer = Ans,                                                 &
    check = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zee_Spectrum,3),2)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- zee_PoloidalEnergySpectrum_cee ----------------------
  zyx_Torvel = 0.0D0
  zyx_Polvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(3*PI*zyx_Xi)

  call scee_Potential2Vector_see_cee(zyx_VX,zyx_VY,zyx_VZ,&
       see_zyx(zyx_Torvel),cee_zyx(zyx_Polvel))

  zee_Ans= 0.0D0
  zee_Ans(:,m,l) = 0.5*( ((l*pi)**2+(m*pi)**2)*(3*PI/(zmax-zmin)*sin(3*PI*zyx_Xi(:,0,0))*0.25)**2 &
                        +((l*pi)**2+(m*pi)**2)**2*(cos(3*PI*zyx_Xi(:,0,0))*0.25)**2 )
  zee_Ans(:,-m,-l) = zee_Ans(:,m,l)
                     
  zee_Ans(:,-m,l)= zee_Ans(:,m,l)

  zee_Ans(:,m,-l) = zee_Ans(:,-m,l)

  Ans = 0.5*IntZYX_zyx(zyx_VX**2 + zyx_VY**2 + zyx_VZ**2)

  zee_Spectrum=zee_PoloidalEnergySpectrum_cee(cee_zyx(zyx_Polvel))

  call AssertEqual(&
    message='zee_PoloidalEnergySpectrum_cee',                     &
    answer = zee_Ans,                                             &
    check = zee_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='zee_PoloidalEnergySpectrum_cee(total)',              &
    answer = Ans,                                                 &
    check = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zee_Spectrum,3),2)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- zee_PoloidalEnergySpectrum_see ----------------------
  zyx_Torvel = 0.0D0
  zyx_Polvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(3*PI*zyx_Xi)

  call scee_Potential2Vector_cee_see(zyx_VX,zyx_VY,zyx_VZ,&
       cee_zyx(zyx_Torvel),see_zyx(zyx_Polvel))

  zee_Ans= 0.0D0
  zee_Ans(:,m,l) = 0.5*( ((l*pi)**2+(m*pi)**2)*(3*PI/(zmax-zmin)*cos(3*PI*zyx_Xi(:,0,0))*0.25)**2 &
                        +((l*pi)**2+(m*pi)**2)**2*(sin(3*PI*zyx_Xi(:,0,0))*0.25)**2 )
  zee_Ans(:,-m,-l) = zee_Ans(:,m,l)
                     
  zee_Ans(:,-m,l)= zee_Ans(:,m,l)

  zee_Ans(:,m,-l) = zee_Ans(:,-m,l)

  Ans = 0.5*IntZYX_zyx(zyx_VX**2 + zyx_VY**2 + zyx_VZ**2)

  zee_Spectrum=zee_PoloidalEnergySpectrum_see(see_zyx(zyx_Polvel))

  call AssertEqual(&
    message='zee_PoloidalEnergySpectrum_see',                     &
    answer = zee_Ans,                                             &
    check = zee_SPectrum,                                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='zee_PoloidalEnergySpectrum_see(total)',              &
    answer = Ans,                                                 &
    check = (xmax-xmin)*(ymax-ymin)*IntZ_z(sum(sum(zee_Spectrum,3),2)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','scee_module_fftj_spectrum_test', &
       'scee_module_fftj toroidal poloidal potential spectrum functions tests suceeded!')

end program scee_module_fftj_spectrum_test

