*DECK THMDRV
      SUBROUTINE  THMDRV(IMPX,IC,JC,KC,XBURN,HX,HY,HZ,TIME,CFLUX,POROS,
     > NCRACT,NFD,SASS,FCOOL,FFUEL,ACOOL,HD,PCH,RGG,RIG,RC,MAXIT1,
     > MAXITL,ERMAX,SPEED,TINLET,PINLET,FRACPU,POW,TCOMB,DCOOL,TCOOL,
     > TSURF)
*
*-----------------------------------------------------------------------
*
*Purpose:
* driver of the thermal-hydraulics module.
*
*Copyright:
* Copyright (C) 2012 Ecole Polytechnique de Montreal.
*
*Author(s): A. Hebert
*
*Parameters: input
* IMPX    printing index (=0 for no print).
* IC      number of meshes along X direction.
* JC      number of meshes along Y direction.
* KC      number of meshes along Z direction (channel direction).
* XBURN   burnup distribution in MWday/tonne.
* HX      X-directed mesh widths.
* HY      Y-directed mesh widths.
* HZ      Z-directed mesh widths.
* TIME    time in s.
* CFLUX   critical heat flux in W/m^2.
* POROS   oxyde porosity.
* NCRACT  number of active fuel rods.
* NFD     number of discretisation points in fuel.
* SASS    assembly surface in m^2.
* FCOOL   power density fraction in coolant.
* FFUEL   power density fraction in fuel.
* ACOOL   assembly coolant cross section in m^2.
* HD      hydraulic diameter of one assembly in m.
* PCH     heating perimeter in m.
* RGG     external clad rod radius in m.
* RIG     internal clad rod radius in m.
* RC      fuel pellet radius in m.
* MAXIT1  maximum number of conduction iterations
* MAXITL  maximum number of center-pellet iterations
* ERMAX   convergence criterion
* SPEED   inlet flow velocity in m/s
* TINLET  inlet temperature in K.
* PINLET  inlet pressure in Pa.
* FRACPU  Plutonium fraction in fuel.
* POW     power distribution in W.
*
*Parameters: output
* TCOMB   averaged fuel temperature distribution in K.
* DCOOL   coolant density distribution in g/cc.
* TCOOL   coolant temperature distribution in K.
* TSURF   surface fuel temperature distribution in K.
*
*-----------------------------------------------------------------------
*
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER IMPX,IC,JC,KC,NCRACT,NFD,MAXIT1,MAXITL
      REAL XBURN(IC,JC,KC),HX(IC),HY(JC),HZ(KC),TIME,CFLUX,POROS,SASS,
     > FCOOL,FFUEL,ACOOL,HD,PCH,RGG,RIG,RC,ERMAX,SPEED,TINLET,PINLET,
     > FRACPU,POW(IC,JC,KC),TCOMB(IC,JC,KC),DCOOL(IC,JC,KC),
     > TCOOL(IC,JC,KC),TSURF(IC,JC,KC)
*----
*  LOCAL VARIABLES
*----
      PARAMETER (KMAXO=100,MAXNPO=15)
      PARAMETER(ZMCM=1.0E2,ZKILO=1.0E3)
      REAL TRE11(MAXNPO),ENT(4),CONDXA(MAXNPO),FRO(MAXNPO),MFLOW
      CHARACTER HSMG*131
      REAL XS(4)
      DATA XS/-0.861136,-0.339981,0.339981,0.861136/
*----
*  LOOP OVER REACTOR CHANNELS
*----
      IF(NFD.GT.MAXNPO) CALL XABORT('THMDRV: MAXNPO OVERFLOW.')
      DO 100 I=1,IC
      DO 100 J=1,JC
      IF(POW(I,J,(KC+1)/2).GT.0.0) THEN
*----
*  COMPUTE THE INLET FLOW ENTHALPY AND VELOCITY
*----
         CALL THMSAT(PINLET,TSAT)
         IF(TINLET.GT.TSAT) THEN
            WRITE(HSMG,'(27HTHMDRV: INLET TEMPERATURE (,1P,E12.4,
     1      41H K) GREATHER THAN SATURATION TEMPERATURE.)') TINLET
            CALL XABORT(HSMG)
         ENDIF
         CALL THMPT(PINLET,TINLET,RHOIN,HINLET,R3,R4,R5)
         MFLOW=SPEED*RHOIN
         HMSUP=HINLET
