!----------------------------------------------------------------------
!     Copyright (c) 2018-2021 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module テストプログラム :: 補間関数のテスト
!
!履歴  2018/06/09  竹広真一
!      2020/11/11  竹広真一  セクター計算オプション導入
!      2021/02/18  竹広真一  a_Interpolate_array100_ut のテスト
!
program ut_mpi_module_interpolate_mint_test

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

!!$  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
!!$  integer,parameter  :: mint=1               ! 経度方向対称性
  integer,parameter  :: im=16, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: mint=2               ! 経度方向対称性
  integer,parameter  :: nm=10, lm=16         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

  integer :: npv = 2
  integer :: iproc, np, ierr

  real(8), allocatable  ::  ut_data(:,:)
  real(8), allocatable  ::  xvh_data(:,:,:)

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

  real(8) :: Alon
  real(8) :: Alat
  real(8) :: Arad
  real(8) :: data_sol

  integer, parameter :: ndata=3
  real(8) :: Alon_ary(ndata)
  real(8) :: data_sol_ary(ndata)

  real(8) :: pi

  !---------------- MPI スタート ---------------------
  !call get_ENV_INT('NPV',npv)

  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  call MessageNotify('M','ut_mpi_module_interpolate_mint_test', &
                         'ut_mpi_module_mint interpolate tests')

  pi = atan(1.0D0)*4.0D0

  Alon = pi/3
  Alat = pi/3
  Arad = 0.75

  Alon_ary = (/pi/4,pi/3,pi/2/)
  
!!$  Alon =pi/4
!!$  Alat =pi/5
!!$  Arad =0.99
!!$  Alon = 3*pi/5
!!$  Alat = -pi/7
!!$  Arad = 1.333

  call ut_mpi_Initial( im, jm, km, nm, lm, ri, ro, npv=npv, mint=mint )

  allocate(ut_data(nmc,0:lm))
  allocate(xvh_data(0:im-1,1:jc,kc))

  !---- Interpolaate_array000_ut のテスト ----
  xvh_data = sqrt(3.0D0/2)*cos(xvh_Lat)**2*cos(2*xvh_Lon) & ! Y_2^2
            * (xvh_Rad-ri) *  (xvh_Rad-ro)

  data_sol = sqrt(3.0D0/2)*cos(ALat)**2*cos(2*ALon) * (ARad-ri) *  (ARad-ro)

  ut_data = ut_xvh(xvh_data)

  call AssertEqual(&
       message='Interpolate_ut Y_2^2',                               &
       answer = data_sol,                                            &
       check = Interpolate_ut(ut_data,alon,alat,arad),               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  !---- a_Interpolaate_array100_ut のテスト ----
  xvh_data = cos(xvh_Lat)**2*sin(xvh_Lat)*cos(2*xvh_Lon) & ! Y_3^2
            * (xvh_Rad-ri) *  (xvh_Rad-ro)

  data_sol_ary = cos(ALat)**2*sin(ALat)*cos(2*ALon_ary)*(ARad-ri)*(ARad-ro)

!!$  xvh_data = sqrt(3.0D0/2)*cos(xvh_Lat)**4*cos(4*xvh_Lon) & ! Y_4^2
!!$            * (xvh_Rad-ri) *  (xvh_Rad-ro)
!!$
!!$  data_sol_ary = sqrt(3.0D0/2)*cos(ALat)**4*cos(4*ALon_ary)*(ARad-ri)*(ARad-ro)

  ut_data = ut_xvh(xvh_data)

  call AssertEqual(&
       message='a_Interpolate_array100_ut Y_3^2',                    &
       answer = data_sol_ary,                                        &
       check = Interpolate_ut(ut_data,alon_ary,alat,arad),           &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','ut_mpi_module_interpolate_test',&
                         'ut_mpi_module_mint interpolate function test succeeded!')

    !------ MPIの終了 ------
  call MPI_FINALIZE(IERR)

end program ut_mpi_module_interpolate_mint_test
