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

NAG AD Library Introduction
Example description
!   D01BD_A1W_F Example Program Text
!   Mark 29.2 Release. NAG Copyright 2023.

    Module d01bd_a1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: nagad_a1w_w_rtype, sin, Assignment (=),         &
                               Operator (**), Operator (*)
      Use nag_library, Only: nag_wp, x01aaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f_a1w
!     .. Parameters ..
      Integer, Parameter, Public       :: nout = 6
    Contains
      Subroutine f_a1w(ad_handle,x,ret,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_a1w_w_rtype), Intent (Out) :: ret
        Type (nagad_a1w_w_rtype), Intent (In) :: x
!       .. Array Arguments ..
        Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Real (Kind=nag_wp)             :: pi, xx
!       .. Executable Statements ..

        pi = x01aaf(xx)
        ret = (x**2)*sin(pi*ruser(1)*x)
        Return

      End Subroutine f_a1w
    End Module d01bd_a1w_fe_mod

    Program d01bd_a1w_fe
!     D01BD_A1W_F Example Main Program

!     .. Use Statements ..
      Use d01bd_a1w_fe_mod, Only: f_a1w, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: abs, d01bd_a1w_f, max,                          &
                               nagad_a1w_get_derivative,                       &
                               nagad_a1w_inc_derivative,                       &
                               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, x10za_a1w_f,          &
                               Assignment (=), Operator (>), Operator (*)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_a1w_w_rtype)         :: a, abserr, b, epsabs, epsrel, result
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: dr
      Integer                          :: ifail
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'D01BD_A1W_F Example Program Results'

      epsabs = 0.0_nag_wp
      epsrel = 1.0E-4_nag_wp
      a = 0.0_nag_wp
      b = 1.0_nag_wp
      ruser(1) = 10.0_nag_wp

!     Create AD tape
      Call x10za_a1w_f

!     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(ruser(1))

      ifail = 0
      Call d01bd_a1w_f(ad_handle,f_a1w,a,b,epsabs,epsrel,result,abserr,iuser,  &
        ruser,ifail)

      Call nagad_a1w_inc_derivative(result,1.0E0_nag_wp)
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

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

!     Get derivative
      dr = nagad_a1w_get_derivative(ruser(1))
      Write (nout,*)
      Write (nout,99999) 'a      - lower limit of integration     = ', a%value
      Write (nout,99999) 'b      - upper limit of integration     = ', b%value
      Write (nout,99998) 'epsabs - absolute accuracy requested    = ',         &
        epsabs%value
      Write (nout,99998) 'epsrel - relative accuracy requested    = ',         &
        epsrel%value
      Write (nout,*)
      Write (nout,99997) 'result - approximation to the integral  = ',         &
        result%value
      Write (nout,99998) 'abserr - estimate to the absolute error = ',         &
        abserr%value
      Write (nout,99998) 'derivative of results w.r.t ruser(1)    = ', dr
      Write (nout,*)

      If (abserr>max(epsabs,(epsrel)*abs(result))) Then
        Write (nout,*)                                                         &
          'Warning - requested accuracy may not have been achieved'
      End If

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

99999 Format (1X,A,F10.4)
99998 Format (1X,A,E9.2)
99997 Format (1X,A,F9.5)
    End Program d01bd_a1w_fe