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

NAG AD Library Introduction
Example description
!   D01PA_A1W_F Example Program Text
!   Mark 30.2 Release. NAG Copyright 2024.

    Module d01pa_a1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: cos, exp, nagad_a1w_w_rtype, Assignment (=),    &
                               Operator (*), Operator (+)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: fun_a1w
!     .. 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_a1w(ad_handle,ndim,x,ret,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_a1w_w_rtype), Intent (Out) :: ret
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_a1w_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_a1w
    End Module d01pa_a1w_fe_mod

    Program d01pa_a1w_fe
!     D01PA_A1W_F Example Main Program

!     .. Use Statements ..
      Use d01pa_a1w_fe_mod, Only: fun_a1w, ldvert, mxord, ndim, nout, sdvert
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01pa_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_w_rtype,         &
                               x10aa_a1w_f, x10ab_a1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: esterr
      Integer                          :: i, ifail, j, maxord, minord, nevals
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: finvls(:), vert(:,:),           &
                                          vert_in(:,:)
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: dfdv(:,:)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

      Write (nout,*) 'D01PA_A1W_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

!     Create AD tape
      Call nagad_a1w_ir_create

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

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(vert_in)
      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 AD routine
        ifail = 0
        Call d01pa_a1w_f(ad_handle,ndim,vert,ldvert,sdvert,fun_a1w,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 adjoints'
      Write (nout,*) ' Computational mode    : algorithmic'

!     Get derivatives
      Call nagad_a1w_inc_derivative(finvls(mxord),1.0_nag_wp)
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      dfdv(1:ndim+1,1:ndim) = nagad_a1w_get_derivative(vert_in)

      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, ndim + 1
        Do j = 1, ndim
          Write (nout,99997) i, j, dfdv(i,j)
        End Do
      End Do
99998 Format (1X,A,1X,F12.5)
99997 Format (1X,I3,1X,I3,1X,E12.4)

!     Remove computational data object and tape
      ifail = 0
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

    End Program d01pa_a1w_fe