!--------------------------------------------------------------------------
! Copyright (c) 2023 SPMODEL Development Group. All rights reserved.!
!--------------------------------------------------------------------------
!
!表題  et_mpi_module テストプログラム
!      Laplacian 逆解き問題
!
!履歴  2023/03/05  竹広真一
!
!
program et_mpi_module_test_LaplaInv

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

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

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

  real(8), allocatable :: vx_A(:,:)     ! 格子データ
  real(8), allocatable :: vx_A_sol(:,:)  ! 格子データ
  real(8), allocatable :: ft_A(:,:)     ! スペクトルデータ
  real(8), allocatable :: fa_values(:,:)

  real(8), parameter :: pi=3.1415926535897932385D0
  integer            :: np, ip, ierr
  integer            :: k=2

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


  call MessageNotify('M','et_mpi_module_test_LaplaInv', &
       'et_mpi_module LaplaInv function 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_A(jc(ip),0:im-1))          ! 格子データ
  allocate(vx_A_sol(jc(ip),0:im-1))      ! 格子データ
  allocate(ft_A(2*kc(ip),0:lm))          ! 格子データ
  allocate(fa_values(2*kc(ip),2))          ! 格子データ
  
  vx_A = sin(k*pi*vx_X)*sin(pi*vx_Y)
  vx_A_sol = -sin(k*pi*vx_X)*sin(pi*vx_Y)/((k*pi)**2 + pi**2)
  ft_A = ft_vx(vx_A)
  fa_values = 0.0D0
  vx_A = vx_ft(ft_LaplaInv_ft(ft_A,fa_values))

  call AssertEqual(                                           &
      message='Test of et_LaplaInv',                            &
      answer = vx_A_sol,                                        &
      check = vx_A,                                             &
      significant_digits = check_digits, ignore_digits = ignore &
      )

  call MPI_FINALIZE(IERR)
 
  call MessageNotify('M','Test of et_LaplaInv', &
    'et_mpi_module Inverse Laplacian tests OK!')

end program et_mpi_module_test_LaplaInv

