C  HOUS_II.FOR  FILE
C-----RESIDENTIAL SPACE-CONDITIONING PROGRAM
C  This program simulates the dynamic operation of a space conditioning
C  system in a residential environment. Structure dynamic temperatures
C  are computed from structure information in response to weather data.
C
C  Please read the README screens, if you have not already done so.
C-----
CMDK NHRO
CMDK NHRP
CMDK NZN
CMDK NIWL
CMDK NOUTDY
CMDK NHBK
CMDK NWL
CMDK NZN7
CMDK NZW
CMDK ACHBQ
CMDK ACHLQ
CMDK BLKSV
CMDK TSTB4
CMDK COLLEC
CMDK EBALA
CMDK ENCBK1
CMDK HANDBK
CMDK IWETHR
CMDK IZZQ
CMDK OUTPUT
CMDK OWETHR
CMDK PRT1
CMDK QACCMQ
CMDK QUAYCH
CMDK QUAYLE
CMDK SIMA
CMDK SOILB
CMDK SOLARB
CMDK STRUCA
CMDK TEMP1
CMDK TIMEB
CMDK BLKTM2
CMDK TSTATC
CMDK UABAS
      CHARACTER*8 CDAY (7)
      INTEGER NDAYCUM(12), NROUT(24)
      INTEGER IPASS(NOUTDY),IPASY(NOUTDY)
      REAL    ACINFL(NZN), ATOUT(12,24), C(5),
     +        DQINT (24), DSOLRH(24), DTOB(24), DWHM(24), DWINT(24),
     +        DWSPED(24),                FLOWIN(NZN),
     +        QINFC(NZN), QINFS(NZN),  ODDBM(25),
     +        TIMEAN(NZN), TISUM(NZN), TMRMN(NZN), TMRSUM(NZN),
     +        WINFC(NZN), WINFSS(NZN)
C     REAL COMFTT(11,21), XHUM1(22), COMFTS(11,21)
C
C
      OPEN(60,FILE='TAPE60',STATUS='NEW',IOSTAT=IO6)
      IF(IO6.NE.0)THEN
        WRITE(60,*)' HOUS_II: CANT OPEN MAIN OUTPUT FILE ON TAPE60'
        STOP ' HOUS_II: CANT OPEN MAIN OUTPUT FILE ON TAPE60'
        ENDIF
      OPEN(1,FILE='TAPE1',STATUS='NEW',IOSTAT=IO1)
      IF(IO1.NE.0)THEN
        WRITE(60,*)' HOUS_II: PROBLEM WITH OPENING TAPE1'
        STOP ' HOUS_II: CANT OPEN TAPE1 FOR OUTPUT OF REL HUM SWINGS!!!'
        ENDIF
      OPEN(5,FILE='TAPE5',STATUS='OLD',IOSTAT=IO5)
      IF(IO5.NE.0)THEN
        WRITE(60,*)' HOUS_II: CANT OPEN MAIN INPUT FILE ON TAPE5'
        STOP ' HOUS_II: CANT OPEN MAIN INPUT FILE ON TAPE5'
        ENDIF
      OPEN(11,FILE='TAPE11',STATUS='NEW',IOSTAT=IO11)
      IF(IO11.NE.0)THEN
        WRITE(60,*)' HOUS_II: PROBLEM WITH OPENING TAPE11'
        STOP ' HOUS_II: CANT OPEN TAPE11 FOR OUTPUT OF TEMP SWINGS!!!'
      ENDIF
C-----INITIALIZATIONS
      DATA CDAY   /'1ST DAY', '2ND DAY', '3RD DAY', '4TH DAY',
     +   '5TH DAY ', '6TH DAY', '7TH/DAY'/
      DATA IPASS  /NOUTDY*1.0/
      DATA NDAYCUM/31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
     +   365/
      DATA TBAS   /80.0/
      DATA WINT   /0.0/
      DATA ITMLS/-1/
C      DATA XHUM1  /0.030, 0.020, 0.019, 0.018, 0.017, 0.016, 0.015,
C     1   0.014, 0.013, 0.012, 0.011, 0.010, 0.009, 0.008, 0.007, 0.006,
C     2   0.005, 0.004, 0.003, 0.002, 0.001, 0.00001/
C
      TIME=.00001
C
      CALL VERSION
