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

NAG AD Library Introduction
Example description
    Program e01eb_t1w_fe

!     E01EB_T1W_F Example Program Text
!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e01ea_t1w_f, e01eb_t1w_f,                       &
                               nagad_t1w_set_derivative, nagad_t1w_w_rtype,    &
                               x10aa_t1w_f, x10ab_t1w_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)               :: dx, dy, fr, xr, yr
      Integer                          :: ifail, m, n, r
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: f(:), pf(:), px(:), py(:),      &
                                          x(:), y(:)
      Integer, Allocatable             :: triang(:)
!     .. Executable Statements ..
      Write (nout,*) 'E01EB_T1W_F Example Program Results'

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

      Read (nin,*) n
      m = 1
      Allocate (f(n),x(n),y(n),px(m),py(m),pf(m),triang(7*n))

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

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

!     Call the AD routine
      ifail = 0
      Call e01ea_t1w_f(n,x,y,triang,ifail)

!     Evaluate interpolant and derivatives at a fixed point
      px(1) = 0.5_nag_wp*(x(n/2)+x(n/2+1))
      py(1) = 0.5_nag_wp*(y(n/2)+y(n/2+1))

      Call nagad_t1w_set_derivative(px(1),1.0_nag_wp)
      ifail = 0
      Call e01eb_t1w_f(ad_handle,m,n,x,y,f,triang,px,py,pf,ifail)
      dx = pf(1)%tangent
      px(1)%tangent = 0.0_nag_wp
      pf(1) = 0.0_nag_wp

      Call nagad_t1w_set_derivative(py(1),1.0_nag_wp)
      ifail = 0
      Call e01eb_t1w_f(ad_handle,m,n,x,y,f,triang,px,py,pf,ifail)
      dy = pf(1)%tangent

      Write (nout,*)
      Write (nout,99999) 'Interpolation point: x = ', px(1)%value, ' y = ',    &
        py(1)%value
      Write (nout,99999) 'Interpolated value = ', pf(1)%value
99999 Format (1X,A,F7.4,A,F7.4)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'
      Write (nout,*)
      Write (nout,*) ' Derivatives of interpolated value w.r.t. fit point'
      Write (nout,*)
      Write (nout,*) '      d/dx     d/dy'
      Write (nout,99998) dx, dy
99998 Format (1X,2(1X,E10.3))

!     Remove computational data object and tape
      ifail = 0
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program e01eb_t1w_fe