*     F11BRF Example Program Text
*     Mark 19 Release. NAG Copyright 1999.
*     .. Parameters ..
      INTEGER          NIN, NOUT
      PARAMETER        (NIN=5,NOUT=6)
      INTEGER          NMAX, LA, LIWORK, LWORK
      PARAMETER        (NMAX=1000,LA=10000,LIWORK=7*NMAX+2,LWORK=6*NMAX)
*     .. Local Scalars ..
      DOUBLE PRECISION ANORM, DTOL, SIGMAX, STPLHS, STPRHS, TOL
      INTEGER          I, IFAIL, IFAIL1, IFAILX, IREVCM, ITERM, ITN,
     +                 LFILL, LWREQ, M, MAXITN, MONIT, N, NNZ, NNZC,
     +                 NPIVM
      LOGICAL          LOOP
      CHARACTER        MILU, NORM, PRECON, PSTRAT, WEIGHT
      CHARACTER*8      METHOD
*     .. Local Arrays ..
      COMPLEX *16      A(LA), B(NMAX), WORK(LWORK), X(NMAX)
      DOUBLE PRECISION WGT(NMAX)
      INTEGER          ICOL(LA), IDIAG(NMAX), IPIVP(NMAX), IPIVQ(NMAX),
     +                 IROW(LA), ISTR(NMAX+1), IWORK(LIWORK)
*     .. External Subroutines ..
      EXTERNAL         F11BRF, F11BSF, F11BTF, F11DNF, F11DPF, F11XNF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F11BRF Example Program Results'
*
*     Skip heading in data file
*
      READ (NIN,*)
      READ (NIN,*) N
      IF (N.LE.NMAX) THEN
*
*        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.0D0
         SIGMAX = 0.0D0
*
*        Read the parameters for the preconditioner
*
         READ (NIN,*) LFILL, DTOL
         READ (NIN,*) MILU, PSTRAT
*
*        Read the number of non-zero elements of the matrix A, then read
*        the non-zero elements
*
         READ (NIN,*) NNZ
         DO 20 I = 1, NNZ
            READ (NIN,*) A(I), IROW(I), ICOL(I)
   20    CONTINUE
*
*        Read right-hand side vector b and initial approximate
*        solution x
*
         READ (NIN,*) (B(I),I=1,N)
         READ (NIN,*) (X(I),I=1,N)
*
*        Calculate incomplete LU factorization
*
         IFAIL = 0
         CALL F11DNF(N,NNZ,A,LA,IROW,ICOL,LFILL,DTOL,PSTRAT,MILU,IPIVP,
     +               IPIVQ,ISTR,IDIAG,NNZC,NPIVM,IWORK,LIWORK,IFAIL)
*
*        Call F11BRF to initialize the solver
*
         IFAIL = 0
         CALL F11BRF(METHOD,PRECON,NORM,WEIGHT,ITERM,N,M,TOL,MAXITN,
     +               ANORM,SIGMAX,MONIT,LWREQ,WORK,LWORK,IFAIL)
*
*        Call repeatedly F11BSF 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
*
         IFAIL = 0
         IREVCM = 0
         LOOP = .TRUE.
*
         LWREQ = LWORK
   40    CONTINUE
         CALL F11BSF(IREVCM,X,B,WGT,WORK,LWREQ,IFAIL)
         IF (IREVCM.EQ.-1) THEN
            IFAILX = -1
            CALL F11XNF('Transpose',N,NNZ,A,IROW,ICOL,'No checking',X,B,
     +                  IFAILX)
         ELSE IF (IREVCM.EQ.1) THEN
            IFAILX = -1
            CALL F11XNF('No transpose',N,NNZ,A,IROW,ICOL,'No checking',
     +                  X,B,IFAILX)
         ELSE IF (IREVCM.EQ.2) THEN
            IFAIL1 = -1
            CALL F11DPF('No transpose',N,A,LA,IROW,ICOL,IPIVP,IPIVQ,
     +                  ISTR,IDIAG,'No checking',X,B,IFAILX)
         ELSE IF (IREVCM.EQ.3) THEN
            IFAIL1 = 0
            CALL F11BTF(ITN,STPLHS,STPRHS,ANORM,SIGMAX,WORK,LWREQ,
     +                  IFAIL1)
            WRITE (NOUT,99999) ITN, STPLHS
            WRITE (NOUT,99998)
            WRITE (NOUT,99996) (X(I),I=1,N)
            WRITE (NOUT,99997)
            WRITE (NOUT,99996) (B(I),I=1,N)
         ELSE IF (IREVCM.EQ.4) THEN
            LOOP = .FALSE.
         END IF
         IF (LOOP) GO TO 40
*
*        Obtain information about the computation
*
         IFAIL1 = 0
         CALL F11BTF(ITN,STPLHS,STPRHS,ANORM,SIGMAX,WORK,LWREQ,IFAIL1)
*
*        Print the output data
*
         WRITE (NOUT,99995)
         WRITE (NOUT,99994) 'Number of iterations for convergence:    ',
     +     ITN
         WRITE (NOUT,99993) 'Residual norm:                           ',
     +     STPLHS
         WRITE (NOUT,99993) 'Right-hand side of termination criterion:',
     +     STPRHS
         WRITE (NOUT,99993) '1-norm of matrix A:                      ',
     +     ANORM
*
*        Output x
*
         WRITE (NOUT,99998)
         WRITE (NOUT,99996) (X(I),I=1,N)
         WRITE (NOUT,99997)
         WRITE (NOUT,99996) (B(I),I=1,N)
      END IF
      STOP
*
99999 FORMAT (/1X,'Monitoring at iteration no.',I4,/1X,1P,'residual no',
     +       'rm: ',D14.4)
99998 FORMAT (/2X,'  Solution vector')
99997 FORMAT (/2X,'  Residual vector')
99996 FORMAT (1X,'(',1P,D16.4,',',1P,D16.4,')')
99995 FORMAT (/1X,'Final Results')
99994 FORMAT (1X,A,I4)
99993 FORMAT (1X,A,1P,D14.4)
      END