*----
*  MAIN LOOP ALONG THE 1D CHANNEL.
*----
         DO K=1,KC
*----
*  INITIALIZATION OF PINCELL TEMPERATURES
*----
           DO L=1,NFD
              TRE11(L)=TCOMB(I,J,K)
           ENDDO
*----
*  COMPUTE THE LINEAR POWER, THE VOLUMIC POWER AND THE THERMAL EXCHANGE
*  COEFFICIENT OF THE GAP
*----
           DV=HX(I)*HY(J)*HZ(K)
*          linear power in W/m
           POWLIN=(POW(I,J,K)/DV)*SASS/NCRACT
*          volumic power in W/cm^3
           QFUEL=POW(I,J,K)*FFUEL/DV
           QCOOL=POW(I,J,K)*FCOOL/DV
*          thermal exchange coefficient of the gap in W/m^2/K.
           CALL THMGAP(POWLIN,XBURN(I,J,K),HGAPM)
*----
*  COMPUTE THE POWER DENSITY AND HEAT FLOW ALONG THE CHANNEL
*----
*          out-of-clad heat flow in W/m2
           PHI2=0.5*QFUEL*RC*RC/RGG
           IF(PHI2.GT.CFLUX) THEN
              WRITE(HSMG,'(23HTHMDRV: THE HEAT FLUX (,1P,E12.4,5H) IS ,
     >        37HGREATER THAN THE CRITICAL HEAT FLUX (,E12.4,2H).)')
     >        PHI2,CFLUX
              CALL XABORT(HSMG)
           ENDIF
*----
*  COMPUTE FOUR VALUES OF ENTHALPY IN J/KG TO BE USED IN GAUSSIAN
*  INTEGRATION. DELTH1 IS THE ENTHALPY INCREASE IN EACH AXIAL MESH.
*----
           DELTH1=(PCH/ACOOL*PHI2+QCOOL)*HZ(K)/MFLOW
           DO I1=1,4
              POINT=(1.0+XS(I1))/2.0
              ENT(I1)=HMSUP+POINT*DELTH1
           ENDDO
           HMSUP=HMSUP+DELTH1
*----
*  STEADY-STATE SOLUTION OF THE CONVECTION EQUATIONS ALONG THE 1D
*  CHANNEL.
*----
           CALL THMH2O(PINLET,MFLOW,ENT,PHI2,HD,TL,RHO,HCONV,TRE11(NFD))
*----
*  STEADY-STATE SOLUTION OF THE CONDUCTION EQUATIONS IN A FUEL PIN.
*----
           DO K1=1,NFD-2
              FRO(K1)=REAL(K1)
              CONDXA(K1)=0.0
           ENDDO
           TIME=0.0
           RO00=0.0
           DTINV=0.0
           ALPHA=1.0
           CALL THMRES(NFD,MAXIT1,MAXITL,ERMAX,ALPHA,DTINV,RC,RIG,
     >     RGG,HCONV,HGAPM,RO00,TL,CONDXA,FRO,QFUEL,XBURN(I,J,K),
     >     POROS,FRACPU,TRE11,TCENT)
*----
*  RECOVER MESHWISE TEMPERATURES AND FLUID DENSITY. USE THE ROWLANDS
*  FORMULA TO COMPUTE THE EFFECTIVE FUEL TEMPERATURE.
*----
           TCOMB(I,J,K)=(4.0*TCENT+5.0*TRE11(NFD-1))/9.0
           TSURF(I,J,K)=TRE11(NFD-1)
           DCOOL(I,J,K)=RHO/ZKILO
           TCOOL(I,J,K)=TL
         ENDDO
         IF(IMPX.GT.2) THEN
           WRITE(6,'(/16H THMDRV: CHANNEL,2I6/1X,27(1H-))') I,J
           WRITE(6,200) 'TCOMB',(TCOMB(I,J,K),K=1,KC)
           WRITE(6,200) 'TSURF',(TSURF(I,J,K),K=1,KC)
           WRITE(6,200) 'DCOOL',(DCOOL(I,J,K),K=1,KC)
           WRITE(6,200) 'TCOOL',(TCOOL(I,J,K),K=1,KC)
         ENDIF
      ENDIF
  100 CONTINUE
      RETURN
*
  200 FORMAT(/1X,A5,1P,10E12.4/(6X,10E12.4))
      END
