C
C
      SUBROUTINE  CDENDS (MARKY,NSTART,L,NSTOP)
C
C     -----------------------------------------------------------------
C
C       SUBROUTINE PURPOSE:
C       EVALUATION OF AIR TEMP. AND CH4/FUME CONTENT AT AIRWAY ENDING.
C
C     -----------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
      PROPJS=PROP(L)
      JX=L
      CH4JS=PRCH4(L)
      JNO(L)=-JNO(L)
      JY=1
      IF (NSTART.NE.1) JY=LOUT(NSTART-1)+1
      JZ=LOUT(NSTART)
      DO 60 K=JY,JZ
         ICFTM=0
         I=NGOUT(K)
         NM=0
C
C     MARKY=0: THE DYNAMIC SIMULATION PART OF THE PROGRAM HAS NOT
C              BEEN REACHED AND EFFECTS OF FIRE SOURCES ARE IGNORED
C              TO OBTAIN PARAMETERS BEFORE THE EVENT.
C
        IF (MARKY.EQ.0.AND.NTEMP.NE.1) GO TO 25
            DO 20 L=1,INFLOW
               IF (NCENT(L).EQ.NO(I)) THEN
C
C       USER-SPECIFIED FIRE CHARACTERISTICS (CONST. HEAT AND CONTAMINANT
C     GENERATION RATES).
C
                  NM=1
                  FACT=1.0
                  IF (QCENT(L).GT.10.0) THEN
                     Q1=Q(I)
                     IF (Q1.LT.QCENT(L)) Q1=Q1+QCENT(L)
                     FACT=1.0+(Q(I)-QCENT(L))/Q1
                  ENDIF
                  CONTAM=CONT(L)*CONC(L)/100.
                  CONTQ=CONT(L)
                  HEATAD=HEAT(L)*FACT
                  IF (O2MIN(L).GT.0.0) THEN
C
C     OXYGIN-RICH FIRE.
C
                     O2SCL=O2MIN(L)+(21.0-O2MIN(L))*(FACT-1.0)
                     CONTAM=(0.21-PROPJS-O2SCL/100.)*Q(I)
                     CONTQ=0.0
                     IF (CONTAM.GT.0.0) THEN
                        HEATAD=CONTAM*437.
                     ELSE
                        HEATAD=0.0
                     ENDIF
                  ENDIF
                  IF (SMPO2(L).LE.0.0) GO TO 30
C
C     FUEL-RICH FIRE. THE ABOVE 3 MODELS SUCCESSIVELY OVERRIDE.
C
                  CONTAM=(0.21-PROPJS)*Q(I)*SMPO2(L)
                  CONTQ=0.0
                  IF (FACT.LE.1.0) THEN
                     HTSCL=HTPO2(L)
                  ELSE
                     HTSCL=HTPO2(L)/FACT
                  ENDIF
                  IF (CONTAM.GT.0.0) THEN
                     HEATAD=(0.21-PROPJS)*Q(I)*HTSCL
                  ELSE
                     HEATAD=0.0
                  ENDIF
                  GO TO 30
               ENDIF
20          CONTINUE
C
C     NO FIRE SOURCE IS LOCATED IN AIRWAY NO(I).
C
25       CONTAM=0.0
         CONTQ=0.0
         HEATAD=0.0
         TFS=0.0
C
C     RDCH4(I): CH4 CONCENTRATION AT THE END OF AIRWAY NO(I).
C     RDPROP(I): FUME CONCENTRATION AT THE END OF AIRWAY NO(I).
C     TRD(I): AIR TEMP. AT THE END OF AIRWAY NO(I).
C     TJS(I): AIR TEMP. AT THE BEGINNING OF AIRWAY NO(I).
C
30       RDCH4(I)=(CH4JS*Q(I)+CH4V(I))/(Q(I)+CH4V(I))
         RDPROP(I)=PROPJS*(Q(I)-CONTQ)/Q(I)+CONTAM/Q(I)
         TJS(I)=T(JX)
         IF (NM.GT.0) THEN
            IF (HEATAD.NE.0.0) THEN
               VART=(9900.0+T(JX))**2+2.0*HEATAD/(Q(I)*DR*2.4E-5)
               IF (VART.LT.9.4E7) THEN
                  WRITE (8,110) NO(I),Q(I)
                  NSTOP=1
                  RETURN
               ENDIF
               TFS=-9900.0+SQRT(VART)
               TJS(I)=TFS
               TFSI(L)=TFS
            ELSE
               TFSI(L)=TJS(I)
            ENDIF
            IF (TJS(I).LT.(-200.0).OR.TJS(I).GT.5000.0) THEN
               WRITE (8,100) NO(I),Q(I),TJS(I)
               NSTOP=1
               RETURN
            ENDIF
         ENDIF
C
C       AN ITERATION PROCESS IS EMPLOYED TO GIVE A BETTER ESTIMATION
C     OF MEAN AIR TEMP. IN THE AIRWAY FOR ASSESSING MORE ACCURATE
C     THERMAL PARAMETERS.
C     TM: MEAN AIR TEMP. IN AIRWAY.
C
         IF (ICFTM.GT.0) GO TO 40
         TM=(TJS(I)+TROCK(I))/2.
         GO TO 50
40       ARGMT=(TJS(I)-TROCK(I))/(TRD(I)-TROCK(I))
         IF (ARGMT.LE.1.) THEN
            WRITE (8,*) 'ERROR IN SUB CDENDS'
            TM=(TJS(I)+TRD(I))/2.
         ELSE
            X=ALOG(ARGMT)/LA(I)
            TM=TROCK(I)+(TJS(I)-TROCK(I))*(1.0-EXP(-X*LA(I)))/(X*LA(I))
         ENDIF
50       CALL KALPHA (OMIT,TM,FX,CP,I,0)
C
C     COAGE: COEFF. OF AGE.
C     XNEW(I): EXPONENTIAL FACTOR IN COMPUTING AIR TEMP. VARIATION
C              ALONG AIRWAY NO(I).
C
         COAGE=BI(I)-FX*BI(I)*BI(I)/(0.375+BI(I))
         DCOAGE(I)=COAGE
         XNEW(I)=HK(I)*O(I)*O(I)*COAGE/(120.*DR*Q(I)*A(I))
         G=XNEW(I)*LA(I)/CP
         IF (G.GE.25.0) THEN
            GX=0.0
         ELSE
            GX=EXP(-G)
         ENDIF
         IF (ICFTM.LE.0) THEN
            TOLD=TROCK(I)
         ELSE
            TOLD=TRD(I)
         ENDIF
         TRD(I)=TROCK(I)+(TJS(I)-TROCK(I))*GX-
     .          (DZRD(I)/(2.*778.26*CP))*(1.+GX)
         TDM=ABS(TOLD-TRD(I))
         IF (TDM.GT.50.) THEN
            ICFTM=1
            GO TO 40
         ENDIF
         IF (JF(I).GT.0) JF(I)=-JF(I)
60    CONTINUE
C
      RETURN
100   FORMAT (/,' * ERR * ABNORMAL TEMPERATURE IN HEAT SOURCE ',I5,/,
     .       ' CURRENT AIRFLOW:',F8.0,'  TEMPERATURE: ',F8.1,/,
     .       ' "HEAT" AND/OR "QCENT" MUST BE ADJUSTED.')
110   FORMAT (/,' * ERR * ABNORMAL TEMPERATURE IN HEAT SOURCE ',I5,/,1X,
     .       'CURRENT AIRFLOW:',F8.0,'. ADJUST "HEAT" AND/OR "QCENT".')
C
      END

