!----------------------------------------------------------------------
!     Copyright (c) 2013 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_plane_module テストプログラム :: 基本変換関数のテスト
!
!履歴  2015/11/16  竹広真一
!
program w_plane_module_base_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use w_plane_module
  implicit none

  integer, parameter :: im=128, jm=64, nm=42
  real(8), parameter :: Radius = 1.0D0

  real(8), parameter :: sigma = 5.0D-1
  integer :: m
  
  real(8), dimension(0:im-1,1:jm)   ::  ps_data
  real(8), dimension(0:im-1,1:jm)   ::  ps_Ddata
  real(8) :: pi

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

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','w_plane_module', &
                         'w_plane_module baseic functions tests') 

  call w_plane_Initial( nm, im, jm, radius )

  do m=0,2
     write(6,*) 'case m=',m
  
     !==================== 変換・微分テスト ====================
     ps_data = exp( -ps_S**2/sigma**2 ) * sin( m * ps_Phi ) * ps_S**m

     !---- 基本変換 ----
     call AssertEqual(&
          message='Test of ps_w, w_ps',                                 &
          answer = ps_data,                                             &
          check = ps_w(w_ps(ps_data)),                                  &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Phi 微分 ----
     ps_Ddata = m * exp( -ps_S**2/sigma**2 ) * cos(m*ps_Phi) * ps_S**m
     call AssertEqual(&
          message='Test of w_Phi_w',                                    &
          answer = ps_Ddata,                                            &
          check = ps_w(w_DPhi_w(w_ps(ps_data))),                        &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Phi 微分 ----
     ps_Ddata = m * exp( -ps_S**2/sigma**2 ) * cos(m*ps_Phi) * ps_S**m
     call AssertEqual(&
          message='Test of w_Phi_ps',                                   &
          answer = ps_Ddata,                                            &
          check = ps_w(w_DPhi_ps(ps_data)),                             &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Phi 微分 ----
     ps_Ddata = m * exp( -ps_S**2/sigma**2 ) * cos(m*ps_Phi) * ps_S**m
     call AssertEqual(&
          message='Test of ps_DPhi_w',                                  &
          answer = ps_Ddata,                                            &
          check = ps_DPhi_w(w_ps(ps_data)),                             &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- S 微分 ----
     ps_Ddata = ( - 2* ps_S/sigma**2 * ps_S**m + m*ps_S**(m-1) ) &
          * exp( -ps_S**2/sigma**2 ) * sin(m*ps_Phi)
     call AssertEqual(&
          message='Test of ps_Ds_w',                                    &
          answer = ps_Ddata,                                            &
          check = ps_DS_w(w_ps(ps_data)),                               &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- X 微分 ----
     ps_Ddata &
          = ( - 2* ps_S/sigma**2 * ps_S**m + m*ps_S**(m-1) ) * cos(ps_Phi) &
          * exp( -ps_S**2/sigma**2 ) * sin(m*ps_Phi) &
          - m*ps_S**(m-1) * exp( -ps_S**2/sigma**2 ) * cos(m*ps_Phi) * sin(ps_Phi)
     call AssertEqual(&
          message='Test of ps_DX_w',                                    &
          answer = ps_Ddata,                                            &
          check = ps_DX_w(w_ps(ps_data)),                               &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Y 微分 ----
     ps_Ddata &
          = ( - 2* ps_S/sigma**2 * ps_S**m + m*ps_S**(m-1) ) * sin(ps_Phi) &
          * exp( -ps_S**2/sigma**2 ) * sin(m*ps_Phi) &
          + m*ps_S**(m-1) * exp( -ps_S**2/sigma**2 ) * cos(m*ps_Phi) * cos(ps_Phi)
     call AssertEqual(&
          message='Test of ps_DY_w',                                    &
          answer = ps_Ddata,                                            &
          check = ps_DY_w(w_ps(ps_data)),                               &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Laplacian ----
     ps_Ddata &
          = ( - 4*(m+1)*ps_S**m/sigma**2 + 4*ps_S**(m+2)/sigma**4 )  &
          * exp( -ps_S**2/sigma**2 ) * sin(m*ps_Phi)
     call AssertEqual(&
          message='Test of ps_Lapla_w',                                 &
          answer = ps_Ddata,                                            &
          check = ps_Lapla_w(w_ps(ps_data)),                            &
          significant_digits = check_digits, ignore_digits = ignore     &
          )

     !---- Inverse Laplacian ----
     ps_Ddata &
          = ( - 4*(m+1)*ps_S**m/sigma**2 + 4*ps_S**(m+2)/sigma**4 )  &
          * exp( -ps_S**2/sigma**2 ) * sin(m*ps_Phi)
     call AssertEqual(&
          message='Test of w_LaplaInv_ps',                              &
          answer = ps_data,                                             &
          check = ps_w(w_LaplaInv_ps(ps_ddata)),                        &
          significant_digits = check_digits, ignore_digits = ignore     &
          )
  
  enddo
  
end program w_plane_module_base_test

