!----------------------------------------------------------------------
!     Copyright (c) 2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wb_base_module テストプログラム :: 基本変換関数のテスト
!
!履歴  2020/07/31  竹広真一
!
program wb_base_mpi_module_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wb_mpi_module
  use wb_mpi_module_base, only : nnc => nc
  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 ::  wb_data(:,:)
  real(8), allocatable ::  pva_data(:,:,:)
  real(8), allocatable ::  xvb_data(:,:,:)
  real(8), allocatable ::  w_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','wb_base_mpi_module_test', &
                         'wb_base_mpi_module functions tests')

  call wb_mpi_Initial( nm, im, jm, km, npv=npv )

  allocate(wb_data(nnc,kc))
  allocate(pva_data(ic,jc,km))
  allocate(xvb_data(0:im-1,jc,kc))
  allocate(pv_data(ic,jc))
  allocate(w_data(nc))

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

  !---- Y_1^* のテスト ----
  wb_data = 0.0D0 ; xvb_data = 0.0D0
  pva_data(:,:,1) = sqrt(3.0D0)*sin(pv_Lat)                   ! Y_1^0
  if ( kp_kl(1) .ne. 0 .AND. l_nm(1,0) .ne. 0 ) &
       wb_data(l_nm(1,0),kp_kl(1))=1.0D0
  if ( kp_kl(1) .ne. 0 ) &
       xvb_data(:,:,kp_kl(1)) = sqrt(3.0D0)*sin(xv_Lat)                ! Y_1^0
       
  pva_data(:,:,2) = sqrt(3.0D0/2)*cos(pv_Lat)*cos(pv_Lon)     ! Y_1^1
  if ( kp_kl(2) .ne. 0 .AND. l_nm(1,1) .ne. 0 ) &
       wb_data(l_nm(1,1),kp_kl(2))=1.0D0/2.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
     
  pva_data(:,:,3) = -sqrt(3.0D0/2)*cos(pv_Lat)*sin(pv_Lon)    ! Y_1^{-1}
  if ( kp_kl(3) .ne. 0 .AND. l_nm(1,-1) .ne. 0 ) &
       wb_data(l_nm(1,-1),kp_kl(3))=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}

!!$  call AssertEqual(&
!!$       message='wb_pva with Y_1^*',                                  &
!!$       answer = wb_pva(pva_data),                                    &
!!$       check = wb_data,                                              &
!!$       significant_digits = check_digits, ignore_digits = ignore     &
!!$       )
!!$  call AssertEqual(&
!!$       message='pva_wb with Y_1^*',                                  &
!!$       answer = pva_wb(wb_data),                                     &
!!$       check = pva_data,                                             &
!!$       significant_digits = check_digits, ignore_digits = ignore     &
!!$       )
  call AssertEqual(&
       message='wb_xvb with Y_1^*',                                  &
       answer = wb_xvb(xvb_data),                                    &
       check = wb_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='xvb_wb with Y_1^*',                                  &
       answer = xvb_wb(wb_data),                                     &
       check = xvb_data,                                             &
       significant_digits = check_digits, ignore_digits = ignore     &
       )

  pv_data = sqrt(3.0D0)*sin(pv_Lat)
  w_data = 0.0D0
  if ( l_nm(1,0) .ne. 0 ) w_data(l_nm(1,0))=1.0D0
  call AssertEqual(&
       message='w_pv with Y_1^0',                                 &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_1^0',                                 &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = sqrt(3.0D0/2)*cos(pv_Lat)*cos(pv_Lon) ! Y_1^1
  w_data = 0.0D0
  if ( l_nm(1,1) .ne. 0 ) w_data(l_nm(1,1))=1.0D0/2.0D0
  call AssertEqual(&
       message='w_pv with Y_1^1',                                 &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_1^1',                                 &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = -sqrt(3.0D0/2)*cos(pv_Lat)*sin(pv_Lon) ! Y_1^{-1}
  w_data = 0.0D0
  if ( l_nm(1,-1) .ne. 0 ) w_data(l_nm(1,-1))=1.0D0/2.0D0
  call AssertEqual(&
       message='w_pv with Y_1^-1',                                &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_1^-1',                                &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !---- Y_2^* のテスト ----
  wb_data = 0.0D0 ; xvb_data = 0.0D0
  pva_data(:,:,1) = sqrt(5.0D0)*(3.0/2*sin(pv_Lat)**2-1/2.0)             ! Y_2^0
  if ( kp_kl(1) .ne. 0 .AND. l_nm(2,0) .ne. 0 ) &
       wb_data(l_nm(2,0),kp_kl(1))=1.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)
       
  pva_data(:,:,2) = sqrt(5.0D0/6)*3.0*sin(pv_Lat)*cos(pv_Lat)*cos(pv_Lon) !Y_2^1
  if ( kp_kl(2) .ne. 0 .AND. l_nm(2,1) .ne. 0 ) &
       wb_data(l_nm(2,1),kp_kl(2))=1.0D0/2.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)
     
  pva_data(:,:,3) = -sqrt(5.0D0/24)*3.0*cos(pv_Lat)**2*sin(2*pv_Lon)   ! Y_2^-2
  if ( kp_kl(3) .ne. 0 .AND. l_nm(2,-2) .ne. 0 ) &
       wb_data(l_nm(2,-2),kp_kl(3))=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)

