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

NAG AD Library Introduction
Example description
!   D01RG_T1W_F Example Program Text
!   Mark 28.3 Release. NAG Copyright 2022.

    Module d01rg_t1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: log, nagad_t1w_w_rtype, sin, Operator (/),      &
                               Operator (*), Operator (-)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: nin = 5, nout = 6
    Contains
      Subroutine f(ad_handle,x,nx,fv,iflag,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: nx
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Out) :: fv(nx)
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: x(nx)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..

        fv = sin(x)/x*log(10.0_nag_wp*(1.0_nag_wp-x))
        Return
      End Subroutine f
    End Module d01rg_t1w_fe_mod

    Program d01rg_t1w_fe

!     D01RG_T1W_F Example Main Program

!     .. Use Statements ..
      Use d01rg_t1w_fe_mod, Only: f, nin, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01rg_t1w_f, nagad_t1w_w_rtype, x10aa_t1w_f,    &
                               x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp, x07caf, x07cbf
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_t1w_w_rtype)         :: a, b, dinest, epsabs, epsrel, errest
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: ar, br, da, db, e1r, e2r
      Integer                          :: ifail, nevals
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype)         :: ruser(1)
      Integer                          :: exmode(3), exmode_old(3), iuser(1)
!     .. Executable Statements ..

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

!     The example function can raise various exceptions - it contains
!     a division by zero and a log singularity - although its integral
!     is well behaved.

      Call x07caf(exmode_old)
!     Save the original halting mode

!     Turn exception halting mode off for the three common exceptions
      exmode = (/0,0,0/)
      Call x07cbf(exmode)

!     Skip first line of data file
      Read (nin,*)
!     Read problem parameters and initialize AD types
      Read (nin,*) ar
      Read (nin,*) br
      Read (nin,*) e1r
      Read (nin,*) e2r

!     Initialize AD types
      a = ar
      b = br
      epsabs = e1r
      epsrel = e2r

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

!     Evaluate the integral using the AD routine
      ifail = -1
      a%tangent = 1.0_nag_wp
      Call d01rg_t1w_f(ad_handle,a,b,f,epsabs,epsrel,dinest,errest,nevals,     &
        iuser,ruser,ifail)

      If (ifail<0) Then
        Write (nout,99999) 'The routine has failed with ifail = ', ifail
        Go To 100
99999   Format (1X,A,I0)
      End If
!     Print inputs and primal outputs
      Write (nout,*)
      Write (nout,99998) 'a     ', 'lower limit of integration', a%value
      Write (nout,99998) 'b     ', 'upper limit of integration', b%value
      Write (nout,99997) 'epsabs', 'absolute accuracy requested', epsabs%value
      Write (nout,99997) 'epsrel', 'relative accuracy requested', epsrel%value
      Write (nout,*)
      If (ifail>=0) Then
        Write (nout,99996) 'dinest', 'approximation to the integral',          &
          dinest%value
        Write (nout,99997) 'errest', 'estimate of the absolute error',         &
          errest%value
        Write (nout,99995) 'nevals', 'number of function evaluations', nevals
      End If
99998 Format (1X,A6,' - ',A30,' = ',F10.4)
99997 Format (1X,A6,' - ',A30,' = ',E10.2)
99996 Format (1X,A6,' - ',A30,' = ',F10.5)
99995 Format (1X,A6,' - ',A30,' = ',I10)

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

!     Get derivatives
      da = dinest%tangent
      a%tangent = 0.0_nag_wp
      b%tangent = 1.0_nag_wp
      ifail = -1
      Call d01rg_t1w_f(ad_handle,a,b,f,epsabs,epsrel,dinest,errest,nevals,     &
        iuser,ruser,ifail)
      db = dinest%tangent

      Write (nout,*) ' Derivatives:'
      Write (nout,99994) 'd/da(x) =', da
      Write (nout,99994) 'd/db(x) =', db
99994 Format (1X,A15,1X,F10.5)

100   Continue
!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

!     Restore the original halting mode
      Call x07cbf(exmode_old)
    End Program d01rg_t1w_fe