      SUBROUTINE DCTFLW(IDBG)
C
C  SET DUCT FLOWS FOR A MULTIZONE DUCT SYSTEM
C  AND SET INTERZONE FLOWS
C   IBRN(I)  =1 FOR "ON" AND =0 FOR "OFF" THERMOSTAT IN ZONE I
CMDK MAXSECT
CMDK NZN
CMDK NWL
CMDK NZW
CMDK BLKICL
CMDK BLSTBK
CMDK DUCTS1
CMDK DUCTS2
CMDK DUCTS3
CMDK ENCBK1
CMDK HUMIDC
CMDK INDIC1
CMDK TIMEB
CMDK TSTATC
CMDK TSTB2
CMDK TSTB4
      LOGICAL FIRST
      INTEGER IA(NZN),IAT(16,NZN),ICTRLZS(NZN),ICTR(NZN)
      REAL ECFMD(16), PCTFLT(MAXSECT,16),CFHIZI(10,16)
      REAL STBAKTS(NZN)
      CHARACTER*2 IZFL1,IZFL2,IZFL3(3),IZFL4(6),IZFL5(10)
      DATA STBAKTS/NZN*9999./
      DATA ICTRLZS/NZN*9999/
      DATA FIRST/.TRUE./
      DATA IZFL1/' 1'/
      DATA IZFL2/'12'/
      DATA IZFL3/'12','13','23'/
      DATA IZFL4/
     +'12','13','14','23','24','34'/
      DATA IZFL5/
     +'12','13','14','15','23','24','25','34','35','45'/
      IDBGSV=IDBG
C     IF(TIME.GT.17.80)IDBG=1
      IF(.NOT.FIRST)GO TO 14
C - - - - - - - - - - - - - - - -
C  THIS SECTION IS CALLED FROM BCLSD/DUCTIN/STRDAY.  NOTE::CARRIER
C  SUPPLY DUCTS SHOULD NO LONGER BE USED WITH A MULTIZONE SIMULATION
C   NZNFLW   NO. OF ZONES TO BE SUPPLIED WITH FLOW--WILL USUALLY BE =
C            TO NROOMS
          READ(2,*)NZNFLW
