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

NAG AD Library Introduction
Example description
!   E04DG_T1W_F Example Program Text
!   Mark 29.3 Release. NAG Copyright 2023.

    Module e04dg_t1w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, nagad_t1w_w_rtype, Operator (+),           &
                               Operator (*)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: objfun
!     .. Parameters ..
      Integer, Parameter, Public       :: nin = 5, nout = 6
    Contains
      Subroutine objfun(ad_handle,mode,n,x,objf,objgrd,nstate,iuser,ruser)
!       Routine to evaluate F(x) and its 1st derivatives.

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t1w_w_rtype), Intent (Out) :: objf
        Integer, Intent (Inout)        :: mode
        Integer, Intent (In)           :: n, nstate
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Out) :: objgrd(n)
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Type (nagad_t1w_w_rtype)       :: expx1, x1, x2, y1, y2
!       .. Executable Statements ..
        x1 = x(1)
        x2 = x(2)
        expx1 = exp(x1)
        y1 = 2.0_nag_wp*x1 + x2
        y2 = x2 + 1.0_nag_wp
        objf = expx1*(y1*y1+y2*y2)

        If (mode==2) Then
          objgrd(1) = expx1*(4.0_nag_wp*y1) + objf
          objgrd(2) = expx1*(2.0_nag_wp*(y1+y2))
        End If

        Return

      End Subroutine objfun
    End Module e04dg_t1w_fe_mod
    Program e04dg_t1w_fe

!     E04DG_T1W_F Example Main Program

!     .. Use Statements ..
      Use e04dg_t1w_fe_mod, Only: nin, nout, objfun
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e04dg_t1w_f, e04wb_t1w_f, nagad_t1w_w_rtype,    &
                               x10aa_t1w_f, x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_t1w_w_rtype)         :: objf
      Integer                          :: i, ifail, iter, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: objgrd(:), work(:), x(:)
      Type (nagad_t1w_w_rtype)         :: ruser(1), rwsav(475)
      Real (Kind=nag_wp), Allocatable  :: dfdx(:), dx(:)
      Integer                          :: iuser(1), iwsav(610)
      Integer, Allocatable             :: iwork(:)
      Logical                          :: lwsav(120)
      Character (80)                   :: cwsav(1)
!     .. Executable Statements ..
      Write (nout,*) 'E04DG_T1W_F Example Program Results'
      Flush (nout)

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n

      Allocate (iwork(n+1),objgrd(n),x(n),dx(n),dfdx(n),work(13*n))

      Read (nin,*) dx(1:n)

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

      Do i = 1, n
        x = dx
        x(i)%tangent = 1.0_nag_wp

!       Initialize sav arrays
        ifail = 0
        Call e04wb_t1w_f('E04DGA',cwsav,1,lwsav,120,iwsav,610,rwsav,475,ifail)

!       Solve the problem
        ifail = 0
        objf = 0.0_nag_wp
        Call e04dg_t1w_f(ad_handle,n,objfun,iter,objf,objgrd,x,iwork,work,     &
          iuser,ruser,lwsav,iwsav,rwsav,ifail)
        dfdx(i) = objf%tangent
      End Do
!     Primal results are printed by default

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

      Write (nout,*)
      Write (nout,*) ' Derivatives:'

      Write (nout,99999) '  dobjf/dx: ', dfdx(1:n)
      Write (nout,99999) '  objgrd  : ', objgrd(1:n)%value
99999 Format (1X,A20,1P,E8.1,4X,E8.1)

!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program e04dg_t1w_fe