C     --------------------------------------------------------
      SUBROUTINE BASIC(NITER,CONV,ISOLVE1,ISOLVE2,ISS,IMATRIX,
     +    ICHECK,DFMIN,SHAPE,RFACT)
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 INFORMATION ON THE NUMBER AND NAMES OF
C     COMPARTMENTS, THE WITHIN-TIMESTEP ITERATIONS, THE EQUATION-SOLVER
C     PARAMETERS, THE TIMESTEP CONFIGURATION, AND THE OUTPUT FREQUENCY.
C     THE SUBROUTINE IS CALLED ONCE BY THE MAIN PROGRAM.
C
      INCLUDE 'DIMEN.INC'
      INCLUDE 'WUNITS.INC'
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER*4 (I-N)
      CHARACTER*80 INFILE,OUTFILE,OUTEXT
      CHARACTER*80 TITLE1,TITLE2
C
C     INTERNAL SCALARS AND ARRAYS:
C     CONV        CONVERGENCE CRITERIA FOR TIME-STEP ITERATIONS.
C     DFMIN       MINIMUM VALUE OF UNDER-RELAXATION FUNCTION.
C     ICHECK      SWITCH FOR INPUT CHECKING ONLY.
C     IMATRIX     SWITCH FOR OUTPUT OR INPUT OF STRUCTURE FOR MATRICIES
C                     A() OR B().
C     INFILE      NAME OF INPUT FILE.
C     IRBAS       INTERNAL READ FOR BASIC UNIT NUMBER.
C     ISOLVE1     SWITCH FOR SOLVER USED FOR FLOW.
C     ISOLVER2    SWITCH FOR SOLVER USED FOR TRANSPORT.
C     ISS         SWITCH FOR STEADY STATE IN FIRST TIME STEP.
C     IWBAS       INTERNAL WRITE BASIC UNIT NUMBER.
C     NITER       MAXIMUM NUMBER OF ITERATIONS WITHIN TIME STEP.
C     OUTFILE     OUTPUT FILE NAME.
C     OUTEXT      EXTENSION FOR OUTPUT FILE.
C     RFACT       FACTOR FOR REDUCING DFMIN WHEN INTERATIONS DIVERGE.
C     SHAPE       COEFFICIENT CONTROLLING SHAPE OF FUNCTION.
C     TITLE1      PRIMARY TITLE FOR SIMULATION.
C     TITLE2      SECONDARY TITLE FOR SIMULATION.
C
C     COMMON BLOCKS.
C     FNAME()     NAMES OF INPUT FILES.
C     RNAME1      SIMULATION NAME FOR GROUNDWATER-FLOW FILES.
C     RNAME2      SIMULATION NAME FOR SOLUTE-TRANSPORT FILES.
C     NCELL       NUMBER OF COMPARTMENTS.
C     CNAME()     NAMES FOR COMPARTMENTS.
C     ITRAN       SWITCH FOR TRANSPORT SIMULATION.
C     NNC()       NUMBER OF NODES WITHIN A COMPARTMENT.
C     NEC()       NUMBER OF WEDGE ELEMENTS WITHIN A COMPARTMENT.
C     X()         X-COORDINATE OF A NODE.
C     Y()         Y-COORDINATE OF A NODE.
C     Z()         Z-COORDINATE OF A NODE.
C     IEL()       PARENT WEDGE ELEMENT FOR TETRAHEDRAL ELEMENT.
C     IN()        ELEMENT INCIDENCE FOR A WEDGE ELEMENT.
C     IN2()       ELEMENT INCIDENCES FOR A TETRAHEDRAL ELEMENT,
C                     WELL LINK, FAULT ELEMENT, OR FAULT LINK.
C     NE          GLOBAL NUMBER OF WEDGE ELEMENTS.
C     NE2         GLOBAL NUMBER OF TETRAHEDRAL ELEMENTS.
C     NE3         GLOBAL NUMBER OF TETRAHEDRAL ELEMENS AND WELL LINKS.
C     NE4         GLOBAL NUMBER OF TETHEDRALAL ELEMENTS, WELL LINKS,
C                     AND FAULT ELEMENTS.
C     NE5         GLOBAL NUMBER OF TETHEDRALAL ELEMENTS, WELL LINKS,
C                     FAULT ELEMENTS, AND FAULT LINKS.
C     NN          GLOBAL NUMBER OF MESH NODES.
C     NN3         GLOBAL NUMBER OF MESH NODES AND WELL NODES.
C     NN5         GLOBAL NUMBER OF MESH NODES, WELL NODES, AND
C                     FAULT NODES.
C     ISTEP       NUMBER OF TIME STEPS WITHIN FLOW SIMULATION.
C     ISTEP1      STARTING TIME STEP NUMBER FOR SIMULATION.
C     ISTEP2      ENDING TIME STEP NUMBER FOR SIMULATION.
C     DELT        LENGTH OF TIME STEP.
C     STIME       INITIAL ELAPSED TIME FOR SIMULATION.
C     IPRN        FLAG TO INDICATE PRINT OUTPUT FOR TIME STEP.
C     IPLT        FLAG TO INDICATE PLOT OUTPUT FOR TIME STEP.
C     NTSTEP      NUMBER OF TRANSPORT STEPS WITHIN FLOW STEP.
C     ITSTEP      TIME STEP NUMBER WITHIN TRANSPORT SIMULATION.
C     A()         VECTOR OF NON-ZERO COEFFICIENTS FOR CONDUCTANCE
C                     MATRIX.
C     B()         VECTOR OF NON-ZERO COEFFICIENTS FOR CAPACITANCE
C                     MATRIX.
C     F()         RIGHTHAND VECTOR FOR SYSTEM OF EQUATIONS.
C     IA()        VECTOR INDICATING POSITIONS OF ROW ENDS IN
C                     VECTOR A() OR B().
C     JA()        VECTOR INDICATION COLUMN NUMBER ASSOCIATED WITH
C                     ENTRIES IN VECTORS A() OR B().
C     NIA         NUMBER OF ROWS REPRESENTED IN VECTORS A() OR B().
C     NJA         NUMBER OF NON-ZERO COEFFICIENTS IN VECTORS A() OR B().
C     QX()        DARCY GROUNDWATER VELOCITY IN THE X-DIRECTION.
C     QY()        DARCY GROUNDWATER VELOCITY IN THE Y-DIRECTION.
C     QZ()        DARCY GROUNDWATER VELOCITY IN THE Z-DIRECTION.
C
      CHARACTER*80 FNAME(30)
      CHARACTER*12 RNAME1,RNAME2
      CHARACTER*20 CNAME(MAXCEL)
      INTEGER*4 NNC(MAXCEL),NEC(MAXCEL)
      INTEGER*4 IN(MAXNE,6),IN2(MAXNE2,4),IEL(MAXNE2)
      REAL*8 X(MAXNN),Y(MAXNN),Z(MAXNN)
      INTEGER*4 IA(MAXNN+1),JA(MAXNON)
      REAL*8 A(MAXNON),B(MAXNON),F(MAXNN)
      REAL*8 QX(MAXNE2),QY(MAXNE2),QZ(MAXNE2)
      COMMON /NAMES/ FNAME,RNAME1,RNAME2
      COMMON /CELLS/ NCELL,CNAME,NNC,NEC
      COMMON /MESH/ X,Y,Z,IN,IN2,IEL,NE,NE2,NE3,NE4,NE5,NN,NN3,NN5
      COMMON /STEPS/ ISTEP,ISTEP1,ISTEP2,DELT,STIME,IPRN,IPLT,NTSTEP,
     +    ITSTEP
      COMMON /MATRIX/ A,B,IA,JA,NIA,NJA,F
      COMMON /SHARE/ QX,QY,QZ,ITRAN
      SAVE
