C     --------------------
      SUBROUTINE WSTART1(H)
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 THE INITIAL HEADS FOR EACH NODE, EITHER
C     GLOBALY OR INDIVIDUALLY.  THE FIRST ENTRY POINT IS CALLED ONCE BY
C     THE MAIN PROGRAM. THE SECOND ENTRY POINT IS CALLED ONCE FOR A
C     GIVEN TIMESTEP AS DEFINED IN THE ISAVE VARIABLE.
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     INFILE      NAME OF INPUT FILE.
C     OUTFILE     OUTPUT FILE NAME.
C     OUTEXT      EXTENSION FOR OUTPUT FILE.
C     XCNAME      COMPARTMENT NAME.
C     XNNC        NUMBER OF NODES REPRESENTED BY INITIAL HEADS FOR
C                 COMPARTMENT.
C     H()         INITIAL HEAD VALUE AT NODE.
C     IGLOBAL     SWITCH TO INPUT GLOBAL INITIAL HEADS FROM PRIOR
C                 SIMULATION.
C     ISAVE       SWIICH FOR OVERWRITING GLOBAL INITIAL HEADS.
C     IWHEN       SWITCH FOR WHICH TIME STEP TO OVERWRITE GLOBAL INITIAL
C                 HEADS.
C     IECHO1      SWITCH FOR ECHO OF HEADS.
C     ISTART      SWITCH FOR INPUT OF UNIFORM INITIAL HEADS FOR
C                 COMPARTMENT.
C     XH0         UNIFORM INITAL HEAD ASSIGNED TO COMPARTMENT.
C
C     INTERNAL ARRAYS.
      INTEGER*4 XNNC
      REAL*8 H(MAXNN)
      CHARACTER*80 INFILE,OUTFILE,OUTEXT
      CHARACTER*20 XCNAME
      LOGICAL DONE
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     NNC()       NUMBER OF NODES IN A COMPARTMENT.
C     NEC()       NUMBER OF WEDGE ELEMENTS WITHIN A COMPARTMENT.
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     IEL()       PARENT WEDGE ELEMENT FOR TETRAHEDRAL ELEMENT.
C     X()         X-COORDINATE OF A NODE.
C     Y()         Y-COORDINATE OF A NODE.
C     Z()         Z-COORDINATE OF A NODE.
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     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     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     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       TIME STEP NUMBER WITHIN FLOW SIMULATION.
C     ISTEP1      STARTING STEP NUMBER FOR SIMULATION.
C     ISTEP2      ENDING STEP FOR SIMULATION.
C     DELT        LENGTH OF TIME STEP.
C     STIME       INITIAL ELAPSED TIME FOR SIMULATION.
C     IPRN        FLAG TO INDICATED 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     NIA         NUMBER OF ROWS REPRESENTED IN VECTORS A() OR B().
C     NJA         NUMBER OF NON-ZERO COEFFICIENTS IN VECTORS A() OR B().
C     ITRAN       SWITCH FOR TRANSPORT SIMULATION.
C     IRINT       INTERNAL READ FOR INT UNIT NUMBER.
C     IWINT       INTERNAL WRITE FOR INT UNIT NUMBER.
C
C     COMMON BLOCKS.
      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(*,900)
      OUTEXT='.INT.OUT'
      INFILE=FNAME(5)
      CALL OPENFILES(RNAME1,INFILE,OUTFILE,OUTEXT,IRINT,IWINT)
      WRITE(IWINT,902)
      WRITE(IWINT,904) INFILE,OUTFILE
C
C     INPUT GLOBAL INITIAL HEADS
      READ(IRINT,906) IGLOBAL,ISAVE,IWHEN,IECHO1
      WRITE(IWINT,908) IGLOBAL,ISAVE,IWHEN,IECHO1
      IF (IGLOBAL.EQ.1) THEN
          READ(IRINT,910) (H(I),I=1,NN)
C
C         OUTPUT GLOBAL INITIAL HEADS
          IF (IECHO1.EQ.1) THEN
              WRITE(IWINT,912)
              DONE=.FALSE.
              I1=-4
              DO WHILE (.NOT.DONE)
                  I1=I1+5
                  I2=I1+4
                      IF (I2.GE.NN) THEN
                          I2=NN
                          DONE=.TRUE.
                      ENDIF
                      WRITE(IWINT,914) I1,I2,(H(I),I=I1,I2)
              END DO
          ENDIF
      ENDIF
C
C
C     INPUT COMPARTMENT INITIAL HEADS
      ILAST=0
      DO ICELL=1,NCELL
          IF (IGLOBAL.EQ.1) EXIT
C
C         INPUT COMPARTMENT INITIAL HEADS.
          READ(IRINT,916) XCNAME
          READ(IRINT,918) XNNC,ISTART,XH0,IECHO1
          WRITE(IWINT,920) NCELL,ICELL,XCNAME,XNNC,ISTART,XH0,IECHO1
C
C         CHECK COMPARTMENT NAME.
          IF (XCNAME.NE.CNAME(ICELL)) THEN
              WRITE(*,922)
              WRITE(*,924)
              STOP
          END IF