C
C-----START EXECUTION
C  IDBG - =1, DEBUG PRINTS; =0, NO DEBUG PRINTS
      READ(5,*)IDBG
      WRITE(60,7091)IDBG
      WRITE(*,*)' --> CALLING STRRUN FROM HOUS_II'
      CALL STRRUN
      CALL VERSION
      CALL CURS(10)
      WRITE(*,*)'             #########   BEGIN TIME-BASED CALCULAT',
     +          'IONS  ##########'
C              ITM1MAX IS MAX NO. OF DTIM1 TIMESTEPS IN 1 HR      
      ITM1MAX=INT(1./DTIM1 + .01)
C
CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA START
CAAAA START OF MONTHLY CYCLE LOOP
CAAA
      IMONTH=1
  180 CONTINUE
      IREP=0
      KM=MONTH(IMONTH)
C     WRITE(60,182)
  182 FORMAT(1X,'CALLING STRMON HERE')
      CALL STRMON (C, HDUM, IFWL, IPASS, IPASY, IWR, IWRB,
     +   NDAYTOT, ODDBM, ODHUM, RDUM, TDUM, TEMPDV, WSC, WTFCT,IDBG)
C
C
CBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB START
CBBBB START OF SIZING LOOP
CBBB
230   IDAYM=1
      DO 241 J=1, NOUTDY
         DO 240 I=1, 8
            OUTDY (I, J)=0.
240         CONTINUE
241      CONTINUE
      OUTDY (8, 44)=999.
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC START
CCCCC** START OF LOOP ON MEAN, MAX, AND MIN DAYS
CCCC          OR  IDAYM=...1....2........3....., 4, 5, 6, 7 (NDAYM MAX)
  250 CONTINUE
C     WRITE(60,252)
  252 FORMAT(1X,'CALLING STRDAY HERE')
      TIME=.00001
      CALL STRDAY (ATOUT, C, CDAY, DQINT, DSOLRH, DTCR, DTIM1,
     + DTIM2, DTOB, DWHM, DWINT, DWSPED, ENDT, HDUM, HUMON, ICYCCP,
     + ICYC1, ICYC6, IDAYM, IDUM, INC, IREP, IRH, ISWING, KDAYM,
     + MPRINT, NROUT, NZONG,ODDBM, ODHUM, QBASVN, QHUMT, QSPT, QWAS,
     + RELHMI, RHDIR, RHIO, RODB, TBAS, TDIR, TDUM, TEMPDV, TIMAX,
     + TIMAXN, TIMIN, TIO, TONMAX, TRO, VENCRV, WATER, WSC, IDBG)
C
C-----START LOOP OF NREP HRDY PERIODS FOR EACH SET OF HRDY DATA
480   TIME=0.00001
C     NOTE:  TINDEX = HOUR-OF-YEAR PLUS 24. IT IS THE SAME AS THE TIME
C                     INDEX IN THE SP43 DATA FILES
C           TIMEOY = TIME OF YEAR
C           JULDAY = JULIAN DAY OF THE YEAR
      JULDAY=NDAYCUM (KM)-NDAYTOT (KM)+KDAYM
      IF (ISDAY.NE.0) JULDAY=ISDAY+IDAYM-1
      TINDEX=JULDAY*24.+TIME
      TIMEOY=TINDEX-24.0
      DO 488 JT=1,NROOMS
      TMRSUM(JT)=0.
  488 TISUM(JT)=0.
      CALL ZERV(6,QBASF(1),QBASF(2),QBWBG(1),QBWBG(2),
     +          QBWBG(3),QBWBG(4),0.,0.,0.,0.)
      ISUM=0
      IT=1
      DATA ITRFU/0/
C
C-----START OF NEW HOUR
  490 CONTINUE
      IF (IGRND.GT.0) THEN
C        CALL GROUND TEMP ROUTINE.     NOTE: TBASM LAGS ONE HOUR
         SOLRHM=0.5*(SOLARH (IT+1)+SOLARH (IT))
         TODDBM=0.5*(ODDB (IT+1)+ODDB (IT))
         WSPEDM=0.5*(WSPEED (IT+1)+WSPEED (IT))
C        WRITE(60,447)
  447    FORMAT(1X,'CALLING GRNDEX HERE')
         ICODE=3
         CALL GRNDEX (TODDBM, WSPEDM, SOLRHM, KM)
         ICODE=0
      END IF