C
C     OPEN INPUT AND OUTPUT FILES.
      WRITE(*,902)
      OUTEXT='.BAS.OUT'
      INFILE=FNAME(1)
      CALL OPENFILES(RNAME1,INFILE,OUTFILE,OUTEXT,IRBAS,IWBAS)
      WRITE(IWBAS,903)
      WRITE(IWBAS,904) INFILE,OUTFILE
C
C     SIMULATION NAME
      READ(IRBAS,906) TITLE1,TITLE2
      WRITE(IWBAS,908) RNAME1,TITLE1,TITLE2
C
C     INPUT NUMBER OF COMPARTMENTS AND COMPARTMENT NAMES.
      READ(IRBAS,910) NCELL
      WRITE(IWBAS,912) NCELL
      WRITE(IWBAS,916)
      DO ICELL=1,NCELL
          READ(IRBAS,914) CNAME(ICELL)
          WRITE(IWBAS,918) ICELL,CNAME(ICELL)
      END DO
C
C     SPECIFY TIME-STEP ITERATIONS AND SIMULATION SOLVER.
      READ(IRBAS,920) NITER,CONV,DFMIN,SHAPE,RFACT
      READ(IRBAS,921) ISOLVE1,ISOLVE2,ISS,ITRAN,IMATRIX,ICHECK
      WRITE(IWBAS,922) NITER,CONV,DFMIN,SHAPE,RFACT,
     +    ISOLVE1,ISOLVE2,ISS,ITRAN,IMATRIX,ICHECK
