!----------------------------------------------------------------------
!     Copyright (c) 2020 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  ua_deriv_mpi_module テストプログラム :: 微分関数のテスト
!
!履歴  2019/08/04  竹広真一
!
program ua_deriv_mpi_module_test

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

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

  real(8), allocatable  ::  pva_data1(:,:,:)              ! 元の関数
  real(8), allocatable  ::  pva_data2(:,:,:)              ! 元の関数
  real(8), allocatable  ::  pva_ddata(:,:,:)              ! 微分の正解
  real(8), allocatable  ::  pv_data1(:,:)                 ! 元の関数
  real(8), allocatable  ::  pv_data2(:,:)                 ! 元の関数
  real(8), allocatable  ::  pv_ddata(:,:)                 ! 微分の正解
  real(8), allocatable  ::  pv_mu(:,:)                    ! μ=sinφ

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

  integer :: ip, np, ierr


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

  call MessageNotify('M','ua_deriv_mpi_module_sypack_test', &
                         'ua_deriv_mpi_module_sypack function tests')

  call ua_mpi_Initial( nm, im, jm, km, npv=npv )

  allocate(pva_data1(0:ic-1,jc,km))
  allocate(pva_data2(0:ic-1,jc,km))
  allocate(pva_ddata(0:ic-1,jc,km))
  allocate(pv_data1(0:ic-1,jc))
  allocate(pv_data2(0:ic-1,jc))
  allocate(pv_ddata(0:ic-1,jc))
  allocate(pv_mu(0:ic-1,jc))

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

  !---- Y_1^-1 Y_2^1 のテスト ----
  pva_data1(:,:,1) = -cos(pv_Lat)*sin(pv_Lon)             ! Y_1^{-1}
  pva_data1(:,:,2)  = sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon) ! Y_2^1

  pva_ddata(:,:,1) = 2*cos(pv_Lat)*sin(pv_Lon)             ! ua_Lapla_ua
  pva_ddata(:,:,2) = -6*sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon) ! ua_Lapla_ua
  call AssertEqual(&
    message='ua_Lapla_ua with Y_1^-1 and Y^2_1',                  &
    answer = pva_ua(ua_Lapla_ua(ua_pva(pva_data1))),              &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

 
  pva_ddata(:,:,1) = 1.0/2.0*cos(pv_Lat)*sin(pv_Lon)      ! ua_LaplaInv_ua
  pva_ddata(:,:,2) = -1.0D0/6.0*sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon)
  call AssertEqual(&
    message='ua_LaplaInv_ua with Y_1^-1 and Y^2_1',               &
    answer = pva_ua(ua_LaplaInv_ua(ua_pva(pva_data1))),           &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pva_ddata(:,:,1) = -cos(pv_Lat)*cos(pv_Lon)         ! ua_DLon_ua
  pva_ddata(:,:,2) = -sin(pv_Lat)*cos(pv_Lat) * sin(pv_Lon)
  call AssertEqual(&
    message='ua_DLon_ua with Y_1^-1 and Y^2_1',                   &
    answer = pva_ua(ua_DLon_ua(ua_pva(pva_data1))),               &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pva_ddata(:,:,1) = -cos(pv_Lon)                     ! pva_GradLon_ua
  pva_ddata(:,:,2) = -sin(pv_Lat) * sin(pv_Lon)
  call AssertEqual(&
    message='pva_GradLon_ua with Y_1^-1 and Y^2_1',               &
    answer = pva_GradLon_ua(ua_pva(pva_data1)),                   &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pva_ddata(:,:,1) = sin(pv_Lat)*sin(pv_Lon)         ! pva_GradLat_ua
  pva_ddata(:,:,2) = cos(2*pv_Lat) * cos(pv_Lon)
  call AssertEqual(&
    message='pva_GradLat_ua with Y_1^-1 and Y^2_1',               &
    answer = pva_GradLat_ua(ua_pva(pva_data1)),                   &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = -cos(pv_Lat)*sin(pv_Lon)                 ! Y_1^{-1}
  pv_ddata = 2*cos(pv_Lat)*sin(pv_Lon)                ! ua_Lapla_ua
  call AssertEqual(&
    message='u_Lapla_u with Y_1^-1',                              &
    answer = pv_u(u_Lapla_u(u_pv(pv_data1))),                     &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  pv_ddata = 1.0/2.0*cos(pv_Lat)*sin(pv_Lon)          ! ua_LaplaInv_ua
  call AssertEqual(&
    message='u_LaplaInv_u with Y_1^-1',                           &
    answer = pv_u(u_LaplaInv_u(u_pv(pv_data1))),                  &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  pv_ddata = sin(pv_Lat)*sin(pv_Lon)                  ! pva_GradLat_ua
  call AssertEqual(&
    message='pv_GradLat_u with Y_1^-1',                           &
    answer = pv_GradLat_u(u_pv(pv_data1)),                        &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon)    ! Y_2^1
  pv_ddata = -6*sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon) ! ua_Lapla_ua
  call AssertEqual(&
    message='u_Lapla_u with Y_2^1',                               &
    answer = pv_u(u_Lapla_u(u_pv(pv_data1))),                     &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  pv_ddata = -1.0D0/6.0*sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon) ! ua_LaplaInv_ua
  call AssertEqual(&
    message='u_LaplaInv_u with Y_2^1',                            &
    answer = pv_u(u_LaplaInv_u(u_pv(pv_data1))),                  &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  pv_ddata = cos(2*pv_Lat) * cos(pv_Lon)                      ! pva_GradLat_ua
  call AssertEqual(&
    message='pv_GradLat_u with Y_2^1',                            &
    answer = pv_GradLat_u(u_pv(pv_data1)),                        &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  
  !---- Y_1^-1 cosφ, Y_2^1 cosφ のテスト ----
  pva_data1(:,:,1) = -cos(pv_Lat)**2*sin(pv_Lon)              ! Y_1^-1 cosφ
  pva_data1(:,:,2) = sin(pv_Lat)*cos(pv_Lat)**2 * cos(pv_Lon) ! Y_2^1 cosφ

  pva_ddata(:,:,1) = -cos(pv_Lat)*cos(pv_Lon)                ! ua_DivLon_pv
  pva_ddata(:,:,2) = -sin(pv_Lat)*cos(pv_Lat)*sin(pv_Lon)    ! ua_DivLon_pv
  call AssertEqual(&
    message='ua_DivLon_pva with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pva_ua(ua_DivLon_pva(pva_data1)),                    &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pva_ddata(:,:,1) = 3*sin(pv_Lat)*cos(pv_Lat)*sin(pv_Lon)        !ua_DivLat_ua
  pva_ddata(:,:,2) = cos(pv_Lat)*(1-4*sin(pv_Lat)**2)*cos(pv_Lon) !ua_DivLat_ua
  call AssertEqual(&
    message='ua_DivLat_pva with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pva_ua(ua_DivLat_pva(pva_data1)),                    &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = -cos(pv_Lat)**2*sin(pv_Lon)              ! Y_1^-1 cosφ
  pv_ddata = -cos(pv_Lat)*cos(pv_Lon)                ! u_DivLon_pv
  call Assertequal(&
    message='u_DivLon_pv with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pv_u(u_DivLon_pv(pv_data1)),                       &
    check = pv_ddata,                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )
  pv_ddata = 3*sin(pv_Lat)*cos(pv_Lat)*sin(pv_Lon)        !u_DivLat_u
  call Assertequal(&
    message='u_DivLat_pv with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pv_u(u_DivLat_pv(pv_data1)),                       &
    check = pv_ddata,                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )
  
  pv_data1 = sin(pv_Lat)*cos(pv_Lat)**2 * cos(pv_Lon) ! Y_2^1 cosφ
  pv_ddata = -sin(pv_Lat)*cos(pv_Lat)*sin(pv_Lon)    ! u_DivLon_pv
  call Assertequal(&
    message='u_DivLon_pv with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pv_u(u_DivLon_pv(pv_data1)),                       &
    check = pv_ddata,                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )
  pv_ddata = cos(pv_Lat)*(1-4*sin(pv_Lat)**2)*cos(pv_Lon)     !u_DivLat_u
  call Assertequal(&
    message='u_DivLat_pv with Y_1^-1 cosφ and Y^2_1 cosφ',    &
    answer = pv_u(u_DivLat_pv(pv_data1)),                       &
    check = pv_ddata,                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  
  !---- Jacobian のテスト ----
  pva_data1(:,:,1) = -cos(pv_Lat)*sin(pv_Lon)          ! Y_1^{-1}
  pva_data1(:,:,2) = -cos(pv_Lat)*sin(pv_Lon)          ! Y_1^{-1}
  pva_data2(:,:,1) = -cos(pv_Lat)*sin(pv_Lon)         ! Y_1^{-1}
  pva_data2(:,:,2) = sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon)   ! Y_2^1
  pva_ddata(:,:,1) = 0.0
  pva_ddata(:,:,2) = sin(pv_Lat)**2 - cos(pv_Lat)**2*cos(pv_Lon)**2

  call AssertEqual(&
    message='ua_JacobianMPI_ua_ua Y_1^-1 and Y^2_1',                 &
    answer = pva_ua(ua_JacobianMPI_ua_ua(ua_pva(pva_data1),ua_pva(pva_data2))), &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = -cos(pv_Lat)*sin(pv_Lon)         ! Y_1^{-1}
  pv_data2 = -cos(pv_Lat)*sin(pv_Lon)         ! Y_1^{-1}
  pv_ddata = 0.0
  call AssertEqual(&
    message='u_JacobianMPI_u_u Y_1^-1 and Y^2_1',                    &
    answer = pv_u(u_JacobianMPI_u_u(u_pv(pv_data1),u_pv(pv_data2))), &
    check = pv_ddata,                                                &
    significant_digits = check_digits, ignore_digits = ignore        &
    )

  pv_data1 = -cos(pv_Lat)*sin(pv_Lon)                ! Y_1^{-1}
  pv_data2 = sin(pv_Lat)*cos(pv_Lat) * cos(pv_Lon)   ! Y_2^1
  pv_ddata = sin(pv_Lat)**2 - cos(pv_Lat)**2*cos(pv_Lon)**2
  call AssertEqual(&
    message='u_JacobianMPI_u_u Y_1^-1 and Y^2_1',                    &
    answer = pv_u(u_JacobianMPI_u_u(u_pv(pv_data1),u_pv(pv_data2))), &
    check = pv_ddata,                                                &
    significant_digits = check_digits, ignore_digits = ignore        &
    )
 

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

  !----- Y_2^0, Y_1^1 のテスト -----
  pva_data1(:,:,1) = 3*pv_mu**2-1                              ! Y_2^0
  pva_data1(:,:,2) = sqrt(1-pv_mu**2)*cos(pv_Lon)              ! Y_1^1

  pva_ddata(:,:,1) = 0.0
  pva_ddata(:,:,2) = -sqrt(1-pv_mu**2)*sin(pv_Lon)
  call AssertEqual(&
    message='pva_GradLambda_ua with Y_2^0 and Y^1_1',             &
    answer = pva_GradLambda_ua(ua_pva(pva_data1)),                &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  pva_ddata(:,:,1) = 6*pv_mu*(1-pv_mu**2)
  pva_ddata(:,:,2) = -pv_mu*sqrt(1-pv_mu**2)*cos(pv_Lon)
  call AssertEqual(&
    message='pva_GradMu_ua with Y_2^0 and Y^1_1',                 &
    answer = pva_GradMu_ua(ua_pva(pva_data1)),                    &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = 3*pv_mu**2-1                              ! Y_2^0
  pv_ddata = 0.0
  call AssertEqual(&
    message='pv_GradLambda_u with Y_2^0',                        &
    answer = pv_GradLambda_u(u_pv(pv_data1)),                    &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  pv_ddata = 6*pv_mu*(1-pv_mu**2)
  call AssertEqual(&
    message='pv_GradMu_u with Y_2^0',                             &
    answer = pv_GradMu_u(u_pv(pv_data1)),                         &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pv_data1 = sqrt(1-pv_mu**2)*cos(pv_Lon)              ! Y_1^1
  pv_ddata = -sqrt(1-pv_mu**2)*sin(pv_Lon)
  call AssertEqual(&
    message='pv_GradLambda_u with Y^1_1',                        &
    answer = pv_GradLambda_u(u_pv(pv_data1)),                    &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  pv_ddata = -pv_mu*sqrt(1-pv_mu**2)*cos(pv_Lon)
  call AssertEqual(&
    message='pv_GradMu_u with Y_1^1',                             &
    answer = pv_GradMu_u(u_pv(pv_data1)),                         &
    check = pv_ddata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  !----- Y_2^0(1-μ^2), Y_1^1 (1-μ^2) のテスト -----
  pva_data1(:,:,1) = (3*pv_mu**2-1)*(1-pv_mu**2)       ! Y_2^0 (1-μ^2)
  pva_data1(:,:,2) = (1-pv_mu**2)**(3.0/2)*cos(pv_Lon) ! Y_1^1 (1-μ^2)

  pva_ddata(:,:,1) = 0.0
  pva_ddata(:,:,2) = -(1-pv_mu**2)**(1.0d0/2)*sin(pv_Lon)

  call AssertEqual(&
    message='ua_DivLambda_pva with Y_2^0 (1-μ^2) and Y^1_1 √(1-μ^2)',&
    answer = pva_ua(ua_DivLambda_pva(pva_data1)),                 &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  pva_ddata(:,:,1) = (2-3*pv_mu**2)*4*pv_mu
  pva_ddata(:,:,2) = -3.0D0*pv_mu*(1-pv_mu**2)**(1.0D0/2)*cos(pv_Lon)
  call AssertEqual(&
    message='ua_DivMu_pva with Y_2^0 (1-μ^2) and Y^1_1 √(1-μ^2)',&
    answer = pva_ua(ua_DivMu_pva(pva_data1)),                     &
    check = pva_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  
  pv_data1 = (3*pv_mu**2-1)*(1-pv_mu**2)       ! Y_2^0 (1-μ^2)
  pv_ddata = 0.0
  call AssertEqual(&
    message='u_DivLambda_pv with Y_2^0 (1-μ^2)',                &
    answer = pv_u(u_DivLambda_pv(pv_data1)),                     &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  pv_ddata = (2-3*pv_mu**2)*4*pv_mu
  call AssertEqual(&
    message='u_DivMu_pv with Y_2^0 (1-μ^2)',                    &
    answer = pv_u(u_DivMu_pv(pv_data1)),                         &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )

  pv_data1 = (1-pv_mu**2)**(3.0/2)*cos(pv_Lon) ! Y_1^1 (1-μ^2)
  pv_ddata = -(1-pv_mu**2)**(1.0d0/2)*sin(pv_Lon)
  call AssertEqual(&
    message='u_DivLambda_pv with Y^1_1 √(1-μ^2)',              &
    answer = pv_u(u_DivLambda_pv(pv_data1)),                     &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )
  pv_ddata = -3.0D0*pv_mu*(1-pv_mu**2)**(1.0D0/2)*cos(pv_Lon)
  call AssertEqual(&
    message='u_DivMu_pv with  Y^1_1 √(1-μ^2)',                 &
    answer = pv_u(u_DivMu_pv(pv_data1)),                         &
    check = pv_ddata,                                            &
    significant_digits = check_digits, ignore_digits = ignore    &
    )

  
  call MessageNotify('M','ua_deriv_mpi_module_test', &
                         'ua_deriv_mpi_module function tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program ua_deriv_mpi_module_test