C
C-----WETHR BLOCK 4: PREPARE WEATHER DATA FOR FIRST TIME INCREMENT
      IF (NSEQW.EQ.0 .OR. NSEQW.EQ.1 .OR. NSEQW.EQ.3) THEN
         TODDB=ODDB (IT)-DTOB (IT)+TOFFSET
         WODHUM=ODHUM (IT)-DWHM (IT)
         WSPED=WSPEED (IT)-DWSPED (IT)
         SOLRH=SOLARH (IT)-DSOLRH (IT)
      ELSE IF (NSEQW.EQ.2) THEN
C        WRITE(60,448)
  448    FORMAT(1X,'CALLING WETHR HERE')
         CALL WETHR (4, NSEQW)
      END IF
      DO 500 I=1, NIJ
         SOLARL (I)=SOLAR (IT, I)-DSOLAR (IT, I)
500      CONTINUE
      QINT=QINTL (IT)-DQINT (IT)
      IF (IWAT.EQ.1) WINT=WINTL (IT)-DWINT (IT)
CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD START
CDDDDD START OUTER TIME LOOP - FOR A HRDY PERIOD
CDDDDD                       - NEW VALUES HAVE SUFFIX "N"
CDDD
      ITM1HR=0
  510 CONTINUE
      ITMQQ=INT(TIME+.001)
      IF(ITMQQ.NE.ITMLS.AND.ITMQQ.NE.24)THEN
        CALL CURS(12)
        WRITE(*,7081)ITMQQ+1,JULDAY
      ENDIF
      ITMLS=ITMQQ
      ITRFU=ITRFU+1
      IDBG=0
C     WRITE(60,449)
  449 FORMAT(1X,'CALLING STRLP1 HERE')
      CALL STRLP1 (ACINFL, ACMAX, ACMIN, AVENC2, DQINT, DSOLRH,
     +DTCR, DTCRW, DTIM1, DTOB, DWHM, DWINT, DWSPED, FLOWIN,
     +IDAYM, IDUM, NZONG,POWA, QCONRF, QINFC, QINFS, QINT,
     +QLOSRF,QRAD,QSKY, RHA, RLOADL, RLOADS, SOLRH, TDUM, TODDBR,
     +TRO, TS,VBMAX,VBMIN, VENCRV, WINFC, WINFSS, WINT, WODFAC,
     +WODHMR, IDBG)
C
CEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE START
CEEEE START OF INNER TIME LOOP
CEEE
C     WRITE(60,451)
  451 FORMAT(1X,'CALLING LOOP HERE')
      CALL LOOP (ACINFL, ATOUT, AVENC2, DTIM2, DTCRW, FLOC,
     +FLOWIN, HDUM, HUMON, ICL1, ICYC1, ICYC6, ICYCCP, IDAYM, ILINES,
     +INC, ISUM, MPRINT, NROUT, POWA, QBASVN, QCONRF, QCRAD,
     +QHUMT, QINFC, QINFS, QINT, QLOSRF,  QRAD, QSKY, QSPT,
     +QWAS, RDUM, RELHMI,RHA, RLOADL, RLOADS, TBAS, TDUM, TISUM,
     +TMRSUM, TODDBR, TONMAX, TRO, TS, WATER, WINFC, WINFSS,
     +WINT, WODFAC, WODHMR, IDBG)
      ITM1HR=ITM1HR+1
CEEE
CEEEE END OF INNER TIMELOOP
CEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE END
C
      CALL ENDLP1 (IDAYM,IREP, IRH, ISWING,
     +NROOMS, POWA, RHA, RHDIR, RHIO, RLOADL, RLOADS, TDIR, TIMAX,
     +TIMAXN, TIMIN, TIO, TS, IDBG)
C
C-----USE INTERPOLATED WEATHER VALUES BETWEEN HOURLY VALUES
      IF(ITM1HR.LT.ITM1MAX)GO TO 510
C              ADJUST TIME TO THE EXACT HOUR SO ERRORS DONT PROPAGATE
      TIME=FLOAT(IT)
C-----NEW HOUR. SAVE CYCLES. AVE BSMT. INCREMENT HOUR COUNTER
      HROUT (28, IT)=ICYC1/2
      CALL QACCUMH
      IF (TIME.LT.ENDT) THEN
         IT=IT+1
         GOTO 490
      END IF
