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

NAG AD Library Introduction
Example description
!   D01FC_T1W_F Example Program Text
!   Mark 27.3 Release. NAG Copyright 2021.

    Module d01fc_t1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, nagad_t1w_w_rtype, Operator (*),           &
                               Operator (+), Operator (/)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: ndim = 4, nout = 6
      Integer, Parameter, Public       :: maxpts = 1000*ndim
      Integer, Parameter, Public       :: lenwrk = (ndim+2)*(1+maxpts/(2**ndim &
                                          +2*ndim*ndim+2*ndim+1))
    Contains
      Subroutine f(ad_handle,ndim,z,fz,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t1w_w_rtype), Intent (Out) :: fz
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: z(ndim)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Type (nagad_t1w_w_rtype)       :: f1, f2, f3
!       .. Executable Statements ..
        f1 = ruser(1)*z(1)*z(3)*z(3)
        f2 = ruser(2)*z(1)*z(3)
        f2 = exp(f2)
        f3 = ruser(3) + z(2) + z(4)
        f3 = f3*f3
        fz = f1*f2/f3
        Return

      End Subroutine f
    End Module d01fc_t1w_fe_mod

    Program d01fc_t1w_fe
!     D01FC_T1W_F Example Main Program

!     .. Use Statements ..
      Use d01fc_t1w_fe_mod, Only: f, lenwrk, maxpts, ndim, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01fc_t1w_f, nagad_t1w_w_rtype, x10aa_t1w_f,    &
                               x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_t1w_w_rtype)         :: acc, eps, finval
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, minpts
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:), b(:), work(:)
      Type (nagad_t1w_w_rtype)         :: ruser(3)
      Real (Kind=nag_wp)               :: dr(3)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

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

      Allocate (a(ndim),b(ndim),work(lenwrk))

      a(1:ndim) = 0.0E0_nag_wp
      b(1:ndim) = 1.0E0_nag_wp
      eps = 0.0001E0_nag_wp
      minpts = 0
      ruser(1) = 4.0_nag_wp
      ruser(2) = 2.0_nag_wp
      ruser(3) = 1.0_nag_wp

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


      Do i = 1, 3
        ruser(i)%tangent = 1.0_nag_wp
!       Call the AD routine
        ifail = 0
        Call d01fc_t1w_f(ad_handle,ndim,a,b,minpts,maxpts,f,eps,acc,lenwrk,    &
          work,finval,iuser,ruser,ifail)

        dr(i) = finval%tangent
        ruser(i)%tangent = 0.0_nag_wp
      End Do

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

      Write (nout,*)
      Write (nout,99999) ' Solution, x =', finval%value
      Write (nout,*)
      Write (nout,*) ' Derivatives:'
      Write (nout,99998) ' d/druser(1) =', dr(1)
      Write (nout,99998) ' d/druser(3) =', dr(2)
      Write (nout,99998) ' d/druser(4) =', dr(3)
99999 Format (1X,A,1X,F12.5)
99998 Format (1X,A,1X,E12.5)

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

    End Program d01fc_t1w_fe