C
C     OPEN SOLVER OUTPUT FILE AND INPUT SOLVER PARAMETERS.
      OUTEXT='.SOL.OUT'
      CALL OPENFILES(RNAME1,INFILE,OUTFILE,OUTEXT,0,IWSOL)
      ISEL=1
      IF (ISOLVE1.EQ.1) THEN
          CALL SRELAX1(RNAME1,ISEL,OUTFILE)
      ELSEIF (ISOLVE1.EQ.2) THEN
          CALL SITPACK1(RNAME1,ISEL,OUTFILE)
      ELSEIF (ISOLVE1.EQ.3) THEN
          CALL SAMG1(RNAME1,ISEL,OUTFILE)
      ENDIF
      IF (ITRAN.EQ.1) THEN
          ISEL=2
          IF (ISOLVE2.EQ.1) THEN
              CALL SRELAX1(RNAME1,ISEL,OUTFILE)
          ELSEIF (ISOLVE2.EQ.2) THEN
              CALL SITPACK1(RNAME1,ISEL,OUTFILE)
          ELSEIF (ISOLVE2.EQ.3) THEN
              CALL SAMG1(RNAME1,ISEL,OUTFILE)
          ENDIF
      ENDIF
C
C     CONFIGURE TIME STEPPING SCHEME.
      CALL STEPS1
C
C     CHECK AVAILABILITY OF MATRIX FILE.
      IF (IMATRIX.EQ.2 .AND. FNAME(13).EQ.'NOTUSED') THEN
          WRITE(*,924)
          WRITE(*,926)
          STOP
      ENDIF
C
C     CLOSE FILES.
      WRITE(*,928)
      WRITE(IWBAS,930)
      CLOSE(IRBAS)
      CLOSE(IWBAS)
      RETURN
C
C     FORMATS:
902   FORMAT(1X,'*START READING IN BASIC')
903   FORMAT(1X,'**START READING IN BASIC**')
904   FORMAT(/1X,'FILE NAMES:'/
     +    1X,'INPUT FILE..............................',A80/
     +    1X,'OUTPUT FILE.............................',A80)
906   FORMAT(A80)
908   FORMAT(/1X,'SIMULATION IDENTIFIERS:'/
     +    1X,'SIMULATION NAME.........................',A12/
     +    1X,'SIMULATION TITLE........................',A80/
     +    1X,'SIMULATION TITLE........................',A80)
910   FORMAT(I10)
912   FORMAT(1X,'NUMBER OF COMPARTMENTS..................',I10)
914   FORMAT(A20)
916   FORMAT(/1X,'COMPARTMENT NAMES:')
918   FORMAT(1X,I10,2X,A20)
920   FORMAT(I10,4F10.0)
921   FORMAT(6I10)
922   FORMAT(/1X,'ITERATION AND SOLVER PARAMETERS:'/
     +    1X,'NUMBER OF TIME-STEP ITERATIONS..........',I10/
     +    1X,'TIME-STEP CONVERGENCE CLOSURE...........',1PE10.2/
     +    1X,'UNDERRELAXATION FACTOR MINIMUM..........',1PE10.2/
     +    1X,'UNDERRELAXATION SHAPE FACTOR............',1PE10.2/
     +    1X,'UNDERRELAXATION REDUCTION FACTOR........',1PE10.2/
     +    1X,'SWITCH FOR FLOW SOLVER..................',I10/
     +    1X,'SWITCH FOR TRANSPORT SOLVER.............',I10/
     +    1X,'SWITCH FOR ININTIAL STEADY STATE........',I10/
     +    1X,'SWITCH FOR TRANSPORT SIMULATION.........',I10/
     +    1X,'SWITCH FOR SAVING MATRIX CONFIGURATION..',I10/
     +    1X,'SWITCH FOR INPUT CHECKING ONLY..........',I10)
924   FORMAT(1X,'*ERROR* MATRIX FILE NOT PROVIDED')
926   FORMAT(1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN BASIC')
928   FORMAT(1X,'*END   READING IN BASIC')
930   FORMAT(/1X,'**END READING IN BASIC**')
      END SUBROUTINE
