!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wsc_mpi_module_supack テストプログラム :: 基本変換関数のテスト
!
!履歴  2017/05/30  竹広真一   wt_mpi_module_supack_test_base.f90 より改造
!
program wsc_mpi_module_supack_base_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wsc_mpi_module_supack
  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 ::  ws_data(:,:)
  real(8), allocatable ::  wc_data(:,:)  
  real(8), allocatable ::  xvz_data(:,:,:)
  real(8), allocatable ::  xvz_data1(:,:,:)
  real(8), allocatable ::  xvz_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','wsc_mpi_module_supack_base_test', &
                         'wsc_mpi_module_supack basic transformation functions tests') 

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

  allocate(wc_data(nc,0:lm))
  allocate(ws_data(nc,lm))  
  allocate(xvz_data(0:im-1,jc,0:km))
  allocate(xvz_data1(0:im-1,jc,0:km))
  allocate(xvz_xi(0:im-1,jc,0:km))

  xvz_xi = (xvz_Rad - ri)/(ro-ri)

  !---- Y_1^* のテスト ----
  xvz_data = sqrt(3.0D0)*sin(xvz_Lat)*cos(PI*xvz_xi)        ! Y_1^0 cos(z)
  wc_data= 0.0D0 ; if( l_nm(1,0) /= 0 ) wc_data(l_nm(1,0),1)=1.0D0

  call AssertEqual(&
    message='wc_xvz with Y_1^0 cos(z)',                           &
    answer = wc_data,                                             &
    check = wc_cleanup_wc(wc_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_wc with Y_1^0 cos(z)',                           &
    answer = xvz_data,                                            &
    check = xvz_wc(wc_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  
  xvz_data = sqrt(3.0D0)*sin(xvz_Lat)*sin(PI*xvz_xi)        ! Y_1^0 sin(z)
  ws_data= 0.0D0 ; if( l_nm(1,0) /= 0 ) ws_data(l_nm(1,0),1)=1.0D0

  call AssertEqual(&
    message='ws_xvz with Y_1^0 sin(z)',                           &
    answer = ws_data,                                             &
    check = ws_cleanup_ws(ws_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_ws with Y_1^0 sin(z)',                           &
    answer = xvz_data,                                            &
    check = xvz_ws(ws_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  
  xvz_data = sqrt(3.0D0/2)*cos(xvz_Lat)*cos(xvz_Lon)     ! Y_1^1 
  wc_data= 0.0D0 ; if( l_nm(1,1) /= 0 ) wc_data(l_nm(1,1),0)=1.0D0/2.0D0*2

  call AssertEqual(&
    message='wc_xvz with Y_1^1',                                  &
    answer = wc_data,                                             &
    check = wc_cleanup_wc(wc_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_wc with Y_1^1',                                  &
    answer = xvz_data,                                            &
    check = xvz_wc(wc_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xvz_data = -sqrt(3.0D0/2)*cos(xvz_Lat)*sin(xvz_Lon)*cos(2*PI*xvz_Xi) !Y_1^{-1}cos(2z)
  wc_data= 0.0D0 ;  if( l_nm(1,-1) /= 0 ) wc_data(l_nm(1,-1),2)=1.0D0/2.0D0

  call AssertEqual(&
    message='wc_xvz with Y_1^-1 cos(2z)',                         &
    answer = wc_data,                                             &
    check = wc_cleanup_wc(wc_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_wc with Y_1^-1 cos(2z)',                         &
    answer = xvz_data,                                            &
    check = xvz_wc(wc_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xvz_data = -sqrt(3.0D0/2)*cos(xvz_Lat)*sin(xvz_Lon)*sin(2*PI*xvz_Xi) !Y_1^{-1}sin(2z)
  ws_data= 0.0D0 ;  if( l_nm(1,-1) /= 0 ) ws_data(l_nm(1,-1),2)=1.0D0/2.0D0

  call AssertEqual(&
    message='ws_xvz with Y_1^-1 sin(2z)',                         &
    answer = ws_data,                                             &
    check = ws_cleanup_ws(ws_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_ws with Y_1^-1 sin(2z)',                         &
    answer = xvz_data,                                            &
    check = xvz_ws(ws_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  ! Y_2^0 cos(3z)
  xvz_data = sqrt(5.0D0)*(3.0/2*sin(xvz_Lat)**2-1/2.0)*cos(3*PI*xvz_Xi)
  wc_data= 0.0D0 ; if( l_nm(2,0) /= 0 ) wc_data(l_nm(2,0),3)=1.0D0

  call AssertEqual(&
    message='wc_xvz with Y_2^0 cos(3z)',                          &
    answer = wc_data,                                             &
    check = wc_cleanup_wc(wc_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_wc with Y_2^0 cos(3z)',                          &
    answer = xvz_data,                                            &
    check = xvz_wc(wc_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^0 sin(3z)
  xvz_data = sqrt(5.0D0)*(3.0/2*sin(xvz_Lat)**2-1/2.0)*sin(3*PI*xvz_Xi)
  ws_data= 0.0D0 ; if( l_nm(2,0) /= 0 ) ws_data(l_nm(2,0),3)=1.0D0

  call AssertEqual(&
    message='ws_xvz with Y_2^0 sin(3z)',                          &
    answer = ws_data,                                             &
    check = ws_cleanup_ws(ws_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_ws with Y_2^0 sin(3z)',                          &
    answer = xvz_data,                                            &
    check = xvz_ws(ws_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !Y_2^1 cos(4z)
  xvz_data = sqrt(5.0D0/6)*3.0*sin(xvz_Lat)*cos(xvz_Lat)*cos(xvz_Lon) &
            *cos(4*Pi*xvz_xi)
  wc_data= 0.0D0 ; if( l_nm(2,1) /= 0 ) wc_data(l_nm(2,1),4)=1.0D0/2.0D0

  call AssertEqual(&
    message='wc_xvz with Y_2^1 cos(4z)',                          &
    answer = wc_data,                                             &
    check = wc_cleanup_wc(wc_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_wc with Y_2^1 cos(4z)',                          &
    answer = xvz_data,                                            &
    check = xvz_wc(wc_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^-2 sin(5z)
  xvz_data = -sqrt(5.0D0/24)*3.0*cos(xvz_Lat)**2*sin(2*xvz_Lon) &
            *sin(5*PI*xvz_Xi)
  ws_data= 0.0D0 ; if( l_nm(2,-2) /= 0 ) ws_data(l_nm(2,-2),5)=1.0D0/2.0D0

  call AssertEqual(&
    message='ws_xvz with Y_2^-2 sin(5z)',                         &
    answer = ws_data,                                             &
    check = ws_cleanup_ws(ws_xvz(xvz_data)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xvz_ws with Y_2^-2 sin(5z)',                         &
    answer = xvz_data,                                            &
    check = xvz_ws(ws_xvz(xvz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wsc_mpi_module_supack_base_test', &
                         'wsc_mpi_module_supack basic functions tests succeeded!') 

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

  call MPI_FINALIZE(IERR)

contains

  function wc_cleanup_wc(wc_data)
    real(8), intent(IN) :: wc_data(nc,0:lm)
    real(8)             :: wc_cleanup_wc(nc,0:lm)

    integer l,nmary(2)

    wc_cleanup_wc = wc_data

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

 end function wc_cleanup_wc

  function ws_cleanup_ws(ws_data)
    real(8), intent(IN) :: ws_data(nc,lm)
    real(8)             :: ws_cleanup_ws(nc,lm)

    integer l,nmary(2)

    ws_cleanup_ws = ws_data

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

 end function ws_cleanup_ws

end program wsc_mpi_module_supack_base_test
