      PROGRAM SAT2D
C
C-----------------------------------------------------------------------
C
C 2-D saturated flow
C MP/CP, March/93
C
C--------------------------------DESCRIPTION OF PARAMETERS AND VARIABLES
C
C Dimensioning Parameters
C -----------------------
C   NMAX   - maximum N    
C   NTRMAX - maximum NT
C   NPMAX  - maximum NP
C   NQMAX  - maximum NQ
C   MAXZON - maximum NZONE
C   MAXTRM - maximum NTERM
C   NRMAX  - maximum NR
C   MAXPRT - maximum NPRT
C   N1MAX  - maximum N1
C   NTPMAX - N1MAX*NMAX
C
C Actual or Minimum Dimensions
C ----------------------------
C   N      - # of nodes in 2-d mesh. 
C   NT     - # of triangles in 2-d mesh
C   NP     - # of Dirichlet nodes in 2-d mesh. 
C   NQ     - # of Neumann nodes in 2-d mesh
C   NZONE  - # of material types in the porous medium
C   NTERM  - # of nonzero elements in system matrices (symmetric storage
C            used for saturated flow equation)
C   NSTEP  - time step index
C   NR     - # of nodes selected for partial output
C   NPRT   - # of time values for detailed output (see description of 
C            TIMPRT)
C   N1     - maximum # of element connections to a node
C
C Integer Parameters, Flags, and Indices
C --------------------------------------
C   KPRT   - index to current time value for detailed output
C   IPRT   - flag for detailed output at all nodes
C            =0 don't print nodal potential head and velocity values
C            =1 print only nodal potential head values
C            =2 print nodal and elemental potential head values
C            =3 print nodal potential head and velocity values
C            =4 print elemental potential head and velocity values
C   IPRT1  - flag for output of input and coordinate data 
C            in subroutines DATIN
C            =0 prints parameters only (default)
C            =1 prints parameters + b.c. + geom. char.
C            =2 prints parameters + b.c. + geom. char. + grid info
C            =3 prints parameters + b.c. + geom. char. + grid info,
C               X, Y, coordinate values and then stops
C            =4 prints system matrix at each time step
C   INDP   - =0 for uniform initial conditions (one value of potential
C            head read in); otherwise nonuniform IC's (one value of
C            potential head read in for each node)
C   HTIDIR - =0 for temporally variable Dir. BC's inputs; otherwise
C            Dir. BC's inputs are homogeneous in time
C            (see also notes following description of DIRINP)
C   HTINEU - =0 for temporally variable Neu. BC's inputs; otherwise
C            Neu. BC's inputs are homogeneous in time
C            (see also notes following description of NEUINP)
C   LUMP   - =0 for distributed mass matrix; otherwise matrix is lumped
C   IRAD   - =0 for 2D simulation 
C            =1 for radial 2D vertical simulation
C   ITMXCG - maximum # of iterations for conjugate gradient linear
C            system solvers
C   NITERT - number of iterations for the linear solver at each 
C            time step
C   ITLIN  - total number of iterations for the linear solver over all
C            time steps
C   KLSFAI - total # of linear solver failures 
C   IMAX   - largest integer number (machine dependent)
C
C Miscellaneous Integer Scalars
C -----------------------------
C   NDZ    - # of zero elements on the diagonal of the system matrices
C            (signals an error condition)
C
C Logical Flags
C -------------
C
C   LSFAIL - flag for linear solver
C             FALSE if linear solver did not fail
C             TRUE  if linear solver failed
C
C Real Scalars for Vertical 
C Discretization, Time Stepping, and Linear Iterations
C ----------------------------------------------------
C   TETAF  - weighting parameter for time stepping scheme 
C            (1.0 Backward Euler; 0.5 Crank-Nicolson;
C            TETAF is set to 1.0 for steady state problem)
C   DELTAT - initial and current time step size (DELTAT >= 1.0e+10
C            on input indicates steady state problem)
C   DTMAX  - maximum time step size allowed
C   DTMAGA - magnification factor for time step size (additive)
C   DTMAGM - magnification factor for time step size (multiplicative)
C   TMAX   - time at end of simulation (TMAX is set to 0.0 for 
C            steady state problem)
C   TIME   - time at current time level
C   TIMEP  - time at previous time level
C   TOLCG  - tolerance for convergence of conjugate gradient linear
C            system solvers
C
C Real Scalars for Mass Balance and Hydrograph Calculations
C ---------------------------------------------------------
C   NDIN   - tot inflow  flux from Dirichlet nodes at curr time level
C   NDOUT  - tot outflow flux from Dirichlet nodes at curr time level
C   NDINP  - tot inflow  flux from Dirichlet nodes at prev time level
C   NDOUTP - tot outflow flux from Dirichlet nodes at prev time level
C   NNIN   - tot inflow  flux from Neumann   nodes at curr time level
C   NNOUT  - tot outflow flux from Neumann   nodes at curr time level
C   NNINP  - tot inflow  flux from Neumann   nodes at prev time level
C   NNOUTP - tot outflow flux from Neumann   nodes at prev time level
C   VNDIN  - tot inflow  volu from Dirichlet nodes over curr time step
C   VNDOUT - tot outflow volu from Dirichlet nodes over curr time step
C   VNNIN  - tot inflow  volu from Neumann   nodes over curr time step
C   VNNOUT - tot outflow volu from Neumann   nodes over curr time step
C   VIN    - VNDIN  + VNNIN  = total inflow  volume 
C            between current and previous time levels (> 0)
C   VOUT   - VNDOUT + VNNOUT = total outflow
C            volume between current and previous time levels (< 0)
C   DSTORE - total volume of storage change between current and 
C            previous time levels (> 0 for net increase in storage)
C   ERRAS  - absolute volume ("mass") balance error over the current 
C            time step
C   ERREL  - relative (percent) mass balance error over the current 
C            time step
C   ETOT   - cumulative (over all time steps) absolute mass balance 
C            error ERRAS
C   VTOT   - cumulative (over all time steps) total net volume 
C            VIN + VOUT
C   VTOTIN - cumulative (over all time steps) total net incoming 
C            volume VIN
C   VTOTOU - cumulative (over all time steps) total net outgoing
C            volume VOUT
C 
C Miscellaneous Real Scalars
C --------------------------  
C   RMAX   - largest double precision number (machine dependent)
C   RMIN   - smallest double precision number (machine dependent)
C
C Real*4 Scalars for Cpu Timing
C -----------------------------
C   CPULIN - total cpu time for assembly and solution of linear
C            system
C   CPUBAL - total cpu time for back-calculation of fluxes at 
C            Dirichlet nodes, for mass balance calculation,
C            and for hydrograph calculation
C   CPUOVH - total cpu time for overhead:
C                      - data input, initialization, and output of
C                        of initial conditions (once)
C                      - construction of tetrahedral elements (once)
C                      - volume calculations (once)
C                      - set up of storage indices and pointers (once)
C                      - velocity calculations (every time step for the
C                        case IPRT > 1)
C                      - final output (once)
C   CPUMN  - total cpu time for the simulation 
C
C Real*4 Array for Cpu Timing
C ---------------------------
C   CPUVEC(6)          - breakdown of cpu times for assembly and 
C                        solution of linear system:
C                        (1) initialization of system matrices
C                        (2) assembly of local system components into
C                            global matrices
C                        (3) calculation of RHS without boundary cond.
C                        (4) construction of global LHS system matrix
C                        (5) calculation of BC contributions to RHS
C                        (6) linear solver and calculation of residual
C 
C Integer Arrays
C --------------
C   TRIANG(4,NT)       - element connectivities in 2-d mesh (TRIANG(4,I)
C                        indicates material type for 2-d element I)
C   TP    (N)          - # of elements connecting to each node
C   IAREA (NT)         - sign of the area of each element
C   CONTP (NP)         - Dirichlet node #'s in 2-d mesh
C   CONTQ (NQ)         - Neumann node #'s in 2-d mesh
C   CONTR (NR)         - node #'s for partial output
C   JA    (N1*N)       - column indices (in ascending order) in storage
C                        of system matrices
C   TOPOL (N+1)        - pointer to first nonzero element of each row
C                        which is stored in the system matrices (the
C                        diagonal entry in symmetric storage case)
C   TRIJA (3,3,NT)     - gives the index within JA (global position) of
C                        each component of the 3 x 3 local system 
C                        matrices (upper triangle of 3 x 3 arrays only
C                        for symmetric case)
C   IP3   (3,3)        - 3 x 3 permutation matrix
C   IER   (7)          - error flags 
C
C Mesh Configuration and Boundary Conditions (Real Arrays)
C --------------------------------------------------------
C   X     (N)          - x-coordinates (for 2-d mesh on input)
C   Y     (N)          - y-coordinates (for 2-d mesh on input)
C   XC    (N)          - x-coordinates of the element centroid
C   YC    (N)          - y-coordinates of the element centroid 
C   ARENOD(N)          - absolute value of area assigned to each node
C   AREA  (NT)         - absolute value of the area of each element
C   AREAR (NT)         - reciprocal of AREA
C   PRESC (NP)         - Dirichlet values at current time
C   DIRTIM(2)          - most current input time values for Dirichlet
C                        b.c. with DIRTIM(1) < TIME <= DIRTIM(2)
C   DIRINP(2,NP)       - Dirichlet values corresponding to DIRTIM 
C                        values. PRESC(I) is obtained by linear 
C                        interpolation
C   NEUTIM(2)          - most current input time values for Neumann
C                        b.c. with NEUTIM(1) < TIME <= NEUTIM(2)
C   NEUINP(2,NQ)       - Neumann values corresponding to NEUTIM 
C                        values. Q(I) is obtained by linear 
C                        interpolation
C   Notes: (a) For a simulation using temporally homogeneous Dirichlet
C              (Neumann) Bc's, input data on unit 9 (10 should 
C              contain a single value of DIRTIM (NEUTIM) (0.0)
C              and a single set of DIRINP (NEUINP) data.
C              Alternatively, to properly handle the case where the
C              datasets for different simulations are kept in the same
C              file (separated by blank lines), the input data on unit
C              9 (10) for temporally homogeneous Dirichlet (Neumann) 
C              BC's should contain, as above, a value of DIRTIM 
C              (NEUTIM) of 0.0 followed by the DIRINP (NEUINP)
C              values, and then a value of DIRTIM (NEUTIM) equal
C              to or larger than TMAX (1.0e+10, say) followed by the
C              same DIRINP (NEUMIN) values specified at time 0.0.
C          (c) If the first input time value is greater than 0.0, we set
C              the initial (time 0.0) Dir. (Neu.) BC's inputs to 0.0.
C          (d) If TIME is larger than the last DIRTIM (NEUTIM) value on
C              unit 9 (10), HTIDIR (HTINEU) is set to 1 and the last 
C              input values are used for the rest of the simulation.
C   Q     (NQ)         - Neumann values (constant in time for TIME > 0;
C                        for time 0 Neumann flux values are taken to be
C                        zero. This is done to avoid oscillations in the
C                        Crank Nicolson scheme.)
C   QPNEW (NP)         - back-calculated flux values at Dirichlet nodes
C                        at current time level
C   QPOLD (NP)         - QPNEW values at previous time level
C
C Material Properties (Real Arrays)
C ---------------------------------
C   PERMX (NZONE)      - saturated hydraulic conductivity-xx
C   PERMY (NZONE)      - saturated hydraulic conductivity-yy
C   ELSTOR(NZONE)      - specific storage
C   SPESS(NTRI)        - layer thickness defined on each triangle
C   SNODI (N)          - specific storage at each node
C 
C Potential Heads, Velocities, and Outputs (Real Arrays)
C ------------------------------------------------------
C   PNEW  (N)          - potential heads at current time level
C   PTIMEP(N)          - potential heads at previous time level (initial
C                        conditions on input)
C   PEL   (NT)         - potential heads at current time level
C                        for each element
C   UNOD  (N)          - velocity-x at each node
C   VNOD  (N)          - velocity-y at each node
C   UU    (NT)         - velocity-x for each element
C   VV    (NT)         - velocity-y for each element
C   TIMPRT(NPRT)       - time values for detailed output. Detailed 
C                        output is produced at initial conditions 
C                        (TIME=0), at time values indicated in TIMPRT,
C                        and at the end of the simulation (TIME=TMAX).
C                        Detailed output consists of: values of 
C                        potential head and velocity (depending on 
C                        setting of IPRT) at all nodes; vertical 
C                        profiles of potential head for the
C                        NODVP surface nodes; potential head 
C                        values at the surface nodes. 
C
C System Components (Real Arrays)
C -------------------------------
C   BI    (3,NT)       - coefficients 'b-i / 2' of the basis functions
C   CI    (3,NT)       - coefficients 'c-i / 2' of the basis functions
C   LMASS (3,3)        - local mass matrix, without the specific
C                        storage term and without the volume term
C   COEF1 (NTERM)      - global stiffness matrix; also used to store
C                        the LHS system matrix
C   COEF2 (NTERM)      - global mass matrix
C   SCR   (NTERM)      - scratch vector
C   TNOTI (N)          - RHS system vector
C   XT5   (N)          - TNOTI before imposition of Dirichlet boundary
C                        conditions (needed for back-calculation of 
C                        fluxes used in mass balance calculations)
C   LHSP  (NP)         - values of diagonal elements of LHS system 
C                        matrix corresponding to Dirichlet nodes before 
C                        imposition of Dirichlet BC's (needed for 
C                        back-calculation of fluxes used in mass 
C                        balance calculations)
C
C Input Units
C -----------
C   1                  - I/O file names
C   5                  - parameters
C   8                  - grid info
C   9                  - Dirichlet BC's
C   10                 - Neumann BC's
C   11                 - material properties
C   12                 - initial conditions
C   13                 - elemental thickness
C
C Output Units
C ------------
C   4                  - terminal
C   6                  - main output
C   15                 - X, Y coordinate values
C   21                 - mass balance behavior at each time step
C   36                 - hydrograph output
C   40                 - potential head output at all nodes
C   41                 - velocity output at all nodes
C   42                 - potential head output at all elements
C   51                 - velocity output at all elements
C   52                 - system matrix output
C
C----------------------------------------DATA AND PARAMETER DECLARATIONS
C




