C
C     --------------------------------------
      SUBROUTINE SRELAX1(RNAME,ISEL,OUTFILE)
C     --------------------------------------
C
C     SUBROUTINE AUTHOR:
C     TIM DURBIN
C     TIMOTHY J. DURBIN, INC.
C     5330 PRIMROSE DRIVE, SUITE 228
C     FAIR OAKS, CA 95628
C     (916) 536-2314
C
C     PURPOSE:
C     THIS SUBROUTINE INPUTS PAPAMETERS FOR SOLVING THE SYSTEM
C     OF LINEAR EQUATIONS A*X=B USING THE POINT OVERRELAXATION METHOD,
C     AND IT SOLVES THE SYSTEM OF EQUATIONS. THE FIRST ENTRY POINT IS
C     CALLED ONCE BY SUBROUTINE BASIC. THE SECOND ENTRY POINT IS CALLED
C     BY SUBROUTINE WFLOW AT EACH TIMESTEP ITERATION.
C
      INCLUDE 'DIMEN.INC'
      INCLUDE 'WUNITS.INC'
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER*4 (I-N)
C
C     INTERNAL SCALARS AND ARRAYS.
C     OUTFILE     OUTPUT FILE NAME.
C     RNAME       SIMULATION NAME.
C     A()         LEFTTHAND MATRIX WITHIN SYSTEM OF EQUATIONS.
C     X()         VECTOR OF UNKNOWNS WITHIN SYSTEM OF EQUATIONS.
C     B()         RIGHTHAND VECTOR WITHIN SYSTEM OF EQUATIONS.
C     IA()        VECTOR INDICATING NUMBER OF NON-ZERO COEFFICIENTS
C                 IN A ROW OF THE LEFTHAND MATRIX.
C     JA()        VECTOR INDICATING COLUMN NUMBER OF A NON-ZERO
C                 COEFFICIENT WITHIN A ROW OF THE LEFTHAND MATRIX.
C     ISEL        SWITCH FOR SELECTING PARAMETER SET FOR SOLVING
C                 GROUNDWATER-FLOW OR SOLUTE-TRANSPORT EQUATIONS.
C     OMEGA()     OVERRELAXATION FACTOR FOR SOLVER.
C     CONV()      CLOSURE CRITERION FOR SOLVER ITERATIONS.
C     NITER()     MAXIMUM NUMBER OF SOLVER ITERATIONS.
C     NOUT()      NUMBER OF ITERATIONS BETWEEN OUTPUT OF INTERMEDIATE
C                 SOLVER RESULTS.
C     DFOUT()     HOLDS FIVE OUTPUT VALUES OF ITERATION CLOSURES.
C     ITOUT()     HOLDS FIVE OUTPUT VALUES OF ITERATION NUMBER.
C
      CHARACTER*80 OUTFILE
      CHARACTER*12 RNAME
      INTEGER*4 NITER(2),NOUT(2)
      REAL*8 OMEGA(2),CONV(2)
      REAL*8 A(MAXNON),B(MAXNN),X(MAXNN),DFOUT(5)
      INTEGER*4 IA(MAXNN+1),JA(MAXNON),ITOUT(5)
      SAVE
C
C     SOLVER PARAMETERS
      IF (ISEL.EQ.1) THEN
          WRITE(IWBAS,902)
      ELSE
          WRITE(IWBAS,904)
      ENDIF
      READ(IRBAS,906) NITER(ISEL),OMEGA(ISEL),CONV(ISEL),NOUT(ISEL)
      WRITE(IWBAS,908) NITER(ISEL),OMEGA(ISEL),CONV(ISEL),NOUT(ISEL),
     +    OUTFILE
      IF (OMEGA(ISEL).GE.2.0) THEN
          WRITE(*,910)
          WRITE(*,912)
          STOP
      ENDIF
C
C     FORMATS:
902   FORMAT(/1X,'FLOW EQUATIONS')
904   FORMAT(/1X,'TRANSPORT EQUATIONS')
906   FORMAT(I10,2F10.0,2I10)
908   FORMAT(1X,'OVER-RELAXATION SOLUTION PARAMETERS:'/
     +    1X,'MAXIMUM ITERATIONS......................',I10/
     +    1X,'RELAXATION FACTOR.......................',F10.2/
     +    1X,'CLOSURE CRITERION.......................',1PE10.2/
     +    1X,'NUMBER OF ITERATIONS BETWEEN OUTPUTS....',I10/
     +    1X,'OUTPUT FILE FOR SOLVER..................',A80)
