!----------------------------------------------------------------------
!     Copyright (c) 2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wt_wave_module テストプログラム
!
!      トロイダルポテンシャルの境界値問題
!
!履歴  2020/11/24  竹広真一   wt_wave_module_svpack_torvelbc_tau_test.f90 を改造
!
program wt_wave_module_torvelbc_tau_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wt_wave_module

  implicit none

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

  real(8), dimension(0:im-1,1:jm,0:km)     :: xyz_Toroidal
  real(8), dimension(2*(nm-m+1),0:lm)      :: wt_Toroidal
  real(8), dimension(0:im-1,1:jm,0:km)     :: xyz_Toroidal_sol
  real(8), dimension(0:im-1,1:jm,0:km)     :: xyz_dToroidaldz
  real(8), dimension(0:im-1,1:jm)          :: xy_Zero=0.0D0

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

  call MessageNotify('M','wt_wave_module_torvelbc_tau_test', &
       'wt_wave_module wt_LaplaPol2polTau_wt subroutine tests')

  call wt_initial(im,jm,km,nm,m,lm,ri,ro)

  !----- wt_TorBoundariesTau (FF) ------
  ! P_1^1
  xyz_Toroidal_sol = cos(xyz_Lat) * xyz_Rad * (xyz_Rad-ri)**2 * (xyz_Rad-ro)**2
  
  wt_Toroidal = wt_xyz(xyz_Toroidal_sol)
  call wt_TorBoundariesTau(wt_Toroidal,cond='FF',new=.true.) 
  xyz_Toroidal = xyz_wt(wt_Toroidal)
  xyz_dToroidaldz   = xyz_wt(wt_DRad_wt(wt_xyz(xyz_Toroidal/xyz_Rad)))

  call AssertEqual(&
    message='wt_TorBoundariesTau (FF)',                          &
    answer =xyz_Toroidal_sol(:,:,1:km-1),                         &
    check = xyz_Toroidal(:,:,1:km-1),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (FF, Top)',                     &
    answer =xy_Zero,                                              &
    check = xyz_dToroidaldz(:,:,0),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (FF, Bottom)',                  &
    answer =xy_Zero,                                              &
    check = xyz_dToroidaldz(:,:,km),                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- wt_Toroidal2PolTau (RF) ------

  ! P_1^1
  xyz_Toroidal_sol = cos(xyz_Lat) * xyz_Rad*(xyz_Rad-ri)**2*(xyz_Rad-ro)
   
  wt_Toroidal = wt_xyz(xyz_Toroidal_sol)
  call wt_TorBoundariesTau(wt_Toroidal,cond='RF',new=.true.) 
  xyz_Toroidal = xyz_wt(wt_Toroidal)
  xyz_dToroidaldz   = xyz_wt(wt_DRad_wt(wt_xyz(xyz_Toroidal/xyz_Rad)))

  call AssertEqual(&
    message='wt_TorBoundariesTau (RF)',                          &
    answer =xyz_Toroidal_sol(:,:,1:km-1),                         &
    check = xyz_Toroidal(:,:,1:km-1),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (RF, Top)',                     &
    answer =xy_Zero,                                              &
    check = xyz_Toroidal(:,:,0),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (RF, Bottom)',                  &
    answer =xy_Zero,                                              &
    check = xyz_dToroidaldz(:,:,km),                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- wt_Toroidal2PolTau (FR) ------
  ! P_1^1
  xyz_Toroidal_sol = cos(xyz_Lat) * xyz_Rad*(xyz_Rad-ri)*(xyz_Rad-ro)**2

  wt_Toroidal = wt_xyz(xyz_Toroidal_sol)
  call wt_TorBoundariesTau(wt_Toroidal,cond='FR',new=.true.) 
  xyz_Toroidal = xyz_wt(wt_Toroidal)
  xyz_dToroidaldz   = xyz_wt(wt_DRad_wt(wt_xyz(xyz_Toroidal/xyz_Rad)))

  call AssertEqual(&
    message='wt_TorBoundariesTau (FR)',                          &
    answer =xyz_Toroidal_sol(:,:,1:km-1),                         &
    check = xyz_Toroidal(:,:,1:km-1),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (FR, Top)',                     &
    answer =xy_Zero,                                              &
    check = xyz_dToroidaldz(:,:,0),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (FR, Bottom)',                  &
    answer =xy_Zero,                                              &
    check = xyz_Toroidal(:,:,km),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- wt_Toroidal2PolTau (RR) ------
  ! P_1^1
  xyz_Toroidal_sol = cos(xyz_Lat) * (xyz_Rad-ri)*(xyz_Rad-ro)

  wt_Toroidal = wt_xyz(xyz_Toroidal_sol)
  call wt_TorBoundariesTau(wt_Toroidal,cond='RR',new=.true.) 
  xyz_Toroidal = xyz_wt(wt_Toroidal)
  xyz_dToroidaldz   = xyz_wt(wt_DRad_wt(wt_xyz(xyz_Toroidal/xyz_Rad)))

  call AssertEqual(&
    message='wt_TorBoundariesTau (RR)',                          &
    answer =xyz_Toroidal_sol(:,:,1:km-1),                         &
    check = xyz_Toroidal(:,:,1:km-1),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (RR, Top)',                     &
    answer =xy_Zero,                                              &
    check = xyz_Toroidal(:,:,0),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='wt_TorBoundariesTau (RR, Bottom)',                  &
    answer =xy_Zero,                                              &
    check = xyz_Toroidal(:,:,km),                                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wt_wave_module_torvelbc_tau_test', &
       'wt_wave_module wt_TorBoundariesTau subroutine tests suceeded!')

end program wt_wave_module_torvelbc_tau_test

