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

NAG AD Library Introduction
Example description
    Program f11jc_t1w_fe

!     F11JC_T1W_F Example Program Text

!     Mark 29.2 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f11ja_t1w_f, f11jc_t1w_f,                       &
                               nagad_t1w_set_derivative, 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)         :: dscale, dtol, rnorm, tol
      Real (Kind=nag_wp)               :: dd
      Integer                          :: i, ifail, itn, la, lfill, liwork,    &
                                          lwork, maxitn, n, nnz, nnzc, npivm
      Character (6)                    :: method
      Character (1)                    :: mic, pstrat
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:), work(:), x(:), y(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:), dxdy(:,:), yr(:)
      Integer, Allocatable             :: icol(:), ipiv(:), irow(:), istr(:),  &
                                          iwork(:)
!     .. Executable Statements ..
      Write (nout,*) 'F11JC_T1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)

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

      la = 3*nnz
      liwork = 2*la + 7*n + 1
      lwork = 6*n + 120

      Allocate (a(la),x(n),y(n),work(lwork),icol(la),ipiv(n),irow(la),         &
        istr(n+1),iwork(liwork))
      Allocate (ar(la),yr(n),dxdy(n,n))

      Read (nin,*) method
      Read (nin,*) lfill, dd
      dtol = dd
      Read (nin,*) mic, dd
      dscale = dd
      Read (nin,*) pstrat
      Read (nin,*) dd, maxitn
      tol = dd

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

!     Read the vector y
      Read (nin,*) yr(1:n)
      y = yr
!     Read initial approximation to solution
      Read (nin,*) yr(1:n)

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

!     Calculate incomplete Cholesky factorization
      ifail = 0
      Call f11ja_t1w_f(ad_handle,n,nnz,a,la,irow,icol,lfill,dtol,mic,dscale,   &
        pstrat,ipiv,istr,nnzc,npivm,iwork,liwork,ifail)

      Do i = 1, n
        Call nagad_t1w_set_derivative(y(i),1.0_nag_wp)

!       Solve Ax = b using F11JC_T1W_F
        x = yr
        ifail = 0
        Call f11jc_t1w_f(ad_handle,method,n,nnz,a,la,irow,icol,ipiv,istr,y,    &
          tol,maxitn,x,rnorm,itn,work,lwork,ifail)

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

      Write (nout,99999) 'Converged in', itn, ' iterations'
      Write (nout,99998) 'Final residual norm =', rnorm%value

!     Output results
      Write (nout,*) ' Solution of linear system'
      Write (nout,99997)(x(i)%value,i=1,n)

      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 Y (A inverse)'
      Write (nout,*)
      Call x04caf('General',' ',n,n,dxdy,n,'       dx(i)/dy(j)',ifail)

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

99999 Format (1X,A,I10,A)
99998 Format (1X,A,1P,E16.3)
99997 Format (1X,1P,E16.4)
    End Program f11jc_t1w_fe