!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ut_mpi_module_svpack テストプログラム :: 基本変換関数のテスト
!
!履歴  2016/07/29  竹広真一   wt_mpi_module_supack_base_test.f90 より改造
!
program ut_mpi_module_svpack_base_test

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

  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: nm=10, lm=10         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

  real(8), allocatable ::  ut_data(:,:)
  real(8), allocatable ::  xyh_data(:,:,:)
  real(8), allocatable ::  xyh_data1(:,:,:)
  real(8), allocatable ::  xyh_xi(:,:,:)

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

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

 !---------------- MPI スタート ---------------------
  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_svpack_base_test', &
                         'ut_mpi_module_svpack basic transformation functions tests') 

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

  allocate(ut_data(nc,0:lm))
  allocate(xyh_data(0:im-1,jm,kc))
  allocate(xyh_data1(0:im-1,jm,kc))
  allocate(xyh_xi(0:im-1,jm,kc))

  xyh_xi = (xyh_Rad - (ro+ri)/2 )*2/(ro-ri)

  !---- Y_1^* のテスト ----
  xyh_data = sqrt(3.0D0)*sin(xyh_Lat)*xyh_xi            ! Y_1^0 T_1
  ut_data= 0.0D0 ; if( l_nm(1,0) /= 0 ) ut_data(l_nm(1,0),1)=1.0D0

  call AssertEqual(&
    message='ut_xyh with Y_1^0 T_1',                              &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_ut with Y_1^0 T_1',                              &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  
  xyh_data = sqrt(3.0D0/2)*cos(xyh_Lat)*cos(xyh_Lon)     ! Y_1^1 T_0
  ut_data= 0.0D0 ; if( l_nm(1,1) /= 0 ) ut_data(l_nm(1,1),0)=1.0D0/2.0D0*2

  call AssertEqual(&
    message='ut_xyh with Y_1^1 T_0',                              &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyh_ut with Y_1^1 T_0',                              &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xyh_data = -sqrt(3.0D0/2)*cos(xyh_Lat)*sin(xyh_Lon)*(2*xyh_xi**2-1) !Y_1^{-1}T_2
  ut_data= 0.0D0 ;  if( l_nm(1,-1) /= 0 ) ut_data(l_nm(1,-1),2)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xyh with Y_1^-1 T_2',                             &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyh_ut with Y_1^-1 T_2',                             &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  ! Y_2^0 T_3
  xyh_data = sqrt(5.0D0)*(3.0/2*sin(xyh_Lat)**2-1/2.0)*(4*xyh_xi**3-3*xyh_xi) 
  ut_data= 0.0D0 ; if( l_nm(2,0) /= 0 ) ut_data(l_nm(2,0),3)=1.0D0

  call AssertEqual(&
    message='ut_xyh with Y_2^0 T_3',                              &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyh_ut with Y_2^0 T_3',                              &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !Y_2^1 T_4
  xyh_data = sqrt(5.0D0/6)*3.0*sin(xyh_Lat)*cos(xyh_Lat)*cos(xyh_Lon) &
            *(8*xyh_xi**4 - 8*xyh_xi**2 + 1 )
  ut_data= 0.0D0 ; if( l_nm(2,1) /= 0 ) ut_data(l_nm(2,1),4)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xyh with Y_2^1 T_4',                              &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyh_ut with Y_2^1 T_4',                              &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^-2
  xyh_data = -sqrt(5.0D0/24)*3.0*cos(xyh_Lat)**2*sin(2*xyh_Lon) &
            *(16*xyh_xi**5-20*xyh_xi**3+5*xyh_xi)
  ut_data= 0.0D0 ; if( l_nm(2,-2) /= 0 ) ut_data(l_nm(2,-2),5)=1.0D0/2.0D0

  call AssertEqual(&
    message='ut_xyh with Y_2^-2 T_5',                             &
    answer = ut_data,                                             &
    check = ut_cleanup_ut(ut_xyh(xyh_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyh_ut with Y_2^-2 T_5',                             &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的関数のテスト ----
  xyh_data = cos(2*xyh_Lon-pi/3) &
       *(sin(xyh_Lat)-1)**2*(sin(xyh_Lat)-0.5)*(sin(xyh_Lat)+1) &
       *exp(xyh_Rad)

  call AssertEqual(&
    message='xyh_ut and ut_xyh with a general function',          &
    answer = xyh_data,                                            &
    check = xyh_ut(ut_xyh(xyh_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ut_mpi_module_svpack_base_test', &
                         'ut_mpi_module_svpack basic functions tests succeeded!') 

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

  call MPI_FINALIZE(IERR)

contains

  function ut_cleanup_ut(ut_data)
    real(8), intent(IN) :: ut_data(nc,0:lm)
    real(8)             :: ut_cleanup_ut(nc,0:lm)

    integer l,nmary(2)

    ut_cleanup_ut = ut_data

    do l=1,nc
       nmary = nm_l(l)
       if ( nmary(1) .lt. 0 ) ut_cleanup_ut(l,:) = 0.0D0
    end do

 end function ut_cleanup_ut

end program ut_mpi_module_svpack_base_test
