!------------------------------------------------------------------------
! Copyright (c) 2007-2011 SPMODEL Development Group. All rights reserved.
!------------------------------------------------------------------------
!
!表題  ee_module テストプログラム (パワースペクトル計算)
!
!履歴  2007/11/09  竹広真一
!      2008/05/10  竹広真一  解像度複数設定チェック
!      2011/02/15  佐々木洋平 dc_test を使うように変更
!
!
program ee_test_spectrum2

  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)

  integer, parameter :: im2=64, jm2=64            ! 格子点の設定(X,Y)
  integer, parameter :: km2=21, lm2=21            ! 切断波数の設定(X,Y)

 !---- 変数 ----
  real(8)            :: yx_Data(0:jm-1,0:im-1)    ! 格子データ
  real(8)            :: ee_Data(-lm:lm,-km:km)    ! スペクトルデータ

  real(8)            :: yx_Data2(0:jm2-1,0:im2-1)      ! 格子データ
  real(8)            :: ee_Data2(-lm2:lm2,-km2:km2)    ! スペクトルデータ

  integer            :: k=2, l=3

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

  real(8), parameter :: pi=3.1415926535897932385D0
  ! 判定誤差設定
  integer, parameter :: check_digits = 10
  integer, parameter :: ignore = -11

  integer            :: id1, id2

  call MessageNotify('M','ee_test_spectrum', &
       'ee_module spectrum function tests')

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

 !------------------- 初期値設定 ----------------------
!!  write(6,*) '*** Test of ee_module : derivative function check.'
!!  write(6,*) '  The result will be printed '
!!  write(6,*) '  only when the error is larger than ', eps
!!  write(6,*)

!!$  write(6,*) '  Input wavenumbers of the grid data, k and l :'
!!$  read(5,*) k,l
!!  write(6,*) '  k,l = ', k,l

  ! id1
  write(*,*) 'for id1'
  call ee_ChangeResolutionDomain(id1)

  yx_Data = sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  ee_Data = 0.0d0
  ee_Data(l,k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data(-l,k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data(l,-k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data(-l,-k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2


  call AssertEqual(                                      &
    message = 'Energy spectrum sin(k*pi*X)*sin(l*pi*Y)', &
    answer = ee_Data,                                    &
    check = ee_EnergyFromStreamfunc_ee(ee_yx(yx_Data)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  call AssertEqual(                                         &
    message = 'total energy sin(k*pi*X)*sin(l*pi*Y)',       &
    answer = ((k*pi)**2 + (l*pi)**2)/2,                     &
    check = sum(ee_EnergyFromStreamfunc_ee(ee_yx(yx_Data))) &
            *(xmax-xmin)*(ymax-ymin),                       &
    significant_digits = check_digits, ignore_digits = ignore     )

  ee_Data = 0.0d0
  ee_Data(l,k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data(-l,k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data(l,-k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data(-l,-k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2

  call AssertEqual(                                         &
    message = 'Enstrophy spectrum sin(k*pi*X)*sin(l*pi*Y)', &
    answer = ee_Data,                                       &
    check = ee_EnstrophyFromStreamfunc_ee(ee_yx(yx_Data)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  call AssertEqual(                                            &
    message = 'total enstrophy sin(k*pi*X)*sin(l*pi*Y)',       &
    answer = ((k*pi)**2 + (l*pi)**2)**2/2,                     &
    check = sum(ee_EnstrophyFromStreamfunc_ee(ee_yx(yx_Data))) &
            *(xmax-xmin)*(ymax-ymin),                          &
    significant_digits = check_digits, ignore_digits = ignore     )

  ! id2
  write(*,*) 'for id2'
  call ee_ChangeResolutionDomain(id2)

  yx_Data2 = sin(k*pi*yx_X) * sin(l*pi*yx_Y)
  ee_Data2 = 0.0d0
  ee_Data2(l,k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data2(-l,k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data2(l,-k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2
  ee_Data2(-l,-k) = ((k*pi)**2 + (l*pi)**2)*0.25**2/2

  call AssertEqual(                                       &
    message = 'Energy spectrum sin(k*pi*X)*sin(l*pi*Y)',  &
    answer = ee_Data2,                                    &
    check = ee_EnergyFromStreamfunc_ee(ee_yx(yx_Data2)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  call AssertEqual(                                          &
    message = 'total energy sin(k*pi*X)*sin(l*pi*Y)',        &
    answer = ((k*pi)**2 + (l*pi)**2)/2,                      &
    check = sum(ee_EnergyFromStreamfunc_ee(ee_yx(yx_Data2))) &
            *(xmax-xmin)*(ymax-ymin),                        &
    significant_digits = check_digits, ignore_digits = ignore     )


  ee_Data2 = 0.0d0
  ee_Data2(l,k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data2(-l,k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data2(l,-k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2
  ee_Data2(-l,-k) = ((k*pi)**2 + (l*pi)**2)**2*0.25**2/2

  call AssertEqual(                                          &
    message = 'Enstrophy spectrum sin(k*pi*X)*sin(l*pi*Y)',  &
    answer = ee_Data2,                                       &
    check = ee_EnstrophyFromStreamfunc_ee(ee_yx(yx_Data2)),  &
    significant_digits = check_digits, ignore_digits = ignore     )

  call AssertEqual(                                             &
    message = 'total enstrophy sin(k*pi*X)*sin(l*pi*Y)',        &
    answer = ((k*pi)**2 + (l*pi)**2)**2/2,                      &
    check = sum(ee_EnstrophyFromStreamfunc_ee(ee_yx(yx_Data2))) &
            *(xmax-xmin)*(ymax-ymin),                           &
    significant_digits = check_digits, ignore_digits = ignore     )


  call MessageNotify('M','ee_test_spectrum', &
       'ee_module spectrum function tests succeeded!')

end program ee_test_spectrum2
