!   D01PAF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE d01pafe_mod

!      D01PAF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: mxord = 5, ndim = 3, nout = 6
       INTEGER, PARAMETER                  :: sdvert = 2*(ndim+1)
       INTEGER, PARAMETER                  :: ldvert = ndim + 1
    CONTAINS
!dec$ attributes offload : mic :: functn
       FUNCTION functn(ndim,x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: functn
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ndim
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(ndim)
!         .. Intrinsic Functions ..
          INTRINSIC                              cos, exp
!         .. Executable Statements ..
          functn = exp(x(1)+x(2)+x(3))*cos(x(1)+x(2)+x(3))

          RETURN

!dec$ attributes offload : mic :: functn
       END FUNCTION functn
    END MODULE d01pafe_mod
    PROGRAM d01pafe

!      D01PAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01paf, nag_wp
       USE d01pafe_mod, ONLY : functn, ldvert, mxord, ndim, nout, sdvert
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: esterr
       INTEGER                             :: ifail, j, maxord, minord, nevals
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: finvls(:), vert(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D01PAF Example Program Results'

       ALLOCATE (finvls(mxord),vert(ldvert,sdvert))

       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

       DO maxord = 1, mxord

          ifail = 0
          CALL d01paf(ndim,vert,ldvert,sdvert,functn,minord,maxord,finvls, &
             esterr,ifail)

          IF (maxord==1) WRITE (nout,99999)
          WRITE (nout,99998) maxord, finvls(maxord), esterr, nevals

          nevals = (nevals*(maxord+ndim+1))/maxord
       END DO

99999  FORMAT (/1X,'MAXORD   Estimated      Estimated         Integrand'/1X, &
          '           value         accuracy        evaluations')
99998  FORMAT (1X,I4,F13.5,E16.3,I15)
    END PROGRAM d01pafe
