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

NAG AD Library Introduction
Example description
!   E04DG_A1W_F Example Program Text
!   Mark 30.0 Release. NAG Copyright 2024.

    Module e04dg_a1w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, nagad_a1w_w_rtype, Assignment (=),         &
                               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_a1w_w_rtype), Intent (Out) :: objf
        Integer, Intent (Inout)        :: mode
        Integer, Intent (In)           :: n, nstate
!       .. Array Arguments ..
        Type (nagad_a1w_w_rtype), Intent (Out) :: objgrd(n)
        Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_a1w_w_rtype), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Type (nagad_a1w_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_a1w_fe_mod
    Program e04dg_a1w_fe

!     E04DG_A1W_F Example Main Program

!     .. Use Statements ..
      Use e04dg_a1w_fe_mod, Only: nin, nout, objfun
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e04dg_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_ir_zero_adjoints &
                               , nagad_a1w_w_rtype, x10aa_a1w_f, x10ab_a1w_f,  &
                               Assignment (=)
      Use nag_library, Only: e04wbf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: objf
      Integer                          :: i, ifail, iter, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: objgrd(:), work(:), x(:)
      Type (nagad_a1w_w_rtype)         :: ruser(1), rwsav(475)
      Real (Kind=nag_wp), Allocatable  :: ddx(:), dx(:)
      Real (Kind=nag_wp)               :: rwsav_r(475)
      Integer                          :: iuser(1), iwsav(610)
      Integer, Allocatable             :: iwork(:)
      Logical                          :: lwsav(120)
      Character (80)                   :: cwsav(1)
!     .. Executable Statements ..
      Write (nout,*) 'E04DG_A1W_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),ddx(n*n),work(13*n))

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

!     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(x)

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

!     Options can be set here, before the next line, via e04dja and/or e04dka
      rwsav(1:475) = rwsav_r(1:475)

!     Solve the problem
      ifail = 0
      Call e04dg_a1w_f(ad_handle,n,objfun,iter,objf,objgrd,x,iwork,work,iuser, &
        ruser,lwsav,iwsav,rwsav,ifail)

!     Primal results are printed by default

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

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

!     Setup evaluation of derivatives via adjoints
      Call nagad_a1w_inc_derivative(objf,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!     Get derivatives
      dx(1:n) = nagad_a1w_get_derivative(x)

      Write (nout,99999) '  dobjf/dx: ', dx(1:n)
99999 Format (1X,A20,1P,E8.1,4X,E8.1)
!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(objgrd(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

        dx(1:n) = nagad_a1w_get_derivative(x)
        Write (nout,99998) '  dobjgrd(', i, ')/dx: ', dx(1:n)
      End Do
99998 Format (1X,A13,I0,A6,1P,E8.1,4X,E8.1)

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

    End Program e04dg_a1w_fe