!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  tee_mpi_module テストプログラム (微分演算)
!
!履歴  2016/09/19  竹広真一
!
program tee_mpi_module_deriv_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_Data(:,:,:)    ! 格子データ
  real(8), allocatable   :: zvx_Deriv(:,:,:)   ! 格子データ
  real(8), allocatable   :: tef_Data(:,:,:)    ! スペクトルデータ

  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 = 11
  integer, parameter :: ignore = -12

  call MessageNotify('M','tee_mpi_module_deriv_test', &
       'tee_mpi_module derivative 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 tee_mpi_initial(im,jm,km,lm,mm,nm,xmin,xmax,ymin,ymax,zmin,zmax) 

  allocate(zvx_Data(0:km,jc(ip),0:im-1))      ! 格子データ
  allocate(zvx_Deriv(0:km,jc(ip),0:im-1))     ! 格子データ
  allocate(tef_Data(0:nm,-mm:mm,2*lc(ip)))    ! スペクトルデータ

 !------------------- 関数チェック ----------------------
  zvx_Data = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z

  zvx_Deriv = l*pi*cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * zvx_Z
  call AssertEqual(&
    message='tef_Dx_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dx_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = m*pi*sin(l*pi*zvx_X) * cos(m*pi*zvx_Y) * zvx_Z
  call AssertEqual(&
    message='tef_Dy_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dy_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = sin(l*pi*zvx_X) * sin(m*pi*zvx_Y) 
  call AssertEqual(&
    message='tef_Dz_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dz_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zvx_X) * sin(m*pi*zvx_Y)*zvx_Z
  call AssertEqual(&
    message='tef_Lapla_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',       &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zvx_X) * sin(m*pi*zvx_Y)*zvx_Z
  call AssertEqual(&
    message='tef_LaplaH_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',      &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaH_tef(tef_zvx(zvx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -sin(l*pi*zvx_X) * sin(m*pi*zvx_Y)*zvx_Z/((l*pi)**2+(m*pi)**2)
  call AssertEqual(&
    message='tef_LaplaHInv_tef with sin(k*pi*X)*sin(l*pi*Y)*Z',   &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaHInv_tef(tef_zvx(zvx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  zvx_Data = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)

  zvx_Deriv = -l*pi*sin(l*pi*zvx_X) *cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  call AssertEqual(&
    message='tef_Dx_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dx_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -m*pi*cos(l*pi*zvx_X) * sin(m*pi*zvx_Y) * (2*zvx_Z**2 -1)
  call AssertEqual(&
    message='tef_Dy_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dy_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4 * zvx_Z
  call AssertEqual(&
    message='tef_Dz_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dz_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * (2*zvx_Z**2 -1) &
       + cos(l*pi*zvx_X) * cos(m*pi*zvx_Y) * 4
  call AssertEqual(&
    message='tef_Lapla_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((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_LaplaH_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaH_tef(tef_zvx(zvx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -1.0D0/((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_LaplaHInv_tef with cos(k*pi*X)*cos(l*pi*Y)*(2Z**2-1)',  &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaHInv_tef(tef_zvx(zvx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = sin(l*pi*zvx_X) * cos(m*pi*zvx_Y)/2.0D0

  zvx_Deriv = l*pi*cos(l*pi*zvx_X) * cos(m*pi*zvx_Y)/2.0D0
  call AssertEqual(&
    message='tef_Dx_tef with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dx_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -m*pi*sin(l*pi*zvx_X) * sin(m*pi*zvx_Y)/2.0D0
  call AssertEqual(&
    message='tef_Dy_tef with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dy_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = 0.0D0
  call AssertEqual(&
    message='tef_Dz_tef with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dz_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zvx_X) * cos(m*pi*zvx_Y)/2.0D0
  call AssertEqual(&
    message='tef_Lapla_tef with sin(k*pi*X)*cos(l*pi*Y)/2',       &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zvx_X) * cos(m*pi*zvx_Y)/2.0D0
  call AssertEqual(&
    message='tef_LaplaH_tef with sin(k*pi*X)*cos(l*pi*Y)/2',      &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaH_tef(tef_zvx(zvx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -1/((l*pi)**2+(m*pi)**2)*sin(l*pi*zvx_X) * cos(m*pi*zvx_Y)/2.0D0
  call AssertEqual(&
    message='tef_LaplaHInv_tef with sin(k*pi*X)*cos(l*pi*Y)/2',   &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaHInv_tef(tef_zvx(zvx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Data = cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z)

  zvx_Deriv = - l*pi*sin(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z)
  call AssertEqual(&
    message='tef_Dx_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dx_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = m*pi*cos(l*pi*zvx_X) * cos(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z)
  call AssertEqual(&
    message='tef_Dy_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dy_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(12*zvx_Z**2-3)
  call AssertEqual(&
    message='tef_Dz_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Dz_tef(tef_zvx(zvx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z) &
       + cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(24*zvx_Z)
  call AssertEqual(&
    message='tef_Lapla_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_Lapla_tef(tef_zvx(zvx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z)
  call AssertEqual(&
    message='tef_LaplaH_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaH_tef(tef_zvx(zvx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zvx_Deriv = -1/((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zvx_X) * sin(m*pi*zvx_Y)*(4*zvx_Z**3-3*zvx_Z)
  call AssertEqual(&
    message='tef_LaplaHInv_tef with cos(k*pi*X)*sin(l*pi*Y)*(4Z**3-3Z)', &
    answer = zvx_Deriv,                                           &
    check = zvx_tef(tef_LaplaHInv_tef(tef_zvx(zvx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)
  
  call MessageNotify('M','tee_mpi_module_deriv_test', &
       'tee_mpi_module derivateiv functions tests succeeded!')

end program tee_mpi_module_deriv_test

