!--------------------------------------------------------------------------
! Copyright (c) 2023 SPMODEL Development Group. All rights reserved.!
!--------------------------------------------------------------------------
!
!表題  et_mpi_module テストプログラム
!      渦度から流線を求める関数
!
!履歴  2023/03/04  竹広真一
!
program et_mpi_module_test_Vor2StrmGrid

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use et_mpi_module
  use mpi
  implicit none

  !---- 空間解像度設定 ----
!!$  integer, parameter :: im=64, jm=128            ! 格子点の設定(X,Y)
!!$  integer, parameter :: km=21, lm=128             ! 切断波数の設定(X,Y)
  integer, parameter :: im=64, jm=32            ! 格子点の設定(X,Y)
  integer, parameter :: km=21, lm=32             ! 切断波数の設定(X,Y)
!!$  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 = 7
  integer, parameter :: ignore = -7


  real(8),allocatable :: vx_Vor(:,:)       ! 格子データ
  real(8),allocatable :: vx_Psi(:,:)       ! 格子データ
  real(8),allocatable :: vx_Vor_sol(:,:)   ! 格子データ
  real(8),allocatable :: vx_Psi_sol(:,:)   ! 格子データ
  real(8),allocatable :: x_Zero(:)            ! 格子データ

  integer            :: np, ip, ierr
  integer            :: k=2

  call MessageNotify('M','et_mpi_module_test_Vor2StrmGrid', &
       'et_mpi_module Stream function calculation from vorticity 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 et_mpi_initial(im,jm,km,lm,xmin,xmax,ymin,ymax)    ! スペクトル初期化

  allocate(vx_Psi(jc(ip),0:im-1))
  allocate(vx_Vor(jc(ip),0:im-1))
  allocate(vx_Psi_sol(jc(ip),0:im-1))
  allocate(vx_Vor_sol(jc(ip),0:im-1))
  allocate(x_Zero(0:im-1))

  !----- fy_Vor2Strm_fy (FF) ------
  vx_Psi_sol = -sin(k*pi*vx_X)*sin(pi*vx_Y)/((k*pi)**2+pi**2)
  vx_Vor_sol = sin(k*pi*vx_X)*sin(pi*vx_Y)
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),cond='FF'))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (FF)',                                &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (FF, vorticity)',                     &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (RF) ------
  vx_Psi_sol = sin(k*pi*vx_X)*(vx_Y-ymin)**3*(vx_Y-ymax)**2
  vx_Vor_sol = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi_sol)))

  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),cond='RF',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (RF)',                                &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (RF, vorticity)',                     &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (FR) ------
  vx_Psi_sol = sin(k*pi*vx_X)*(vx_Y-ymin)**2*(vx_Y-ymax)**3
  vx_Vor_sol = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi_sol)))
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),cond='FR',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (FR)',                                &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (FR, vorticity)',                     &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (RR) ------
  vx_Psi_sol = sin(k*pi*vx_X)*(vx_Y-ymin)**2*(vx_Y-ymax)**2
  vx_Vor_sol = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi_sol)))
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),cond='RR',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (RR)',                                &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (RR, vorticity)',                     &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (RF,k=0) ------
  vx_Psi_sol = sin(pi*vx_Y/2)
  vx_Vor_sol = - sin(pi*vx_Y/2)*(pi**2/4)
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),values=(/1.0D0,0.0D0/),cond='RF',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (RF,k=0)',                            &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (RF,k=0, vorticity)',                 &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (FR,k=0) ------
  vx_Psi_sol = cos(pi*vx_Y/2)
  vx_Vor_sol = - cos(pi*vx_Y/2)*(pi**2/4)
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),values=(/0.0D0,1.0D0/),cond='FR',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (FR,k=0)',                            &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (FR,k=0, vorticity)',                 &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- fy_Vor2Strm_fy (RR,k=0) ------
  vx_Psi_sol = cos(pi*vx_Y)
  vx_Vor_sol = - cos(pi*vx_Y)*(pi**2)
  vx_Psi = vx_fy(fy_Vor2Strm_fy(fy_vx(vx_Vor_sol),values=(/-1.0D0,1.0D0/),cond='RR',new=.true.))
  vx_Vor = vx_ft(ft_Lapla_ft(ft_vx(vx_Psi)))

  call AssertEqual(&
    message='fy_Vor2Strm_fy (RR,k=0)',                            &
    answer =vx_Psi_sol,                                           &
    check = vx_Psi,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='fy_Vor2Strm_fy (RR,k=0, vorticity)',                 &
    answer =vx_Vor_sol,                                           &
    check = vx_Vor,                                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MPI_FINALIZE(IERR)

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

end program et_mpi_module_test_Vor2StrmGrid
