!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_interpolate_module テストプログラム :: 補間関数のテスト
!
!履歴  2019/03/19  竹広真一
!
program w_interpolate_mpi_module_test

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

  integer, parameter :: im=32, jm=16, nm=10

  real(8), allocatable     ::  w_data(:)
  real(8), allocatable     ::  xv_data(:,:)

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

  real(8) :: Alon
  real(8) :: Alat
  real(8) :: data_sol
  real(8) :: Alon_array(2)
  real(8) :: data_sol_array(2)
  real(8) :: pi

  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)

  pi = atan(1.0D0)*4.0D0
  Alon = pi/5 ; Alat = pi/3
  Alon_array =(/pi/4,-pi/6/)

  call w_mpi_Initial( nm, im, jm )
  allocate(w_data(nc))
  allocate(xv_data(0:im-1,jc))


  call MessageNotify('M','w_Interpolate00_mpi_module_test', &
       'w_interpolate_module interpolate(00) tests')

  !---- Y_1^0 のテスト ----
  xv_data = sqrt(3.0D0)*sin(xv_Lat)                      ! Y_1^0
  data_sol = sqrt(3.0D0)*sin(ALat)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^0',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^1 のテスト ----
  xv_data = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^1
  data_sol = sqrt(3.0D0/2)*cos(ALat)*cos(ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^1',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^-1 のテスト ----
  xv_data = -sqrt(3.0D0)*cos(xv_Lat)*sin(xv_Lon)         ! Y_1^{-1}
  data_sol = -sqrt(3.0D0)*cos(ALat)*sin(ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^-1',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^0 のテスト ----
  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)                 ! Y_2^0
  data_sol = sqrt(5.0D0)*(3.0/2*sin(alat)**2-1.0/2)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^0',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^1 のテスト ----
  xv_data = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * cos(xv_Lon)! Y_2^1
  data_sol = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * cos(ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^1',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-1 のテスト ----
  xv_data = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * sin(xv_Lon)! Y_2^{-1}
  data_sol = -sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * sin(ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-1',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^2 のテスト ----
  xv_data = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*cos(2*xv_Lon)             ! Y_2^2
  data_sol = sqrt(5.0D0/24)*3.0*cos(ALat)**2*cos(2*ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^2',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-2 のテスト ----
  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)          ! Y_2^-2
  data_sol = -sqrt(5.0D0/24)*3.0*cos(ALat)**2*sin(2*ALon)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-2',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的な関数のテスト ----
  xv_data = cos(2*xv_Lon-pi/3) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)-0.5)*(sin(xv_Lat)+1)
  data_sol = cos(2*ALon-pi/3) &
       *(sin(ALat)-1)**2*(sin(ALat)-0.5)*(sin(ALat)+1)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with generaul function',               &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','w_interpolate00_mpi_module_test', &
       'w_interpolate_module interpolate(00) tests succeeded')


  !---- Y_1^0 のテスト ----
  xv_data = sqrt(3.0D0)*sin(xv_Lat)                      ! Y_1^0
  data_sol_array = sqrt(3.0D0)*sin(ALat)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^0',                           &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^1 のテスト ----
  xv_data = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^1
  data_sol_array = sqrt(3.0D0/2)*cos(ALat)*cos(Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^1',                           &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^-1 のテスト ----
  xv_data = -sqrt(3.0D0)*cos(xv_Lat)*sin(xv_Lon)         ! Y_1^{-1}
  data_sol_array = -sqrt(3.0D0)*cos(ALat)*sin(Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^-1',                          &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^0 のテスト ----
  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)                 ! Y_2^0
  data_sol_array = sqrt(5.0D0)*(3.0/2*sin(alat)**2-1.0/2)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^0',                           &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^1 のテスト ----
  xv_data = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * cos(xv_Lon)! Y_2^1
  data_sol_array = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * cos(Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^1',                           &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-1 のテスト ----
  xv_data = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * sin(xv_Lon)! Y_2^{-1}
  data_sol_array = -sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * sin(Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-1',                          &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^2 のテスト ----
  xv_data = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*cos(2*xv_Lon)             ! Y_2^2
  data_sol_array = sqrt(5.0D0/24)*3.0*cos(ALat)**2*cos(2*Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^2',                           &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-2 のテスト ----
  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)          ! Y_2^-2
  data_sol_array = -sqrt(5.0D0/24)*3.0*cos(ALat)**2*sin(2*Alon_Array)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-2',                          &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的な関数のテスト ----
  xv_data = cos(2*xv_Lon-pi/3) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)-0.5)*(sin(xv_Lat)+1)
  data_sol_array = cos(2*Alon_Array-pi/3) &
       *(sin(ALat)-1)**2*(sin(ALat)-0.5)*(sin(ALat)+1)
  w_data = w_xv(xv_data)
  call AssertEqual(&
    message='Interpolate_w with generaul function',               &
    answer = data_sol_array,                                      &
    check = Interpolate_w(w_data,alon_array,alat),                &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','w_interpolate10_mpi_module_test', &
       'w_interpolate_module interpolate(10) tests succeeded')

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

  call MPI_FINALIZE(IERR)

end program w_interpolate_mpi_module_test