C
C  NSECT  (FROM BCLSD IN /DUCTS3/)  NO. OF SUPPLY DUCT SECTIONS
C  ISECT(I) (FROM BCLSD IN /DUCTS3/  DUCT SECTION NO.
C
C  FIND THE NO. OF ZONE FLOW COMBINATIONS.  FOR EX: WITH NZNFLW=3,
C  THE COMBINATIONS ARE  1,2,3,1&2,1&3,2&3,123=7
          IF(NZNFLW.EQ.1)THEN
              NSUM=1
          ELSE
              NSUM=0
              DO 2 I=1,NZNFLW
    2         NSUM=NSUM+I
              NSUM=NSUM+1
              ENDIF
          WRITE(60,512)NZNFLW,NSUM
          IF(NZNFLW.GT.5)STOP 'DCTFLW: NZNFLW CANT BE GT THAN NZN'
          IF(NSUM.GT.16)STOP' DCTFLW:INCR J DIM:CFHIZI,ECFMD,PCTFLT'
C  INITIALIZE IASET VALUES
          CALL IASET(NZNFLW,IV,NSUM,IA,IAT,IDBG)
          WRITE(60,518)
          IF(NZNFLW.EQ.1)WRITE(60,513)1
          IF(NZNFLW.EQ.2)WRITE(60,514)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
          IF(NZNFLW.EQ.3)WRITE(60,515)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
          IF(NZNFLW.EQ.4)WRITE(60,516)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
          IF(NZNFLW.EQ.5)WRITE(60,517)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
C
C  NOW READ NSECT ROWS BY NSUM COLUMNS OF PERCENT FLOW RATIOS
          DO 4 I=1, NSECT
          READ(2,*)ISCT,(PCTFLT(I,J),J=1,NSUM)
          IF(ISCT.NE.ISECT(I))WRITE(60,511)
          WRITE(60,520)ISECT(I),(PCTFLT(I,J),J=1,NSUM)
          DO 4 J=1,NSUM
          PCTFLT(I,J)=PCTFLT(I,J)*0.01
    4     CONTINUE
C
C    READ MULTIPLIER ON TOTAL FLOWS (SO ORIGINAL VALUES OF ECFMD CAN
C    BE SCALED UP OR DOWN WITH OUT CHANGING VALUES FOR ECFMD)
          READ(2,*)FLWMULT
C  NOW READ NSUM TOTAL FLOWS (CFM)--FLOW CHANGES WHEN VARIOUS ZONES
C   CALL FOR HEAT BECAUSE OF VARYING RESTRICTION IN DUCTING
          READ(2,*)(ECFMD(I),I=1,NSUM)
          WRITE(60,501)(ECFMD(I)*FLWMULT,I=1,NSUM)
C  NOW READ INTERZONE FLOWS--FOR EACH ZONE COMB.(FOR EX.,
C  WITH 4 ZONES, THE COMBINATIONS ARE:1-2,1-3,1-4,2-3,2-4,3-4)
C    FIND NO. OF INTERZONE FLOWS
      IF(NROOMS.LE.2)THEN
          NCFHIZ=1
      ELSE
          NCFHIZ=0
          NRM1=NROOMS-1
          DO 6 I=1,NRM1
    6     NCFHIZ=NCFHIZ+I
          ENDIF
      WRITE(60,508)NCFHIZ
      IF(NZNFLW.EQ.1)WRITE(60,513)1
      IF(NZNFLW.EQ.2)WRITE(60,514)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
      IF(NZNFLW.EQ.3)WRITE(60,515)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
      IF(NZNFLW.EQ.4)WRITE(60,516)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
      IF(NZNFLW.EQ.5)WRITE(60,517)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
      READ(2,*)((CFHIZI(I,J),J=1,NSUM),I=1,NCFHIZ)
      CLOSE(2)
      DO 8 I=1,NCFHIZ
      IF(NCFHIZ.EQ.1)WRITE(60,509)IZFL1,(CFHIZI(I,J),J=1,NSUM)
      IF(NCFHIZ.EQ.2)WRITE(60,509)IZFL2,(CFHIZI(I,J),J=1,NSUM)
      IF(NCFHIZ.EQ.3)WRITE(60,509)IZFL3(I),(CFHIZI(I,J),J=1,NSUM)
      IF(NCFHIZ.EQ.4)WRITE(60,509)IZFL4(I),(CFHIZI(I,J),J=1,NSUM)
      IF(NCFHIZ.EQ.5)WRITE(60,509)IZFL5(I),(CFHIZI(I,J),J=1,NSUM)
    8 CONTINUE

C - - - - - - - - - - - -
C  THIS SECTION IS CALLED FROM THERMOSTAT DRIVER PROG, TSTATD,
C        WHEN IBRN VALUES HAVE CHANGED
C - - - - - - - -
   14 CONTINUE
          IZERO=0
          IF(NZNFLW.EQ.1)THEN
              ICOL=1
              GO TO 30
              ENDIF
          IF(IDBG.NE.0)WRITE(60,500)
C  FIND COLUMN NO. OF ZONE FLOW COMBINATION (ICOL)
          DO 20 IV=1,NSUM
C
C  GENERATE A ROW OF IA VALUES
          CALL IASET(NZNFLW,IV,NSUM,IA,IAT,IDBG)
C  COMPARE ELEMENTS OF ZONE DAMPER STATES AND IA.  IF ALL AGREE,
C   ICOL IS FOUND !!!!!!
          IF(IMODE.EQ.2)THEN
C   HEATING
             CALL IFGSET(NZNFLW,IA,IBRN,IFLAG)
          ELSE
C   COOLING
             CALL IFGSET(NZNFLW,IA,IACZ,IFLAG)
             ENDIF
          IF(IDBG.NE.0)THEN
              WRITE(60,502)(IV,I,IBRN(I),IA(I),I=1,NZNFLW)
              WRITE(60,506)IV,IFLAG,IMODE
              ENDIF
          IF(IFLAG.EQ.1)THEN
              ICOL=IV
              GO TO 30
              ENDIF
   20     CONTINUE

C  DUCT DAMPER PATTERN IS 000 HERE!
          IBN=0
          DO 22 I=1,NZNFLW
   22     IBN=IBN+IBRN(I)
          IF(IBN.NE.0)STOP 'DCTFLW:AFTER 22, ICOL NOT FOUND!!'
C   SET ICOL TO OPEN ALL DAMPERS IN CONTROLLED ZONES WHEN IA VALUES
C   ARE ALL = 0
          IZERO=1
C  NEXT CODE NEEDED TO ACCOMMODATE SETBACK OR CHANGE IN ICTRLZ
C     ICTRLZ WAS CHANGED IN TSTATD TO ACCOMMODATE THE WAY
C     DATA ON 121486 IN HOUSE A WAS RUN
          DATA IZPU/0/
          DO 23 I = 1, NROOMS
          NRM=NRMA(I)
          IF(STBAKT(NRM).NE.STBAKTS(NRM))IZPU=0
   23     IF(ICTRLZ(NRM).NE.ICTRLZS(NRM))IZPU=0

          IF(IZPU.EQ.0)THEN
          DO 231 I = 1, NROOMS
          NRM=NRMA(I)
          ICTR(NRM)=ICTRLZ(NRM)
          IF(STBAKT(NRM).NE.STBAKTS(NRM).AND.STBAKT(NRM).NE.0.)THEN
C  CLOSE DAMPER INTO ZONE THAT IS SETBACK, EVEN THOUGH ICTRLZ = 1 IN
C  THAT ZONE
            ICTR(NRM)=0
            ENDIF
  231     CONTINUE
C  FIND IA PATTERN THAT IS THE SAME AS ICTR
             IZPU=1
             DO 24 IV=1,NSUM
             CALL IASET(NZNFLW,IV,NSUM,IA,IAT,IDBG)
             CALL IFGSET(NZNFLW,IA,ICTR,IFLAG)
             IF(IFLAG.EQ.1)THEN
                ICOLU=IV
                GO TO 26
                ENDIF
   24        CONTINUE
             STOP 'DCTFLW:ICTRLZ NE IA AT LABEL 24'
             ENDIF
   26     ICOL=ICOLU
   30     CONTINUE
          IF(IDBG.NE.0)WRITE(60,503)ICOL
C   NOW SET TOTAL FLOW
          IF(IZERO.EQ.0)THEN
             ECFMC1=ECFMD(ICOL)*FLWMULT
C         ELSE
C               SET FLOW TO MINIMUM IF ALL DAMPERS WERE SHUT
C   NOTE: IN RETDUCT, MIN FLOW IS USED WHEN IEFAN (OR I10=1) IS 0
C         REGARDLESS OF FLOW SET BELOW, SO BELOW STMT IS PROBABLY
C         REDUNDANT AND NOT USED.
C   IF MIN FLOW IS SET BELOW, PROGRAM WILL BOMB ON UNDERFLOW!!!!
C            ECFMC1=ECFMC2
             ENDIF
          ECFMH1=ECFMC1
          IF(IDBG.NE.0)WRITE(60,505)ECFMC1
C   AND FLOW FRACTIONS IN ALL DUCT SECTIONS--
          IF(IDBG.NE.0)WRITE(60,521)
          DO 40 I=1,NSECT
          PCTF(I)=PCTFLT(I,ICOL)
          IF(IDBG.NE.0)WRITE(60,504)ISECT(I),PCTF(I)
   40     CONTINUE
C  CALC NEW RWSDCT VALUES
          DO 50 I=1,NROOMS
   50     RWSDCT(I)=0.
          DO 60 I=1,NSECT
          IZ=IZNSUP(I)
   60     IF(IZ.GT.0.AND.PCTF(I).GT.0.)RWSDCT(IZ)=RWSDCT(IZ)+PCTF(I)
          IF(IDBG.NE.0)THEN
             WRITE(60,522)
             DO 80 I=1,NROOMS
             NRM=NRMA(I)
             WRITE(60,507)NRM,RWSDCT(NRM)
   80        CONTINUE
             ENDIF
      DO 82 I=1,10
   82 CFHIZ(I)=0.
C  SET INTERZONE FLOWS
C    IF BLOWER IS OFF, LEAVE INTERZONE FLOWS AT ZERO
      IF(IEFAN.NE.0)THEN
         DO 90 I=1,NCFHIZ
   90    CFHIZ(I)=CFHIZI(I,ICOL)
         DATA IDCTI/30/
         IDCTI=IDCTI+1
         IF(IDCTI.LE.20.OR.IDBG.NE.0)THEN
           WRITE(60,510)TIME,ICOL,(I,PCTF(I),I=1,NSECT)
           WRITE(60,523)(I,CFHIZ(I),I=1,NCFHIZ)
           ENDIF
         ENDIF
      IF(.NOT.FIRST)CALL DUCTN(IDBG)
      DO 92 I = 1, NROOMS
      NRM=NRMA(I)
      ICTRLZS(NRM)=ICTRLZ(NRM)
   92 STBAKTS(NRM)=STBAKT(NRM)
      FIRST=.FALSE.
      IDBG=IDBGSV
      RETURN
  500 FORMAT(1X,'DCTFLW:FIND INDICE OF ZONE FLOW COMBINATION -- '/
     +'   FOUND WHEN EACH IB=IA')
  501 FORMAT((1X,'BLWR FLOW,CFM= ',10F10.1))
  502 FORMAT((1X,'IV,I,IB,IA= ',2I4,5X,2I4))
  503 FORMAT(1X,'*--*--ICOL= ',I4)
  504 FORMAT(1X,I3,G13.5)
  505 FORMAT(1X,'NEW BLWR FLOW(ECFMH1),CFM= ',G13.5)
  506 FORMAT(1X,'IV,IFLAG,IMODE= ',3I4)
  507 FORMAT(1X,'ZONE NO.= ',I4,'  RWSDCT= ',F10.5)
  508 FORMAT(1X,'INTERZONE AIR FLOWS,CFH--FOR EA SET',
     +' OF'/'  OPERATING ZONES FOR EA INTERZONE FLOW'/
     +'  FOR EX: WITH 3 ZONES, THE OPERATING ZONES MAY BE'/
     +'             1,2,3,1&2,1&3,2&3,1&2&3 '/
     +'          AND INTERZONE FLOW S ARE:'/
     +'             1-2,1-3,2-3'/
     +5X,'NCFHIZ= ',I4/1H0,10X,'TABLE OF INTERZONE FLOWS(CFH)')
  509 FORMAT(1X,A2,16F8.1)
  510 FORMAT(1X,'DCTFLW:TIME= ',G13.5,' ICOL= ',I5/
     +(20X,'DUCT SECT#= ',I3,' PCTF= ',G13.5/))
  511 FORMAT(1X,'DCTFLW:OOPS!!! ISCT ON READ OF PCTFLT NE ISECT')
  512 FORMAT(1X,'DCTFLW:NZNFLW,NSUM= ',2I5)
  513 FORMAT(3X,3X,I2)
  514 FORMAT(3X,3(3X,I1,I1,3X))
  515 FORMAT(3X,7(3X,I1,I1,I1,2X))
  516 FORMAT(3X,11(2X,I1,I1,I1,I1,2X))
  517 FORMAT(3X,16(2X,I1,I1,I1,I1,I1,1X))
  518 FORMAT(1X,'DCT',15X,'FLOW FRACTIONS FOR EACH SET OF DAMPER',
     +' SETTINGS'/1X,'SECT')
  520 FORMAT(1X,I2,16F8.3)
  521 FORMAT(1X,'DCT',2X,'FLOW FRACTION'/1X,'SECT')
  522 FORMAT(1X,'RWSDCT(BELOW) IS DECIMAL FRACTION OF FLOW ',
     +'DELIVERED TO ZONE')
  523 FORMAT(1X,'DCTFLW:CFHIZ= ',/(15X,'ZONE= ',I3,'CFHIZ= ',G13.5/))
      END
      SUBROUTINE IFGSET(NZNFLW,IA,IB,IFLAG)
C
C  SET IFLAG=1 IF ALL ELEMENTS OF IB=THAT OF IA
CMDK NZN
      INTEGER IA(NZN),IB(NZN)
      IFLAG=1
      IQ=0
      DO 10 I=1,NZNFLW
      IF(IB(I).NE.IA(I))IQ=IQ+1
   10 CONTINUE
      IF(IQ.NE.0)IFLAG=0
      RETURN
      END
      SUBROUTINE IASET(NZNFLW,IV,NSUM,IA,IAT,IDBG)
C
C  SET VALUES IN IA ARRAY REPRESENTING ON-OFF VECTOR OF ZONE DAMPERS
C    CONTROLLED BY A THERMOSTAT IN EACH ZONE
C  IV  IS INTERZONE FLOW INDICE
CMDK NZN
      DIMENSION IA(NZN),IAT(16,NZN)
      LOGICAL FIRST
      DATA FIRST/.TRUE./
      IF(NZNFLW.GT.5)STOP 'IASET:I DIM. OF IAT(I,J) MUST BE INCR'
      IF(NSUM.GT.16)STOP 'IASET:J DIM. OF IAT(I,J) MUST BE INCR'
      IF(.NOT.FIRST)GO TO 68
C  SET UP IAT ARRAY FOR POSSIBLE DAMPER SETTINGS IN ALL ZONES
C     SET PATTERN:  1 0 0 (MEANS ZONE 1 ONLY)
C                   0 1 0 (MEANS ZONE 2 ONLY)  FOR EX. FOR 3 ZONES
C                   0 0 1 (MEANS ZONE 3 ONLY)
C                   1 1 0 (MEANS ZONE 1 AND 2 ONLY,ETC)
C                   1 0 1
C                   0 1 1
C                   1 1 1
          DO 10 IQ=1,NZNFLW
          DO 10 I=1,NZNFLW
          IF(I.EQ.IQ)THEN
              IAT(IQ,I)=1
          IF(IDBG.NE.0)CALL IAOUT(1,IQ,I,IAT(IQ,I))
          ELSE
              IAT(IQ,I)=0
          IF(IDBG.NE.0)CALL IAOUT(2,IQ,I,IAT(IQ,I))
              ENDIF
   10     CONTINUE
C  NOW FILL IN THE REST OF THE PATTERN
          IROW=NZNFLW
          NZ1=NZNFLW
C
          DO 60 IQR=1,NZNFLW
          NZ1=NZ1-1
          IF(NZ1.EQ.0)THEN
              DO 20 I=1,NZNFLW
              IAT(NSUM,I)=1
              IF(IDBG.NE.0)CALL IAOUT(3,NSUM,I,IAT(NSUM,I))
   20         CONTINUE
              GO TO 65
              ENDIF
C  FIND NO. OF LEADING ZEROS
          NZER=NZNFLW-1-NZ1
          IF(NZER.EQ.0)THEN
C  SET LEADING 1 S
              DO 30 IR=1,NZ1
              IRR=IROW+IR
              IAT(IRR,1)=1
              IF(IDBG.NE.0)CALL IAOUT(4,IRR,1,IAT(IRR,1))
   30         CONTINUE
          ELSE
C  SET LEADING ZEROS
              DO 40 IR=1,NZ1
              DO 40 IS=1,NZER
              IRR=IROW+IR
              IAT(IRR,IS)=0
              IF(IDBG.NE.0)CALL IAOUT(5,IRR,IS,IAT(IRR,IS))
   40         CONTINUE
C  SET A COLUMN OF 1 S  FOLLOWING THE LEADING ZEROS
              DO 42 IR=1,NZ1
              IRR=IROW+IR
              NZER1=NZER+1
              IAT(IRR,NZER1)=1
              IF(IDBG.NE.0)CALL IAOUT(6,IRR,NZER1,IAT(IRR,NZER1))
   42         CONTINUE
              ENDIF
C  NOW GO BACK AND FILL IN VALUES FOR THIS NZ1 SET
          NZR1=NZER+1
          IF(NZ1.EQ.1)THEN
              IR1=IROW+1
              IAT(IR1,NZNFLW)=1
              IF(IDBG.NE.0)CALL IAOUT(7,IR1,NZNFLW,IAT(IR1,NZNFLW))
              GO TO 60
              ENDIF
          DO 50 IQ=1,NZ1
          IROW=IROW+1
          ND=NZR1
          DO 50 I=1,NZ1
          ND=ND+1
          IF(I.EQ.IQ)THEN
              IAT(IROW,ND)=1
              IF(IDBG.NE.0)CALL IAOUT(8,IROW,ND,IAT(IROW,ND))
          ELSE
              IAT(IROW,ND)=0
              IF(IDBG.NE.0)CALL IAOUT(9,IROW,ND,IAT(IROW,ND))
              ENDIF
   50     CONTINUE
   60     CONTINUE
   65     IF(IDBG.NE.0)WRITE(60,500)((IAT(I,J),J=1,NZNFLW),I=1,NSUM)
          FIRST=.FALSE.
          RETURN
C  * * * * * * * * * SET IA ARRAY BELOW
   68  CONTINUE
          DO 70 I=1,NZNFLW
   70     IA(I)=IAT(IV,I)
          RETURN
C  FORMAT BELOW IS FOR NZNFLW=3
  500 FORMAT(1H0,'IASET:IAT VALUES'/
     +((10X,3I5/)))
      END
      SUBROUTINE IAOUT(ICOD,I,J,IAT)
C
C  PRINT IAT VALUES FOR DEBUG
      WRITE(60,500)ICOD,I,J,IAT
      RETURN
  500 FORMAT(1X,'IASET:ICOD= ',I5,' I,J= ',2I5,'  IAT= ',I5)
      END
