!------------------------------------------------------------------------
! Copyright (c) 2005-2011 SPMODEL Development Group. All rights reserved.
!------------------------------------------------------------------------
!
!表題  ee_module テストプログラム (微分計算)
!
!履歴  2005/07/19  竹広真一
!      2007/11/09  竹広真一  エラーメッセージ追加
!      2011/02/15  佐々木洋平 dc_test を使うように変更
!
program ee_test_derivative

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

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

 !---- 変数 ----
  real(8)            :: yx_Data(0:jm-1,0:im-1)    ! 格子データ
  real(8)            :: yx_Deriv(0:jm-1,0:im-1)   ! 格子データ

  integer            :: k=2, l=3

 !---- 座標変数など ----
  real(8), parameter :: xmin = -1.0d0, xmax=1.0d0
  real(8), parameter :: ymin = -1.0d0, ymax=1.0d0

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

  real(8), parameter :: pi=3.1415926535897932385D0

  call MessageNotify('M','ee_test_derivative', &
       'ee_module derivative function tests')

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

 !------------------- 初期値設定 ----------------------
!!$  write(6,*) '  Input wavenumbers of the grid data, k and l :'
!!$  read(5,*) k,l
!!$  write(6,*) '  k,l = ', k,l

  yx_Data = sin(k*pi*yx_X) * sin(l*pi*yx_Y)

  yx_Deriv = k*pi*cos(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dx_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dx(sin(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = l*pi*sin(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dy_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dy(sin(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = -((k*pi)**2 + (l*pi)**2) * sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Lapla_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Lapla(sin(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = -1.0/((k*pi)**2 + (l*pi)**2) * sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_LaplaInv_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'LaplaInv(sin(k*pi*X)*sin(l*pi*Y))')

  yx_Data = cos(k*pi*yx_X) * cos(l*pi*yx_Y)

  yx_Deriv = -k*pi*sin(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dx_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dx(cos(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -l*pi*cos(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dy_ee(ee_yx(yx_Data))),&
    yx_Deriv, 'Dy(cos(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -((k*pi)**2 + (l*pi)**2) * cos(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Lapla_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Lapla(cos(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -1.0/((k*pi)**2 + (l*pi)**2) * cos(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_LaplaInv_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'LaplaInv(cos(k*pi*X)*cos(l*pi*Y))')

  yx_Data = sin(k*pi*yx_X) * cos(l*pi*yx_Y)

  yx_Deriv = k*pi*cos(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dx_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dx(sin(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -l*pi*sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dy_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dy(sin(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -((k*pi)**2 + (l*pi)**2) * sin(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Lapla_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Lapla(sin(k*pi*X)*cos(l*pi*Y))')

  yx_Deriv = -1.0/((k*pi)**2 + (l*pi)**2) * sin(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_LaplaInv_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'LaplaInv(sin(k*pi*X)*cos(l*pi*Y))')

  yx_Data = cos(k*pi*yx_X) * sin(l*pi*yx_Y)

  yx_Deriv = -k*pi*sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dx_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dx(cos(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = l*pi*cos(k*pi*yx_X) * cos(l*pi*yx_Y)
  call check2d(yx_ee(ee_Dy_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Dy(cos(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = -((k*pi)**2 + (l*pi)**2) * cos(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_Lapla_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'Lapla(cos(k*pi*X)*sin(l*pi*Y))')

  yx_Deriv = -1.0/((k*pi)**2 + (l*pi)**2) * cos(k*pi*yx_X) * sin(l*pi*yx_Y)
  call check2d(yx_ee(ee_LaplaInv_ee(ee_yx(yx_Data))), &
    yx_Deriv, 'LaplaInv(cos(k*pi*X)*sin(l*pi*Y))')

  call MessageNotify('M','ee_test_derivative', &
       'ee_module derivative function tests succeeded!')

contains

  subroutine check2d(var,sol,funcname)
    real(8) :: var(:,:)                ! 判定する配列
    real(8) :: sol(:,:)                ! 解析解
    character(len=*) :: funcname       ! 関数名

    call AssertEqual(     &
      message = funcname, &
      answer = sol,       &
      check = var,        &
      significant_digits = check_digits, ignore_digits = ignore     )

  end subroutine check2d

end program ee_test_derivative
