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

NAG AD Library Introduction
Example description
!   D01BD_T1W_F Example Program Text
!   Mark 30.1 Release. NAG Copyright 2024.

    Module d01bd_t1w_fe_mod

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

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t1w_w_rtype), Intent (Out) :: ret
        Type (nagad_t1w_w_rtype), Intent (In) :: x
!       .. Array Arguments ..
        Type (nagad_t1w_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
    End Module d01bd_t1w_fe_mod

    Program d01bd_t1w_fe
!     D01BD_T1W_F Example Main Program

!     .. Use Statements ..
      Use d01bd_t1w_fe_mod, Only: f, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: abs, d01bd_t1w_f, max, nagad_t1w_w_rtype,       &
                               x10aa_t1w_f, x10ab_t1w_f, Assignment (=),       &
                               Operator (>), Operator (*)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_t1w_w_rtype)         :: a, abserr, b, epsabs, epsrel, result
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: dr
      Integer                          :: ifail
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'D01BD_T1W_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 configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      ruser(1)%tangent = 1.0_nag_wp

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

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

      dr = result%tangent

      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
      ifail = 0
      Call x10ab_t1w_f(ad_handle,ifail)

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