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

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

    Module f01ef_a1w_fe_mod

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

!     nin:       the input channel number
!     nout:      the output channel number

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: cos, nagad_a1w_w_rtype, Assignment (=)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: nin = 5, nout = 6
    Contains
      Subroutine f(ad_handle,iflag,n,x,fx,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Type (nagad_a1w_w_rtype), Intent (Out) :: fx(n)
        Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_a1w_w_rtype), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..

        fx(1:n) = cos(x(1:n))

        Return
      End Subroutine f
    End Module f01ef_a1w_fe_mod
    Program f01ef_a1w_fe

!     F01EF_A1W_F Example Main Program

!     .. Use Statements ..
      Use f01ef_a1w_fe_mod, Only: f, nin, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f01ef_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               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,  &
                               x10za_a1w_f, Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ierr, ifail, iflag, j, lda, n
      Character (1)                    :: uplo
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:)
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: a_r(:,:)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'F01EF_A1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      Read (nin,*) uplo

      lda = n
      Allocate (a(lda,n),a_in(lda,n),a_r(lda,n))

!     Read A from data file
      a_r = 0.0_nag_wp
      If (uplo=='U' .Or. uplo=='u') Then
        Read (nin,*)(a_r(i,i:n),i=1,n)
      Else
        Read (nin,*)(a_r(i,1:i),i=1,n)
      End If
      a_in = a_r

!     Create AD tape
      Call x10za_a1w_f

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

!     Register variables to differentiate w.r.t.
      Do i = 1, n
        Call nagad_a1w_ir_register_variable(a_in(i,i))
      End Do

      a = a_in

!     Find f( A )
      ifail = 0
      Call f01ef_a1w_f(ad_handle,uplo,n,a,lda,f,iuser,ruser,iflag,ifail)

!     Print solution
      a_r = a
      ierr = 0
      Call x04caf(uplo,'N',n,n,a_r,lda,'Symmetric f(A)',ierr)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of diagonal of f(A) w.r.t. diagonal of A'
      Write (nout,*)
!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(a(i,i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        Do j = 1, n
          a_r(i,j) = nagad_a1w_get_derivative(a_in(j,j))
        End Do
      End Do
      Call x04caf('General',' ',n,n,a_r,n,'       d(fA(i,i))/da(j,j)',ifail)

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

    End Program f01ef_a1w_fe