    PROGRAM f11jdfe

!      F11JDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11gdf, f11gef, f11gff, f11jdf, f11xef, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: anorm, omega, sigerr, sigmax,        &
                                          sigtol, stplhs, stprhs, tol
       INTEGER                         :: i, ifail, ifail1, irevcm, iterm,     &
                                          itn, its, liwork, lwneed, lwork,     &
                                          maxitn, maxits, monit, n, nnz
       CHARACTER (1)                   :: ckjdf, ckxef, norm, precon, sigcmp,  &
                                          weight
       CHARACTER (6)                   :: method
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), rdiag(:), wgt(:),        &
                                          work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11JDF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n
       READ (nin,*) nnz
       liwork = n + 1
       lwork = 6*n + 120

       ALLOCATE (a(nnz),b(n),rdiag(n),wgt(n),work(lwork),x(n),icol(nnz), &
          irow(nnz),iwork(liwork))
       READ (nin,*) method
       READ (nin,*) precon, sigcmp, norm, iterm
       READ (nin,*) tol, maxitn
       READ (nin,*) anorm, sigmax
       READ (nin,*) sigtol, maxits
       READ (nin,*) omega

!      Read 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)

!      Call F11GDF to initialize solver

       weight = 'N'
       monit = 0

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

!      Calculate reciprocal diagonal matrix elements.

       iwork(1:n) = 0

       DO i = 1, nnz
          IF (irow(i)==icol(i)) THEN
             iwork(irow(i)) = iwork(irow(i)) + 1
             IF (a(i)/=0.0E0_nag_wp) THEN
                rdiag(irow(i)) = 1.0E0_nag_wp/a(i)
             ELSE
                WRITE (nout,*) 'Matrix has a zero diagonal element'
                GO TO 20
             END IF
          END IF
       END DO

       DO i = 1, n
          IF (iwork(i)==0) THEN
             WRITE (nout,*) 'Matrix has a missing diagonal element'
             GO TO 20
          END IF
          IF (iwork(i)>=2) THEN
             WRITE (nout,*) 'Matrix has a multiple diagonal element'
             GO TO 20
          END IF
       END DO

!      Call F11GEF to solve the linear system

       irevcm = 0
       ckxef = 'C'
       ckjdf = 'C'

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

          IF (irevcm/=4) THEN
             ifail1 = -1
             SELECT CASE (irevcm)
             CASE (1)
!               Compute matrix vector product

                CALL f11xef(n,nnz,a,irow,icol,ckxef,x,b,ifail1)

                ckxef = 'N'
             CASE (2)
!               SSOR preconditioning

                CALL f11jdf(n,nnz,a,irow,icol,rdiag,omega,ckjdf,x,b,iwork, &
                   ifail1)

                ckjdf = 'N'
             END SELECT
             IF (ifail1/=0) irevcm = 6
          ELSE IF (ifail/=0) THEN
             WRITE (nout,99996) ifail
             GO TO 20
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP

!      Termination

       CALL f11gff(itn,stplhs,stprhs,anorm,sigmax,its,sigerr,work,lwork,ifail)

       WRITE (nout,99999) 'Converged in', itn, ' iterations'
       WRITE (nout,99998) 'Final residual norm =', stplhs

!      Output x

       WRITE (nout,99997) x(1:n)

20     CONTINUE

99999  FORMAT (1X,A,I10,A)
99998  FORMAT (1X,A,1P,E16.3)
99997  FORMAT (1X,1P,E16.4)
99996  FORMAT (1X/1X,' ** F11GEF returned with IFAIL = ',I5)
    END PROGRAM f11jdfe
