!----------------------------------------------------------------------
!     Copyright (c) 2018--2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module テストプログラム :: 基本変換関数のテスト
!
!履歴  2018/06/08  竹広真一
!      2020/11/11  竹広真一  セクター計算オプション導入
!
program ut_mpi_module_base_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

  real(8), allocatable :: ut_data(:,:)
  real(8), allocatable :: wt_data(:,:)
  real(8), allocatable :: wh_data(:,:)
  real(8), allocatable :: wz_data(:,:)
  real(8), allocatable :: xvh_data(:,:,:)
  real(8), allocatable :: xvh_xi(:,:,:)

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

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

  !---------------- 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)

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','ut_mpi_module_base_mint_test', &
                         'ut_mpi_module_mint basic transformation functions tests')

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

  allocate(ut_data(nmc,0:lm))
  allocate(wt_data(nc,0:lm))
  allocate(wz_data(nc,0:km))
  allocate(wh_data(nc,kc))
  allocate(xvh_data(0:im-1,jc,kc))
  allocate(xvh_xi(0:im-1,jc,kc))

  xvh_xi = (xvh_Rad - (ro+ri)/2 )*2/(ro-ri)

  !---- Y_1^* のテスト ----
  xvh_data = sqrt(3.0D0)*sin(xvh_Lat)*xvh_xi         ! Y_1^0 T_1
  wt_data= 0.0D0 ; if (l_nm(1,0).ne.0) wt_data(l_nm(1,0),1)=1.0D0
  wz_data= 0.0D0 ; if (l_nm(1,0).ne.0) wz_data(l_nm(1,0),:)=(z_Rad - (ro+ri)/2 )*2/(ro-ri)
  wh_data = 0.0D0 ; if (l_nm(1,0).ne.0) wh_data(l_nm(1,0),:)=(h_Rad - (ro+ri)/2 )*2/(ro-ri)

  ut_data = 0.0D0
  if ( lu_nm(1,0) .ne. 0 ) ut_data(lu_nm(1,0),1) = 1.0D0

  call AssertEqual(&
    message='ut_wt with Y_1^0 T_1',                               &
    answer = ut_data,                                             &
    check = ut_wt(wt_data),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_ut with Y_1^0 T_1',                               &
    answer = wt_data,                                             &
    check = wt_ut(ut_data),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='ut_wh with Y_1^0 T_1',                               &
    answer = ut_data,                                             &
    check = ut_wh(wh_data),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wh_ut with Y_1^0 T_1',                               &
    answer = wh_data,                                             &
    check = wh_ut(ut_data),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='ut_xvh with Y_1^0 T_1',                              &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_1^0 T_1',                              &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_data),                                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

!!$  xvh_data = -sqrt(3.0D0/2)*cos(xvh_Lat)*sin(xvh_Lon)*(2*xvh_xi**2-1) !Y_1^{-1}T_2
!!$  ut_data= 0.0D0 ; if(lu_nm(1,-1).ne.0) ut_data(lu_nm(1,-1),2)=1.0D0/2.0D0
!!$
!!$  call AssertEqual(&
!!$    message='ut_xvh with Y_1^-1 T_2',                             &
!!$    answer = ut_data,                                             &
!!$    check = ut_xvh(xvh_data),                                     &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$  call AssertEqual(&
!!$    message='xvh_ut with Y_1^-1 T_2',                             &
!!$    answer = xvh_data,                                            &
!!$    check = xvh_ut(ut_xvh(xvh_data)),                             &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )

  !---- Y_2^* のテスト ----
  ! Y_2^0 T_3
  xvh_data = sqrt(5.0D0)*(3.0/2*sin(xvh_Lat)**2-1/2.0)*(4*xvh_xi**3-3*xvh_xi)
  ut_data= 0.0D0 ; if(lu_nm(2,0).ne.0) ut_data(lu_nm(2,0),3)=1.0D0

  call AssertEqual(&
    message='ut_xvh with Y_2^0 T_3',                              &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_2^0 T_3',                              &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^2 T_4
  xvh_data = sqrt(5.0D0/24)*3.0*cos(xvh_Lat)**2*cos(2*xvh_Lon) &
            *(8*xvh_xi**4 - 8*xvh_xi**2 + 1 )
  ut_data= 0.0D0 ; if(lu_nm(2,2).ne.0) ut_data(lu_nm(2,2),4)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xvh with Y_2^2 T_4',                              &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_2^2 T_4',                              &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^-2
  xvh_data = -sqrt(5.0D0/24)*3.0*cos(xvh_Lat)**2*sin(2*xvh_Lon) &
            *(16*xvh_xi**5-20*xvh_xi**3+5*xvh_xi)
  ut_data= 0.0D0 ; if(lu_nm(2,-2).ne.0) ut_data(lu_nm(2,-2),5)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xvh with Y_2^-2 T_5',                             &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_2^-2 T_5',                             &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_4^* のテスト ----
  xvh_data = sqrt(9.0D0)*(35.0/8*sin(xvh_Lat)**4-30.0/8*sin(xvh_Lat)**2+3.0/8) &
       *(2*xvh_xi**2-1) ! Y_4^0 T_2
  ut_data= 0.0D0 ; if(lu_nm(4,0).ne.0) ut_data(lu_nm(4,0),2)=1.0D0

  call AssertEqual(&
    message='ut_xvh with Y_4^0 T_2',                              &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_4^0 T_2',                              &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  
  xvh_data &
       = sqrt(9.0D0/360)*15.0/2*cos(xvh_Lat)**2*(7*sin(xvh_Lat)**2-1) &
       * cos(2*xvh_Lon)*xvh_xi         ! Y_4^2 T_1
  ut_data= 0.0D0 ; if(lu_nm(4,2).ne.0) ut_data(lu_nm(4,2),1)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xvh with Y_4^2 T_1',                              &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_4^2 T_1',                              &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xvh_data = - sqrt(9.0D0/40320)*105.0*cos(xvh_Lat)**4*sin(4*xvh_Lon)*0.5D0   ! Y_4^-4 T_0
  ut_data= 0.0D0 ; if(lu_nm(4,-4).ne.0) ut_data(lu_nm(4,-4),0)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xvh with Y_4^-4 T_0',                             &
    answer = ut_data,                                             &
    check = ut_xvh(xvh_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvh_ut with Y_4^-4 T_0',                             &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ut_mpi_module_base_test', &
                         'ut_mpi_module_mint basic functions tests succeeded!')

  !------ MPIの終了 ------
  call MPI_FINALIZE(IERR)


end program ut_mpi_module_base_mint_test
