!----------------------------------------------------------------------
! Copyright (c) 2002-2009 SPMODEL Development Group. All rights reserved.!
!----------------------------------------------------------------------
!
!表題  asc_module テストプログラム
!
!履歴  2017/05/19  竹広真一
!
program asc_test_base2d

  use dc_message, only : MessageNotify
  use dc_test,    only : AssertEqual
  use asc_module
  implicit none
  integer, parameter :: im=16, km=10, nm=2
  real(8), parameter :: xmin=0.0D0, xmax=1.0D0
  real(8), parameter :: pi=3.1415926535897932385D0
  real(8), dimension(nm,0:im) :: ag_f, ag_dfdx_sol
  real(8), dimension(nm,km)   :: as_f_sol
  real(8), dimension(nm,0:km) :: ac_f_sol
  ! 判定誤差設定
  integer, parameter :: check_digits = 14
  integer, parameter :: ignore = -13


  call MessageNotify('M','asc_test_base2d','asc_module 2-dim function tests')

  call asc_initial(im,km,xmin,xmax)

  !----------- sin transform test ---------------
  
  ag_f(1,:) = sin(2.0D0*pi*g_x)
  ag_f(2,:) = sin(4.0D0*pi*g_x)

  as_f_sol = 0.0D0
  as_f_sol(1,2)=1.0D0
  as_f_sol(2,4)=1.0D0

  ag_dfdx_sol(1,:) =  2.0D0*pi*cos(2.0D0*pi*g_x)
  ag_dfdx_sol(2,:) =  4.0D0*pi*cos(4.0D0*pi*g_x)


  call MessageNotify('M','test function ',&
    'f(1,:) = sin(2*pi*g_x)')
  call MessageNotify('M','test function ',&
    'f(2,:) = sin(4*pi*g_x)')

  call AssertEqual(&
    message = 'Forward sin Transform ',                         &
    answer = as_f_sol,                                          &
    check = as_ag(ag_f),                                        &
    significant_digits = check_digits, ignore_digits = ignore   &
  )
  call AssertEqual(&
    message = 'Inverse sin Transform ',                         &
    answer = ag_f,                                              &
    check = ag_as(as_ag(ag_f)),                                 &
    significant_digits = check_digits, ignore_digits = ignore   &
    )
  call AssertEqual(&
    message = 'Differentiation',                                &
    answer = ag_dfdx_sol,                                       &
    check  = ag_ac(ac_Dx_as(as_ag(ag_f))),                      &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  !----------- cos transform test ---------------

  ag_f(1,:) = 1.0D0
  ag_f(2,:) = cos(4.0D0*pi*g_x)

  ac_f_sol = 0.0D0
  ac_f_sol(1,0)=2.0D0
  ac_f_sol(2,4)=1.0D0

  ag_dfdx_sol(1,:) =  0.0
  ag_dfdx_sol(2,:) =  -4.0D0*pi*sin(4.0D0*pi*g_x)

  call MessageNotify('M','test function ',&
    'f(2,:) = 1.0')
  call MessageNotify('M','test function ',&
    'f(3,:) = cos(4*pi*g_x)')

  call AssertEqual(&
    message = 'Forward cos Transform ',                         &
    answer = ac_f_sol,                                          &
    check = ac_ag(ag_f),                                        &
    significant_digits = check_digits, ignore_digits = ignore   &
  )
  call AssertEqual(&
    message = 'Inverse cos Transform ',                         &
    answer = ag_f,                                              &
    check = ag_ac(ac_ag(ag_f)),                                 &
    significant_digits = check_digits, ignore_digits = ignore   &
    )
  call AssertEqual(&
    message = 'Differentiation',                                &
    answer = ag_dfdx_sol,                                       &
    check  = ag_as(as_Dx_ac(ac_ag(ag_f))),                      &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call MessageNotify('M',&
    'ae_test_base2d','ae_module 2-dim function tests succeeded!')

end program asc_test_base2d

