    PROGRAM f11dafe

!      F11DAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11daf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: dtol
       INTEGER                         :: i, ifail, la, lfill, liwork, n, nnz, &
                                          nnzc, npivm
       CHARACTER (1)                   :: milu, pstrat
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:)
       INTEGER, ALLOCATABLE            :: icol(:), idiag(:), ipivp(:),         &
                                          ipivq(:), irow(:), istr(:), iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DAF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file

       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n
       READ (nin,*) nnz

       la = 2*nnz
       liwork = 7*n + 2

       ALLOCATE (a(la),icol(la),idiag(n),ipivp(n),ipivq(n),irow(la),istr(n+1), &
          iwork(liwork))
       READ (nin,*) lfill, dtol
       READ (nin,*) pstrat
       READ (nin,*) milu

!      Read the matrix A

       DO i = 1, nnz
          READ (nin,*) a(i), irow(i), icol(i)
       END DO

!      Calculate incomplete LU factorization

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

!      Output original matrix

       WRITE (nout,*) ' Original Matrix'
       WRITE (nout,'(A,I4)') ' N     =', n
       WRITE (nout,'(A,I4)') ' NNZ   =', nnz
       DO i = 1, nnz
          WRITE (nout,'(I8,E16.4,2I8)') i, a(i), irow(i), icol(i)
       END DO
       WRITE (nout,*)

!      Output details of the factorization

       WRITE (nout,*) ' Factorization'
       WRITE (nout,'(A,I4)') ' N     =', n
       WRITE (nout,'(A,I4)') ' NNZ   =', nnzc
       WRITE (nout,'(A,I4)') ' NPIVM =', npivm
       DO i = nnz + 1, nnz + nnzc
          WRITE (nout,'(I8,E16.4,2I8)') i, a(i), irow(i), icol(i)
       END DO
       WRITE (nout,*)

       WRITE (nout,*) '        I       IPIVP(I)  IPIVQ(I)'
       DO i = 1, n
          WRITE (nout,'(3I10)') i, ipivp(i), ipivq(i)
       END DO

    END PROGRAM f11dafe
