!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_deriv_mpi_module_svpack テストプログラム :: 微分関数のテスト
!
!履歴  2016/07/30  竹広真一
!
program ua_deriv_mpi_module_svpack_test

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

  integer, parameter :: im=64, jm=32, nm=21, km=2

  real(8), allocatable  ::  xyb_data1(:,:,:)              ! 元の関数
  real(8), allocatable  ::  xyb_data2(:,:,:)              ! 元の関数
  real(8), allocatable  ::  xyb_ddata(:,:,:)              ! 微分の正解
  real(8), allocatable  ::  xy_mu(:,:)                    ! μ=sinφ

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

  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 MessageNotify('M','ua_deriv_mpi_module_svpack_test', &
                         'ua_deriv_mpi_module_svpack function tests') 

!!$  call w_Initial( nm, im, jm )
!!$  call ua_base_mpi_Initial( km )
!!$  call ua_deriv_mpi_Initial
  call ua_mpi_Initial( nm, im, jm, km )  

  allocate(xyb_data1(0:im-1,jm,kc))
  allocate(xyb_data2(0:im-1,jm,kc))
  allocate(xyb_ddata(0:im-1,jm,kc))
  allocate(xy_mu(0:im-1,jm))

  !============== 微分計算 (lon,lat座標系用) のテスト ==============

  !---- Y_1^-1 Y_2^1 のテスト ----
  if ( kp_kl(1) /= 0 ) &
       xyb_data1(:,:,kp_kl(1)) = -cos(xy_Lat)*sin(xy_Lon)             ! Y_1^{-1}
  if ( kp_kl(2) /= 0 ) &
       xyb_data1(:,:,kp_kl(2))  = sin(xy_Lat)*cos(xy_Lat) * cos(xy_Lon) ! Y_2^1

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 2*cos(xy_Lat)*sin(xy_Lon)             ! ua_Lapla_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -6*sin(xy_Lat)*cos(xy_Lat) * cos(xy_Lon) ! Y_2^1
  call AssertEqual(&
    message='ua_Lapla_ua with Y_1^-1 and Y^2_1',                  &
    answer = xyb_ua(ua_Lapla_ua(ua_xyb(xyb_data1))),              &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 1.0/2.0*cos(xy_Lat)*sin(xy_Lon)      ! ua_LaplaInv_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -1.0D0/6.0*sin(xy_Lat)*cos(xy_Lat) * cos(xy_Lon)

  call AssertEqual(&
    message='ua_LaplaInv_ua with Y_1^-1 and Y^2_1',               &
    answer = xyb_ua(ua_LaplaInv_ua(ua_xyb(xyb_data1))),           &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = -cos(xy_Lat)*cos(xy_Lon)         ! ua_DLon_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -sin(xy_Lat)*cos(xy_Lat) * sin(xy_Lon)
  call AssertEqual(&
    message='ua_DLon_ua with Y_1^-1 and Y^2_1',                   &
    answer = xyb_ua(ua_DLon_ua(ua_xyb(xyb_data1))),               &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = -cos(xy_Lon)                     ! xyb_GradLon_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -sin(xy_Lat) * sin(xy_Lon)
  call AssertEqual(&
    message='xyb_GradLon_ua with Y_1^-1 and Y^2_1',               &
    answer = xyb_GradLon_ua(ua_xyb(xyb_data1)),                   &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = sin(xy_Lat)*sin(xy_Lon)         ! xyb_GradLat_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = cos(2*xy_Lat) * cos(xy_Lon)
  call AssertEqual(&
    message='xyb_GradLat_ua with Y_1^-1 and Y^2_1',               &
    answer = xyb_GradLat_ua(ua_xyb(xyb_data1)),                   &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^-1 cosφ, Y_2^1 cosφ のテスト ----
  if ( kp_kl(1) /= 0 ) &
       xyb_data1(:,:,kp_kl(1)) = -cos(xy_Lat)**2*sin(xy_Lon)              ! Y_1^-1 cosφ
  if ( kp_kl(2) /= 0 ) &
       xyb_data1(:,:,kp_kl(2)) = sin(xy_Lat)*cos(xy_Lat)**2 * cos(xy_Lon) ! Y_2^1 cosφ

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = -cos(xy_Lat)*cos(xy_Lon)                ! ua_DivLon_xv
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -sin(xy_Lat)*cos(xy_Lat)*sin(xy_Lon)    ! ua_DivLon_xv
  call AssertEqual(&
    message='ua_DivLon_xyb with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = xyb_ua(ua_DivLon_xyb(xyb_data1)),                    &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 3*sin(xy_Lat)*cos(xy_Lat)*sin(xy_Lon)        !ua_DivLat_ua
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = cos(xy_Lat)*(1-4*sin(xy_Lat)**2)*cos(xy_Lon) !ua_DivLat_ua
  call AssertEqual(&
    message='ua_DivLat_xyb with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = xyb_ua(ua_DivLat_xyb(xyb_data1)),                    &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Jacobian のテスト ----
  if ( kp_kl(1) /= 0 ) &
       xyb_data1(:,:,kp_kl(1)) = -cos(xy_Lat)*sin(xy_Lon)          ! Y_1^{-1}

  if ( kp_kl(2) /= 0 ) &
       xyb_data1(:,:,kp_kl(2)) = -cos(xy_Lat)*sin(xy_Lon)          ! Y_1^{-1}

  if ( kp_kl(1) /= 0 ) &
       xyb_data2(:,:,kp_kl(1)) = -cos(xy_Lat)*sin(xy_Lon)         ! Y_1^{-1}
  if ( kp_kl(2) /= 0 ) &
       xyb_data2(:,:,kp_kl(2)) = sin(xy_Lat)*cos(xy_Lat) * cos(xy_Lon)   ! Y_2^1

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 0.0
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = sin(xy_Lat)**2 - cos(xy_Lat)**2*cos(xy_Lon)**2

  call AssertEqual(&
    message='ua_JacobianMPI_ua_ua Y_1^-1 and Y^2_1',                 &
    answer = xyb_ua(ua_JacobianMPI_ua_ua(ua_xyb(xyb_data1),ua_xyb(xyb_data2))), &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  !============== 微分計算 (λ,μ座標系用) のテスト ==============
  xy_mu = sin(xy_Lat)

  !----- Y_2^0, Y_1^1 のテスト -----
  if ( kp_kl(1) /= 0 ) &
       xyb_data1(:,:,kp_kl(1)) = 3*xy_mu**2-1                              ! Y_2^0
  if ( kp_kl(2) /= 0 ) &
       xyb_data1(:,:,kp_kl(2)) = sqrt(1-xy_mu**2)*cos(xy_Lon)              ! Y_1^1

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 0.0
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -sqrt(1-xy_mu**2)*sin(xy_Lon)
  call AssertEqual(&
    message='xyb_GradLambda_ua with Y_2^0 and Y^1_1',             &
    answer = xyb_GradLambda_ua(ua_xyb(xyb_data1)),                &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 6*xy_mu*(1-xy_mu**2)
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -xy_mu*sqrt(1-xy_mu**2)*cos(xy_Lon)
  call AssertEqual(&
    message='xyb_GradMu_ua with Y_2^0 and Y^1_1',                 &
    answer = xyb_GradMu_ua(ua_xyb(xyb_data1)),                    &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- Y_2^0(1-μ^2), Y_1^1 (1-μ^2) のテスト -----
  if ( kp_kl(1) /= 0 ) &
       xyb_data1(:,:,kp_kl(1)) = (3*xy_mu**2-1)*(1-xy_mu**2)       ! Y_2^0 (1-μ^2)
  if ( kp_kl(2) /= 0 ) &
       xyb_data1(:,:,kp_kl(2)) = (1-xy_mu**2)**(3.0/2)*cos(xy_Lon) ! Y_1^1 (1-μ^2)

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = 0.0
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -(1-xy_mu**2)**(1.0d0/2)*sin(xy_Lon)
  call AssertEqual(&
    message='ua_DivLambda_xyb with Y_2^0 (1-μ^2) and Y^1_1 √(1-μ^2)',&
    answer = xyb_ua(ua_DivLambda_xyb(xyb_data1)),                 &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  if ( kp_kl(1) /= 0 ) &
       xyb_ddata(:,:,kp_kl(1)) = (2-3*xy_mu**2)*4*xy_mu
  if ( kp_kl(2) /= 0 ) &
       xyb_ddata(:,:,kp_kl(2)) = -3.0D0*xy_mu*(1-xy_mu**2)**(1.0D0/2)*cos(xy_Lon)
  call AssertEqual(&
    message='ua_DivMu_xyb with Y_2^0 (1-μ^2) and Y^1_1 √(1-μ^2)',&
    answer = xyb_ua(ua_DivMu_xyb(xyb_data1)),                     &
    check = xyb_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ua_deriv_mpi_module_svpack_test', &
                         'ua_deriv_mpi_module_svpack function tests succeeded!') 

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

  call MPI_FINALIZE(IERR)

end program ua_deriv_mpi_module_svpack_test
