! -----------------------------------------------------------------------------
! Milk as a mixture of conventional components:
! mixture H2O (Aspen Plus) + FAT + PROTEIN + LACTOSE (Aspen Plus) + ASH
!   - Liquid thermal conductivity
! -----------------------------------------------------------------------------
C
      SUBROUTINE KL2U (T, P, Z, N, IDX, IRW, IIW, KCALC, KOP,
     &     NDS, KDIAG, QMX, DQMX, KER)
C
        IMPLICIT NONE
        INTEGER N, DMS_KCCIDC, IMW, IMF,IMP,IMC,IMA
        INTEGER IDX(N), IOFF, DMS_IFCMNC
        INTEGER LMWW, LMWF, LMWP,LMWC, LMWA
        INTEGER IRW, IIW, KCALC, KOP(10), NDS, KDIAG, KER     
        INTEGER I, II, IIMW,IIMF, IIMP, IIMC, IIMA, J
        INTEGER NBOPST(6), NAME(2), KV, KH, KS, KG, KBASE
        INTEGER KPHI
        REAL*8 Z(N), P, FC, X, WC, T, TN, QMX, DQMX, B(1),
     &       MWW, MWF, MWP, MWC, MWA,
     &       KLDIP, KWATER, KLMX, CONCFACT, TS,
     &       MASSW, MASSF, MASSP, MASSC, MASSA, MASSTOT,
     &       FRACW, FRACF, FRACP, FRACC, FRACA,
     &       LAMBDAW,LAMBDAF,LAMBDAP,LAMBDAC,LAMBDAA,
     &       RHOW,RHOF,RHOP,RHOC,RHOA,RHOTOT,
     &       VOLW,VOLF,VOLP, VOLC,VOLA
        EQUIVALENCE (B(1), IB(1))
        INTEGER IER
        REAL*8 PC, CC, AC
C       
#include "dms_ncomp.cmn"
#include "ppexec_user.cmn"
#include "dms_ppwork.cmn"
#include "dms_ipwork.cmn"
#include "dms_plex.cmn"
#include "dms_maxwrt.cmn"
C       
!       Statement functions
        KLDIP(I,J) = DMS_IFCMNC('KLDIP') + 11*(J -1) + I
!       Retrieving components index:
        CALL COMPO(N, IDX, Z, IMW, IMF, IMP, IMC, IMA,
     *       IIMW, IIMF, IIMP, IIMC, IIMA, 
     *       WC, FC, PC, CC, AC,
     *       MWW, MWF, MWP, MWC, MWA, IER)
        IF(IER.NE.0) RETURN

!       If WC = 1 then do not use other components
        IF (WC.EQ.1) THEN
!         Retrieve thermal conductivity of water
 13       KWATER = B(KLDIP(1,IMW)) + B(KLDIP(2,IMW))*T
     &         + B(KLDIP(3,IMW))*T**2 + B(KLDIP(4,IMW))*T**3
          KLMX =  KWATER
        ELSE
!         Conversion to mass fraction:
!         4) Calculate mass of components in 1 mole
          MASSW = MWW*WC
          MASSF = MWF*FC
          MASSP = MWP*PC
          MASSC = MWC*CC
          MASSA = MWA*AC
!         MASSTOT = Total mass of 1 mole of milk mixture
          MASSTOT = MASSW+MASSF+MASSP+MASSC+MASSA
!         5) Convert molar fraction to mass fraction
          FRACW = MASSW / MASSTOT
          FRACF = MASSF / MASSTOT
          FRACP = MASSP / MASSTOT
          FRACC = MASSC / MASSTOT
          FRACA = MASSA / MASSTOT
!         Temperature in °C
          TN = T -273.15
!         6) Thermal conductivity of the mixture (W.m^-1.K^-1)
          IF (KOP(1).EQ.0) THEN
!           Use model from Cuevas and Cheryan (1978)
            CONCFACT = (1 - FRACW)/0.1
            TS = (10 + 2*FRACF)*CONCFACT
            KLMX = (0.565 +0.0018 *TN -0.0000058*TN**2)*(1 - 0.005*TS)
          ELSE IF (KOP(1).EQ.1) THEN
!           Use model from Choi & Okos (1986)
            LAMBDAW = 0.57109 +1.7625E-03 *T -6.7036E-06 *T**2
            LAMBDAF = 1.8071E-01 -2.7604E-03 *T -1.7749E-07 *T**2
            LAMBDAP = 0.17881 +1.1958E-03 *T -2.7178E-06 *T**2
            LAMBDAC = 2.0141E-01 +1.3874E-03 *T -4.3312E-06 *T**2
            LAMBDAA = 3.2962E-01 +1.4011E-03 *T -2.9069E-06 *T**2
!           Mixture density and pure component density calculated
!           with the Choi & Okos (1986) correlations.
            RHOW = 997.18 + 3.1439E-3*T -3.7574e-3*T**2
            RHOF = 925.59 -4.1757E-1*T
            RHOP = 1329.9 -5.184E-1*T
            RHOC = 1599.1 -3.1046E-1*T
            RHOA = 2423.8 -2.8063E-1*T
            RHOTOT = 1/(FRACW/RHOW + FRACF/RHOF + FRACP/RHOP
     &           + FRACC/RHOC + FRACA/RHOA)
!           Volume fractions
            VOLW = FRACW/(RHOW / RHOTOT)
            VOLF = FRACF/(RHOF / RHOTOT)
            VOLP = FRACP/(RHOP / RHOTOT)
            VOLC = FRACC/(RHOC / RHOTOT)
            VOLA = FRACA/(RHOA / RHOTOT)	
!           Result for thermal conductivity
            KLMX = VOLW*LAMBDAW + VOLF*LAMBDAF + VOLP*LAMBDAP
     &           + VOLC*LAMBDAC + VOLA*LAMBDAA
          ELSE IF (KOP(1).EQ.2) THEN
!           For sensitivity studies: use thermal conductivity model 
!           from Cuevas and Cheryan (1978) with +10% increase
!           of the calculated thermal conductivity value
            CONCFACT = (1 - FRACW)/0.07
            TS = (10 + 2*FRACF)*CONCFACT
            KLMX = ((0.565 +0.0018 *TN -0.0000058*TN**2)
     &           *(1 - 0.005*TS))*1.1
          ELSE IF (KOP(1).EQ.3) THEN
            KLMX = (326.58 +1.0412 *TN -0.00337*TN**2)
     &           *(0.46 + 0.54*FRACW)*1.73*1e-3
          ELSE IF (KOP(1).EQ.4) THEN
            KLMX = ((326.58 +1.0412 *TN -0.00337*TN**2)
     &           *(0.46 + 0.54*FRACW)*1.73*1e-3)*1.1
          ELSE
!           Consider pure water
            GOTO 13		  
          ENDIF
        ENDIF
        QMX = KLMX
C
      END
