!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_base_mpi_module_sypack テストプログラム :: 基本変換関数のテスト
!
!履歴  2019/03/17  竹広真一
!     2020/11/09  竹広真一 セクター計算テスト
!
program w_mpi_module_base_mint_test

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

!!$  integer, parameter :: im=1024, jm=512, nm=341
!!$  integer, parameter :: im=128, jm=64, nm=42
!!$  integer, parameter :: im=128, jm=64, nm=42, mint=1
  integer, parameter :: im=64, jm=64, nm=42, mint=2

  real(8), allocatable  :: w_data(:)
  real(8), allocatable  :: xv_data(:,:)
  real(8), allocatable  :: xv_Ddata(:,:)

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

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

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','w_base_mpi_module_mint_test', &
                         'w_mpi_module_mint base functions 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 w_mpi_Initial( nm, im, jm, mint=mint )

  allocate(w_data(nc))
  allocate(xv_data(0:im-1,jc))
  allocate(xv_Ddata(0:im-1,jc))

  !========================= xv_w, w_xv のテスト===========================

  call MessageNotify('M','w_base_mpi_module_test', &
                         'w_base_mpi_module_mint functions xv_w, w_xv tests')

  !==================== Y_1^0 のテスト ====================
  xv_data = sqrt(3.0D0)*sin(xv_Lat)                      ! Y_1^0
  w_data= 0.0D0 ; if ( l_nm(1,0) .ne. 0 ) w_data(l_nm(1,0))=1.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(3.0D0) * ( cos(xv_Lat)**2 - 2*sin(xv_Lat)**2 )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(3.0D0)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(3.0D0) * sin(xv_Lat)**2
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_1^0','Test of Y_1^0 succeeded!')


  !==================== Y_2^0 のテスト ====================
  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_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='Y_2^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0)*(   3.0*sin(xv_Lat)*cos(xv_Lat)**2  &
                           - 3.0*sin(xv_Lat)**3 + sin(xv_Lat)  )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0)*3.0*sin(xv_Lat)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)*sin(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^0','Test of Y_2^0 succeeded!')


  !==================== Y_2^2 のテスト ====================
  xv_data = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*cos(2*xv_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='Y_2^2 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* (-2)* sin(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = - sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = -sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^2','Test of Y_2^2 succeeded!')

  !==================== Y_2^-2 のテスト ====================
  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_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='Y_2^-2 Test of w_xv(xv_data)',                       &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data))',                 &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* 2*cos(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=-1))',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=-1)',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=2))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=2)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^-2','Test of Y_2^-2 succeeded!')


 !------ MPIの終了 ------
  call MessageNotify('M','w_base_mpi_module_mint_test', &
                         'w_mpi_module_mint base functions tests succeeded!')

  call MPI_FINALIZE(IERR)

end program w_mpi_module_base_mint_test
