!--------------------------------------------------------------------------
! Copyright (c) 2023 SPMODEL Development Group. All rights reserved.!
!--------------------------------------------------------------------------
!
!表題  et_mpi_module テストプログラム
!      境界値問題
!
!履歴  2023/03/04  竹広真一  
!      
!
program et_mpi_module_test_boundaries

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

  !---- 空間解像度設定 ----
  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=21              ! 切断波数の設定(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
  real(8), parameter :: eps = 1.0d-10            ! 判定誤差

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

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

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

  call MessageNotify('M','et_mpi_module_test_boudaries', &
       'et_mpi_module subroutine for boundary value problem 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_Asol(jc(ip),0:im-1))       ! 格子データ
  allocate(vx_dAdy(jc(ip),0:im-1))       ! 格子データ
  allocate(ft_A(2*kc(ip),0:lm))          ! 格子データ
  allocate(fa_values(2*kc(ip),2))        ! 格子データ

  !----- ft_Boundaries (DD) ------
  vx_A    = sin(k*pi*vx_X)*sin(pi*vx_Y)
  vx_Asol = sin(k*pi*vx_X)*sin(pi*vx_Y)
  ft_A = ft_vx(vx_A)
  fa_values = 0.0D0

  call ft_Boundaries(ft_A,values=fa_values,cond='DD')
  vx_A = vx_ft(ft_A)

  call AssertEqual(                                             &
      message='Test of ft_boundaries(DD)',                      &
      answer = vx_Asol,                                         &
      check = vx_A,                                             &
      significant_digits = check_digits, ignore_digits = ignore &
      )
 
  !----- et_Boundaries (DN) ------
  vx_A    = sin(k*pi*vx_X)*cos(pi/2*vx_Y)
  vx_Asol = sin(k*pi*vx_X)*cos(pi/2*vx_Y)
  ft_A    = ft_vx(vx_A)

  call ft_Boundaries(ft_A,fa_values,cond='DN')
  vx_A = vx_ft(ft_A)

  call AssertEqual(                                             &
      message='Test of ft_boundaries(DN)',                      &
      answer = vx_Asol,                                         &
      check = vx_A,                                             &
      significant_digits = check_digits, ignore_digits = ignore &
      )

  !----- et_Boundaries (ND) ------
  vx_A    = sin(k*pi*vx_X)*sin(pi/2*vx_Y)
  vx_Asol = sin(k*pi*vx_X)*sin(pi/2*vx_Y)
  ft_A    = ft_vx(vx_A)

  call ft_Boundaries(ft_A,fa_values,cond='ND')
  vx_A = vx_ft(ft_A)

  call AssertEqual(                                             &
      message='Test of ft_boundaries(ND)',                      &
      answer = vx_Asol,                                         &
      check = vx_A,                                             &
      significant_digits = check_digits, ignore_digits = ignore &
      )

  !----- et_Boundaries (NN) ------
  vx_A    = sin(k*pi*vx_X)*cos(pi*vx_Y)
  vx_Asol = sin(k*pi*vx_X)*cos(pi*vx_Y)
  ft_A    = ft_vx(vx_A)

  call ft_Boundaries(ft_A,fa_values,cond='NN')
  vx_A = vx_ft(ft_A)

  call AssertEqual(                                             &
      message='Test of ft_boundaries(NN)',                      &
      answer = vx_Asol,                                         &
      check = vx_A,                                             &
      significant_digits = check_digits, ignore_digits = ignore &
      )

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','et_mpi_test_boudaries', &
       'et_mpi_module subroutine for boundary value problem tests succeeded!')

end program et_mpi_module_test_boundaries

