!-------------------------------------------------------------------------
! Copyright (c) 2007-2012 SPMODEL Development Group. All rights reserved.!
!-------------------------------------------------------------------------
!
!表題  et_module テストプログラム
!      渦度から流線を求める関数
!
!履歴  2007/11/20  竹広真一
!      2011/03/06  佐々木洋平 dc_test を使うように修正
!      2012/07/06  竹広真一   RF, FR, RR 条件のテスト追加
!
program et_test_Vor2StrmGrid

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

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

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

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

  real(8)            :: yx_Vor(0:jm,0:im-1)     ! 格子データ
  real(8)            :: yx_Psi(0:jm,0:im-1)     ! 格子データ
  real(8)            :: yx_Vor_sol(0:jm,0:im-1) ! 格子データ
  real(8)            :: yx_Psi_sol(0:jm,0:im-1) ! 格子データ
  real(8)            :: yx_dPsidy(0:jm,0:im-1)  ! 格子データ
  real(8)            :: yx_d2Psidy2(0:jm,0:im-1)! 格子データ
  real(8)            :: x_Zero(0:im-1) = 0.0D0  ! 格子データ

  integer            :: k=2

  call MessageNotify('M','et_test_Vor2StrmGrid', &
       'et_module Stream function calculation from vorticity tests')

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

!----- et_Vor2Strm (FF) ------
  yx_Vor_sol = sin(k*pi*yx_X)*sin(pi*yx_Y)
  yx_Psi_sol = -sin(k*pi*yx_X)*sin(pi*yx_Y)/((k*pi)**2+pi**2)
  yx_Psi = yx_ey(ey_Vor2Strm_ey(ey_yx(yx_Vor_sol), &
                                      cond='FF',new=.true.))
  yx_Vor = yx_et(et_Lapla_et(et_yx(yx_Psi)))
  yx_dPsidy = yx_et(et_Dy_et(et_yx(yx_Psi)))
  yx_d2Psidy2 = yx_et(et_Dy_et(et_Dy_et(et_yx(yx_Psi))))

  call AssertEqual(&
    message='ey_Vor2Strm_ey (FF)',                                &
    answer =yx_Psi_sol,                                           &
    check = yx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FF, vorticity)',                     &
    answer =yx_Vor_sol(2:jm-2,:),                                 &
    check = yx_Vor(2:jm-2,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FF, Top)',                           &
    answer =x_Zero,                                               &
    check = yx_d2Psidy2(0,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FF, Bottom)',                        &
    answer =x_Zero,                                               &
    check = yx_d2Psidy2(jm,:),                                    &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

!----- et_Vor2Strm (RF) ------
  yx_Psi_sol = sin(k*pi*yx_X)*(yx_Y-ymin)**3*(yx_Y-ymax)**2
  yx_Vor_sol = yx_et(et_Lapla_et(et_yx(yx_Psi_sol)))

  yx_Psi = yx_ey(ey_Vor2Strm_ey(ey_yx(yx_Vor_sol), &
                                      cond='RF',new=.true.))
  yx_Vor = yx_et(et_Lapla_et(et_yx(yx_Psi)))
  yx_dPsidy = yx_et(et_Dy_et(et_yx(yx_Psi)))
  yx_d2Psidy2 = yx_et(et_Dy_et(et_Dy_et(et_yx(yx_Psi))))

  call AssertEqual(&
    message='ey_Vor2Strm_ey (RF)',                                &
    answer =yx_Psi_sol,                                           &
    check = yx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RF, vorticity)',                     &
    answer =yx_Vor_sol(2:jm-2,:),                                 &
    check = yx_Vor(2:jm-2,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RF, Top)',                           &
    answer =x_Zero,                                               &
    check = yx_dPsidy(0,:),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RF, Bottom)',                        &
    answer =x_Zero,                                               &
    check = yx_d2Psidy2(jm,:),                                    &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

!----- et_Vor2Strm (FR) ------
  yx_Psi_sol = sin(k*pi*yx_X)*(yx_Y-ymin)**2*(yx_Y-ymax)**3
  yx_Vor_sol = yx_et(et_Lapla_et(et_yx(yx_Psi_sol)))
  yx_Psi = yx_ey(ey_Vor2Strm_ey(ey_yx(yx_Vor_sol), &
                                      cond='FR',new=.true.))
  yx_Vor = yx_et(et_Lapla_et(et_yx(yx_Psi)))
  yx_dPsidy = yx_et(et_Dy_et(et_yx(yx_Psi)))
  yx_d2Psidy2 = yx_et(et_Dy_et(et_Dy_et(et_yx(yx_Psi))))

  call AssertEqual(&
    message='ey_Vor2Strm_ey (FR)',                                &
    answer =yx_Psi_sol,                                           &
    check = yx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FR, vorticity)',                     &
    answer =yx_Vor_sol(2:jm-2,:),                                 &
    check = yx_Vor(2:jm-2,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FR, Top)',                           &
    answer =x_Zero,                                               &
    check = yx_d2Psidy2(0,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (FR, Bottom)',                        &
    answer =x_Zero,                                               &
    check = yx_dPsidy(jm,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

!----- et_Vor2Strm (RR) ------
  yx_Psi_sol = sin(k*pi*yx_X)*(yx_Y-ymin)**2*(yx_Y-ymax)**2
  yx_Vor_sol = yx_et(et_Lapla_et(et_yx(yx_Psi_sol)))
  yx_Psi = yx_ey(ey_Vor2Strm_ey(ey_yx(yx_Vor_sol), &
                                      cond='RR',new=.true.))
  yx_Vor = yx_et(et_Lapla_et(et_yx(yx_Psi)))
  yx_dPsidy = yx_et(et_Dy_et(et_yx(yx_Psi)))
  yx_d2Psidy2 = yx_et(et_Dy_et(et_Dy_et(et_yx(yx_Psi))))

  call AssertEqual(&
    message='ey_Vor2Strm_ey (RR)',                                &
    answer =yx_Psi_sol,                                           &
    check = yx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RR, vorticity)',                     &
    answer =yx_Vor_sol(2:jm-2,:),                                 &
    check = yx_Vor(2:jm-2,:),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RR, Top)',                           &
    answer =x_Zero,                                               &
    check = yx_dPsidy(0,:),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='ey_Vor2Strm_ey (RR, Bottom)',                        &
    answer =x_Zero,                                               &
    check = yx_dPsidy(jm,:),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','et_test_Vor2StrmGrid', &
       'et_module Stream function calculation from vorticity tests OK!')

end program et_test_Vor2StrmGrid
