!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_deriv_mpi_module テストプログラム :: 微分関数のテスト
!
!履歴  2019/03/19  竹広真一
!      2020/11/09  竹広真一 セクター計算テスト
!
program w_mpi_module_deriv_mint_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use w_mpi_module_mint
  use mpi

  implicit none

!!$  integer, parameter :: im=64, jm=32, nm=21, mint=1
  integer, parameter :: im=32, jm=32, nm=21, mint=2
  
  real(8), allocatable  ::  xv_data1(:,:)              ! 元の関数
  real(8), allocatable  ::  xv_data2(:,:)              ! 元の関数
  real(8), allocatable  ::  xv_ddata(:,:)              ! 微分の正解
  real(8), allocatable  ::  xv_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','w_mpi_module_deriv_mint_test', &
                         'w_deriv_mpi_module_mint function tests')

  call w_mpi_Initial( nm, im, jm, mint=mint )

  !------------ 変数割付け -------------
  allocate(xv_data1(0:im-1,jc))
  allocate(xv_data2(0:im-1,jc))
  allocate(xv_ddata(0:im-1,jc))
  allocate(xv_mu(0:im-1,jc))


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

  !---- Y_1^0 のテスト ----
  xv_data1 = sin(xv_Lat)                     ! Y_1^0

  xv_ddata = -2*sin(xv_Lat)                  ! w_Lapla_w
  call AssertEqual(&
    message='Y_1^0 test of w_Lapla_w',                            &
    answer = xv_ddata,                                            &
    check = xv_w(w_Lapla_w(w_xv(xv_data1))),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = -1.0/2.0*sin(xv_Lat)            ! w_LaplaInv_w
  call AssertEqual(&
    message='Y_1^0 test of w_LaplaInv_w',                         &
    answer = xv_ddata,                                            &
    check = xv_w(w_LaplaInv_w(w_xv(xv_data1))),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = 0.0D0                           ! w_DLon_w
  call AssertEqual(&
    message='Y_1^0 test of w_DLon_w',                             &
    answer = xv_ddata,                                            &
    check = xv_w(w_DLon_w(w_xv(xv_data1))),                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat)**2                           ! w_cosLatDLat_w
  call AssertEqual(&
    message='Y_1^0 test of w_cosLatDLat_w',                       &
    answer = xv_ddata,                                            &
    check = xv_w(w_cosLatDLat_w(w_xv(xv_data1))),                 &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = 0.0D0                           ! xv_GradLon_w
  call AssertEqual(&
    message='Y_1^0 test of xv_GradLon_w',                         &
    answer = xv_ddata,                                            &
    check = xv_GradLon_w(w_xv(xv_data1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat)                     ! xv_GradLat_w
  call AssertEqual(&
    message='Y_1^0 test of xv_GradLat_w',                         &
    answer = xv_ddata,                                            &
    check = xv_GradLat_w(w_xv(xv_data1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^0 cosφ のテスト ----
  xv_data1 = sin(xv_Lat)*cos(xv_Lat)                 ! Y_1^0 cosφ
  xv_ddata = 0.0D0                                   ! w_DivLon_xv
  call AssertEqual(&
    message='Y_1^0 cosφ test of xv_DivLon_w',                    &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivLon_xv(xv_data1)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat)**2 - 2*sin(xv_Lat)**2       ! w_DivLat_w
  call AssertEqual(&
    message='Y_1^0 cosφ test of xv_DivLat_w',                    &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivLat_xv(xv_data1)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Jacobian のテスト ----
  xv_data1 = sin(xv_Lat)                             ! Y_1^0
  xv_data2 = cos(xv_Lat)**2*sin(2*xv_Lon)            ! Y_2^-2

  xv_ddata = 0.0
  call AssertEqual(&
    message='Y_1^0 test of w_JacobianMPI_w_w',                        &
    answer = xv_ddata,                                            &
    check = xv_w(w_JacobianMPI_w_w(w_xv(xv_data1),w_xv(xv_data1))),  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = - cos(xv_Lat) * cos(xv_Lat)*2*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_1^0 and Y_2^-2 test of w_JacobianMPI_w_w',            &
    answer = xv_ddata,                                            &
    check = xv_w(w_JacobianMPI_w_w(w_xv(xv_data1),w_xv(xv_data2))),  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat) * cos(xv_Lat)*2*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 and Y_1^0 test of w_JacobianMPI_w_w',            &
    answer = xv_ddata,                                            &
    check = xv_w(w_JacobianMPI_w_w(w_xv(xv_data2),w_xv(xv_data1))),  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  !---- Y_2^2 のテスト ----
  xv_data1 = cos(xv_Lat)**2*cos(2*xv_Lon)       ! Y_2^2

  xv_ddata = -6*cos(xv_Lat)**2*cos(2*xv_Lon)    ! w_Lapla_w
  call AssertEqual(&
    message='Y_2^2 test of w_Lapla_w',                            &
    answer = xv_ddata,                                            &
    check = xv_w(w_Lapla_w(w_xv(xv_data1))),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = -1.0D0/6.0D0*cos(xv_Lat)**2*cos(2*xv_Lon) ! w_LaplaInv_w
  call AssertEqual(&
    message='Y_2^2 test of w_LaplaInv_w',                         &
    answer = xv_ddata,                                            &
    check = xv_w(w_LaplaInv_w(w_xv(xv_data1))),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat)**2*(-2)*sin(2*xv_Lon)     ! w_DLon_w
  call AssertEqual(&
    message='Y_2^2 test of w_DLon_w',                             &
    answer = xv_ddata,                                            &
    check = xv_w(w_DLon_w(w_xv(xv_data1))),                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = cos(xv_Lat)*(-2)*sin(2*xv_Lon)        ! xv_GradLon_w
  call AssertEqual(&
    message='Y_2^2 test of xv_GradLon_w',                         &
    answer = xv_ddata,                                            &
    check = xv_GradLon_w(w_xv(xv_data1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = (-2)*cos(xv_Lat)*sin(xv_Lat)*cos(2*xv_Lon)  ! xv_GradLat_w
  call AssertEqual(&
    message='Y_2^2 test of xv_GradLat_w',                         &
    answer = xv_ddata,                                            &
    check = xv_GradLat_w(w_xv(xv_data1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^2 cosφ のテスト ----
  xv_data1 = cos(xv_Lat)**3*cos(2*xv_Lon)            ! Y_2^2 cosφ

  xv_ddata = cos(xv_Lat)**2*(-2)*sin(2*xv_Lon)       ! w_DivLon_xv
  call AssertEqual(&
    message='Y_2^2 cosφ test of xv_DivLon_w',                    &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivLon_xv(xv_data1)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = (-4)*cos(xv_Lat)**2*sin(xv_Lat)*cos(2*xv_Lon) ! w_DivLat_w
  call AssertEqual(&
    message='Y_2^2 cosφ test of xv_DivLat_w',                    &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivLat_xv(xv_data1)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

!!$  !---- Jacobian のテスト ----
!!$  xv_data1 = sin(xv_Lat)                             ! Y_2^2
!!$  xv_data2 = sin(xv_Lat)                             ! Y_2^2
!!$
!!$  xv_ddata = 0.0
!!$  call AssertEqual(&
!!$    message='Y_2^2 test of w_JacobianMPI_w_w',                        &
!!$    answer = xv_ddata,                                            &
!!$    check = xv_w(w_JacobianMPI_w_w(w_xv(xv_data1),w_xv(xv_data1))),  &
!!$    significant_digits = check_digits, ignore_digits = ignore     &
!!$    )
!!$
  !============== 微分計算 (λ,μ座標系用) のテスト ==============
  xv_mu = sin(xv_Lat)

  !----- Y_2^0 のテスト -----
  xv_data1 = 3*xv_mu**2-1                              ! Y_2^0

  xv_ddata = 0.0
  call AssertEqual(&
    message='Y_2^0 test of xv_GradLambda_w',                      &
    answer = xv_ddata,                                            &
    check = xv_GradLambda_w(w_xv(xv_data1)),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = 6*xv_mu*(1-xv_mu**2)
  call AssertEqual(&
    message='Y_2^0 test of xv_GradMu_w',                          &
    answer = xv_ddata,                                            &
    check = xv_GradMu_w(w_xv(xv_data1)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----- Y_2^0 cosφ のテスト -----
  xv_data1 = (3*xv_mu**2-1)*(1-xv_mu**2)                ! Y_2^0 (1-μ^2)

  xv_ddata = 0.0
  call AssertEqual(&
    message='Y_2^0 (1-μ^2) test of xv_DivLambda_w',                 &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivLambda_xv(xv_data1)),                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_ddata = (2-3*xv_mu**2)*4*xv_mu
  call AssertEqual(&
    message='Y_2^0 (1-μ^2) test of xv_DivMu_w',                  &
    answer = xv_ddata,                                            &
    check = xv_w(w_DivMu_xv(xv_data1)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  call MessageNotify('M','w_mpi_module_deriv_mint_test', &
                         'w_deriv_module_mint function tests succeeded!')

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

  call MPI_FINALIZE(IERR)

end program w_mpi_module_deriv_mint_test
