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

NAG AD Library Introduction
Example description
    Program f07aa_a1w_fe

!     F07AA_A1W_F Example Program Text
!     Mark 29.0 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f07aa_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: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, j, lwork, n, nrhs
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), b(:,:),      &
                                          b_in(:,:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), br(:,:), dxdb(:,:)
      Integer, Allocatable             :: ipiv(:)
!     .. Executable Statements ..
      Write (nout,*) 'F07AA_A1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, nrhs

      lwork = 64*n
      Allocate (a(n,n),a_in(n,n),ipiv(n))
      Allocate (b(n,nrhs),b_in(n,nrhs))
      Allocate (ar(n,n),br(n,nrhs),dxdb(n,n))

!     Read A from data file
      Read (nin,*)(ar(i,1:n),i=1,n)
      a_in = ar

!     Read B from data file
      Read (nin,*)(br(i,1:nrhs),i=1,n)
      b_in = br

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

      a = a_in
      b = b_in

!     Solve system
      Call f07aa_a1w_f(ad_handle,n,nrhs,a,n,ipiv,b,n,ifail)

!     Print solution
      br(1:n,1:nrhs) = b(1:n,1:nrhs)%value

!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('General',' ',n,nrhs,br,n,'Solution',ifail)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of solution X w.r.t. RHS B (A inverse)'
      Write (nout,*)
!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(b(i,1),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        Do j = 1, n
          dxdb(i,j) = nagad_a1w_get_derivative(b_in(j,1))
        End Do
      End Do
      Call x04caf('General',' ',n,n,dxdb,n,'       dx(i,1)/db(j,1)',ifail)

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

    End Program f07aa_a1w_fe