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

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

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

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

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

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

  real(8), allocatable :: zvx_VXans(:,:,:)   ! 格子データ
  real(8), allocatable :: zvx_VYans(:,:,:)   ! 格子データ
  real(8), allocatable :: zvx_VZans(:,:,:)   ! 格子データ

  real(8), allocatable :: zvx_Ans(:,:,:)     ! 格子データ

  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_torpol_test', &
       'tee_mpi_module toroidal poloidal potential 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(tef_Torvel(0:nm,-mm:mm,2*lc(ip)))  ! スペクトルデータ
  allocate(tef_Polvel(0:nm,-mm:mm,2*lc(ip)))  ! スペクトルデータ

  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(zvx_VXans(0:km,jc(ip),0:im-1))   ! 格子データ
  allocate(zvx_VYans(0:km,jc(ip),0:im-1))   ! 格子データ
  allocate(zvx_VZans(0:km,jc(ip),0:im-1))   ! 格子データ

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

  !------------- tef_ZRot_zvx_zvx, tef_ZRotRot_zvx_zvx_zvx ---------------
  zvx_VX = - m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
              - l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4 * zvx_Z
  zvx_VY =   l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
              - m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * 4 * zvx_Z
  zvx_VZ = ((l*pi)**2 +(m*pi)**2) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  zvx_Ans = ((l*pi)**2 +(m*pi)**2) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  
  call AssertEqual(&
    message='tef_ZRot_zvx_zvx',                                   &
    answer = zvx_Ans,                                             &
    check = zvx_tef(tef_ZRot_zvx_zvx(zvx_VX,zvx_VY)),             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Ans = -((l*pi)**2 +(m*pi)**2) &
       * (4 - ((l*pi)**2 +(m*pi)**2)*(2*zvx_Z**2 -1)) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) 
  
  call AssertEqual(&
    message='tef_ZRotRot_zvx_zvx_zvx',                            &
    answer = zvx_Ans,                                             &
    check = zvx_tef(tef_ZRotRot_zvx_zvx_zvx(zvx_VX,zvx_VY,zvx_VZ)),&
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  zvx_VXans = - m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
              - l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4 * zvx_Z
  zvx_VYans =   l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
              - m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * 4 * zvx_Z
  zvx_VZans = ((l*pi)**2 +(m*pi)**2) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Polvel = tef_zvx(zvx_Polvel)
 
  call tef_Potential2Vector(zvx_VX,zvx_VY,zvx_VZ,tef_Torvel,tef_Polvel)

  call AssertEqual(&
    message='tef_Potential2Vector[VX]',                           &
    answer = zvx_VXans,                                           &
    check = zvx_VX,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='tef_Potential2Vector[VZ]',                           &
    answer = zvx_VYans,                                           &
    check = zvx_VY,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='tef_Potential2Vector[VZ]',                           &
    answer = zvx_VZans,                                           &
    check = zvx_VZ,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  zvx_VXans =  - m*pi*((l*pi)**2 +(m*pi)**2) &
       * cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
     - l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4*zvx_Z &
     + m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * 4

  zvx_VYans = - m*pi* cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * 4*zvx_Z &
              - l*pi* sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4 &
         + l*pi*((l*pi)**2 +(m*pi)**2) &
             * sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  zvx_VZans = ((l*pi)**2 + (m*pi)**2) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  tef_Torvel = tef_zvx(zvx_Torvel)
  tef_Polvel = tef_zvx(zvx_Polvel)
 
  call tef_Potential2Rotation(zvx_VX,zvx_VY,zvx_VZ,tef_Torvel,tef_Polvel)

  call AssertEqual(&
    message='tef_Potential2Rotation[VX]',                         &
    answer = zvx_VXans,                                           &
    check = zvx_VX,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='tef_Potential2Rotation[VY]',                         &
    answer = zvx_VYans,                                           &
    check = zvx_VY,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='tef_Potential2Rotation[VZ]',                         &
    answer = zvx_VZans,                                           &
    check = zvx_VZ,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

end program tee_mpi_module_torpol_test


