!------------------------------------------------------------------------
! Copyright (c) 2023 SPMODEL Development Group. All rights reserved.
!------------------------------------------------------------------------
!
!表題  et_mpi_module テストプログラム (正逆変換)
!
!履歴  2023/03/06  竹広真一
!
program et_mpi_module_test_base2

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

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

 !---- 変数 ----
  real(DP), allocatable  :: vx_Data(:,:)         ! 格子データ
  real(DP), allocatable  :: ft_Data(:,:)         ! スペクトルデータ
!!$  real(DP), allocatable  :: ft_Data1(:,:)        ! スペクトルデータ
!!$
!!$  real(DP), allocatable  :: fy_Data(:,:)         ! スペクトルデータ
!!$  real(DP), allocatable  :: fy_Data1(:,:)         ! スペクトルデータ
  real(DP), allocatable  :: vf_Data(:,:)         ! スペクトルデー
!!$  real(DP), allocatable  :: ve_Data1(:,:)         ! スペクトルデー

  real(DP), allocatable  :: vx_Xi(:,:)           ! 格子データ
  real(DP), allocatable  :: y_Xi(:)              ! 格子データ

  integer            :: k=2,l=5
  integer            :: np, ip, ierr
  integer            :: iip

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

  real(DP), parameter :: pi=3.1415926535897932385D0

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


  call MessageNotify('M','et_mpi_module_test_base2', &
       'et_mpi_module basic 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_Data(js(ip):je(ip),0:im-1))
  allocate(ft_Data(2*kc(ip),0:lm))
!!$  allocate(ft_Data1(2*kc(ip),0:lm))
  allocate(vf_Data(js(ip):je(ip),2*kc(ip)))
!!$  allocate(ve_Data1(js(ip):je(ip),-km:km))
!!$  allocate(fy_Data(2*kc(ip),0:jm))
!!$  allocate(fy_Data1(2*kc(ip),0:jm))

  allocate(vx_Xi(js(ip):je(ip),0:im-1))
  allocate(y_Xi(0:jm))

  vx_Xi = (vx_Y - (ymin+ymax)/2 )*2/(ymax-ymin)
  y_Xi = (Y_Y - (ymin+ymax)/2 )*2/(ymax-ymin)

  !------------------- 初期値設定 ----------------------
  vx_Data = cos(k*pi*vx_X) * vx_xi

  ft_Data = 0.0D0
  if ( kf_k(k) > 0 ) then
     ft_Data(kf_k(k),1) = 0.5
  endif

  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform cos(k*pi*X)*T_1(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform cos(k*pi*X)*T_1(Y)')

  vx_Data = sin(k*pi*vx_X) * vx_xi

  ft_Data = 0.0D0
  if ( kf_k(-k) > 0 ) then
     ft_Data(kf_k(-k),1) = -0.5D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform sin(k*pi*X)*T_1(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform sin(k*pi*X)*T_1(Y)')

  vx_Data = cos(l*pi*vx_X) * (2*vx_Xi**2-1.0D0)
  ft_Data = 0.0
  if ( kf_k(l) > 0 ) then
     ft_Data(kf_k(l),2) = 0.5D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform cos(k*pi*X)*T_2(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform cos(k*pi*X)*T_2(Y)')

  vx_Data = sin(l*pi*vx_X) * (2*vx_Xi**2-1.0D0)
  ft_Data = 0.0
  if ( kf_k(l) > 0 ) then
     ft_Data(kf_k(-l),2) = -0.5D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform sin(k*pi*X)*T_2(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform sin(k*pi*X)*T_2(Y)')

  !------ l=0 モード ------
  vx_Data = sin(k*pi*vx_X)/2
  ft_Data = 0.0
  if ( kf_k(-k) > 0 ) then
     ft_Data(kf_k(-k),0) = -0.5
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform sin(k*pi*X)/2')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform sin(k*pi*X)/2')

  vx_Data = cos(k*pi*vx_X)/2
  ft_Data = 0.0
  if ( kf_k(k) > 0 ) then
     ft_Data(kf_k(k),0) = 0.5
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform cos(k*pi*X)/2')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform cos(k*pi*X)/2')

  !------ k=0 モード ------
  vx_Data = vx_Xi
  ft_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ft_Data(kf_k(0),1) = 1.0D0
     ft_Data(2*kc(ip)-kf_k(0)+1,1) = 1.0D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform T_1(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform T_1(Y)')

  vx_Data = 2*vx_Xi**2-1.0D0
  ft_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ft_Data(kf_k(0),2) = 1.0D0
     ft_Data(2*kc(ip)-kf_k(0)+1,2) = 1.0D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform T_2(Y)')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform T_2(Y)')

  !------ k=l=0 モード ------
  vx_Data = 1.0D0/2.0D0
  ft_Data = 0.0
  if ( kf_k(0) > 0 ) then
     ft_Data(kf_k(0),0) = 1.0
     ft_Data(2*kc(ip)-kf_k(0)+1,0) = 1.0D0
  endif
  call check(transpose(af_ae(transpose(at_ay(ay_av(transpose(ae_ax(vx_data))))))), ft_Data, &
       'Redundant Transform constatn field')
  call check(transpose(av_ay(transpose(ax_ae(ae_af(transpose(ay_at(ft_data))))))), vx_Data, &
       'Redundant Inverse transform constatn field')

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','et_mpi_test_base2', &
       'et_mpi_module basic function tests succeeded!')
contains

  subroutine check(var,sol,funcname)
    real(DP) :: var(:,:)                ! 判定する配列
    real(DP) :: sol(:,:)                ! 誤差
    character(len=*) :: funcname

    call AssertEqual(     &
      message = funcname, &
      answer = sol,       &
      check = var,        &
      significant_digits = check_digits, ignore_digits = ignore     )

  end subroutine check

end program et_mpi_module_test_base2
