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

NAG AD Library Introduction
Example description
    Program f11db_t1w_fe

!     F11DB_T1W_F Example Program Text

!     Mark 30.3 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f11da_t1w_f, f11db_t1w_f, nagad_t1w_w_rtype,    &
                               x10aa_t1w_f, x10ab_t1w_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
      Type (nagad_t1w_w_rtype)         :: dtol
      Integer                          :: i, ifail, la, lfill, liwork, n, nnz, &
                                          nnzc, npivm
      Character (1)                    :: check, milu, pstrat, trans
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:), x(:), y(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:), dxdy(:,:), xr(:), yr(:)
      Integer, Allocatable             :: icol(:), idiag(:), ipivp(:),         &
                                          ipivq(:), irow(:), istr(:), iwork(:)
!     .. Executable Statements ..
      Write (nout,*) 'F11DB_T1W_F Example Program Results'
      Write (nout,*)

!     Skip heading in data file
      Read (nin,*)

!     Read order of matrix and number of nonzero entries
      Read (nin,*) n
      Read (nin,*) nnz

      la = 2*nnz
      liwork = 7*n + 2
      Allocate (a(la),x(n),y(n),icol(la),idiag(n),ipivp(n),ipivq(n),irow(la),  &
        istr(n+1),iwork(liwork))
      Allocate (ar(la),xr(n),yr(n),dxdy(n,n))

!     Read the matrix A
      Do i = 1, nnz
        Read (nin,*) ar(i), irow(i), icol(i)
      End Do
      a = ar

!     Read the vector y
      Read (nin,*) yr(1:n)
      y = yr

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

!     Calculate LU factorization
      lfill = -1
      pstrat = 'C'
      milu = 'N'
      dtol = 0.E0_nag_wp

!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call f11da_t1w_f(ad_handle,n,nnz,a,la,irow,icol,lfill,dtol,pstrat,milu,  &
        ipivp,ipivq,istr,idiag,nnzc,npivm,iwork,liwork,ifail)

!     Check value of npivm

      If (npivm>0) Then

        Write (nout,*) 'Factorization is not complete'
        Go To 100
      End If

      Do i = 1, n
        y(i)%tangent = 1.0_nag_wp

!       Solve P L D U x = y

        trans = 'N'
        check = 'C'

        ifail = 0
        Call f11db_t1w_f(ad_handle,trans,n,a,la,irow,icol,ipivp,ipivq,istr,    &
          idiag,check,y,x,ifail)

        y(i)%tangent = 0.0_nag_wp
        dxdy(1:n,i) = x(1:n)%tangent

      End Do

!     Output results

      Write (nout,*) ' Solution of linear system'
      Write (nout,'(E16.4)') x(1:n)%value

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of solution X w.r.t. RHS B (A inverse)'
      Write (nout,*)
      Call x04caf('General',' ',n,n,dxdy,n,'       dx(i)/dy(j)',ifail)

!     Remove computational data object
100   Call x10ab_t1w_f(ad_handle,ifail)

    End Program f11db_t1w_fe