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

NAG AD Library Introduction
Example description
!   D01FC_A1W_F Example Program Text
!   Mark 28.4 Release. NAG Copyright 2022.

    Module d01fc_a1w_fe_mod

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

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

    Program d01fc_a1w_fe
!     D01FC_A1W_F Example Main Program

!     .. Use Statements ..
      Use d01fc_a1w_fe_mod, Only: f_a1w, lenwrk, maxpts, ndim, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01fc_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 (nagad_a1w_w_rtype)         :: acc, eps, finval
      Type (c_ptr)                     :: ad_handle
      Integer                          :: ifail, minpts
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:), b(:), work(:)
      Type (nagad_a1w_w_rtype)         :: ruser(3)
      Real (Kind=nag_wp)               :: dr(3)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

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

!     Call the AD routine
      ifail = 0
      Call d01fc_a1w_f(ad_handle,ndim,a,b,minpts,maxpts,f_a1w,eps,acc,lenwrk,  &
        work,finval,iuser,ruser,ifail)

      Call nagad_a1w_inc_derivative(finval,1.0E0_nag_wp)
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

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

!     Get derivatives
      dr(1:3) = nagad_a1w_get_derivative(ruser)

      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 and tape
      ifail = 0
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

    End Program d01fc_a1w_fe