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

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

    Module d01pa_p0w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nag_library, Only: nag_wp
!     .. 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
        Real (Kind=nag_wp), Intent (Out) :: ret
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: x(ndim)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: cos, exp
!       .. Executable Statements ..
        ret = exp(x(1)+x(2)+x(3))*cos(x(1)+x(2)+x(3))
        Return

      End Subroutine fun
    End Module d01pa_p0w_fe_mod

    Program d01pa_p0w_fe
!     D01PA_P0W_F Example Main Program

!     .. Use Statements ..
      Use d01pa_p0w_fe_mod, Only: fun, ldvert, mxord, ndim, nout, sdvert
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01pa_p0w_f
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: esterr
      Integer                          :: ifail, j, maxord, minord, nevals
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: dfdv(:,:), finvls(:), vert(:,:),     &
                                          vert_in(:,:)
      Real (Kind=nag_wp)               :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

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

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

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

      minord = 0
      nevals = 1

      vert(1:ldvert,1:ndim) = vert_in(1:ldvert,1:ndim)

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

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

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

      Write (nout,*)
      Write (nout,99998) ' Solution, I =', finvls(mxord)
99998 Format (1X,A,1X,F12.5)

    End Program d01pa_p0w_fe