!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_integrale_module テストプログラム :: 積分・平均関数のテスト
!
!履歴  2019/05/01  竹広真一
!
program ua_integral_mpi_module

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

  integer, parameter :: im=128, jm=64, nm=42, km=2, npv=2

  real(8), allocatable             ::  b_idata(:)          ! 積分・平均の正解
  real(8), allocatable             ::  xvb_data(:,:,:)     ! 元の関数
  real(8), allocatable             ::  xb_idata(:,:)       ! 積分・平均の正解
  real(8), allocatable             ::  vb_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_integral_mpi_module_test', &
                         'ua_integral_mpi_module function tests')

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


  allocate(xvb_data(0:im-1,jc,kc))
  allocate(xb_idata(0:im-1,kc))
  allocate(vb_idata(jc,kc))
  allocate(b_idata(kc))

  !---- 積分のテスト(分割領域) ----
  xvb_data = 0.0D0
  if ( kp_kl(1) /= 0 ) &
       xvb_data(:,:, kp_kl(1)) = sin(xv_Lat)**2*(1-cos(xv_Lon)**2)
  if ( kp_kl(2) /= 0 ) &
       xvb_data(:,:,kp_kl(2)) = sin(xv_Lat)**4*(1-sin(xv_Lon)**2)

  if ( kp_kl(1) /= 0 ) b_idata(kp_kl(1)) = 2*pi/3       ! a_IntLonLat_xvb
  if ( kp_kl(2) /= 0 ) b_idata(kp_kl(2)) = 2*pi/5       ! a_IntLonLat_xvb
  call AssertEqual(&
    message='a_IntLonLat_xvb',                                    &
    answer = b_IntLonLat_xvb(xvb_data),                           &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) vb_idata(:,kp_kl(1)) = pi*sin(v_Lat)**2  ! v_IntLon_xvb
  if ( kp_kl(2) /= 0 ) vb_idata(:,kp_kl(2)) = pi*sin(v_Lat)**4  ! v_IntLon_xvb
  call AssertEqual(&
    message='vb_IntLon_xvb',                                      &
    answer = vb_IntLon_xvb(xvb_data),                             &
    check =  vb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='b_IntLat_vb',                                        &
    answer = b_IntLat_vb(vb_idata),                               &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xb_idata(:,kp_kl(1)) = 2.0D0/3*(1-cos(x_Lon)**2)       ! x_IntLat_xvb
  if ( kp_kl(2) /= 0 ) &
       xb_idata(:,kp_kl(2)) = 2.0D0/5*(1-sin(x_Lon)**2)       ! x_IntLat_xvb

  call AssertEqual(&
    message='xb_IntLat_xvb',                                      &
    answer = xb_IntLat_xvb(xvb_data),                             &
    check =  xb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='b_IntLon_xb',                                        &
    answer = b_IntLon_xb(xb_idata),                               &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 平均のテスト(分割領域) ----
  if ( kp_kl(1) /= 0 ) &
       xvb_data(:,:,kp_kl(1)) = sin(xv_Lat)**2*(1-cos(xv_Lon)**2)
  if ( kp_kl(2) /= 0 ) &
       xvb_data(:,:,kp_kl(2)) = sin(xv_Lat)**4*(1-sin(xv_Lon)**2)

  if ( kp_kl(1) /= 0 ) b_idata(kp_kl(1)) = 1.0D0/6       ! AvrLonLat_xvb
  if ( kp_kl(2) /= 0 ) b_idata(kp_kl(2)) = 1.0D0/10      ! AvrLonLat_xvb
  call AssertEqual(&
    message='b_AvrLonLat_xvb',                                    &
    answer = b_AvrLonLat_xvb(xvb_data),                           &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) vb_idata(:,kp_kl(1)) = 0.5*sin(v_Lat)**2   ! v_AvrLon_xvb
  if ( kp_kl(2) /= 0 ) vb_idata(:,kp_kl(2)) = 0.5*sin(v_Lat)**4   ! v_AvrLon_xvb
  call AssertEqual(&
    message='vb_AvrLon_xvb',                                      &
    answer = vb_AvrLon_xvb(xvb_data),                             &
    check =  vb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='b_AvrLat_vb',                                        &
    answer = b_AvrLat_vb(vb_idata),                               &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xb_idata(:,kp_kl(1)) = 1.0D0/3*(1-cos(x_Lon)**2)       ! x_AvrLat_xvb
  if ( kp_kl(2) /= 0 ) &
       xb_idata(:,kp_kl(2)) = 1.0D0/5*(1-sin(x_Lon)**2)       ! x_AvrLat_xvb

  call AssertEqual(&
    message='xb_AvrLat_xvb',                                      &
    answer = xb_AvrLat_xvb(xvb_data),                             &
    check =  xb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='b_AvrLon_xb',                                        &
    answer = b_AvrLon_xb(xb_idata),                               &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  call MessageNotify('M','ua_integral_mpi_module_test', &
       'ua_integral_mpi_module function tests succeded!')

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

  call MPI_FINALIZE(IERR)

end program ua_integral_mpi_module
