    PROGRAM f11gdfe

!      F11GDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11gdf, f11gef, f11gff, f11jaf, f11jbf, f11xef, &
                               nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: anorm, dscale, dtol, sigerr, sigmax, &
                                          sigtol, stplhs, stprhs, tol
       INTEGER                         :: i, ifail, ifail1, irevcm, iterm,     &
                                          itn, its, la, lfill, liwork, lwork,  &
                                          lwreq, maxitn, maxits, monit, n,     &
                                          nnz, nnzc, npivm
       CHARACTER (6)                   :: method
       CHARACTER (1)                   :: mic, norm, precon, pstrat, sigcmp,   &
                                          weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), wgt(:), work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), ipiv(:), irow(:), istr(:),  &
                                          iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11GDF Example Program Results'

!      Skip heading in data file

       READ (nin,*)
       READ (nin,*) n
       READ (nin,*) nnz
       la = 2*nnz
       liwork = 2*la + 7*n + 1
       lwork = 200

       ALLOCATE (a(la),b(n),wgt(n),work(lwork),x(n),icol(la),ipiv(n),irow(la), &
          istr(n+1),iwork(liwork))

!      Read or initialize the parameters for the iterative solver

       READ (nin,*) method
       READ (nin,*) precon, sigcmp, norm, weight, iterm
       READ (nin,*) tol, maxitn
       READ (nin,*) monit
       anorm = 0.0E0_nag_wp
       sigmax = 0.0E0_nag_wp
       sigtol = 1.0E-2_nag_wp
       maxits = n

!      Read the parameters for the preconditioner

       READ (nin,*) lfill, dtol
       READ (nin,*) mic, dscale
       READ (nin,*) pstrat

!      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 x

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

       IF (method=='CG') THEN
          WRITE (nout,99999)
       ELSE IF (method=='SYMMLQ') THEN
          WRITE (nout,99998)
       ELSE IF (method=='MINRES') THEN
          WRITE (nout,99997)
       END IF

!      Calculate incomplete Cholesky factorization

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11jaf(n,nnz,a,la,irow,icol,lfill,dtol,mic,dscale,pstrat,ipiv, &
          istr,nnzc,npivm,iwork,liwork,ifail)

!      Call F11GDF to initialize the solver

       ifail = 0
       CALL f11gdf(method,precon,sigcmp,norm,weight,iterm,n,tol,maxitn,anorm, &
          sigmax,sigtol,maxits,monit,lwreq,work,lwork,ifail)

!      Call repeatedly F11GEF 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
       lwreq = lwork

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

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

                CALL f11xef(n,nnz,a,irow,icol,'No checking',x,b,ifail1)

             CASE (2)

                CALL f11jbf(n,a,la,irow,icol,ipiv,istr,'No checking',x,b, &
                   ifail1)

             CASE (3)

                ifail1 = 0
                CALL f11gff(itn,stplhs,stprhs,anorm,sigmax,its,sigerr,work, &
                   lwreq,ifail1)

                WRITE (nout,99996) itn, stplhs
                WRITE (nout,99995)
                WRITE (nout,99994) (x(i),b(i),i=1,n)
             END SELECT
             IF (ifail1/=0) irevcm = 6
          ELSE IF (ifail/=0) THEN
             WRITE (nout,99990) ifail
             GO TO 20
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP

!      Obtain information about the computation

       ifail1 = 0
       CALL f11gff(itn,stplhs,stprhs,anorm,sigmax,its,sigerr,work,lwreq, &
          ifail1)

!      Print the output data

       WRITE (nout,99993)
       WRITE (nout,99992) 'Number of iterations for convergence:    ', itn
       WRITE (nout,99991) 'Residual norm:                           ', stplhs
       WRITE (nout,99991) 'Right-hand side of termination criterion:', stprhs
       WRITE (nout,99991) '1-norm of matrix A:                      ', anorm
       WRITE (nout,99991) 'Largest singular value of A_bar:         ', sigmax

!      Output x

       WRITE (nout,99995)
       WRITE (nout,99994) (x(i),b(i),i=1,n)
20     CONTINUE

99999  FORMAT (/1X,'Solve a system of linear equations using the conjug', &
          'ate gradient method')
99998  FORMAT (/1X,'Solve a system of linear equations using the Lanczo', &
          's method (SYMMLQ)')
99997  FORMAT (/1X,'Solve a system of linear equations using the minimu', &
          'm residual method (MINRES)')
99996  FORMAT (/1X,'Monitoring at iteration no.',I4/1X,1P,'residual no', &
          'rm: ',E14.4)
99995  FORMAT (2X,'Solution vector',2X,'Residual vector')
99994  FORMAT (1X,1P,E16.4,1X,E16.4)
99993  FORMAT (/1X,'Final Results')
99992  FORMAT (1X,A,I4)
99991  FORMAT (1X,A,1P,E14.4)
99990  FORMAT (1X/1X,' ** F11GEF returned with IFAIL = ',I5)
    END PROGRAM f11gdfe
