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

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

    Module d01fb_a1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, nagad_a1w_w_rtype, Assignment (=),         &
                               Operator (/), Operator (**), Operator (+),      &
                               Operator (-), Operator (*)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: fun_a1w
!     .. Parameters ..
      Integer, Parameter, Public       :: ndim = 4, nout = 6
    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 = (x(1)*x(2)*x(3))**6/(x(4)+2.0E0_nag_wp)**8*                      &
          exp(-2.0_nag_wp*x(2)-0.5_nag_wp*x(3)*x(3))
        Return

      End Subroutine fun_a1w
    End Module d01fb_a1w_fe_mod

    Program d01fb_a1w_fe
!     D01FB_A1W_F Example Main Program

!     .. Use Statements ..
      Use d01fb_a1w_fe_mod, Only: fun_a1w, ndim, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01fb_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: d01tbf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: ans
      Real (Kind=nag_wp)               :: a, b
      Integer                          :: i, ifail, j, k, lwa
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: abscis_a1w(:), weight_a1w(:)
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: abscis(:), weight(:)
      Integer                          :: iuser(1), nptvec(ndim)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: sum
!     .. Executable Statements ..

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

      nptvec(1:ndim) = (/4,4,4,4/)
      lwa = sum(nptvec(1:ndim))

      Allocate (abscis(lwa),weight(lwa),abscis_a1w(lwa),weight_a1w(lwa))
      j = 1

!     Evaluate primal weights and abscisae in each dimension
      Do i = 1, 4
        ifail = 0
        Select Case (i)
        Case (1)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(0,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (2)
          a = 0.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(-3,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (3)
          a = 0.0E0_nag_wp
          b = 0.5E0_nag_wp
          Call d01tbf(-4,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (4)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(-5,a,b,nptvec(i),weight(j),abscis(j),ifail)
        End Select

        j = j + nptvec(i)
      End Do

!     Create AD tape
      Call nagad_a1w_ir_create

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

      weight_a1w(1:lwa) = weight(1:lwa)
      abscis_a1w(1:lwa) = abscis(1:lwa)

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(weight_a1w)
      Call nagad_a1w_ir_register_variable(abscis_a1w)

!     Call the AD routine
      ifail = 0
      Call d01fb_a1w_f(ad_handle,ndim,nptvec,lwa,weight_a1w,abscis_a1w,        &
        fun_a1w,ans,iuser,ruser,ifail)

      Call nagad_a1w_inc_derivative(ans,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
      weight(1:lwa) = nagad_a1w_get_derivative(weight_a1w)
      abscis(1:lwa) = nagad_a1w_get_derivative(abscis_a1w)

      Write (nout,*)
      Write (nout,99999) ' Solution, x =', ans%value
      Write (nout,*) ' Derivatives:'
      Write (nout,*) ' dim  j   d/dweight    d/dabscis'
      j = 0
      Do i = 1, 4
        j = j + 1
        Write (nout,99998) i, 1, weight(j), abscis(j)
        Do k = 2, nptvec(i)
          j = j + 1
          Write (nout,99997) k, weight(j), abscis(j)
        End Do
      End Do
99999 Format (1X,A,1X,F12.5)
99998 Format (1X,I3,1X,I3,1X,E12.5,1X,E12.5)
99997 Format (6X,I2,1X,E12.5,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 d01fb_a1w_fe