!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  scee_module_fftj テストプログラム (微分演算)
!
!履歴  2017/05/27  竹広真一
!
program scee_module_fftj_deriv_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_Data(0:km,0:jm-1,0:im-1)    ! 格子データ
  real(8)            :: zyx_Deriv(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 = 11
  integer, parameter :: ignore = -12

  call MessageNotify('M','scee_module_fftj_deriv_test', &
       'scee_module_fftj derivative function tests')

  !---------------- 座標値の設定 ---------------------
  ! スペクトル初期化
  call scee_initial(im,jm,km,lm,mm,nm,xmin,xmax,ymin,ymax,zmin,zmax)
  
  zyx_Xi = (zyx_Z - zmin)/(zmax-zmin)
  
  !------------------- 関数チェック (sin) ----------------------
  zyx_Data = sin(l*pi*zyx_X) * sin(m*pi*zyx_Y) * sin(PI*zyx_Xi)

  zyx_Deriv = l*pi*cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * sin(PI*zyx_Xi)
  call AssertEqual(&
    message='see_Dx_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)',     &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dx_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = m*pi*sin(l*pi*zyx_X) * cos(m*pi*zyx_Y) * sin(PI*zyx_Xi)
  call AssertEqual(&
    message='see_Dy_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)',     &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dy_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = sin(l*pi*zyx_X) * sin(m*pi*zyx_Y) * PI/(zmax-zmin) * cos(PI*zyx_Xi)
  call AssertEqual(&
    message='cee_Dz_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)',     &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dz_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2+ (PI/(zmax-zmin))**2) &
            * sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(PI*zyx_Xi)
  call AssertEqual(&
    message='see_Lapla_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)',  &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Lapla_see(see_zyx(zyx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1.0D0/((l*pi)**2+(m*pi)**2+ (PI/(zmax-zmin))**2) &
            * sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(PI*zyx_Xi)
  call AssertEqual(&
    message='see_LaplaInv_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)',  &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaInv_see(see_zyx(zyx_Data))),         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2) &
            *sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(PI*zyx_Xi)
  call AssertEqual(&
    message='see_LaplaH_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaH_see(see_zyx(zyx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(PI*zyx_Xi) &
            /((l*pi)**2+(m*pi)**2)
  call AssertEqual(&
    message='see_LaplaH_see with sin(k*pi*X)*sin(l*pi*Y)*sin(Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaHInv_see(see_zyx(zyx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- 関数チェック (cos) ----------------------
  zyx_Data = cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)

  zyx_Deriv = -l*pi*sin(l*pi*zyx_X) *cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_Dx_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)',    &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dx_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -m*pi*cos(l*pi*zyx_X) * sin(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_Dy_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)',    &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dy_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * 2*PI/(zmax-zmin) * sin(2*PI*zyx_Xi)
  call AssertEqual(&
    message='see_Dz_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)',    &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dz_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2+(2*PI/(zmax-zmin))**2) &
       * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_Lapla_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Lapla_cee(cee_zyx(zyx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  
  zyx_Deriv = -1.0D0/((l*pi)**2+(m*pi)**2+(2*PI/(zmax-zmin))**2) &
       * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_LaplaInv_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaInv_cee(cee_zyx(zyx_Data))),         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_LaplaH_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)',&
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaH_cee(cee_zyx(zyx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1.0D0/((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zyx_X) * cos(m*pi*zyx_Y) * cos(2*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_LaplaH_cee with cos(k*pi*X)*cos(l*pi*Y)*cos(2Z)',&
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaHInv_cee(cee_zyx(zyx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !------------------- 関数チェック (cos) ----------------------
  zyx_Data = sin(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0

  zyx_Deriv = l*pi*cos(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_Dx_cee with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dx_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -m*pi*sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_Dy_cee with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dy_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = 0.0D0
  call AssertEqual(&
    message='cee_Dz_cee with sin(k*pi*X)*cos(l*pi*Y)/2',          &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dz_cee(cee_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_Lapla_cee with sin(k*pi*X)*cos(l*pi*Y)/2',       &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Lapla_cee(cee_zyx(zyx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1.0D0/((l*pi)**2+(m*pi)**2)*sin(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_LaplaInv_cee with sin(k*pi*X)*cos(l*pi*Y)/2',    &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaInv_cee(cee_zyx(zyx_Data))),         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2)*sin(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_LaplaH_cee with sin(k*pi*X)*cos(l*pi*Y)/2',      &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaH_cee(cee_zyx(zyx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1/((l*pi)**2+(m*pi)**2)*sin(l*pi*zyx_X) * cos(m*pi*zyx_Y)/2.0D0
  call AssertEqual(&
    message='cee_LaplaHInv_cee with sin(k*pi*X)*cos(l*pi*Y)/2',   &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_LaplaHInv_cee(cee_zyx(zyx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! sin 展開
  zyx_Data = cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)

  zyx_Deriv = - l*pi*sin(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_Dx_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dx_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = m*pi*cos(l*pi*zyx_X) * cos(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_Dy_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)',    &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Dy_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*3*PI/(zmax-zmin)*cos(3*PI*zyx_Xi)
  call AssertEqual(&
    message='cee_Dz_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)',    &
    answer = zyx_Deriv,                                           &
    check = zyx_cee(cee_Dz_see(see_zyx(zyx_Data))),               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2 + (3*PI/(zmax-zmin))**2) &
       * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_Lapla_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_Lapla_see(see_zyx(zyx_Data))),            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1.0D0/((l*pi)**2+(m*pi)**2 + (3*PI/(zmax-zmin))**2) &
       * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_LaplaInv_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaInv_see(see_zyx(zyx_Data))),         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_LaplaH_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)',&
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaH_see(see_zyx(zyx_Data))),           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  zyx_Deriv = -1/((l*pi)**2+(m*pi)**2) &
       * cos(l*pi*zyx_X) * sin(m*pi*zyx_Y)*sin(3*PI*zyx_Xi)
  call AssertEqual(&
    message='see_LaplaHInv_see with cos(k*pi*X)*sin(l*pi*Y)*sin(3Z)', &
    answer = zyx_Deriv,                                           &
    check = zyx_see(see_LaplaHInv_see(see_zyx(zyx_Data))),        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','scee_module_fftj_deriv_test', &
       'scee_module_fftj derivateiv functions tests succeeded!')

end program scee_module_fftj_deriv_test
