!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_spectrum_module テストプログラム :: スペクトル解析関数のテスト
!
!履歴  2019/03/19  竹広真一   w_test_spectrum.f90 を改造, SYPACK 対応
!      2020/11/09  竹広真一 セクター計算テスト
!
program w_mpi_module_spectrum_mint_test

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

!!$  integer, parameter :: im=32, jm=16, nm=10, mint=1
  integer, parameter :: im=32, jm=16, nm=10, mint=2

  real(DP), allocatable :: w_data(:)
  real(DP), allocatable :: xv_data(:,:)

  real(DP), dimension(0:nm,-nm:nm) ::  nm_data
  real(DP), dimension(0:nm)        ::  n_data
  real(DP)                         ::  data

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

  integer :: n,m
  integer :: l, nmary(2)
  integer(8) :: n8, m8, icom8

  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)
  call MPI_BARRIER(MPI_COMM_WORLD,IERR)
  
  call w_mpi_Initial(nm, im, jm, mint=mint )

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

  call MessageNotify('M', &
    & 'w_mpi_module_spectrum_mint_test', &
    & 'w_mpi_module_mint spectrum tests')

!!$  !================= 2Y_1^0+3Y_2^1 のテスト ================
!!$  xv_data = 2*sqrt(3.0D0)*sin(xv_Lat) &        ! 2Y_1^0+3Y_2^1
!!$       + 3*sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * cos(xv_Lon)!
!!$  w_data = w_xv(xv_data)
!!$
!!$  !--------- Energy spectrum ---------
!!$  nm_data = w_spectrum_VMiss
!!$  do n=0,nm
!!$     do m=-n,n
!!$        nm_data(n,m) = 0.0D0
!!$     enddo
!!$  enddo
!!$  nm_data(1,0) = 1*2*(2**2)/2
!!$  nm_data(2,1) = 2*3*(3.0D0/2)**2/2
!!$  nm_data(2,-1) =  nm_data(2,1)
!!$  call AssertEqual(&
!!$    message='nm_EnergyFromStreamfunc_w with 2Y_1^0+3Y_2^1 ',      &
!!$    answer = nm_data,                                             &
!!$    check = nm_EnergyFromStreamfunc_w(w_data),                    &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  n_data = 0.0; n_data(1) = 4.0D0 ;   n_data(2) = 2*3*(3.0D0/2)**2
!!$  call AssertEqual(&
!!$    message='n_EnergyFromStreamfunc_w with 2Y_1^0+3Y_2^1 ',       &
!!$    answer = n_data,                                              &
!!$    check = n_EnergyFromStreamfunc_w(w_data),                     &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  nm_Data = nm_EnergyFromStreamfunc_w(w_data)
!!$  data = 0.0D0
!!$  do n=0,nm
!!$     do m=-n,n
!!$        data = data + nm_Data(n,m)
!!$     enddo
!!$  enddo
!!$
!!$  call AssertEqual(&
!!$    message='total energy by nm-spectrum with 2Y_1^0+3Y_2^1 ',    &
!!$    answer = AvrLonLat_xv( &
!!$         0.5*(xv_GradLon_w(w_data)**2+xv_GradLat_w(w_data)**2) ), &
!!$    check = data,                                                 &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  data = AvrLonLat_xv( 0.5*(xv_GradLon_w(w_data)**2+xv_GradLat_w(w_data)**2) )
!!$  call AssertEqual(&
!!$    message='total energy by n-spectrum with 2Y_1^0+3Y_2^1 ',     &
!!$    answer = data,                                                &
!!$    check = sum(n_EnergyFromStreamfunc_w(w_data)),                &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  !--------- Enstrophy spectrum ---------
!!$  nm_data=w_spectrum_VMiss
!!$  do n=0,nm
!!$     do m=-n,n
!!$        nm_data(n,m) = 0.0
!!$     enddo
!!$  enddo
!!$  nm_data(1,0) = (1**2)*(2**2)*(2**2)/2
!!$  nm_data(2,1) = (2**2)*(3**2)*(3.0D0/2.0D0)**2/2
!!$  nm_data(2,-1) = nm_data(2,1)
!!$  call AssertEqual(&
!!$    message='nm_EnstrophyFromStreamfunc_w with 2Y_1^0+3Y_2^1 ',   &
!!$    answer = nm_data,                                             &
!!$    check = nm_EnstrophyFromStreamfunc_w(w_data),                 &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  n_data = 0.0;
!!$  n_data(1) = (1**2)*(2**2)*(2**2)/2
!!$  n_data(2) = (2**2)*(3**2)*(3.0D0/2.0D0)**2
!!$  call AssertEqual(&
!!$    message='n_EnstrophyFromStreamfunc_w with 2Y_1^0+3Y_2^1 ',    &
!!$    answer = n_data,                                              &
!!$    check = n_EnstrophyFromStreamfunc_w(w_data),                  &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  nm_Data = nm_EnstrophyFromStreamfunc_w(w_data)
!!$  data = 0.0D0
!!$  do n=0,nm
!!$     do m=-n,n
!!$        data = data + nm_Data(n,m)
!!$     enddo
!!$  enddo
!!$
!!$  call AssertEqual(&
!!$    message='total enstrophy by nm-spectrum with 2Y_1^0+3Y_2^1 ', &
!!$    answer =  AvrLonLat_xv( 0.5*(xv_w(w_Lapla_w(w_data))**2)),    &
!!$    check = data,                                                 &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
!!$  data = AvrLonLat_xv( 0.5*(xv_w(w_Lapla_w(w_data))**2))
!!$  call AssertEqual(&
!!$    message='total enstrophy by n-spectrum with 2Y_1^0+3Y_2^1 ',  &
!!$    answer = data,                                                &
!!$    check = sum(n_EnstrophyFromStreamfunc_w(w_data)),             &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )

  !======== 3*Y_1^0 + 2Y_1^1 + 2Y_2^0 +Y_2^-2 のテスト ========
  xv_data = 3*sqrt(3.0D0)*sin(xv_Lat) &
          + 2*sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0) &
          - sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)
  w_data = w_xv(xv_data)

  !--------- Energy spectrum ---------
  nm_data=w_spectrum_VMiss
  do n=0,nm
     do m=-n,n
        nm_data(n,m) = 0.0
     enddo
  enddo
  nm_data(1,0)  = 1*2*3**2/2
  nm_data(2,0)  = 2*3*2**2/2
  nm_data(2,-2) = 2*3*(1.0D0/2.0D0)**2/2
  nm_data(2,2)  = nm_data(2,-2)
  call AssertEqual(&
    message='nm_EnergyFromStreamfunc_w with 3Y_1^0 + 2Y_2^0 +Y_2^-2',&
    answer = nm_data,                                             &
    check = nm_EnergyFromStreamfunc_w(w_data),                    &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  n_data = 0.0
  n_data(1) = 1*2*3**2/2
  n_data(2) = 2*3*2**2/2 + 2*3*(1.0D0/2.0D0)**2
  call AssertEqual(&
    message='n_EnergyFromStreamfunc_w with 3Y_1^0 + 2Y_2^0 +Y_2^-2', &
    answer = n_data,                                              &
    check = n_EnergyFromStreamfunc_w(w_data),                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  nm_Data = nm_EnergyFromStreamfunc_w(w_data)
  data = 0.0D0
  do n=0,nm
     do m=-n,n
        data = data + nm_Data(n,m)
     enddo
  enddo

  call AssertEqual(&
    message='total energy by nm-spectrum with 3Y_1^0 + 2Y_2^0 +Y_2^-2', &
    answer = AvrLonLat_xv( &
         0.5*(xv_GradLon_w(w_data)**2+xv_GradLat_w(w_data)**2) ), &
    check = data,                                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  data = AvrLonLat_xv( 0.5*(xv_GradLon_w(w_data)**2+xv_GradLat_w(w_data)**2) )
  call AssertEqual(&
    message='total energy by n-spectrum with 3Y_1^0 + 2Y_1^1 + 2Y_2^0 +Y_2^-2', &
    answer = data,                                                &
    check = sum(n_EnergyFromStreamfunc_w(w_data)),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !--------- Enstrophy spectrum ---------
  nm_data=w_spectrum_VMiss
  do n=0,nm
     do m=-n,n
        nm_data(n,m) = 0.0
     enddo
  enddo
  nm_data(1,0)  = (1**2)*(2**2)*3**2/2
  nm_data(2,0)  = (2**2)*(3**2)*2**2/2
  nm_data(2,-2) = (2**2)*(3**2)*(1.0D0/2.0D0)**2/2
  nm_data(2,2)  = nm_data(2,-2)
  call AssertEqual(&
    message='nm_EnstrophyFromStreamfunc_w with 3Y_1^0 + 2Y_2^0 +Y_2^-2', &
    answer = nm_data,                                             &
    check = nm_EnstrophyFromStreamfunc_w(w_data),                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  n_data = 0.0
  n_data(1) = (1**2)*(2**2)*3**2/2 
  n_data(2) = (2**2)*(3**2)*2**2/2 + (2**2)*(3**2)*(1.0D0/2.0D0)**2
  call AssertEqual(&
    message='n_EnstrophyFromStreamfunc_w with 3Y_1^0 + 2Y_2^0 +Y_2^-2', &
    answer = n_data,                                              &
    check = n_EnstrophyFromStreamfunc_w(w_data),                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  nm_Data = nm_EnstrophyFromStreamfunc_w(w_data)
  data = 0.0D0
  do n=0,nm
     do m=-n,n
        data = data + nm_Data(n,m)
     enddo
  enddo

  call AssertEqual(&
    message='total enstrophy by nm-spectrum with 3Y_1^0 + 2Y_2^0 +Y_2^-2',&
    answer =  AvrLonLat_xv( 0.5*(xv_w(w_Lapla_w(w_data))**2)),    &
    check = data,                                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  data = AvrLonLat_xv( 0.5*(xv_w(w_Lapla_w(w_data))**2))
  call AssertEqual(&
    message='total enstrophy by n-spectrum with 3Y_1^0 + 2Y_1^1 + 2Y_2^0 +Y_2^-2',&
    answer = data,                                                &
    check = sum(n_EnstrophyFromStreamfunc_w(w_data)),             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','w_mpi_module_spectrum_mint_test', &
                         'w_mpi_module_mint spectrum tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program w_mpi_module_spectrum_mint_test