---------- LETTURA DATI


C
C
C  unit 5 input 
C
      READ(5,*) IPRT1
      READ(5,*) TETAF,LUMP,IRAD
      READ(5,*) ITMXCG,TOLCG
      READ(5,*) DELTAT,DTMAX,TMAX
      READ(5,*) DTMAGA,DTMAGM
      READ(5,*) IPRT,NPRT,(TIMPRT(I),I=1,NPRT)
      READ(5,*) NR
      IF (NR .NE. 0) READ(5,*)(CONTR(I),I=1,NR)
      WRITE(6,1010) IPRT1,IPRT,NPRT,NR
      WRITE(6,1000) TETAF,LUMP,IRAD
      WRITE(6,1005) ITMXCG,TOLCG
      WRITE(6,1030) DELTAT,DTMAX,TMAX
      WRITE(6,1035) DTMAGA,DTMAGM
C
C  unit 8 input
C
      READ(8,*) NZONE,N1
      READ(8,*) N,NT
      WRITE(6,1020) N,NT,NZONE,N1
      READ(8,*)((TRIANG(I,K),I=1,4),K=1,NT)
      READ(8,*)(X(K),Y(K),K=1,N)
      IF (IPRT1 .EQ. 3) THEN
         DO I=1,N
            WRITE(15,1040) I,X(I),Y(I)
         END DO
         DO I=1,NT
            WRITE(15,1050) I,(TRIANG(J,I),J=1,4)
         END DO
         WRITE(6,1060) 
         WRITE(4,1060) 
         CALL CLOSIO(ITERM)
         STOP
      END IF
