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

NAG AD Library Introduction
Example description
    Program f07ce_a1w_fe

!     F07CE_A1W_F Example Program Text
!     Mark 29.1 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f07cd_a1w_f, f07ce_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, ldb, n, nrhs
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: b(:,:), d(:), df(:), dl(:),     &
                                          dlf(:), du(:), du2(:), duf(:),       &
                                          x(:,:)
      Real (Kind=nag_wp), Allocatable  :: dxdb(:,:), dxdd(:,:), dxddl(:,:),    &
                                          dxddu(:,:), dxddu2(:,:), xv(:,:)
      Integer, Allocatable             :: ipiv(:)
!     .. Executable Statements ..
      Write (nout,*) 'F07CE_A1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, nrhs

      ldb = n
      Allocate (b(ldb,nrhs),d(n),dl(n-1),du(n-1),du2(n-2),ipiv(n))
      Allocate (df(n),dlf(n-1),duf(n-1))
      Allocate (x(ldb,nrhs))
      Allocate (dxdb(n,n),dxdd(n,n),dxddl(n,(n-1)),dxddu(n,(n-1)))
      Allocate (dxddu2(n,(n-2)),xv(ldb,nrhs))

!     Read the tridiagonal matrix A from data file

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

!     Read the right hand matrix B
      Read (nin,*)(xv(i,1:nrhs),i=1,n)
      b(1:n,1:nrhs) = xv(1:n,1:nrhs)

!     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(du)
      Call nagad_a1w_ir_register_variable(d)
      Call nagad_a1w_ir_register_variable(dl)
      Call nagad_a1w_ir_register_variable(b)

      df = d
      duf = du
      dlf = dl

!     Factorize the tridiagonal matrix A
      ifail = 0
      Call f07cd_a1w_f(ad_handle,n,dlf,df,duf,du2,ipiv,ifail)

!     Solve the equations AX = B
      x = b
      ifail = 0
      Call f07ce_a1w_f(ad_handle,'N',n,nrhs,dlf,df,duf,du2,ipiv,x,ldb,ifail)

!     Print the solution
      xv(1:n,1:nrhs) = x(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,xv,ldb,'Solution(s)',ifail)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of first solution column 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),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        dxddl(i,1:n-1) = nagad_a1w_get_derivative(dl)
        dxdd(i,1:n) = nagad_a1w_get_derivative(d)
        dxddu(i,1:n-1) = nagad_a1w_get_derivative(du)
        dxdb(i,1:n) = nagad_a1w_get_derivative(b(1:n,1))
      End Do
      ifail = 0
      Call x04caf('General',' ',n,n-1,dxddu,n,'dx/d(du)',ifail)
      Call x04caf('General',' ',n,n,dxdd,n,'dx/d(d)',ifail)
      Call x04caf('General',' ',n,n-1,dxddl,n,'dx/d(dl)',ifail)
      Call x04caf('General',' ',n,n,dxdb,n,'dx/d(b)',ifail)

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

    End Program f07ce_a1w_fe