C
C         CHECK NUMBER OF NODES MATCHES COMPARTMENT.
          IF (XNNC.NE.NNC(ICELL)) THEN
              WRITE(*,926)
              WRITE(*,924)
              STOP
          ENDIF
C
C         CHECK GLOBAL NUMBER OF NODES.
          IF (NN.GT.MAXNN) THEN
              WRITE(*,928)
              WRITE(*,924)
              STOP
          END IF
C
C         ASSIGN HEADS.
          IF (ISTART.EQ.1) THEN
C
C             ASSIGN UNIFORM HEADS.
              DO I=1,NNC(ICELL)
                  H(I+ILAST)=XH0
              END DO
          ELSE
C
C             INPUT NODAL HEADS.
              READ(IRINT,930) (H(I+ILAST),I=1,NNC(ICELL))
          ENDIF
C
C         OUTPUT INITIAL HEADS
          IF (IECHO1.EQ.1) THEN
              WRITE(IWINT,932)
              DONE=.FALSE.
              I1=-4
              DO WHILE (.NOT.DONE)
                  I1=I1+5
                  I2=I1+4
                      IF (I2.GE.NNC(ICELL)) THEN
                          I2=NNC(ICELL)
                          DONE=.TRUE.
                      ENDIF
                      WRITE(IWINT,934) I1,I2,I1+ILAST,I2+ILAST,
     +                    (H(I+ILAST),I=I1,I2)
              END DO
          ENDIF
          ILAST=ILAST+NNC(ICELL)
      END DO
C
C     CLOSE FILES AND EXIT WSTART
      WRITE(*,936)
      WRITE(IWINT,938)
      IF (ISAVE.EQ.0) CLOSE (IRINT)
      CLOSE (IWINT)
      RETURN
C
C     FORMATS.
900   FORMAT(1X,'*START READING IN START')
902   FORMAT(1X,'**START READING IN START**')
904   FORMAT(/1X,'FILE NAMES:'/
     +    1X,'INPUT FILE..............................',A80/
     +    1X,'OUTPUT FILE.............................',A80)
906   FORMAT(4I10)
908   FORMAT(/1X,'INPUT PARAMETERS:'/
     +    1X,'SWITCH FOR GLOBAL INITIAL HEADS.........',I10/
     +    1X,'SWITCH FOR OVERWRITING INITIAL VALUES...',I10/
     +    1X,'STEP FOR OVERWRITING INITIAL VALUES.....',I10/
     +    1X,'SWITCH FOR ECHO OF INITIAL HEADS........',I10)
910   FORMAT(20X,5F15.0)
912   FORMAT(/1X,'INITIAL HEADS:'/
     +    1X,4X,'GLOBAL',4X,'GLOBAL'/
     +    1X,6X,'NODE',6X,'NODE',5(6X,'HEAD'))
914   FORMAT(1X,2I10,5F10.2)
916   FORMAT(A20)
918   FORMAT(2I10,F10.0,I10)
920   FORMAT(/1X,'PARAMETERS FOR INITIAL HEADS:'/
     +    1X,'NUMBER OF COMPARTMENTS..................',I10/
     +    1X,'COMPARTMENT NUMBER......................',I10/
     +    1X,'COMPARTMENT NAME........................',A20/
     +    1X,'NUMBER OF INITIAL HEAD NODES............',I10/
     +    1X,'SWITCH FOR INPUT OF UNIFORM HEAD........',I10/
     +    1X,'UNIFORM HEAD............................',F10.2/
     +    1X,'SWITCH FOR ECHO OF INITIAL HEADS........',I10)
922   FORMAT(1X,'*ERROR* COMPARTMENT NAME NOT IN LIST')
924   FORMAT(/1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN START')
926   FORMAT(/1X,'*ERROR* NUMBER OF NODES DOES NOT MATCH PREVIOUS')
928   FORMAT(/1X,'*ERROR* NUMBER OF NODES EXCEEDS DIMENSION')
930   FORMAT(5F10.0)
932   FORMAT(/1X,'INITIAL HEADS:'/
     +    1X,2(5X,'LOCAL'),2(4X,'GLOBAL')/
     +    1X,4(6X,'NODE'),5(6X,'HEAD'))
934   FORMAT(1X,4I10,5F10.2)
936   FORMAT(1X,'*END   READING IN START')
938   FORMAT(/1X,'**END READING IN START**')
C
C     ----------------
      ENTRY WSTART2(H)
C     ----------------
C
      IF (ISAVE.EQ.0) RETURN
      IF (ISTEP.NE.IWHEN) RETURN
C
C     OVERWRITE FILE OF INITIAL HEADS.
      IGLOBAL=1
      REWIND (IRINT)
      WRITE(IRINT,940) IGLOBAL,ISAVE,IWHEN,IECHO1
      WRITE(IRINT,942) (H(I),I=1,NN)
C
C     CLOSE FILE AND EXIT WSTART.
      CLOSE (IRINT)
      RETURN
C
C     FORMATS:
940   FORMAT(4I10)
942   FORMAT(20X,5F15.5)
      END SUBROUTINE
