NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
    Program e01bf_t2w_fe

!     E01BF_T2W_F Example Program Text
!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e01be_t2w_f, e01bf_t2w_f, nagad_t2w_w_rtype,    &
                               x10aa_t2w_f, x10ab_t2w_f, Assignment (=),       &
                               Operator (*), Operator (+)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: fr, xr
      Integer                          :: i, ifail, m, n, r
!     .. Local Arrays ..
      Type (nagad_t2w_w_rtype), Allocatable :: d(:), f(:), pf(:), px(:), x(:)
      Real (Kind=nag_wp), Allocatable  :: df(:), dx(:)
!     .. Executable Statements ..

      Write (nout,*) 'E01BF_T2W_F Example Program Results'

!     Skip heading in data file
      Read (nin,*)

      Read (nin,*) n
      m = 1
      Allocate (d(n),f(n),x(n),dx(n),df(n),px(m),pf(m))

!     Initialize x and f arrays using data
      Do r = 1, n
        Read (nin,*) xr, fr
        x(r) = xr
        f(r) = fr
      End Do

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t2w_f(ad_handle,ifail)

      px(1) = 0.5_nag_wp*(x(n/2)+x(n/2+1))

      Do i = 1, 2*n
        If (i<=n) Then
          x(i)%value%tangent = 1.0_nag_wp
          x(i)%tangent%value = 1.0_nag_wp
        Else
          f(i-n)%value%tangent = 1.0_nag_wp
          f(i-n)%tangent%value = 1.0_nag_wp
        End If
!       Call the AD routine
        ifail = 0
        Call e01be_t2w_f(ad_handle,n,x,f,d,ifail)

!       Evaluate interpolant and derivatives at a mid-point
        Call e01bf_t2w_f(ad_handle,n,x,f,d,m,px,pf,ifail)

        If (i<=n) Then
          dx(i) = pf(1)%tangent%tangent
          x(i)%value%tangent = 0.0_nag_wp
          x(i)%tangent%value = 0.0_nag_wp
        Else
          df(i-n) = pf(1)%tangent%tangent
          f(i-n)%value%tangent = 0.0_nag_wp
          f(i-n)%tangent%value = 0.0_nag_wp
        End If
      End Do
      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: Second order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'


!     Get derivative of interpolated value w.r.t data points and values


      Write (nout,*)
      Write (nout,99999) 'Interpolated value at x = ', px(1)%value%value,      &
        ' is: ', pf(1)%value%value
99999 Format (1X,A,F7.3,A,E12.5)
      Write (nout,*)
      Write (nout,*) ' Derivatives of interpolated value w.r.t. x and f:'
      Write (nout,*)
      Write (nout,*) '    i     d^2/dx^2     d^2/df^2'
      Do i = 1, n
        Write (nout,99998) i, dx(i), df(i)
      End Do
99998 Format (1X,I5,1X,E12.5,1X,E12.5)

!     Remove computational data object
      ifail = 0
      Call x10ab_t2w_f(ad_handle,ifail)

    End Program e01bf_t2w_fe