!----------------------------------------------------------------------
!     Copyright (c) 2019-2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_base_module テストプログラム :: 基本変換関数のテスト
!
!履歴  2020/08/05  竹広真一
!      2020/11/10  竹広真一  セクター計算オプション導入
!
program ua_mpi_module_base_mint_test

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

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

  real(8), allocatable ::  ua_data(:,:)
  real(8), allocatable ::  xvb_data(:,:,:)
  real(8), allocatable ::  pva_data(:,:,:)
  real(8), allocatable ::  u_data(:)
  real(8), allocatable ::  xv_data(:,:)
  real(8), allocatable ::  pv_data(:,:)

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

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

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

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','ua_base_mpi_module_test', &
                         'ua_base_mpi_module functions tests')

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

  allocate(ua_data(nmc,km))
  allocate(pva_data(0:ic-1,jc,km))
  allocate(pv_data(0:ic-1,jc))
  allocate(u_data(nmc))

  !========================= xvb_wb, wb_xvb のテスト===========================

  !---- Y_1^0, Y_2^* のテスト ----
  ua_data = 0.0D0
  if ( lu_nm(1,0).ne.0) ua_data(lu_nm(1,0),1)=1.0D0
  pva_data(:,:,1) = sqrt(3.0D0)*sin(pv_Lat)                          ! Y_1^0

  if ( lu_nm(2,0).ne.0) ua_data(lu_nm(2,0),2)=1.0D0
  pva_data(:,:,2) = sqrt(5.0D0)*(3.0/2*sin(pv_Lat)**2-1/2.0)         ! Y_2^0

  if (lu_nm(2,-2).ne.0) ua_data(lu_nm(2,-2),3)=1.0D0/2.0D0
  pva_data(:,:,3) = -sqrt(5.0D0/24)*3.0*cos(pv_Lat)**2*sin(2*pv_Lon) ! Y_2^-2
  
  call AssertEqual(&
       message='ua_pva with Y_1^0 and Y_2^*',                        &
       answer = ua_pva(pva_data),                                    &
       check = ua_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='pva_ua with Y_1^0 and Y_2^*',                                  &
       answer = pva_ua(ua_data),                                     &
       check = pva_data,                                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  pv_data = sqrt(5.0D0)*(3.0/2*sin(pv_Lat)**2-1/2.0) ! Y_2^0
  u_data = 0.0D0 ; if ( lu_nm(2,0).ne.0) u_data(lu_nm(2,0))=1.0D0
  call AssertEqual(&
       message='u_pv with Y_2^0',                                 &
       answer = u_pv(pv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_u with Y_2^0',                                 &
       answer = pv_u(u_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = -sqrt(5.0D0/24)*3.0*cos(pv_Lat)**2*sin(2*pv_Lon)   ! Y_2^-2
  u_data = 0.0D0 ; if (lu_nm(2,-2).ne.0) u_data(lu_nm(2,-2))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_pv with Y_2^-2',                                &
       answer = u_pv(pv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_u with Y_2^-2',                                &
       answer = pv_u(u_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !---- Y_4^* のテスト ----
  ua_data = 0.0D0
  if ( lu_nm(4,0).ne.0) ua_data(lu_nm(4,0),1)=1.0D0
  pva_data(:,:,1) = sqrt(9.0D0)*(35.0/8*sin(pv_Lat)**4-30.0/8*sin(pv_Lat)**2+3.0/8) ! Y_4^0
  if ( lu_nm(4,2).ne.0) ua_data(lu_nm(4,2),2)=1.0D0/2.0D0
  pva_data(:,:,2) = sqrt(9.0D0/360)*15.0/2*cos(pv_Lat)**2*(7*sin(pv_Lat)**2-1)*cos(2*pv_Lon)         ! Y_4^2  
  if ( lu_nm(4,4).ne.0) ua_data(lu_nm(4,4),3)=1.0D0/2.0D0
  pva_data(:,:,3) = sqrt(9.0D0/40320)*105.0*cos(pv_Lat)**4*cos(4*pv_Lon)         ! Y_4^4

  call AssertEqual(&
       message='ua_pva with Y_4^*',                                  &
       answer = ua_pva(pva_data),                                    &
       check = ua_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='pva_ua with Y_4^*',                                  &
       answer = pva_ua(ua_data),                                     &
       check = pva_data,                                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  pv_data = sqrt(9.0D0)*(35.0/8*sin(pv_Lat)**4-30.0/8*sin(pv_Lat)**2+3.0/8) ! Y_4^0
  u_data = 0.0D0 ; if ( lu_nm(4,0).ne.0) u_data(lu_nm(4,0))=1.0D0
  call AssertEqual(&
       message='u_pv with Y_4^0',                                 &
       answer = u_pv(pv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_u with Y_4^0',                                 &
       answer = pv_u(u_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = -sqrt(9.0D0/360)*15.0/2*cos(pv_Lat)**2*(7*sin(pv_Lat)**2-1)*sin(2*pv_Lon)         ! Y_4^-2  
  u_data = 0.0D0 ; if ( lu_nm(4,-2).ne.0) u_data(lu_nm(4,-2))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_pv with Y_4^-2',                                &
       answer = u_pv(pv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_u with Y_4^-2',                                &
       answer = pv_u(u_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = -sqrt(9.0D0/40320)*105.0*cos(pv_Lat)**4*sin(4*pv_Lon)         ! Y_4^-4
  u_data = 0.0D0 ; if ( lu_nm(4,-4).ne.0) u_data(lu_nm(4,-4))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_pv with Y_4^-4',                                &
       answer = u_pv(pv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_u with Y_4^-4',                                &
       answer = pv_u(u_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  call MessageNotify('M','ua_mpi_module_base_test', &
                         'ua_mpi_module_mint base functions tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program ua_mpi_module_base_mint_test

