    PROGRAM f11drfe

!      F11DRF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11brf, f11bsf, f11btf, f11drf, f11xnf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: anorm, omega, sigmax, stplhs,        &
                                          stprhs, tol
       INTEGER                         :: i, ifail, ifail1, irevcm, iterm,     &
                                          itn, liwork, lwneed, lwork, m,       &
                                          maxitn, monit, n, nnz
       CHARACTER (1)                   :: ckdrf, ckxnf, norm, precon, trans,   &
                                          weight
       CHARACTER (8)                   :: method
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), rdiag(:), work(:), x(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: wgt(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DRF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n, m
       READ (nin,*) nnz
       lwork = max(121+n*(3+m)+m*(m+5),120+7*n,120+(2*n+m)*(m+2)+2*n,120+10*n)
       liwork = 2*n + 1
       ALLOCATE (a(nnz),b(n),rdiag(n),work(lwork),x(n),wgt(n),icol(nnz), &
          irow(nnz),iwork(liwork))
       READ (nin,*) method
       READ (nin,*) precon, norm, iterm
       READ (nin,*) tol, maxitn
       READ (nin,*) anorm, sigmax
       READ (nin,*) omega

!      Read the matrix A

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

!      Read rhs vector b and initial approximate solution x

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

!      Call F11BRF 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 f11brf(method,precon,norm,weight,iterm,n,m,tol,maxitn,anorm, &
          sigmax,monit,lwneed,work,lwork,ifail)

!      Calculate reciprocal diagonal matrix elements if necessary

       IF (precon=='P' .OR. precon=='p') THEN

          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,0.0E0_nag_wp)) THEN
                   rdiag(irow(i)) = (1.0E0_nag_wp,0.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

       END IF

!      Call F11BSF to solve the linear system

       irevcm = 0
       ckxnf = 'C'
       ckdrf = 'C'

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

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

                CALL f11xnf(trans,n,nnz,a,irow,icol,ckxnf,x,b,ifail1)

                ckxnf = 'N'
             CASE (-1)
!               Compute conjugate transposed matrix-vector product
                trans = 'T'

                CALL f11xnf(trans,n,nnz,a,irow,icol,ckxnf,x,b,ifail1)

                ckxnf = 'N'
             CASE (2)
!               SSOR preconditioning
                trans = 'N'

                CALL f11drf(trans,n,nnz,a,irow,icol,rdiag,omega,ckdrf,x,b, &
                   iwork,ifail1)

                ckdrf = 'N'
             END SELECT
             IF (ifail1/=0) irevcm = 6
          ELSE IF (ifail==0) THEN
!            Termination

             ifail = 0
             CALL f11btf(itn,stplhs,stprhs,anorm,sigmax,work,lwork,ifail)

             WRITE (nout,99996) itn
             WRITE (nout,99997) 'Matrix norm =', anorm
             WRITE (nout,99997) 'Final residual norm =', stplhs
             WRITE (nout,*)

!            Output x
             WRITE (nout,*) '                   X'
             WRITE (nout,99998) x(1:n)

             EXIT LOOP
          ELSE
             WRITE (nout,99999) ifail
             EXIT LOOP
          END IF
       END DO LOOP
20     CONTINUE

99999  FORMAT (1X/1X,' ** F11BSF returned with IFAIL = ',I5)
99998  FORMAT (1X,'(',1P,E16.4,',',1P,E16.4,')')
99997  FORMAT (1X,A,1P,E16.3)
99996  FORMAT (1X,'Converged in',I10,' iterations')
    END PROGRAM f11drfe