CDDD
CDDDD END OF OUTER TIME LOOP
CDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD END
C-----END OF DAY REACHED
C-----CALCULATE MEAN ROOM TEMPERATURES FOR DAY
      DO 520 JT=1,NROOMS
      TMRMN(JT)=TMRSUM(JT)/ISUM
  520 TIMEAN(JT)=TISUM(JT)/ISUM
      WRITE(60,2300)(JT,TIMEAN(JT),JT=1,NROOMS)
      WRITE(60,2302)(JT,TMRMN(JT),JT=1,NROOMS)
      IREP=IREP+1
      IF (IREP.EQ.NDPRE .OR. NDPRE.EQ.0) ILINES=2
      IF (IREP.LE.NREP .OR. IREP.LE.NDPRE) GO TO 480
C
C-----DO THE END-OF-DAY CALCULATIONS
      CALL ENDLP2 (ATOUT, CDAY, DTIM1, HUMON, ICYC1, ICYC6,
     +ICYCCP, IDAYM, ILINES, IPASS, IPASY, IRH, ISWING, KDAYM,
     +NROUT, QHUMT, QSPT, QWAS, TIMAX, TIMAXN, TIMIN, TONMAX,
     +WATER, WEIGHT, WTFCT)
C
C-----CHECK FOR MEAN DAY CALC
      IDAYM=IDAYM+1
C  MONTHLY SUMMARIES AFTER EACH DAY????
      IF(IDAYMN.NE.0)GO TO 530
      IF (IDAYM.LE.NDAYM) GO TO 250
CCCC
CCCCC END OF MEAN DAY LOOP
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC END
C-----PERFORM OPERATIONS FOR END OF MEAN DAY
 530  CALL ENDDAY 
      WRITE(60,7082)
      DO 532 JT=1,NROOMS
      NRM=NRMA(JT)
      IF(EBALT(NRM).NE.0.)THEN
         EBL=EBAL(NRM)/EBALT(NRM)*100.
      ELSE
         EBL=99999.
         ENDIF
      WRITE(60,7084)NRM,EBL
  532 CONTINUE
      IF(IDAYMN.NE.0)THEN
         IF(IDAYM.LE.NDAYM)GO TO 250
      ENDIF
CBBB
CBBBB END OF SIZING LOOP
CBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB END
C
  540 CALL ENDMON 
C
      IMONTH=IMONTH+1
      IF (IMONTH.LE.NMONTH) THEN
C        FINISH THERMAL CALCULATIONS IN GROUND FOR REST OF MONTH
         IF (IGRND.GT.0) THEN
           ICODE=4
           CALL GRNDEX (TODDBM, WSPEDM, SOLRHM, KM)
           ICODE=0
         ENDIF
         GO TO 180
      END IF
CAAA
CAAAA END OF MONTHLY LOOP
CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA END
C-----DO THE END-OF-RUN CALCULATIONS
C      CALL ENDRUN (IPASS, XHUM1)
      CALL ENDRUN (IPASS)
C
1210  WRITE(60, 7080) VBMAX,VBMIN,(NRM,ACMAX(NRM),ACMIN(NRM),
     +NRM=1,NROOMS)
      WRITE(60,7082)
      DO 1212 JT=1,NROOMS
      NRM=NRMA(JT)
      EBL=EBAL(NRM)/EBALT(NRM)*100.
      WRITE(60,7084)NRM,EBL
 1212 CONTINUE
      STOP 'END OF RUN'
C
2300  FORMAT (1H0/' MEAN ZONE AIR TEMPERATURES: '/
     +10X,5(I2,2X,F10.2))
2302  FORMAT (1H0/' AVERAGE MEAN RADIANT TEMPS: '/
     +10X,5(I2,2X,F10.2))
7080  FORMAT (/, '0BASEMENT INFILTRATION(ACH/HR) MAX, MIN=',2G13.5/
     +,(' ZONE NO.,ZONE INFILTRATION(ACH/HR) MAX,MIN= ',I5,2G13.5/))
 7081 FORMAT(27X,'HOUR= ',I3,'    DAY= ',I4)
 7082 FORMAT(1H0,'ENERGY BALANCES IN EACH ZONE * * * * * * * * *')
 7084 FORMAT(15X,'ZONE= ',I3,' ENERGY BAL= ',G13.5,' PCT')
 7091 FORMAT(1X,' IDBG -- FROM HOUS_II-- = ',I5)
C
      END
