!----------------------------------------------------------------------
!     Copyright (c) 2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wb_integrale_module テストプログラム :: 積分・平均関数のテスト
!
!履歴  2020/08/03  竹広真一
!      2020/11/10  竹広真一  セクター計算オプション導入
!
program ua_mpi_module_integral_mint_test

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

!!$  integer, parameter :: im=64, jm=32, nm=21, km=2, npv=2, mint=1
  integer, parameter :: im=32, jm=32, nm=21, km=2, npv=2, mint=2

  real(8), allocatable             ::  a_idata(:)          ! 積分・平均の正解
  real(8), allocatable             ::  pva_data(:,:,:)     ! 元の関数
  real(8), allocatable             ::  pa_idata(:,:)       ! 積分・平均の正解
  real(8), allocatable             ::  va_idata(:,:)       ! 積分・平均の正解

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

  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

  call MessageNotify('M','ua_mpi_module_integral_mint_test', &
                         'ua_mpi_module_mint integral function tests')

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

  allocate(pva_data(ic,jc,km))
  allocate(pa_idata(ic,km))
  allocate(va_idata(jc,km))
  allocate(a_idata(km))

  !---- 積分のテスト(分割領域) ----
  pva_data = 0.0D0
  pva_data(:,:,1) = sin(pv_Lat)**2*(1-cos(pv_Lon)**2)
  pva_data(:,:,2) = sin(pv_Lat)**4*(1-sin(pv_Lon)**2)

  a_idata(1) = 2*pi/3/mint       ! a_IntLonLat_pva
  a_idata(2) = 2*pi/5/mint       ! a_IntLonLat_pva
  call AssertEqual(&
    message='a_IntLonLat_pva',                                    &
    answer = a_IntLonLat_pva(pva_data),                           &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='IntLonLat_pv',                                       &
    answer = IntLonLat_pv(pva_data(:,:,1)),                       &
    check =  a_idata(1),                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  va_idata(:,1) = pi*sin(v_Lat)**2/mint  ! v_IntLon_pva
  va_idata(:,2) = pi*sin(v_Lat)**4/mint  ! v_IntLon_pva
  call AssertEqual(&
    message='va_IntLon_pva',                                      &
    answer = va_IntLon_pva(pva_data),                             &
    check =  va_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_IntLat_va',                                        &
    answer = a_IntLat_va(va_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pa_idata(:,1) = 2.0D0/3*(1-cos(p_Lon)**2)       ! x_IntLat_pva
  pa_idata(:,2) = 2.0D0/5*(1-sin(p_Lon)**2)       ! x_IntLat_pva

  call AssertEqual(&
    message='pa_IntLat_pva',                                      &
    answer = pa_IntLat_pva(pva_data),                             &
    check =  pa_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_IntLon_pa',                                        &
    answer = a_IntLon_pa(pa_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='IntLon_p',                                           &
    answer = IntLon_p(pa_idata(:,1)),                             &
    check =  a_idata(1),                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 平均のテスト(分割領域) ----
  pva_data(:,:,1) = sin(pv_Lat)**2*(1-cos(pv_Lon)**2)
  pva_data(:,:,2) = sin(pv_Lat)**4*(1-sin(pv_Lon)**2)

  a_idata(1) = 1.0D0/6       ! AvrLonLat_pva
  a_idata(2) = 1.0D0/10      ! AvrLonLat_pva
  call AssertEqual(&
    message='a_AvrLonLat_pva',                                    &
    answer = a_AvrLonLat_pva(pva_data),                           &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='AvrLonLat_pv',                                       &
    answer = AvrLonLat_pv(pva_data(:,:,2)),                       &
    check =  a_idata(2),                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  va_idata(:,1) = 0.5*sin(v_Lat)**2   ! v_AvrLon_pva
  va_idata(:,2) = 0.5*sin(v_Lat)**4   ! v_AvrLon_pva
  call AssertEqual(&
    message='va_AvrLon_pva',                                      &
    answer = va_AvrLon_pva(pva_data),                             &
    check =  va_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_AvrLat_va',                                        &
    answer = a_AvrLat_va(va_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pa_idata(:,1) = 1.0D0/3*(1-cos(p_Lon)**2)       ! x_AvrLat_pva
  pa_idata(:,2) = 1.0D0/5*(1-sin(p_Lon)**2)       ! x_AvrLat_pva

  call AssertEqual(&
    message='pa_AvrLat_pva',                                      &
    answer = pa_AvrLat_pva(pva_data),                             &
    check =  pa_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_AvrLon_pa',                                        &
    answer = a_AvrLon_pa(pa_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='AvrLon_p',                                           &
    answer = AvrLon_p(pa_idata(:,2)),                             &
    check =  a_idata(2),                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  call MessageNotify('M','ua_mpi_module_integral_mint_test', &
       'ua_mpi_module_mint integral function tests succeded!')

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

  call MPI_FINALIZE(IERR)

end program ua_mpi_module_integral_mint_test
