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

NAG AD Library Introduction
Example description
    Program f11jc_p0w_fe

!     F11JC_P0W_F Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f11ja_p0w_f, f11jc_p0w_f
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: dscale, dtol, rnorm, tol
      Integer                          :: i, ifail, itn, la, lfill, liwork,    &
                                          lwork, maxitn, n, nnz, nnzc, npivm
      Character (6)                    :: method
      Character (1)                    :: mic, pstrat
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:), work(:), x(:), y(:)
      Integer, Allocatable             :: icol(:), ipiv(:), irow(:), istr(:),  &
                                          iwork(:)
!     .. Executable Statements ..
      Write (nout,*) 'F11JC_P0W_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))

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

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

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

!     Read initial approximation to solution
      Read (nin,*) x(1:n)

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

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

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

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

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