!----------------------------------------------------------------------
!     Copyright (c) 2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_interpolate_module テストプログラム :: 補間関数のテスト
!
!履歴  2020/08/04  竹広真一
!
program ua_interpol_mpi_module

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

  integer, parameter :: im=32, jm=16, nm=10, km=3, npv=2

  real(8), allocatable :: pva_data(:,:,:)
  real(8), allocatable :: ua_data(:,:)
  real(8), allocatable :: wb_data(:,:)
  real(8), allocatable :: b_data_sol(:)

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

  real(8) :: Alon
  real(8) :: Alat
  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)

  pi = atan(1.0D0)*4.0D0
  Alon = pi/5 ; Alat = pi/3

  call ua_mpi_Initial( nm, im, jm, km, npv=npv )

  call MessageNotify('M','ua_Interpolate00_mpi_module_test', &
                         'ua_interpolate_module interpolate (00) tests')

  allocate(pva_data(ic,jc,km))
  allocate(ua_data(nmc,km))
  allocate(wb_data(nc,kc))
  allocate(b_data_sol(kc))

  !---- Y_1^* のテスト ----
  pva_data(:,:,1) = sqrt(3.0D0)*sin(pv_Lat)                ! Y_1^0
  pva_data(:,:,2) = sqrt(3.0D0/2)*cos(pv_Lat)*cos(pv_Lon)  ! Y_1^1
  pva_data(:,:,3) = -sqrt(3.0D0)*cos(pv_Lat)*sin(pv_Lon)   ! Y_1^{-1}

  if ( kp_kl(1) /= 0 ) b_data_sol(kp_kl(1)) = sqrt(3.0D0)*sin(ALat)
  if ( kp_kl(2) /= 0 ) b_data_sol(kp_kl(2)) = sqrt(3.0D0/2)*cos(ALat)*cos(ALon)
  if ( kp_kl(3) /= 0 ) b_data_sol(kp_kl(3)) = -sqrt(3.0D0)*cos(ALat)*sin(ALon)

  ua_data = ua_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_ua with Y_1^*',                        &
    answer = b_Interpolate_ua(ua_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  wb_data = wb_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_wb with Y_1^*',                        &
    answer = b_Interpolate_wb(wb_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  pva_data(:,:,1) = sqrt(5.0D0)*(3.0/2*sin(pv_Lat)**2-1/2.0)    !Y_2^0
  pva_data(:,:,2) = sqrt(5.0D0/6)*3.0*sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon)!Y_2^1
  pva_data(:,:,3) = -sqrt(5.0D0/24)*3.0*cos(pv_Lat)**2*sin(2*pv_Lon)    ! Y_2^-2

  if ( kp_kl(1) /= 0 ) b_data_sol(kp_kl(1)) = sqrt(5.0D0)*(3.0/2*sin(alat)**2-1.0/2)
  if ( kp_kl(2) /= 0 ) b_data_sol(kp_kl(2)) = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * cos(ALon)
  if ( kp_kl(3) /= 0 ) b_data_sol(kp_kl(3)) = -sqrt(5.0D0/24)*3.0*cos(ALat)**2*sin(2*ALon)

  ua_data = ua_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_ua with Y_2^*',                        &
    answer = b_Interpolate_ua(ua_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  wb_data = wb_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_wb with Y_2^*',                        &
    answer = b_Interpolate_wb(wb_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的な関数のテスト ----
  pva_data(:,:,1) = cos(2*pv_Lon-pi/3) &
          *(sin(pv_Lat)-1)**2*(sin(pv_Lat)-0.5)*(sin(pv_Lat)+1)
  pva_data(:,:,2) = sin(2*pv_Lon-pi/3)*(sin(pv_Lat)-1)**2*(sin(pv_Lat)+1)**2
  pva_data(:,:,3) = sin(2*pv_Lon+pi/3)*(sin(pv_Lat)-1)**3*(sin(pv_Lat)+1)

  if ( kp_kl(1) /= 0 ) b_data_sol(kp_kl(1)) = cos(2*ALon-pi/3) &
       *(sin(ALat)-1)**2*(sin(ALat)-0.5)*(sin(ALat)+1)
  if ( kp_kl(2) /= 0 ) b_data_sol(kp_kl(2)) = sin(2*aLon-pi/3)*(sin(aLat)-1)**2*(sin(aLat)+1)**2
  if ( kp_kl(3) /= 0 ) b_data_sol(kp_kl(3)) = sin(2*aLon+pi/3)*(sin(aLat)-1)**3*(sin(aLat)+1)

  ua_data = ua_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_ua with a general function',           &
    answer = b_Interpolate_ua(ua_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  wb_data = wb_pva(pva_data)
  call AssertEqual(&
    message='b_Interpolate_wb with a general function',           &
    answer = b_Interpolate_wb(wb_data,alon,alat),                 &
    check =  b_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ub_Interpolate_mpi_module_test', &
       'ub_interpolate_module interpolate(00) tests succeeded')

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

  call MPI_FINALIZE(IERR)

end program ua_interpol_mpi_module
