!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_base_module テストプログラム :: 基本変換関数のテスト
!
!履歴  2019/04/30  竹広真一
!
program ua_base_mpi_module_test

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

  integer, parameter :: im=64, jm=32, nm=21, km=3
!  integer, parameter :: im=32, jm=16, nm=10, km=3
  integer, parameter :: npv=2

  real(8), allocatable ::  ua_data(:,:)
  real(8), allocatable ::  xvb_data(:,:,:)
  real(8), allocatable ::  u_data(:)
  real(8), allocatable ::  xv_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 )

  allocate(ua_data(nmc,km))
  allocate(xvb_data(0:im-1,jc,kc))
  allocate(xv_data(0:im-1,jc))
  allocate(u_data(nmc))

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

  !---- Y_1^* のテスト ----
  xvb_data = 0.0D0 ; ua_data = 0.0D0
  if ( kp_kl(1) .ne. 0 ) &
       xvb_data(:,:,kp_kl(1)) = sqrt(3.0D0)*sin(xv_Lat)          ! Y_1^0
  if ( lu_nm(1,0).ne.0) ua_data(lu_nm(1,0),1)=1.0D0
  if ( kp_kl(2) .ne. 0 ) &
       xvb_data(:,:,kp_kl(2)) = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon) ! Y_1^1
  if (lu_nm(1,1).ne.0) ua_data(lu_nm(1,1),2)=1.0D0/2.0D0
  if ( kp_kl(3) .ne. 0 ) &
       xvb_data(:,:,kp_kl(3)) = -sqrt(3.0D0/2)*cos(xv_Lat)*sin(xv_Lon) ! Y_1^{-1}
  if (lu_nm(1,-1).ne.0) ua_data(lu_nm(1,-1),3)=1.0D0/2.0D0

  call AssertEqual(&
       message='ua_xvb with Y_1^*',                                  &
       answer = ua_xvb(xvb_data),                                    &
       check = ua_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='xvb_ua with Y_1^*',                                  &
       answer = xvb_ua(ua_xvb(xvb_data)),                            &
       check = xvb_data,                                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  xv_data = sqrt(3.0D0)*sin(xv_Lat)
  u_data = 0.0D0 ; if ( lu_nm(1,0).ne.0) u_data(lu_nm(1,0))=1.0D0
  call AssertEqual(&
       message='u_xv with Y_1^0',                                 &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_1^0',                                 &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  xv_data = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon) ! Y_1^1
  u_data = 0.0D0 ; if (lu_nm(1,1).ne.0) u_data(lu_nm(1,1))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_xv with Y_1^1',                                 &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_1^1',                                 &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  xv_data = -sqrt(3.0D0/2)*cos(xv_Lat)*sin(xv_Lon) ! Y_1^{-1}
  u_data = 0.0D0 ; if (lu_nm(1,-1).ne.0) u_data(lu_nm(1,-1))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_xv with Y_1^-1',                                &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_1^-1',                                &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !---- Y_2^* のテスト ----
  xvb_data = 0.0D0 ; ua_data = 0.0D0
  if ( kp_kl(1) .ne. 0 ) &
       xvb_data(:,:,kp_kl(1)) = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0) ! Y_2^0
  if ( lu_nm(2,0).ne.0) ua_data(lu_nm(2,0),1)=1.0D0
  if ( kp_kl(2) .ne. 0 ) &
       xvb_data(:,:,kp_kl(2)) = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*cos(xv_Lon)!Y_2^1
  if ( lu_nm(2,1).ne.0) ua_data(lu_nm(2,1),2)=1.0D0/2.0D0
  if ( kp_kl(3) .ne. 0 ) &
       xvb_data(:,:,kp_kl(3)) = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)   ! Y_2^-2
  if (lu_nm(2,-2).ne.0) ua_data(lu_nm(2,-2),3)=1.0D0/2.0D0

  call AssertEqual(&
       message='ua_xvb with Y_2^*',                                  &
       answer = ua_xvb(xvb_data),                                    &
       check = ua_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='xvb_ua with Y_2^*',                                  &
       answer = xvb_ua(ua_xvb(xvb_data)),                            &
       check = xvb_data,                                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_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_xv with Y_2^0',                                 &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_2^0',                                 &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  xv_data = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*cos(xv_Lon)!Y_2^1
  u_data = 0.0D0 ; if ( lu_nm(2,1).ne.0) u_data(lu_nm(2,1))=1.0D0/2.0D0
  call AssertEqual(&
       message='u_xv with Y_2^1',                                 &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_2^1',                                 &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_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_xv with Y_2^-2',                                &
       answer = u_xv(xv_data),                                    &
       check = u_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='xv_u with Y_2^-2',                                &
       answer = xv_u(u_xv(xv_data)),                              &
       check = xv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  
  !---- 一般的関数のテスト ----
  if ( kp_kl(1) .ne. 0 ) &
       xvb_data(:,:,kp_kl(1)) = cos(2*xv_Lon-pi/3) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)-0.5)*(sin(xv_Lat)+1)
  if ( kp_kl(2) .ne. 0 ) &
       xvb_data(:,:,kp_kl(2)) = sin(2*xv_Lon+pi/2) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)+1)**2
  if ( kp_kl(3) .ne. 0 ) &
       xvb_data(:,:,kp_kl(3)) = cos(2*xv_Lon+pi/3) &
       *(sin(xv_Lat)-1)*(sin(xv_Lat)-0.5)**2*(sin(xv_Lat)+1)

  call AssertEqual(&
    message='ua_xvb, xvb_ua with a general function',             &
    answer = xvb_ua(ua_xvb(xvb_data)),                            &
    check = xvb_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data = cos(2*xv_Lon-pi/3) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)-0.5)*(sin(xv_Lat)+1)
  call AssertEqual(&
    message='u_xv, xv_u with a general function',                 &
    answer = xv_u(u_xv(xv_data)),                                 &
    check = xv_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

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

  call MPI_FINALIZE(IERR)

end program ua_base_mpi_module_test
