*DECK THMH2O
      SUBROUTINE THMH2O(PINLET,MFLOW,EPS,ENT,PHI,HD,IHCONV,KHCONV,TL,
     > RHO,RHOL,HCONV,TG,X)
*
*-----------------------------------------------------------------------
*
*Purpose:
* solution of the convection equation along a single channel.
*
*Copyright:
* Copyright (C) 2012 Ecole Polytechnique de Montreal.
*
*Author(s): A. Hebert, P. Gallet
*
*Parameters: input
* PINLET  pressure in Pascal
* MFLOW   massic coolant flow rate in Kg/m^2/s
* EPS     coolant void fraction
* ENT     four values of enthalpy in J/Kg to be used in Gaussian
*         integration
* PHI     heat flow exchanged between clad and fluid in W/m^2
* HD      hydraulic diameter in m
* IHCONV  flag indicating HCONV chosen (0=default/1=user-provided).
* KHCONV  fixed user-provided HCONV value in W/m^2/K.
*
*Parameters: output
* TL      fluid temperature in K
* RHO     fluid density in Kg/m^3
* RHOL    liquid density in kg/m^3
* HCONV   heat transfer coefficient between clad and fluid in W/m^2/K
* TG      clad temperature in K
* X       steam thermodynamic quality
*
*-----------------------------------------------------------------------
*
      IMPLICIT NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER IHCONV
      REAL PINLET,MFLOW,EPS,ENT(4),TL,PHI,RHO,RHOL,HCONV,HD,KHCONV,TG,X
*----
*  LOCAL VARIABLES
*----
      REAL HGSAT,XMULS,W(4),CPL,XLL,TL1,TL2,TL3,TL4,RHOL1,RHOL2,RHOL3,
     > RHOL4,HL(4),DELTH,REL,PRL,HLAVG,HLSAT,R1,R2,R3,R4,R5,R11,TSAT,
     > RHOLSAT,RHOG,HMAVG,TB
*
      DATA W /0.347855,0.652145,0.652145,0.347855/
      DATA DELTH /1.0/
*----
*  COMPUTE THE DENSITY OF THE SATURATED STEAM
*----
      CALL THMSAT(PINLET,TSAT)
      CALL THMTX(TSAT,0.0,RHOLSAT,HLSAT)
      CALL THMTX(TSAT,1.0,RHOG,HGSAT)
*----
*  COMPUTE THE DENSITY AND TEMPERATURE OF THE LIQUID
*----
      HL(1)=MIN1(ENT(1),HLSAT)
      HL(2)=MIN1(ENT(2),HLSAT)
      HL(3)=MIN1(ENT(3),HLSAT)
      HL(4)=MIN1(ENT(4),HLSAT)
      HLAVG=0.5*(HL(2)+HL(3))
      HMAVG=0.5*(ENT(2)+ENT(3))
      CALL THMPH(PINLET,HL(1),R11,TL1)
      CALL THMPH(PINLET,HL(2),R11,TL2)
      CALL THMPH(PINLET,HL(3),R11,TL3)
      CALL THMPH(PINLET,HL(4),R11,TL4)
      CALL THMPT(PINLET,TL1,RHOL1,R2,R3,R4,R5)
      CALL THMPT(PINLET,TL2,RHOL2,R2,R3,R4,R5)
      CALL THMPT(PINLET,TL3,RHOL3,R2,R3,R4,R5)
      CALL THMPT(PINLET,TL4,RHOL4,R2,R3,R4,R5)
      IF(ABS(TSAT-TL1).LT.0.1) CALL THMTX(TSAT,0.0,RHOL1,R2)
      IF(ABS(TSAT-TL2).LT.0.1) CALL THMTX(TSAT,0.0,RHOL2,R2)
      IF(ABS(TSAT-TL3).LT.0.1) CALL THMTX(TSAT,0.0,RHOL3,R2)
      IF(ABS(TSAT-TL4).LT.0.1) CALL THMTX(TSAT,0.0,RHOL4,R2)
      TL=0.5*(W(1)*TL1+W(2)*TL2+W(3)*TL3+W(4)*TL4)
      RHOL=0.5*(W(1)*RHOL1+W(2)*RHOL2+W(3)*RHOL3+W(4)*RHOL4)
      TB=TSAT-0.1
      IF(TL.LT.TB) THEN
         CALL THMPT(PINLET,TL,R1,R2,XLL,XMULS,CPL)
      ELSE
         CALL THMPT(PINLET,TB,R1,R2,XLL,XMULS,CPL)
      ENDIF
*----
*  COMPUTE THE FLUID DENSITY
*----
      IF(ABS(HMAVG-HLAVG).LE.DELTH) THEN
*        One phase liquid
         X=0.0
         IF(EPS.EQ.0.0) THEN
           RHO=RHOL
         ELSE
           RHO=EPS*RHOG+(1.0-EPS)*RHOLSAT
         ENDIF
      ELSE IF((HMAVG-HGSAT).LE.DELTH) THEN
*        Two-phase flow
         X=(HMAVG-HLSAT)/(HGSAT-HLSAT)
         IF(EPS.EQ.0.0) THEN
           RHO=1.0/(X/RHOG+(1.0-X)/RHOL)
         ELSE
           RHO=EPS*RHOG+(1.0-EPS)*RHOLSAT
         ENDIF
      ELSE
*        Saturated steam
         X=1.0
         RHO=RHOG
      ENDIF
*
      IF(IHCONV.EQ.0) THEN
*----
*  THERMAL EXCHANGE BETWEEN CLAD AND FLUID USING THE DITTUS AND BOELTER
*  CORRELATION
*  REL: Reynolds number of liquid phase
*  PRL: Prandtl number of liquid phase
*----
         IF(X.GE.0.0) THEN
            REL=MFLOW*(1.0-X)*HD/XMULS
         ELSE
            REL=MFLOW*HD/XMULS
         ENDIF
         PRL=XMULS*CPL/XLL
         HCONV=0.023*XLL/HD*REL**0.8*PRL**0.4
      ELSE IF(IHCONV.EQ.1) THEN
         HCONV=KHCONV
      ENDIF
      TG=TL+PHI/HCONV
      RETURN
      END