C
C  unit 12 input
C
      READ(12,*) INDP,ISTAP
      IF (INDP .EQ. 0) THEN
         READ(12,*) PTIMEP(1)
         IF (IPRT1 .GE. 2) WRITE(6,1070) PTIMEP(1)
         DO K=2,N
            PTIMEP(K)=PTIMEP(1)
         END DO
      ELSE
         READ(12,*)(PTIMEP(K),K=1,N)
      END IF
      IF (INDP .NE. 0 .AND. ISTAP .NE. 0 .AND. IPRT1 .GE. 1)
     1                WRITE(6,1090)(K,PTIMEP(K),K=1,N)
C
C  unit 11 input: material properties
C
      WRITE(6,1100)
      DO J=1,NZONE
         READ(11,*) PERMX(J),PERMY(J),ELSTOR(J)
         IF(IPRT1.GE.2) WRITE(6,1110) J,PERMX(J), PERMY(J),
     1                         ELSTOR(J)
      END DO
C
C  unit 9 input: Dirichlet B.C.'s
C
      READ(9,*) NP
      WRITE(6,1120) NP
      IF (NP .NE. 0) THEN
         READ(9,*) (CONTP(I),I=1,NP)
         IF (IPRT1 .GE. 1) THEN
            WRITE(6,1130)
            WRITE(6,1140) (CONTP(I),I=1,NP)
         END IF
      END IF
