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

NAG AD Library Introduction
Example description
    Program f07ca_a1w_fe

!     F07CA_A1W_F Example Program Text
!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f07ca_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, nagad_symbolic,            &
                               x10aa_a1w_f, x10ab_a1w_f, x10ac_a1w_f,          &
                               Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6, nrhs = 1
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, mode, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: b(:), d(:), df(:), dl(:),       &
                                          dlf(:), du(:), duf(:), x(:)
      Real (Kind=nag_wp), Allocatable  :: dxdb(:), dxdd(:), dxddl(:), dxddu(:)
!     .. Executable Statements ..
      Write (nout,*) 'F07CA_A1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      Read (nin,*) mode

      Allocate (b(n),d(n),dl(n-1),du(n-1))
      Allocate (x(n),df(n),dlf(n-1),duf(n-1))
      Allocate (dxdb(n),dxdd(n),dxddl(n-1),dxddu(n-1))

!     Read the tridiagonal matrix A and the right hand side B from
!     data file and initialize AD arrays

      Read (nin,*) dxddu(1:n-1)
      Read (nin,*) dxdd(1:n)
      Read (nin,*) dxddl(1:n-1)
      Read (nin,*) dxdb(1:n)
      du(1:n-1) = dxddu(1:n-1)
      d(1:n) = dxdd(1:n)
      dl(1:n-1) = dxddl(1:n-1)
      b(1:n) = dxdb(1:n)

!     Create AD tape
      Call nagad_a1w_ir_create

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

!     Set AD computational mode
      ifail = 0
      Call x10ac_a1w_f(ad_handle,mode,ifail)

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(dl)
      Call nagad_a1w_ir_register_variable(d)
      Call nagad_a1w_ir_register_variable(du)
      Call nagad_a1w_ir_register_variable(b)

      dlf = dl
      df = d
      duf = du
      x = b

!     Solve the equations Ax = b for x

!     The NAG name equivalent of dgtsv_a1w is f07ca_a1w_f
      ifail = 0
      Call f07ca_a1w_f(ad_handle,n,nrhs,dlf,df,duf,x,n,ifail)

      If (ifail==0) Then

!       Print primal solution

        Write (nout,*) 'Solution'
        Write (nout,99999) x(1:n)%value

      Else
        Write (nout,99998) 'Element U(', ifail, ',', ifail, ') is zero'
        Go To 100
      End If

99999 Format ((1X,7F11.4))
99998 Format (1X,A,I0,A,I0,A)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order adjoints'
      If (mode==nagad_symbolic) Then
        Write (nout,*) ' Computational mode    : symbolic'
      Else
        Write (nout,*) ' Computational mode    : algorithmic'
      End If

      Write (nout,*)
      Write (nout,*) ' Derivatives of solution w.r.t. inputs:'

!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(x(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        dxddu(1:n-1) = nagad_a1w_get_derivative(du)
        dxdd(1:n) = nagad_a1w_get_derivative(d)
        dxddl(1:n-1) = nagad_a1w_get_derivative(dl)
        dxdb(1:n) = nagad_a1w_get_derivative(b)

        Write (nout,*)
        Write (nout,'(1X,A,I0)') '  Solution point ', i
        Write (nout,99997) '      dx/d(du) ', dxddu
        Write (nout,99996) '      dx/d(d)  ', dxdd
        Write (nout,99996) '      dx/d(dl) ', dxddl
        Write (nout,99996) '      dx/d(b)  ', dxdb
      End Do
99997 Format (1X,A,10X,6(1X,F9.2))
99996 Format (1X,A,7(1X,F9.2))

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


    End Program f07ca_a1w_fe