!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  scee_module_fftj テストプログラム (トロイダルポロイダルポテンシャル)
!
!履歴  2017/05/27  竹広真一
!
program scee_module_fftj_torpol_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)            :: cee_Torvel(0:nm,-mm:mm,-lm:lm)  ! スペクトルデータ
  real(8)            :: see_Polvel(nm,-mm:mm,-lm:lm)    ! スペクトルデータ

  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)            :: zyx_VXans(0:km,0:jm-1,0:im-1)   ! 格子データ
  real(8)            :: zyx_VYans(0:km,0:jm-1,0:im-1)   ! 格子データ
  real(8)            :: zyx_VZans(0:km,0:jm-1,0:im-1)   ! 格子データ

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

  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 = 8
  integer, parameter :: ignore = -9

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

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

  zyx_Xi = (zyx_Z - zmin)/(zmax-zmin)
  
  !------------------- Potential2Vector ----------------------
  zyx_Torvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(PI*zyx_Xi)
  zyx_Polvel = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(2*PI*zyx_Xi)

  zyx_VXans = - m*pi* cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * cos(PI*zyx_Xi) &
              - l*pi*2*pi/(zmax-zmin) &
                 * sin(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  zyx_VYans =   l*pi* sin(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(PI*zyx_Xi) &
              - m*pi*2*pi/(zmax-zmin) &
                 * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  zyx_VZans = ((l*pi)**2 +(m*pi)**2) &
       * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(2*PI*zyx_Xi)

  cee_Torvel = cee_zyx(zyx_Torvel)
  see_Polvel = see_zyx(zyx_Polvel)
 
  call scee_Potential2Vector_cee_see(zyx_VX,zyx_VY,zyx_VZ,cee_Torvel,see_Polvel)

  call AssertEqual(&
    message='scee_Potential2Vector_cee_see[VX]',                  &
    answer = zyx_VXans,                                           &
    check = zyx_VX,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='scee_Potential2Vector_cee_see[VZ]',                  &
    answer = zyx_VYans,                                           &
    check = zyx_VY,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='scee_Potential2Vector_cee_see[VZ]',                  &
    answer = zyx_VZans,                                           &
    check = zyx_VZ,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------- cee_ZRot_zyx_zyx, see_ZRotRot_zyx_zyx_zyx ---------------  

  zyx_Ans = ((l*pi)**2 +(m*pi)**2) &
             * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(PI*zyx_Xi)
  call AssertEqual(&
    message='cee_ZRot_zyx_zyx',                                   &
    answer = zyx_Ans,                                             &
    check = zyx_cee(cee_ZRot_zyx_zyx(zyx_VX,zyx_VY)),             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Ans = ((l*pi)**2 +(m*pi)**2) * ((l*pi)**2 +(m*pi)**2 +(2*pi/(zmax-zmin))**2 ) &
            * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(2*PI*zyx_Xi)
  call AssertEqual(&
    message='see_ZRotRot_zyx_zyx_zyx',                            &
    answer = zyx_Ans,                                             &
    check = zyx_see(see_ZRotRot_zyx_zyx_zyx(zyx_VX,zyx_VY,zyx_VZ)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- Potential2Rotation ----------------------
  zyx_VXans = - m*pi * ((l*pi)**2 +(m*pi)**2 + (2*PI/(zmax-zmin))**2) &
                  * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * sin(2*PI*zyx_Xi) &
              + l*pi*pi/(zmax-zmin) &
                 * sin(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(PI*zyx_Xi)
  zyx_VYans =   l*pi * ((l*pi)**2 +(m*pi)**2 + (2*PI/(zmax-zmin))**2) &
                  * sin(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(2*PI*zyx_Xi) &
              + m*pi*pi/(zmax-zmin) &
                  * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * sin(PI*zyx_Xi)
  zyx_VZans = ((l*pi)**2 +(m*pi)**2) &
                  * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(PI*zyx_Xi)
 
  call scee_Potential2Rotation_cee_see(zyx_VX,zyx_VY,zyx_VZ,cee_Torvel,see_Polvel)

  call AssertEqual(&
    message='scee_Potential2Rotation_cee_see[VX]',                &
    answer = zyx_VXans,                                           &
    check = zyx_VX,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='scee_Potential2Rotation_cee_see[VY]',                &
    answer = zyx_VYans,                                           &
    check = zyx_VY,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='scee_Potential2Rotation_cee_see[VZ]',                &
    answer = zyx_VZans,                                           &
    check = zyx_VZ,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------- see_ZRot_zyx_zyx, cee_ZRotRot_zyx_zyx_zyx ---------------
  zyx_Ans =   ((l*pi)**2 +(m*pi)**2) &
            * ((l*pi)**2 +(m*pi)**2 + (2*PI/(zmax-zmin))**2) &
            * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(2*PI*zyx_Xi)

  call AssertEqual(&
    message='see_ZRot_zyx_zyx',                                   &
    answer = zyx_Ans,                                             &
    check = zyx_see(see_ZRot_zyx_zyx(zyx_VX,zyx_VY)),             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Ans = ((l*pi)**2 +(m*pi)**2) * ((l*pi)**2 +(m*pi)**2 +(pi/(zmax-zmin))**2 ) &
            * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(PI*zyx_Xi)
  call AssertEqual(&
    message='see_ZRotRot_zyx_zyx_zyx',                            &
    answer = zyx_Ans,                                             &
    check = zyx_cee(cee_ZRotRot_zyx_zyx_zyx(zyx_VX,zyx_VY,zyx_VZ)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

end program scee_module_fftj_torpol_test