C
C  unit 10 input: Neumann B.C.'s
C
      READ(10,*) NQ
      WRITE(6,1180) NQ
      IF (NQ .NE. 0) THEN
         READ(10,*) (CONTQ(I),I=1,NQ)
         IF (IPRT1 .GE. 1) THEN
            WRITE(6,1190)
            WRITE(6,1140) (CONTQ(I),I=1,NQ)
         END IF
      END IF
C
C  unit 12 input
C
      READ(13,*) INDS,ISTAP
      IF (INDS .EQ. 0) THEN
         READ(13,*) SPESS(1)
         IF (IPRT1 .GE. 2) WRITE(6,1200) SPESS(1)
         DO K=2,NT
            SPESS(K)=SPESS(1)
         END DO
      ELSE
         READ(13,*)(SPESS(K),K=1,NT)
      END IF
      IF (INDS .NE. 0 .AND. ISTAP .NE. 0 .AND. IPRT1 .GE. 1)
     1                WRITE(6,1210)(K,SPESS(K),K=1,NT)
C
      WRITE(6,1300) N,NT,NP
C
C  format statements
C
 1000 FORMAT(  5X,'TETAF  (EG: 1 BACKWARD EULER, 0.5 C-N)  = ',1PE15.5,
     1       /,5X,'LUMP   (MASS LUMPING IF NONZERO)        = ',I6,/,
     2       /,5X,'IRAD   (RADIAL FLOW IF NONZERO)         = ',I6)
 1005 FORMAT(  5X,'ITMXCG (MAX ITER FOR CG LINEAR SOLVERS) = ',I6,
     1       /,5X,'TOLCG  (TOLER. FOR CG LINEAR SOLVERS)   = ',1PE15.5)
 1010 FORMAT(  5X,'IPRT1  (FOR OUTPUT OF DATA)             = ',I6,
     1       /,5X,'  =0 prints parameters only (default)',
     2       /,5X,'  =1 prints par. + b.c. + geom. char.',
     3       /,5X,'  =2 prints par. + b.c. + geom. char.',
     4       /,5X,'     + grid info',
     5       /,5X,'  =3 prints par. + b.c. + geom. char.',
     6       /,5X,'     + grid info + X, Y, coordinate',
     7       /,5X,'     and then stops',
     8       /,5X,'IPRT   (FOR DETAILED NODAL OUTPUT)      = ',I6,
     9       /,5X,'  =0 don''t print nodal pot. head ',
     A       /,5X,'     and vel. values',
     B       /,5X,'  =1 print only nodal pot. heads',
     C       /,5X,'  =2 print nodal and el. pot. heads',
     D       /,5X,'  =3 print nodal pot. head and vel.',
     E       /,5X,'  =4 print el. pot. head and vel.',
     F       /,5X,'NPRT   (# OF TIME VALUES FOR DET OUTPUT)= ',I6,
     G       /,5X,'NR     (# OF NODES FOR PARTIAL OUTPUT)  = ',I6)
 1020 FORMAT(/,5X,'N      (NUM. NODI RETICOLO)             = ',I6,
     1       /,5X,'NT     (NUM. TRIANGOLI RETICOLO   )     = ',I6,
     2       /,5X,'NZONE  (NUMERO ZONE (MATERIAL TYPES))   = ',I6,
     3       /,5X,'N1     (NUM. MAX CONTATTI NODALI)       = ',I6)
 1030 FORMAT(/,5X,'DELTAT (INITIAL TIME STEP SIZE)         = ',1PE15.5,
     1       /,5X,'DTMAX  (MAXIMUM TIME STEP SIZE)         = ',1PE15.5,
     2       /,5X,'TMAX   (TIME AT END OF SIMULATION)      = ',1PE15.5)
 1035 FORMAT(  5X,'DTMAGA (MAG. FACTOR FOR DELTAT, ADD.)   = ',1PE15.5,
     1       /,5X,'DTMAGM (MAG. FACTOR FOR DELTAT, MULT.)  = ',1PE15.5)
 1040 FORMAT(I7,2(1PE15.6))
 1050 FORMAT(5I10)
 1060 FORMAT(//,1X,'  IPRT1=3: END OF RUN')
 1070 FORMAT(/,5X,'INITIAL POTENTIAL HEAD (UNIFORM)        = ',1PE15.5)
 1090 FORMAT(/,1X,' INITIAL POTENTIAL HEADS',/,(4(I6,2X,1PE11.3)))
 1100 FORMAT(//,3X,
     1'SATURATED HYDRAULIC CONDUCTIVITY, SPECIFIC STORAGE, AND ',
     2'THICKNESS VALUES',/,1X,
     3'  MAT.TYPE  X-PERM       Y-PERM       STORAGE      THICKNESS')
 1110 FORMAT(1X,I8,2X,4(1PE13.5))
 1120 FORMAT(/,5X,'NP (# OF DIRICHLET NODES)               = ',I6)  
 1130 FORMAT(/,5X,'DIRICHLET NODES')
 1140 FORMAT(1X,10I7)
 1180 FORMAT(/,5X,'NQ   (# OF NEUMANN NODES)               = ',I6)  
 1190 FORMAT(/,5X,'NEUMANN NODES')
 1200 FORMAT(/,5X,'LAYER THICKNESS (UNIFORM)               = ',1PE15.5)
 1210 FORMAT(/,1X,' LAYER THICKNESS',/,(4(I6,2X,1PE11.3)))
 1300 FORMAT(/,5X,'N     (# OF NODES)                      = ',I6,
     1       /,5X,'NT    (# OF TRIANGLES)                  = ',I6,
     2       /,5X,'NP    (TOTAL # OF DIRICHLET NODES)      = ',I6)