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

NAG AD Library Introduction
Example description
!   D01PA_T1W_F Example Program Text
!   Mark 30.0 Release. NAG Copyright 2024.

    Module d01pa_t1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: cos, exp, nagad_t1w_w_rtype, Operator (*),      &
                               Operator (+)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: fun
!     .. Parameters ..
      Integer, Parameter, Public       :: mxord = 5, ndim = 3, nout = 6
      Integer, Parameter, Public       :: sdvert = 2*(ndim+1)
      Integer, Parameter, Public       :: ldvert = ndim + 1
    Contains
      Subroutine fun(ad_handle,ndim,x,ret,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t1w_w_rtype), Intent (Out) :: ret
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: x(ndim)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        ret = exp(x(1)+x(2)+x(3))*cos(x(1)+x(2)+x(3))
        Return

      End Subroutine fun
    End Module d01pa_t1w_fe_mod

    Program d01pa_t1w_fe
!     D01PA_T1W_F Example Main Program

!     .. Use Statements ..
      Use d01pa_t1w_fe_mod, Only: fun, ldvert, mxord, ndim, nout, sdvert
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01pa_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 (c_ptr)                     :: ad_handle
      Type (nagad_t1w_w_rtype)         :: esterr
      Integer                          :: i, ifail, j, maxord, minord, nevals
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: finvls(:), vert(:,:)
      Type (nagad_t1w_w_rtype)         :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: dfdv(:,:)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

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

      Allocate (finvls(mxord),vert(ldvert,sdvert),dfdv(ldvert,ndim))

      vert(1:ldvert,1:ndim) = 0.0_nag_wp
      Do j = 2, ldvert
        vert(j,j-1) = 1.0_nag_wp
      End Do

      minord = 0
      nevals = 1

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

      Write (nout,*) ' Maxord   Estimated      Estimated         Integrand'
      Write (nout,*) '           value         accuracy        evaluations'
      Do maxord = 1, mxord
!       Call the AD routine
        ifail = 0
        Call d01pa_t1w_f(ad_handle,ndim,vert,ldvert,sdvert,fun,minord,maxord,  &
          finvls,esterr,iuser,ruser,ifail)


        Write (nout,99999) maxord, finvls(maxord)%value, esterr%value, nevals

        nevals = (nevals*(maxord+ndim+1))/maxord
      End Do
99999 Format (1X,I4,F13.5,E16.3,I15)


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

      Write (nout,*)
      Write (nout,99998) ' Solution, I =', finvls(mxord)%value
      Write (nout,*) ' Derivatives w.r.t vertices:'
      Write (nout,*) '  i   j   d/dv(i,j)'
      Do i = 1, ldvert
        Do j = 1, ndim
          vert(i,j)%tangent = 1.0_nag_wp
          ifail = 0
          minord = 0
          Call d01pa_t1w_f(ad_handle,ndim,vert,ldvert,sdvert,fun,minord,mxord, &
            finvls,esterr,iuser,ruser,ifail)
          Write (nout,99997) i, j, finvls(mxord)%tangent
          vert(i,j)%tangent = 0.0_nag_wp
        End Do
      End Do
99998 Format (1X,A,1X,F12.5)
99997 Format (1X,I3,1X,I3,1X,E12.4)

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

    End Program d01pa_t1w_fe