!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wtq_mpi_module_supack テストプログラム :: スペクトル関数のテスト
!
!履歴  2016/04/04  竹広真一 
!
program wtq_mpi_module_supack_spectrum_test

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

  integer,parameter  :: im=32, jm=16         ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: kmo=16, kmi=10       ! 格子点の設定(球殻動径, 球動径)
  integer,parameter  :: nm=10                ! 切断波数の設定(水平)
  integer,parameter  :: lmo=10, lmi=16       ! 切断波数の設定(球殻動径, 球動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径
 
  real(8), allocatable ::  xvz_Torvel(:,:,:)
  real(8), allocatable ::  wt_Torvel(:,:)

  real(8), allocatable ::  xvz_Polvel(:,:,:)
  real(8), allocatable ::  wt_Polvel(:,:)

  real(8), allocatable ::  xvz_Vlon(:,:,:)
  real(8), allocatable ::  xvz_Vlat(:,:,:)
  real(8), allocatable ::  xvz_Vrad(:,:,:)

  real(8), dimension(0:nm,-nm:nm,0:kmo) ::  nmz_EkTor
  real(8), dimension(0:nm,-nm:nm,0:kmo) ::  nmz_EkPol
  real(8), dimension(0:nm,0:kmo)        ::  nz_EkTor
  real(8), dimension(0:nm,0:kmo)        ::  nz_EkPol

  real(8), dimension(0:kmo)             ::  z_Data

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

  real(8) :: PI
  integer :: n,m
  integer :: iproc, np, ierr

  PI = 4.0D0*Atan(1.0D0)

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  call wtq_mpi_Initial(im,jm,kmi,kmo,nm,lmi,lmo,ri,ro)
 
  call MessageNotify('M','wtq_mpi_module_supack_spectrum_test',&
                         'wtq_mpi_module_supack spectrum functions tests.') 


  allocate(xvz_Torvel(0:im-1,jc,0:kmo))
  allocate(wt_Torvel(nc,0:lmo))

  allocate(xvz_Polvel(0:im-1,jc,0:kmo))
  allocate(wt_Polvel(nc,0:lmo))

  allocate(xvz_Vlon(0:im-1,jc,0:kmo))
  allocate(xvz_Vlat(0:im-1,jc,0:kmo))
  allocate(xvz_Vrad(0:im-1,jc,0:kmo))


  ! (2Y_1^0+3Y_2^1) * r
  xvz_Torvel =( 2*sqrt(3.0D0)*sin(xvz_Lat) &
              + 3*sqrt(5.0D0/6)*3.0*sin(xvz_Lat)*cos(xvz_Lat) * cos(xvz_Lon) )&
              * xvz_Rad

  ! (3*Y_1^0 + 2Y_1^1 + 2Y_2^0 +Y_2^-2) * r^2
  xvz_Polvel =( 3*sqrt(3.0D0)*sin(xvz_Lat) & 
                + 2* sqrt(3.0D0/2)*cos(xvz_Lat)*cos(xvz_Lon) &
                + 2*sqrt(5.0D0)*(3.0/2*sin(xvz_Lat)**2-1/2.0) &
                - sqrt(5.0D0/24)*3.0*cos(xvz_Lat)**2*sin(2*xvz_Lon) ) &
              * xvz_Rad**2

  wt_Torvel = wt_xvz(xvz_Torvel)
  wt_Polvel = wt_xvz(xvz_Polvel)

  !--------- Energy spectrum ---------
  nmz_EkTor=wt_VMiss
  nmz_EkPol=wt_VMiss

  do n=0,nm
     do m=-n,n
        nmz_EkTor(n,m,:) = 0.0D0
        nmz_EkPol(n,m,:) = 0.0D0
     enddo
  enddo

  nmz_EkTor(1,0,:) = 4.0D0 * z_Rad**2 * (4*pi)*z_Rad**2
  nmz_EkTor(2,1,:) = 2*3*(3.0D0/2)**2/2 * z_Rad**2 * (4*pi)*z_Rad**2 
  nmz_EkTor(2,-1,:)= nmz_EkTor(2,1,:)

  nmz_EkPol(1,0,:) = 9.0D0 * (9*z_Rad**4 + 2.0D0*z_Rad**4) * (4*pi)
  nmz_EkPol(1,1,:) = 1*2*(2/2)**2/2 &
       * (9*z_Rad**4 + 1*2* z_Rad**4) * (4*pi)
  nmz_EkPol(1,-1,:)= nmz_EkPol(1,1,:)
  nmz_EkPol(2,0,:) = 12.0D0 *(9*z_Rad**4 + 6.0D0*z_Rad**4) * (4*pi)
  nmz_EkPol(2,-2,:)= 2*3*(1.0D0/2)**2/2 &
       *(9*z_Rad**4 + 2*3* z_Rad**4 ) * (4*pi)
  nmz_EkPol(2,2,:) = nmz_EkPol(2,-2,:)

  call AssertEqual(&
    message='nmz_ToroidalEnergySpectrum_wt',                      &
    answer = nmz_ToroidalEnergySpectrum_wt(wt_Torvel),            &
    check =  nmz_EkTor,                                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='nmz_PoloidalEnergySpectrum_wt',                      &
    answer = nmz_PoloidalEnergySpectrum_wt(wt_Polvel),            &
    check =  nmz_EkPol,                                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  nz_EkTor = 0.0D0
  nz_EkTor(1,:) = 4.0D0 * z_Rad**2 * (4*pi)*z_Rad**2 
  nz_EkTor(2,:) = 2 * 3 *(3.0D0/2)**2 * z_Rad**2 * (4*pi)*z_Rad**2 

  nz_EkPol = 0.0D0
  nz_EkPol(1,:) = (9.0D0+1*2*(2/2)**2/2*2 )*(9*z_Rad**4+2*z_Rad**4)*(4*pi)
  nz_EkPol(2,:) = (12.0D0+2*3*(1.0D0/2)**2)*(9*z_Rad**4+2*3*z_Rad**4)*(4*pi)

  call AssertEqual(&
    message='nz_ToroidalEnergySpectrum_wt',                       &
    answer = nz_ToroidalEnergySpectrum_wt(wt_Torvel),             &
    check =  nz_EkTor,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='nz_PoloidalEnergySpectrum_wt',                       &
    answer = nz_PoloidalEnergySpectrum_wt(wt_Polvel),             &
    check =  nz_EkPol,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call wt_Potential2VectorMPI(xvz_Vlon,xvz_Vlat,xvz_Vrad,wt_Torvel,wt_Polvel)

  nmz_EkTor = nmz_ToroidalEnergySpectrum_wt(wt_Torvel)
  nmz_EkPol = nmz_PoloidalEnergySpectrum_wt(wt_Polvel)

  z_Data = 0.0D0
  do n=0,nm
     do m=-n,n
        z_Data = z_Data + nmz_EkTor(n,m,:) + nmz_EkPol(n,m,:)
     enddo
  enddo

  call AssertEqual(&
    message='Total energy by nmz-spectrum',                        &
    answer = IntLonLatRad_xvz(xvz_VLon**2+xvz_VLat**2+xvz_VRad**2)/2.0D0, &
    check =  IntRad_z(z_Data/z_Rad**2),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  nz_EkTor = nz_ToroidalEnergySpectrum_wt(wt_Torvel)
  nz_EkPol = nz_PoloidalEnergySpectrum_wt(wt_Polvel)

  call AssertEqual(&
    message='Total energy by nz-spectrum',                        &
    answer = IntLonLatRad_xvz(xvz_VLon**2+xvz_VLat**2+xvz_VRad**2)/2.0D0, &
    check =   IntRad_z(sum(nz_EkTor,1)/z_Rad**2) &
            + IntRad_z(sum(nz_EkPol,1)/z_Rad**2), &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wtq_mpi_module_supack_spectrum_test',&
                         'wtq_mpi_module_supack spectrum functions tests suceeded!') 

  CALL MPI_FINALIZE(IERR)

end program wtq_mpi_module_supack_spectrum_test
