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

NAG AD Library Introduction
Example description
    Program e01eb_a1w_fe

!     E01EB_A1W_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_a1w_f, e01eb_a1w_f,                       &
                               nagad_a1w_get_derivative,                       &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_w_rtype,         &
                               x10aa_a1w_f, x10ab_a1w_f, Assignment (=)
      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, yr
      Integer                          :: i, ifail, m, n, r
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: f(:), pf(:), px(:), py(:),      &
                                          x(:), y(:)
      Real (Kind=nag_wp), Allocatable  :: df(:), dx(:), dy(:)
      Integer, Allocatable             :: triang(:)
!     .. Executable Statements ..
      Write (nout,*) 'E01EB_A1W_F Example Program Results'

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

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

      Do r = 1, n
        Read (nin,*) xr, yr, fr
        x(r) = xr
        y(r) = yr
        f(r) = fr
      End Do

!     Create AD tape
      Call nagad_a1w_ir_create

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

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(x)
      Call nagad_a1w_ir_register_variable(y)
      Call nagad_a1w_ir_register_variable(f)

!     Call the AD routine
      ifail = 0
      Call e01ea_a1w_f(n,x,y,triang,ifail)
!     Evaluate interpolant and derivatives at a fixed point
      px(1) = 0.5_nag_wp*(x(n/2)%value+x(n/2+1)%value)
      py(1) = 0.5_nag_wp*(y(n/2)%value+y(n/2+1)%value)
      ifail = 0
      Call e01eb_a1w_f(ad_handle,m,n,x,y,f,triang,px,py,pf,ifail)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order adjoints'
      Write (nout,*) ' Computational mode    : algorithmic'

!     Get derivative of interpolated value w.r.t data points and values
      Call nagad_a1w_inc_derivative(pf(1),1.0_nag_wp)
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

      dx(1:n) = nagad_a1w_get_derivative(x)
      dy(1:n) = nagad_a1w_get_derivative(y)
      df(1:n) = nagad_a1w_get_derivative(f)

      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 of interpolated value w.r.t. x,  y and f:'
      Write (nout,*)
      Write (nout,*) '    i      d/dx         d/dy         d/df'
      Do i = 1, n
        Write (nout,99998) i, dx(i), dy(i), df(i)
      End Do
99998 Format (1X,I5,3(1X,F12.5))

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

    End Program e01eb_a1w_fe