*DECK THMH2O
      SUBROUTINE THMH2O(PINLET,MFLOW,ENT,PHI,HD,TL,RHO,HCONV,TG)
*
*-----------------------------------------------------------------------
*
*Purpose:
* solution of the convection equation along a single channel.
*
*Copyright:
* Copyright (C) 2012 Ecole Polytechnique de Montreal.
*
*Author(s): A. Hebert
*
*Parameters: input
* PINLET  pressure in Pascal
* MFLOW   massic coolant flow rate in Kg/m^2/s
* ENT     four values of enthalpy in J/Kg to be used in Gaussian
*         integration
* PHI     heat flow eschanged between clad and fluid in W/m^2
* HD      hydraulic diameter in m
*
*Parameters: output
* TL      fluid temperature in K
* RHO     fluid density in Kg/m^3
* HCONV   heat transfer coefficient between clad and fluid in
*         W/m^2/K
* TG      clad temperature in K
*
*-----------------------------------------------------------------------
*
      IMPLICIT NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      REAL PINLET,MFLOW,ENT(4),TL,PHI,RHO,HCONV,HD,TG
*----
*  LOCAL VARIABLES
*----
      REAL HGSAT,XMULS,W(4),CPL,XLL,TL1,TL2,TL3,TL4,RHOL1,RHOL2,RHOL3,
     > RHOL4,HL(4),DELTH,REL,PRL,X,HLAVG,HLSAT,R1,R2,R3,R4,R5,R11,TSAT,
     > RHOLSAT,RHOG,RHOL,HMAVG
*
      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 THMPT(PINLET,TSAT,RHOLSAT,HLSAT,R3,R4,R5)
      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)
      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)
      CALL THMPT(PINLET,TL,R1,R2,XLL,XMULS,CPL)
*----
*  COMPUTE THE FLUID DENSITY
*----
      IF(ABS(HMAVG-HLAVG).LE.DELTH) THEN
*        One phase liquid
         RHO=RHOL
      ELSE IF((HMAVG-HGSAT).LE.DELTH) THEN
*        Two-phase flow
         X=(HMAVG-HLAVG)/(HGSAT-HLAVG)
         RHO=1.0/(X/RHOG+(1.0-X)/RHOL)
      ELSE
*        Saturated steam
         RHO=RHOG
      ENDIF
*----
*  THERMAL EXCHANGE BETWEEN CLAD AND FLUID USING THE DITTUS AND BOELTER
*  CORRELATION
*  REL: Reynolds number of liquid phase
*  PRL: Prandtl number of liquid phase
*----
      REL=MFLOW*HD/XMULS
      PRL=XMULS*CPL/XLL
      HCONV=0.023*XLL/HD*REL**0.8*PRL**0.4
      TG=TL+PHI/HCONV
      RETURN
      END
