    PROGRAM f11dkfe

!      F11DKF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11bdf, f11bef, f11bff, f11dkf, f11xaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: anorm, sigmax, stplhs, stprhs, tol
       INTEGER                         :: i, ifail, ifail1, irevcm, iterm,     &
                                          itn, lwork, lwreq, m, maxitn, monit, &
                                          n, niter, nnz
       CHARACTER (1)                   :: init, norm, precon, weight
       CHARACTER (8)                   :: method
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), diag(:), wgt(:),         &
                                          work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DKF Example Program Results'

!      Skip heading in data file

       READ (nin,*)
       READ (nin,*) n
       READ (nin,*) nnz
       lwork = 200
       ALLOCATE (a(nnz),b(n),diag(n),wgt(n),work(lwork),x(n),icol(nnz), &
          irow(nnz))

!      Read or initialize the parameters for the iterative solver

       READ (nin,*) method
       READ (nin,*) precon, norm, weight, iterm
       READ (nin,*) m, tol, maxitn
       READ (nin,*) monit
       anorm = 0.0E0_nag_wp
       sigmax = 0.0E0_nag_wp

!      Read the parameters for the preconditioner

       READ (nin,*) niter

!      Read the non-zero elements of the matrix A

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

!      Read right-hand side vector b and initial approximate solution

       READ (nin,*) b(1:n)
       READ (nin,*) x(1:n)

!      Call F11BDF to initialize the solver

!        ifail: behaviour on error exit
!                 =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11bdf(method,precon,norm,weight,iterm,n,m,tol,maxitn,anorm, &
          sigmax,monit,lwreq,work,lwork,ifail)

!      Call repeatedly F11BEF to solve the equations
!      Note that the arrays B and X are overwritten

!      On final exit, X will contain the solution and B the residual
!      vector

       irevcm = 0
       init = 'I'

       ifail = 1
LOOP:  DO
          CALL f11bef(irevcm,x,b,wgt,work,lwreq,ifail)

          IF (irevcm/=4) THEN
             ifail1 = -1
             SELECT CASE (irevcm)
             CASE (-1)

                CALL f11xaf('Transpose',n,nnz,a,irow,icol,'No checking',x,b, &
                   ifail1)

             CASE (1)

                CALL f11xaf('No transpose',n,nnz,a,irow,icol,'No checking',x, &
                   b,ifail1)

             CASE (2)

                CALL f11dkf('Non symmetric','N',init,niter,n,nnz,a,irow,icol, &
                   'Check',x,b,diag,work(lwreq+1),ifail1)

                init = 'N'
             CASE (3)

                ifail1 = 0
                CALL f11bff(itn,stplhs,stprhs,anorm,sigmax,work,lwreq,ifail1)

                WRITE (nout,99999) itn, stplhs
             END SELECT
             IF (ifail1/=0) irevcm = 6
          ELSE IF (ifail/=0) THEN
             WRITE (nout,99993) ifail
             GO TO 20
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP

!      Obtain information about the computation

       ifail1 = 0
       CALL f11bff(itn,stplhs,stprhs,anorm,sigmax,work,lwreq,ifail1)

!      Print the output data
       WRITE (nout,99996)
       WRITE (nout,99995) 'Number of iterations for convergence:    ', itn
       WRITE (nout,99994) 'Residual norm:                           ', stplhs
       WRITE (nout,99994) 'Right-hand side of termination criterion:', stprhs
       WRITE (nout,99994) '1-norm of matrix A:                      ', anorm

!      Output x

       WRITE (nout,99998)
       WRITE (nout,99997) (x(i),b(i),i=1,n)
20     CONTINUE

99999  FORMAT (/1X,'Monitoring at iteration no.',I4/1X,1P,'residual no', &
          'rm: ',E14.4)
99998  FORMAT (/2X,'  Solution vector',2X,' Residual vector')
99997  FORMAT (1X,1P,E16.4,1X,E16.4)
99996  FORMAT (/1X,'Final Results')
99995  FORMAT (1X,A,I4)
99994  FORMAT (1X,A,1P,E14.4)
99993  FORMAT (1X/1X,' ** F11BEF returned with IFAIL = ',I5)
    END PROGRAM f11dkfe