!!$  call AssertEqual(&
!!$       message='wb_pva with Y_2^*',                                  &
!!$       answer = wb_pva(pva_data),                                    &
!!$       check = wb_data,                                              &
!!$       significant_digits = check_digits, ignore_digits = ignore     &
!!$       )
!!$  call AssertEqual(&
!!$       message='pva_wb with Y_2^*',                                  &
!!$       answer = pva_wb(wb_data),                                     &
!!$       check = pva_data,                                             &
!!$       significant_digits = check_digits, ignore_digits = ignore     &
!!$       )
  call AssertEqual(&
       message='wb_xvb with Y_2^*',                                  &
       answer = wb_xvb(xvb_data),                                    &
       check = wb_data,                                              &
       significant_digits = check_digits, ignore_digits = ignore     &
       )
  call AssertEqual(&
       message='xvb_wb with Y_2^*',                                  &
       answer = xvb_wb(wb_data),                                     &
       check = xvb_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
  w_data = 0.0D0
  if ( l_nm(2,0) .ne. 0 ) w_data(l_nm(2,0))=1.0D0
  call AssertEqual(&
       message='w_pv with Y_2^0',                                 &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_2^0',                                 &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  pv_data = sqrt(5.0D0/6)*3.0*sin(pv_Lat)*cos(pv_Lat)*cos(pv_Lon)!Y_2^1
  w_data = 0.0D0
  if ( l_nm(2,1) .ne. 0 ) w_data(l_nm(2,1))=1.0D0/2.0D0
  call AssertEqual(&
       message='w_pv with Y_2^1',                                 &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_2^1',                                 &
       answer = pv_w(w_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
  w_data = 0.0D0
  if ( l_nm(2,-2) .ne. 0 ) w_data(l_nm(2,-2))=1.0D0/2.0D0
  call AssertEqual(&
       message='w_pv with Y_2^-2',                                &
       answer = w_pv(pv_data),                                    &
       check = w_data,                                            &
       significant_digits = check_digits, ignore_digits = ignore  &
       )
  call AssertEqual(&
       message='pv_w with Y_2^-2',                                &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !---- 一般的関数のテスト ----
  pva_data(:,:,1) = cos(2*pv_Lon-pi/3) &
       *(sin(pv_Lat)-1)**2*(sin(pv_Lat)-0.5)*(sin(pv_Lat)+1)
  pva_data(:,:,2) = sin(2*pv_Lon+pi/2) &
       *(sin(pv_Lat)-1)**2*(sin(pv_Lat)+1)**2
  pva_data(:,:,3) = cos(2*pv_Lon+pi/3) &
       *(sin(pv_Lat)-1)*(sin(pv_Lat)-0.5)**2*(sin(pv_Lat)+1)
  
!!$  call AssertEqual(&
!!$    message='wb_pva, pva_wb with a general function',             &
!!$    answer = pva_wb(wb_pva(pva_data)),                            &
!!$    check = pva_data,                                             &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )

  pv_data = cos(2*pv_Lon-pi/3) &
       *(sin(pv_Lat)-1)**2*(sin(pv_Lat)-0.5)*(sin(pv_Lat)+1)
  call AssertEqual(&
       message='pv_w with a general function',                    &
       answer = pv_w(w_pv(pv_data)),                              &
       check = pv_data,                                           &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  call MessageNotify('M','wb_base_mpi_module_test', &
                         'wb_base_mpi_module functions tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program wb_base_mpi_module_test
