!----------------------------------------------------------------------
!     Copyright (c) 2018--2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module テストプログラム
!      積分・平均計算
!
!履歴  2018/06/09  竹広真一
!      2020/11/11  竹広真一  セクター計算オプション導入
!
program ut_mpi_module_integral_mint_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use ut_mpi_module_mint
  use mpi

  implicit none

!!$  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
!!$  integer,parameter  :: mint=1               ! 経度方向対称性
  integer,parameter  :: im=16, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: mint=2               ! 経度方向対称性
  integer,parameter  :: nm=10, lm=16         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

  integer :: npv = 2
  integer :: iproc, np, ierr

  real(8), dimension(0:im-1)                 :: x_Data
  real(8)                                    :: Data

  real(8), allocatable :: xvh_Data(:,:,:)

  real(8), allocatable :: yh_Data(:,:)
  real(8), allocatable :: xh_Data(:,:)
  real(8), allocatable :: xv_Data(:,:)
  real(8), allocatable :: vh_Data(:,:)
  real(8), allocatable :: v_Data(:)
  real(8), allocatable :: h_Data(:)

  ! 判定誤差設定
  integer, parameter :: check_digits = 8
  integer, parameter :: ignore = -9

  real(8), parameter  :: pi=3.1415926535897932385D0

  !---------------- MPI スタート ---------------------
  !call get_ENV_INT('NPV',npv)

  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  !============================ 積分(xyz,xvh) ==============================

  call MessageNotify('M','ut_mpi_module_integral_mint_test', &
       'ut_mpi_module_mint integration functions tests')

  call ut_mpi_Initial(im,jm,km,nm,lm,ri,ro,npv=npv,mint=mint)

  allocate(xvh_Data(0:im-1,1:jc,kc))
  allocate(xh_Data(0:im-1,1:kc))
  allocate(yh_Data(jc,1:kc))

  allocate(xv_Data(0:im-1,1:jc))
  allocate(vh_Data(1:jc,kc))
  allocate(v_Data(1:jc))
  allocate(h_Data(1:kc))

 !---------- 定数 ----------
  xvh_Data = 1.0D0

  vh_Data = 2*pi/mint
  call AssertEqual(&
       message='vh_IntLon_xvh with constant field',                &
       answer = vh_Data,                                           &
       check = vh_IntLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 2.0D0
  call AssertEqual(&
       message='xh_IntLat_xvh with constant field',                &
       answer = xh_Data,                                           &
       check = xh_IntLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='xv_IntRad_xvh with constant field',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 4*pi/mint
  call AssertEqual(&
       message='h_IntLonLat_xvh with constant field',              &
       answer = h_Data,                                            &
       check = h_IntLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='x_IntLatRad_xvh with constant field',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi/3.0D0*(ro**3-ri**3)/mint
  call AssertEqual(&
       message='v_IntLonRad_xvh with constant field',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 4*pi/3*(ro**3-ri**3)/mint
  call AssertEqual(&
       message='IntLonLatRad_xvh with constant field',             &
       answer = Data,                                              &
       check = IntLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )


  !------------------ r -----------------------
  xvh_Data = xvh_Rad

  vh_Data = 2*pi* xvh_Rad(1,:,:)/mint
  call AssertEqual(&
       message='vh_IntLon_xvh with r-field',                       &
       answer = vh_Data,                                           &
       check = vh_IntLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 2.0D0 * xvh_Data(:,1,:)
  call AssertEqual(&
       message='xh_IntLat_xvh with r-field',                       &
       answer = xh_Data,                                           &
       check = xh_IntLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/4.0D0*(ro**4-ri**4)
  call AssertEqual(&
       message='xv_IntRad_xvh with r field',                       &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 4*pi * xvh_Data(1,1,:)/mint
  call AssertEqual(&
       message='h_IntLonLat_xvh with r field',                     &
       answer = h_Data,                                            &
       check = h_IntLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/2.0D0*(ro**4-ri**4)
  call AssertEqual(&
       message='x_IntLatRad_xvh with r field',                     &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi/2.0D0*(ro**4-ri**4)/mint
  call AssertEqual(&
       message='v_IntLonRad_xvh with constant field',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = pi*(ro**4-ri**4)/mint
  call AssertEqual(&
       message='IntLonLatRad_xvh with constant field',              &
       answer = Data,                                            &
       check = IntLonLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !--------- sin(phi)**2  ---------
  xvh_Data = sin(xvh_Lat)**2

  vh_Data = 2*pi* xvh_Data(1,:,:)/mint
  call AssertEqual(&
       message='vh_IntLon_xvh with sin^2φ field',                 &
       answer = vh_Data,                                           &
       check = vh_IntLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='xh_IntLat_xvh with sin^2φ field',                 &
       answer = xh_Data,                                           &
       check = xh_IntLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*(ro**3-ri**3) * xvh_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvh with sin^2φ field',                 &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 4*pi/3.0/mint
  call AssertEqual(&
       message='h_IntLonLat_xvh with sin^2φ field',               &
       answer = h_Data,                                            &
       check = h_IntLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/9.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='x_IntLatRad_xvh with sin^2φ field',               &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi/3.0D0*(ro**3-ri**3) * xvh_Data(1,:,1)/mint
  call AssertEqual(&
       message='v_IntLonRad_xvh with sin^2φ field',               &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 4*pi/9*(ro**3-ri**3)/mint
  call AssertEqual(&
       message='IntLonLatRad_xvh with constant field',             &
       answer = Data,                                              &
       check = IntLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvh_Data = sin(xvh_Lon)**2

  vh_Data = pi/mint
  call AssertEqual(&
       message='vh_IntLon_xvh with sin(lambda)**2',                &
       answer = vh_Data,                                           &
       check = vh_IntLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 2.0D0 * xvh_Data(:,1,:)
  call AssertEqual(&
       message='xh_IntLat_xvh with sin(lambda)**2',                &
       answer = xh_Data,                                           &
       check = xh_IntLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*(ro**3-ri**3) * xvh_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvh with sin(lambda)**2',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 2*pi/mint
  call AssertEqual(&
       message='h_IntLonLat_xvh with sin(lambda)**2',              &
       answer = h_Data,                                            &
       check = h_IntLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0/3.0D0*(ro**3-ri**3) * xvh_Data(:,1,1)
   call AssertEqual(&
       message='x_IntLatRad_xvh with sin(lambda)**2',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi/3.0D0*(ro**3-ri**3)/mint
  call AssertEqual(&
       message='v_IntLonRad_xvh with sin(lambda)**2',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvh with sin(lambda)**2',             &
       answer = 2*pi/3*(ro**3-ri**3)/mint,                         &
       check = IntLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !============================ 積分(xy,xv) =============================
  ! 定数
  xv_Data = 1.0D0

  v_Data = 2*pi/mint
  call AssertEqual(&
       message='v_IntLon_xv with constant field',                  &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0
   call AssertEqual(&
       message='x_IntLat_xv with constant field',                  &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with constant field',                 &
       answer = 4*pi/mint,                                         &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xv_Data = sin(xv_Lon)**2

  v_Data = pi/mint
  call AssertEqual(&
       message='v_IntLon_xv with sin(lambda)**2',                  &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0d0 * xv_Data(:,1)
   call AssertEqual(&
       message='x_IntLat_xv with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with sin(lambda)**2',                 &
       answer = 2*pi/mint,                                         &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  xv_Data = sin(xv_Lat)**2

  v_Data = 2*pi* xv_Data(1,:)/mint
  call AssertEqual(&
       message='v_IntLon_xv with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='x_IntLat_xv with sin(phi)**2',                     &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with sin(phi)**2',                    &
       answer = 4*pi/3.0/mint,                                     &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(yz,vh) =============================
  ! 定数
  vh_Data = 1.0D0

  h_Data = 2.0D0
  call AssertEqual(&
       message='h_IntLat_vh with constant field',                  &
       answer = h_Data,                                            &
       check = h_IntLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='v_IntRad_vh with constant field',                  &
       answer = v_Data,                                            &
       check = v_IntRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vh with constant field',                 &
       answer = 2/3.0D0*(ro**3-ri**3),                             &
       check = IntLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  vh_Data = xvh_Rad(0,:,:)**2

  h_Data = 2.0D0 * vh_Data(1,:)
  call AssertEqual(&
       message='h_IntLat_vh with r^2',                             &
       answer = h_Data,                                            &
       check = h_IntLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/5.0D0*(ro**5-ri**5)
  call AssertEqual(&
       message='v_IntRad_vh with r^2',                             &
       answer = v_Data,                                            &
       check = v_IntRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vh with r^2',                            &
       answer = 2/5.0D0*(ro**5-ri**5),                             &
       check = IntLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  vh_Data = sin(xvh_Lat(0,:,:))**2

  h_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='h_IntLat_vh with sin(phi)**2',                     &
       answer = h_Data,                                            &
       check = h_IntLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/3.0D0*(ro**3-ri**3) * vh_Data(:,1)
  call AssertEqual(&
       message='v_IntRad_vh with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_IntRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vh with sin(phi)**2',                    &
       answer = 2/9.0D0*(ro**3-ri**3),                             &
       check = IntLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )
!!$
 !============================ 積分(xh) =============================
  ! 定数
  xh_Data = 1.0D0

  h_Data = 2*pi/mint
  call AssertEqual(&
       message='h_IntLon_xh with constant field',                  &
       answer = h_Data,                                            &
       check = h_IntLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='x_IntRad_xh with constant field',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xh with constant field',                 &
       answer = 2*pi/3.0D0*(ro**3-ri**3)/mint,                     &
       check = IntLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xh_Data = sin(xvh_Lon(:,1,:))**2

  h_Data = pi/mint
  call AssertEqual(&
       message='h_IntLon_xh with sin(lambda)**2',                  &
       answer = h_Data,                                            &
       check = h_IntLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/3.0D0*(ro**3-ri**3) * xh_Data(:,1)
  call AssertEqual(&
       message='x_IntRad_xh with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xh with sin(lambda)**2',                 &
       answer = pi/3.0D0*(ro**3-ri**3)/mint,                       &
       check = IntLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  xh_Data = spread(h_Rad**2,1,im)

  h_Data = 2*pi* h_Rad**2/mint
  call AssertEqual(&
       message='h_IntLon_xh with r^2',                             &
       answer = h_Data,                                            &
       check = h_IntLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/5.0D0*(ro**5-ri**5)
  call AssertEqual(&
       message='x_IntRad_xh with r^2',                             &
       answer = x_Data,                                            &
       check = x_IntRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xh with r^2',                            &
       answer = 2*pi/5.0D0*(ro**5-ri**5)/mint,                     &
       check = IntLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(x) =============================
  ! 定数
  x_Data = 1.0D0

  call AssertEqual(&
       message='IntLon_x with constant field',                     &
       answer = 2*pi/mint,                                         &
       check = IntLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  x_Data = sin(x_Lon)**2

  call AssertEqual(&
       message='IntLon_x with sin(lambda)**2',                     &
       answer = pi/mint,                                           &
       check = IntLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(y,v) =============================
  ! 定数
  v_Data = 1.0D0

  call AssertEqual(&
       message='IntLat_v with constant field',                     &
       answer = 2.0D0,                                             &
       check = IntLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  v_Data = sin(v_Lat)**2

  call AssertEqual(&
       message='IntLat_v with sin(phi)**2',                        &
       answer = 2.0D0/3.0D0,                                       &
       check = IntLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !============================ 積分(z) =============================
  ! 定数
  h_Data = 1.0D0

  call AssertEqual(&
       message='IntRad_r with constant field',                     &
       answer = 1/3.0D0*(ro**3-ri**3),                             &
       check = IntRad_h(h_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  h_Data = h_Rad**2

  call AssertEqual(&
       message='IntRad_r with r^2',                                &
       answer = 1/5.0D0*(ro**5-ri**5),                             &
       check = IntRad_h(h_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

!============================ 平均(xyz,xvh) ==============================

 !---------- 定数 ----------
  xvh_Data = 1.0D0

  vh_Data = 1.0D0
  call AssertEqual(&
       message='vh_AvrLon_xvh with constant field',                &
       answer = vh_Data,                                           &
       check = vh_AvrLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 1.0D0
  call AssertEqual(&
       message='xh_AvrLat_xvh with constant field',                &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1.0D0
  call AssertEqual(&
       message='xv_AvrRad_xvh with constant field',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 1.0D0
  call AssertEqual(&
       message='h_AvrLonLat_xvh with constant field',              &
       answer = h_Data,                                            &
       check = h_AvrLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrLatRad_xvh with constant field',              &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrLonRad_xvh with constant field',              &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 1.0D0
  call AssertEqual(&
       message='AvrLonLatRad_xvh with constant field',             &
       answer = Data,                                              &
       check = AvrLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !------------------ r -----------------------
  xvh_Data = xvh_Rad

  vh_Data = xvh_Rad(1,:,:)
  call AssertEqual(&
       message='vh_AvrLon_xvh with r-field',                &
       answer = vh_Data,                                           &
       check = vh_AvrLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = xvh_Data(:,1,:)
  call AssertEqual(&
       message='xh_AvrLat_xvh with r-field',                &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1.0D0/4.0D0*(ro**4-ri**4)/(1/3.0D0*(ro**3-ri**3))
  call AssertEqual(&
       message='xv_AvrRad_xvh with r field',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = xvh_Data(1,1,:)
  call AssertEqual(&
       message='h_AvrLonLat_xvh with r field',                     &
       answer = h_Data,                                            &
       check = h_AvrLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/2.0D0*(ro**4-ri**4)/(2/3.0D0*(ro**3-ri**3))
  call AssertEqual(&
       message='x_AvrLatRad_xvh with r field',                     &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi/2.0D0*(ro**4-ri**4)/(2*pi/3.0D0*(ro**3-ri**3))
  call AssertEqual(&
       message='v_AvrLonRad_xvh with constant field',              &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = pi*(ro**4-ri**4)/(4*pi/3*(ro**3-ri**3))
  call AssertEqual(&
       message='AvrLonLatRad_xvh with constant field',              &
       answer = Data,                                            &
       check = AvrLonLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !--------- sin(phi)**2  ---------
  xvh_Data = sin(xvh_Lat)**2

  vh_Data = xvh_Data(1,:,:)
  call AssertEqual(&
       message='vh_AvrLon_xvh with sin^2φ field',                 &
       answer = vh_Data,                                           &
       check = vh_AvrLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='xh_AvrLat_xvh with sin^2φ field',                 &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = xvh_Data(:,:,1)
  call AssertEqual(&
       message='xv_AvrRad_xvh with sin^2φ field',                 &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='h_AvrLonLat_xvh with sin^2φ field',               &
       answer = h_Data,                                            &
       check = h_AvrLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='x_AvrLatRad_xvh with sin^2φ field',               &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = xvh_Data(1,:,1)
  call AssertEqual(&
       message='v_AvrLonRad_xvh with sin^2φ field',               &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 1.0D0/3D0
  call AssertEqual(&
       message='AvrLonLatRad_xvh with constant field',             &
       answer = Data,                                              &
       check = AvrLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvh_Data = sin(xvh_Lon)**2

  vh_Data = 0.5D0
  call AssertEqual(&
       message='vh_AvrLon_xvh with sin(lambda)**2',                &
       answer = vh_Data,                                           &
       check = vh_AvrLon_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = xvh_Data(:,1,:)
  call AssertEqual(&
       message='xh_AvrLat_xvh with sin(lambda)**2',                &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = xvh_Data(:,:,1)
  call AssertEqual(&
       message='xv_AvrRad_xvh with sin(lambda)**2',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvh(xvh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 0.5D0
  call AssertEqual(&
       message='h_AvrLonLat_xvh with sin(lambda)**2',              &
       answer = h_Data,                                            &
       check = h_AvrLonLat_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xvh_Data(:,1,1)
  call AssertEqual(&
       message='x_AvrLatRad_xvh with sin(lambda)**2',              &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 0.5D0
  call AssertEqual(&
       message='v_AvrLonRad_xvh with sin(lambda)**2',              &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvh(xvh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLatRad_xvh with sin(lambda)**2',             &
       answer = 0.5D0,                                             &
       check = AvrLonLatRad_xvh(xvh_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(xy,xv) =============================
  ! 定数
  xv_Data = 1.0D0

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrLon_xv with constant field',                  &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrLat_xv with constant field',                  &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xv_Data = sin(xv_Lon)**2

  v_Data = 0.5D0
  call AssertEqual(&
       message='v_AvrLon_xv with sin(lambda)**2',                  &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xv_Data(:,1)
  call AssertEqual(&
       message='x_AvrLat_xv with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with sin(lambda)**2',                 &
       answer = 0.5D0,                                             &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  xv_Data = sin(xv_Lat)**2

  v_Data = xv_Data(1,:)
  call AssertEqual(&
       message='v_AvrLon_xv with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='x_AvrLat_xv with sin(phi)**2',                     &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with sin(phi)**2',                    &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(yz,vh) =============================
  ! 定数
  vh_Data = 1.0D0

  h_Data = 1.0D0
  call AssertEqual(&
       message='h_AvrLat_vh with constant field',                  &
       answer = h_Data,                                            &
       check = h_AvrLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrRad_vh with constant field',                  &
       answer = v_Data,                                            &
       check = v_AvrRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vh with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  vh_Data = xvh_Rad(0,:,:)**2

  h_Data = vh_Data(1,:)
  call AssertEqual(&
       message='h_AvrLat_vh with r^2',                             &
       answer = h_Data,                                            &
       check = h_AvrLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3)
  call AssertEqual(&
       message='v_AvrRad_vh with r^2',                             &
       answer = v_Data,                                            &
       check = v_AvrRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vh with r^2',                            &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  vh_Data = sin(xvh_Lat(0,:,:))**2

  h_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='h_AvrLat_vh with sin(phi)**2',                     &
       answer = h_Data,                                            &
       check = h_AvrLat_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = vh_Data(:,1)
  call AssertEqual(&
       message='v_AvrRad_vh with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_AvrRad_vh(vh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vh with sin(phi)**2',                    &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLatRad_vh(vh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(xh) =============================
  ! 定数
  xh_Data = 1.0D0

  h_Data = 1.0D0
  call AssertEqual(&
       message='h_AvrLon_xh with constant field',                  &
       answer = h_Data,                                            &
       check = h_AvrLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrRad_xh with constant field',                  &
       answer = x_Data,                                            &
       check = x_AvrRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xh with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xh_Data = sin(xvh_Lon(:,1,:))**2

  h_Data = 0.5D0
  call AssertEqual(&
       message='h_AvrLon_xh with sin(lambda)**2',                  &
       answer = h_Data,                                            &
       check = h_AvrLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xh_Data(:,1)
  call AssertEqual(&
       message='x_AvrRad_xh with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_AvrRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xh with sin(lambda)**2',                 &
       answer = 0.5D0,                                             &
       check = AvrLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  xh_Data = spread(h_Rad**2,1,im)

  h_Data = h_Rad**2
  call AssertEqual(&
       message='h_AvrLon_xh with r^2',                             &
       answer = h_Data,                                            &
       check = h_AvrLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3)
  call AssertEqual(&
       message='x_AvrRad_xh with r^2',                             &
       answer = x_Data,                                            &
       check = x_AvrRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xh with r^2',                            &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrLonRad_xh(xh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(x) =============================
  ! 定数
  x_Data = 1.0D0

  call AssertEqual(&
       message='AvrLon_x with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  x_Data = sin(x_Lon)**2

  call AssertEqual(&
       message='AvrLon_x with sin(lambda)**2',                     &
       answer = 0.5D0,                                             &
       check = AvrLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(y,v) =============================
  ! 定数
  v_Data = 1.0D0

  call AssertEqual(&
       message='AvrLat_v with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  v_Data = sin(v_Lat)**2

  call AssertEqual(&
       message='AvrLat_v with sin(phi)**2',                        &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(r) =============================
  ! 定数
  h_Data = 1.0D0

  call AssertEqual(&
       message='AvrRad_h with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrRad_h(h_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2
  h_Data = h_Rad**2

  call AssertEqual(&
       message='AvrRad_h with r^2',                                &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrRad_h(h_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call MessageNotify('M','ut_mpi_module_integral_mint_test', &
       'ut_mpi_module_mint integration/averaging function tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program ut_mpi_module_integral_mint_test