910   FORMAT(1X,'*ERROR* RELAXATION FACTOR TOO LARGE'/
     +    1X,'OMEGA=    ',F10.2/
     +    1X,'ISEL=     ',I10)
912   FORMAT(1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN BASIC')
      RETURN
C
C     -----------------------------------------------
      ENTRY SRELAX2(IA,JA,A,X,B,NEQ,ISTEP,JITER,ISEL)
C     -----------------------------------------------
C
C     INITIAL OUTPUT.
      IF (ISEL.EQ.1) THEN
          WRITE(IWSOL,914)
      ELSE
          WRITE(IWSOL,916)
      ENDIF
      WRITE(IWSOL,918) ISTEP,JITER
      IOUT=0
C
C     SOLVE EQUATION A*X=F
      ITER_LOOP: DO ITER=1,NITER(ISEL)
C
C         LOOP OVER ROWS.
          DIFF=0.0
          DO II=1,NEQ
              IJA1=IA(II)
              IJA2=IA(II+1)-1
              DIAG=0.0
              SUM=0.0
C
C             CALCULATE NEW VALUE FOR ROW.
              DO IJA=IJA1,IJA2
                  JJ=JA(IJA)
                  IF (II.EQ.JJ) THEN
                      DIAG=A(IJA)
                  ELSE
                      SUM=SUM+A(IJA)*X(JJ)
                  ENDIF
              END DO
              IF (DIAG.EQ.0.0) THEN
                  WRITE(*,919)
                  WRITE(*,920)
                  STOP
              ENDIF
              XTEMP=X(II)+OMEGA(ISEL)*(B(II)/DIAG-X(II)-SUM/DIAG)
              DIFF=MAX(DIFF,ABS(X(II)-XTEMP))
              X(II)=XTEMP
          END DO
C
C         INTERMEDIATE OUTPUT.
          IF (MOD(ITER,NOUT(ISEL)).EQ.0) THEN
              IOUT=IOUT+1
              ITOUT(IOUT)=ITER
              DFOUT(IOUT)=DIFF
              IF (IOUT.EQ.5) THEN
                  WRITE(IWSOL,921) (ITOUT(I),DFOUT(I),I=1,IOUT)
                  IOUT=0
              ENDIF
          ENDIF
C
C         TEST FOR CLOSURE.
          IF (DIFF.LE.CONV(ISEL)) EXIT
      END DO ITER_LOOP
C
C     FINAL OUTPUT.
      ITER=MIN(ITER,NITER(ISEL))
      IF (IOUT.NE.0) THEN
          WRITE(IWSOL,921) (ITOUT(I),DFOUT(I),I=1,IOUT),ITER,DIFF
      ELSE
          WRITE(IWSOL,921) ITER,DIFF
      ENDIF
      WRITE(IWSOL,922) ISTEP,ITER,CONV(ISEL),DIFF
      IF (ITER.NE.NITER(ISEL)) THEN
          WRITE(IWSOL,924)
      ELSE
          WRITE(IWSOL,926)
      ENDIF
      WRITE(*,928) ITER,DIFF
C
C     FORMATS:
914   FORMAT(/1X,'FLOW EQUATIONS')
916   FORMAT(/1X,'TRANSPORT EQUATIONS')
918   FORMAT(/1X,'ITERATION RESULTS:'/
     +    1X,'TIME STEP...............................',I10/
     +    1X,'TIME STEP ITERATION.....................',I10//
     +    1X,5(1X,'ITERATION',2X,'RESIDUAL'))
919   FORMAT(1X,'*ERROR* ZERO ON DIAGONAL')
920   FORMAT(1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN SRELAX')
921   FORMAT(1X,5(I10,1PE10.2))
922   FORMAT(/1X,'FINAL SOLUTION RESULTS:'/
     +    1X,'TIME STEP...............................',I10/
     +    1X,'NUMBER OF ITERATIONS....................',I10/
     +    1X,'CLOSURE CRITERION.......................',1PE10.2/
     +    1X,'FINAL RESIDUAL..........................',1PE10.2)
924   FORMAT(1X,'CLOSURE STATUS..........................','OK')
926   FORMAT(1X,'CLOSURE STATUS..........................','NO CLOSURE')
928   FORMAT(1X,' SOLVER ITERATIONS=',I6,' CLOSURE=',1PE9.2)
      RETURN
      END SUBROUTINE
