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

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

  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  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_test_base', &
                         'ut_mpi_module basic transformation functions tests')

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

  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^1 T_4
  xvh_data = sqrt(5.0D0/6)*3.0*sin(xvh_Lat)*cos(xvh_Lat)*cos(xvh_Lon) &
            *(8*xvh_xi**4 - 8*xvh_xi**2 + 1 )
  ut_data= 0.0D0 ; if (lu_nm(2,1).ne.0) ut_data(lu_nm(2,1),4)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xvh with Y_2^1 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^1 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     &
    )

  !---- 一般的関数のテスト ----
  xvh_data = cos(2*xvh_Lon-pi/3) &
       *(sin(xvh_Lat)-1)**2*(sin(xvh_Lat)-0.5)*(sin(xvh_Lat)+1) &
       *exp(xvh_Rad)

  call AssertEqual(&
    message='xvh_ut and ut_xvh with a general function',          &
    answer = xvh_data,                                            &
    check = xvh_ut(ut_xvh(xvh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ut_mpi_module_test_base', &
                         'ut_mpi_module basic functions tests succeeded!')

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


end program ut_mpi_module_base_test
