*DECK THMJLB
      SUBROUTINE THMJLB(NZ,HZ,V,P,PHI,TP,TL,HC,S,XFL,EPS)
*
*-----------------------------------------------------------------------
*
*Purpose:
* compute the void fraction and the steam flow quality distribution in
* channel using the Jens-Lottes correlation and Bowring's model
*
*Copyright:
* Copyright (C) 2014 Ecole Polytechnique de Montreal.
*
*Author(s): P. Gallet
*
*Parameters: input
* NZ      number of points in channel.
* HZ      Z-directed mesh widths.
* V       coolant velocity distribution in channel in kg/m^2/s.
* P       pressure distribution in channel in Pa.
* PHI     thermal flux distribution in channel in W/m^2.
* TP      clad temperature distribution channel in in Celsius.
* TL      coolant temperature distribution in channel in Celsius.
* HC      heat transfer coefficient distribution over the channel
*         in W/m^2/K.
* S       slip ratio of vapor phase speed to liquid phase speed.
*
*Parameters: output
* XFL     steam flow quality distribution in channel.
* EPS     void fraction distribution in channel.
*
*-----------------------------------------------------------------------
*
      IMPLICIT NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER NZ
      REAL HZ(NZ),V(NZ),P(NZ),PHI(NZ),TP(NZ),TL(NZ),HC(NZ),S(NZ),
     > XFL(NZ),EPS(NZ)
*----
*  LOCAL VARIABLES
*----
      INTEGER I1,I2,I,J,K
      REAL TSAT,DSAT(NZ),THETAD,ETA,THETA(NZ),THETASP,HLV,PHISP(NZ),
     > RL,HL,RV,HV,CP,PI(NZ),R1,R2,R3,R4,F1,F2,F3,DTSAT(NZ),TB
*
*     calculate the value of the overheat with the Jens and Lottes
*     correlation over the channel
      DO I=1,NZ
         IF(PHI(I).EQ.0.0) CYCLE
         CALL THMSAT(P(I),TSAT)
         DTSAT(I)=TP(I)-TSAT
         IF(PHI(I).GE.1.0E3) THEN
            DSAT(I)=0.7906*PHI(I)**0.25/EXP(P(I)*1.613E-7)
         ELSE
            DSAT(I)=0.0
         ENDIF
      ENDDO
      I1=0
      DO 10 I=1,NZ
         IF(PHI(I).EQ.0.0) CYCLE
         CALL THMSAT(P(I),TSAT)     
*        check if subcool boiling has occurred using Jens & Lottes
*        formula and calculate the temperature difference
         IF(DTSAT(I).GE.DSAT(I)) THEN
           I1=I
           GO TO 20
         ELSE
           EPS(I)=0.0
           XFL(I)=0.0
           PHISP(I)=0.0
           PI(I)=0.0
           THETA(I)=TSAT-TL(I)
         ENDIF
   10 CONTINUE
      GO TO 60
*     compute the values of steam flow quality and
*     void fraction along the channel
   20 I2=0
      DO 30 J=I1,NZ
         IF(PHI(J).EQ.0.0) CYCLE
*        compute the temperature difference between coolant
*        and saturation temperature over the channel
         CALL THMSAT(P(J),TSAT)
         THETA(J)=TSAT-TL(J)
         ETA=14.0+0.1*P(J)/(1.01325E+05)
         THETAD=ETA*PHI(J)/V(J)*1.0E-6
         THETASP=0.7*PHI(J)/HC(J)-DSAT(J)*0.7**(0.25)
         IF(THETA(J).LE.THETAD) THEN
            I2=J
            EPS(J)=0.0
            XFL(J)=0.0
*           compute the value of heat flux between patches of bubbles
            IF(THETA(J).GT.THETASP) THEN
               PHISP(J)=HC(J)*THETA(J)
            ELSE
               PHISP(J)=0.0
            ENDIF
*           compute the value of the enthalpy of vaporisation
            CALL THMTX(TSAT,0.0,RL,HL)
            CALL THMTX(TSAT,1.0,RV,HV)
            HLV=HV-HL
*           compute the value of the steam flow quality
            TB=TSAT-0.1
            IF(TL(J).LT.TB) THEN
               CALL THMPT(P(J),TL(J),R1,R2,R3,R4,CP)
            ELSE
               CALL THMPT(P(J),TB,R1,R2,R3,R4,CP)
            ENDIF
            PI(J)=RL*CP/(RV*HLV)
            IF(J.EQ.NZ) THEN
              GO TO 60
            ELSE
              GO TO 40
            ENDIF
         ELSE
            EPS(J)=0.0
            XFL(J)=0.0
            IF(J.EQ.NZ) THEN
              GO TO 60
            ELSE
              GO TO 30
            ENDIF
         ENDIF
   30 CONTINUE
*   
   40 DO K=I2+1,NZ
         IF(PHI(K).EQ.0.0) CYCLE
*        compute the value of heat flux between patches of bubbles
         CALL THMSAT(P(K),TSAT)
         THETA(K)=TSAT-TL(K)
         THETASP=0.7*PHI(K)/HC(K)-DSAT(K)*(0.7)**(0.25)
         IF(THETA(K).GT.THETASP) THEN
           PHISP(K)=HC(K)*THETA(K)
         ELSE 
           PHISP(K)=0.0
         ENDIF
*        compute the value of the enthalpy of vaporisation
         CALL THMTX(TSAT,0.0,RL,HL)
         CALL THMTX(TSAT,1.0,RV,HV)
         HLV=HV-HL
*        compute the value of the steam flow quality
         TB=TSAT-0.1
         IF(TL(K).LT.TB) THEN
           CALL THMPT(P(K),TL(K),R1,R2,R3,R4,CP)
         ELSE
           CALL THMPT(P(K),TB,R1,R2,R3,R4,CP)
         ENDIF
         PI(K)=RL*CP/(RV*HLV)
         F1=(PHI(K-1)-PHISP(K-1))/(1.0+PI(K-1))*HZ(K-1)/2.0
         F2=(PHI(K)-PHISP(K))/(1.0+PI(K))*HZ(K)/2.0
         XFL(K)=XFL(K-1)+(F1+F2)/(RL*HZ(K)*V(K)*HLV)
*        compute the value of the void fraction
         F3=S(K)*RV*(1.0-XFL(K))/(RL*XFL(K))
         EPS(K)=1.0/(1.0+F3)
      ENDDO
   60 RETURN
      END
