!----------------------------------------------------------------------
!     Copyright (c) 2009--2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_integrale_module テストプログラム :: 積分・平均関数のテスト
!
!履歴  2009/09/06  竹広真一 wa_test_deriv.f90 より SVPACK 用に改造
!      2011/03/12  竹広真一 use 文 bug fix
!      2013/02/13  竹広真一 comment 修正
!      2016/04/21  竹広真一 svpack 用に改造
!      2016/08/17  竹広真一 MPI 並列化
!
program ua_integral_mpi_module_svpack_test

  use dc_message, only : MessageNotify
  use ua_mpi_module_svpack
  use dc_test, only : AssertEqual
  use dc_trace, only : SetDebug
  use mpi
  implicit none

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

  real(8), allocatable ::  xyb_data(:,:,:)          ! 元の関数
  real(8), allocatable ::  xb_idata(:,:)            ! 積分・平均の正解
  real(8), allocatable ::  yb_idata (:,:)           ! 積分・平均の正解
  real(8), allocatable ::  b_idata(:)               ! 積分・平均の正解

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

  integer :: iproc, np, ierr
  real(8) :: pi

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','ua_integral_module_svpack_test', &
                         'ua_integral_module_svpack function tests') 

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  call ua_mpi_Initial( nm, im, jm, km )

  allocate(xyb_data(0:im-1,1:jm,kc))       ! 元の関数
  allocate(xb_idata(0:im-1,kc))             ! 積分・平均の正解
  allocate(yb_idata(1:jm,kc))               ! 積分・平均の正解
  allocate(b_idata(kc))                    ! 積分・平均の正解

  
  !---- 積分のテスト ----
  if ( kp_kl(1) /= 0 ) then
     xyb_data(:,:,kp_kl(1)) = sin(xy_Lat)**2*(1-cos(xy_Lon)**2)
     b_idata(kp_kl(1)) = 2*pi/3       ! a_IntLonLat_xya
  endif
  if ( kp_kl(2) /= 0 ) then
     xyb_data(:,:,kp_kl(2)) = sin(xy_Lat)**4*(1-sin(xy_Lon)**2)
     b_idata(kp_kl(2)) = 2*pi/5       ! a_IntLonLat_xya
  endif
  call AssertEqual(&
    message='a_IntLonLat_xya',                                    &
    answer = a_IntLonLat_xya(xyb_data),                           &
    check =  b_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       yb_idata(:,kp_kl(1)) = pi*sin(y_Lat)**2       ! y_IntLon_xya
  if ( kp_kl(2) /= 0 ) &
       yb_idata(:,kp_kl(2)) = pi*sin(y_Lat)**4       ! y_IntLon_xya
  call AssertEqual(&
    message='ya_IntLon_xya',                                      &
    answer = ya_IntLon_xya(xyb_data),                             &
    check =  yb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_IntLat_ya',                                        &
    answer = a_IntLat_ya(yb_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_xya
  if ( kp_kl(2) /= 0 ) &
       xb_idata(:,kp_kl(2)) = 2.0D0/5*(1-sin(x_Lon)**2)       ! x_IntLat_xya

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

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

  !---- 平均のテスト ----
  if ( kp_kl(1) /= 0 ) &
       xyb_data(:,:,kp_kl(1)) = sin(xy_Lat)**2*(1-cos(xy_Lon)**2)
  if ( kp_kl(2) /= 0 ) &
       xyb_data(:,:,kp_kl(2)) = sin(xy_Lat)**4*(1-sin(xy_Lon)**2)

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

  if ( kp_kl(1) /= 0 ) &
       yb_idata(:,kp_kl(1)) = 0.5*sin(y_Lat)**2       ! y_AvrLon_xya
  if ( kp_kl(2) /= 0 ) &
       yb_idata(:,kp_kl(2)) = 0.5*sin(y_Lat)**4       ! y_AvrLon_xya
  call AssertEqual(&
    message='ya_AvrLon_xya',                                      &
    answer = ya_AvrLon_xya(xyb_data),                             &
    check =  yb_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_AvrLat_ya',                                        &
    answer = a_AvrLat_ya(yb_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_xya
  if ( kp_kl(2) /= 0 ) &
       xb_idata(:,kp_kl(2)) = 1.0D0/5*(1-sin(x_Lon)**2)       ! x_AvrLat_xya

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

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

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','ua_integral_mpi_module_svpack_test', &
                         'ua_mpi_module_svpack integral function tests succeded!') 

end program ua_integral_mpi_module_svpack_test
