!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wsc_mpi_module_supack テストプログラム :: 補間関数のテスト
!
!履歴  2017/05/31  竹広真一   wt_mpi_module_test_interplolate.f90 より改造
!
program wsc_mpi_module_supack_interpol

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

  integer, parameter :: im=128, jm=64, km=16, nm=42, lm=16
  real(8), parameter :: ri = 0.5, ro= 1.5

  real(8), allocatable  ::  wc_data(:,:)
  real(8), allocatable  ::  ws_data(:,:)  
  real(8), allocatable  ::  xvz_data(:,:,:)

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

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

  integer :: iproc, np, ierr

 !---------------- MPI スタート ---------------------
  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','wsc_mpi_module_supack_interpolate_test', &
                         'wsc_mpi_module_supack interpolate tests') 

  pi = atan(1.0D0)*4.0D0

  Alon = pi/3
  Alat = pi/3
  Arad = 0.75
!!$  Alon =pi/4
!!$  Alat =pi/5
!!$  Arad =0.99
!!$  Alon = 3*pi/5
!!$  Alat = -pi/7
!!$  Arad = 1.333 

  call wsc_mpi_Initial( im, jm, km, nm, lm, ri, ro )

  allocate(wc_data(nc,0:lm))
  allocate(ws_data(nc,lm))  
  allocate(xvz_data(0:im-1,1:jc,0:km))

  !---- Interpolaate_wc のテスト ----
  xvz_data = sqrt(3.0D0/2)*cos(xvz_Lat)*cos(xvz_Lon) & ! Y_1^1 cos(z)
            * cos(PI*(xvz_Rad-Ri)/(Ro-Ri))

  data_sol = sqrt(3.0D0/2)*cos(ALat)*cos(ALon) * cos(PI*(ARad-Ri)/(Ro-Ri))


  wc_data = wc_xvz(xvz_data)

  call AssertEqual(&
       message='Interpolate_wt Y_1^1 cos(z)',                        &
       answer = data_sol,                                            &
       check = Interpolate_wc(wc_data,alon,alat,arad),               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  !---- Interpolaate_ws のテスト ----
  xvz_data = sqrt(3.0D0/2)*cos(xvz_Lat)*sin(xvz_Lon) & ! Y_1^-1 sin(z)
            * sin(PI*(xvz_Rad-Ri)/(Ro-Ri))

  data_sol = sqrt(3.0D0/2)*cos(ALat)*sin(ALon) * sin(PI*(ARad-Ri)/(Ro-Ri))


  ws_data = ws_xvz(xvz_data)

  call AssertEqual(&
       message='Interpolate_wt Y_1^-1 sin(z)',                       &
       answer = data_sol,                                            &
       check = Interpolate_ws(ws_data,alon,alat,arad),               &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  call MessageNotify('M','wsc_mpi_module_supack_interpolate_test',&
                         'wsc_mpi_module_supack interpolate function test succeeded!') 

 !------ MPIの終了 ------

  call MPI_FINALIZE(IERR)
  
end program wsc_mpi_module_supack_interpol
