wrf-fire /wrfv2_fire/phys/module_sf_bem.F

Language Fortran 77 Lines 2346
MD5 Hash eba7fd746f42e2808e01b8d95dc369de Estimated Cost $33,835 (why?)
Repository git://github.com/jbeezley/wrf-fire.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
MODULE module_sf_bem
! -----------------------------------------------------------------------
!  Variables and constants used in the BEM module
! -----------------------------------------------------------------------
         
        real emins		!emissivity of the internal walls
        parameter (emins=0.9) 
        real albins	        !albedo of the internal walls
!!      parameter (albins=0.5)
        parameter (albins=0.3)

        real thickwin           !thickness of the window [m] 
        parameter (thickwin=0.006)
        real cswin		!Specific heat of the windows [J/(m3.K)]
        parameter(cswin= 2.268e+06)

        real temp_rat            !power of the A.C. heating/cooling the indoor air [K/s]
        parameter(temp_rat=0.001)

        real hum_rat            !power of the A.C. drying/moistening the indoor air [(Kg/kg)/s]
        parameter(hum_rat=1.e-06)


    CONTAINS

!====6================================================================72
!====6================================================================72	
	
	subroutine BEM(nzcanm,nlev,nhourday,dt,bw,bl,dzlev,            &
                       nwal,nflo,nrof,ngrd,hswalout,gswal,             &
                       hswinout,hsrof,gsrof,                           &
                       latent,sigma,albwal,albwin,albrof,              &
     		       emrof,emwal,emwin,rswal,rlwal,rair,cp,          &
     		       rhoout,tout,humout,press,                       &
     		       rs,rl,dzwal,cswal,kwal,pwin,cop,beta,sw_cond,   &
                       timeon,timeoff,targtemp,gaptemp,targhum,gaphum, &
                       perflo,hsesf,hsequip,dzflo,                     &
     		       csflo,kflo,dzgrd,csgrd,kgrd,dzrof,csrof,        &
     		       krof,tlev,shumlev,twal,twin,tflo,tgrd,trof,     &
     		       hsout,hlout,consump,hsvent,hlvent)


! ---------------------------------------------------------------------
	implicit none
	
! ---------------------------------------------------------------------	
!		       TOP
!	      ---------------------	
!	      !	----------------- !--->roof	(-) : level number	
!	      !	!		! !		rem: the windows are given 
!	      !	!---------------! !                  with respect to the 
!	      !	!---------------! !                  vertical walls-->win(2) 
!	   (n)! !(1)	     (1)!-!(n)
!	      !	!---------------! !		2D vision of the building
!   WEST      ! !-------4-------! !	EAST
!	    I ! ! 1    ilev    2! ! II               ^
!	      !	!-------3--------! !		     !  	
!	      ! !---------------! !--->floor 1	     ! 				
!	      !	!		! !                  !
!	      ! !		! !                  !
!	      !	----------------- !          <--------------(n)  	
!	      ------------------------>ground	------------(1)
!		     BOTTOM
!				i(6)			
!			        i
!                     +---------v-----+ 
!                    /|              /|    3D vision of a room	
!                   / | 4           / |		
!                  /  |            /  |
!                 /   |           /   |
!                /    |          /    |
!               +---------------+     |
!               |  1   |        |  2  |
!               |     +---------|-----+
!       dzlev   |    /          |    /
!               |   /    3      |   /
!               |  /bw          |  /
!               | /             | /  
!               |/              |/
!               +---------------+
!                     ^ bl
!		      i          
!                     i
!		     (5)	
!-----------------------------------------------------------------------


! Input:
! -----	

	real dt				!time step [s]
                                       
        integer nzcanm                  !Maximum number of vertical levels in the urban grid
	integer nlev			!number of floors in the building
	integer nwal                    !number of levels inside the wall
	integer nrof                    !number of levels inside the roof
	integer nflo                    !number of levels inside the floor
	integer ngrd                    !number of levels inside the ground
	real dzlev			!vertical grid resolution [m]			
	real bl				!Building length [m]
	real bw                         !Building width [m]
	
	real albwal			!albedo of the walls 				
	real albwin		 	!albedo of the windows
	real albrof			!albedo of the roof
	
	real emwal 	          	!emissivity of the walls
	
	real emrof			!emissivity of the roof
        real emwin                      !emissivity of the windows

	real pwin                       !window proportion
	real,    intent(in) :: cop      !Coefficient of performance of the A/C systems
	real,    intent(in) :: beta     !Thermal efficiency of the heat exchanger
        integer, intent(in) :: sw_cond  ! Air Conditioning switch
        real,    intent(in) :: timeon   ! Initial local time of A/C systems
        real,    intent(in) :: timeoff  ! Ending local time of A/C systems
        real,    intent(in) :: targtemp ! Target temperature of A/C systems
        real,    intent(in) :: gaptemp  ! Comfort range of indoor temperature
        real,    intent(in) :: targhum  ! Target humidity of A/C systems
        real,    intent(in) :: gaphum   ! Comfort range of specific humidity
        real,    intent(in) :: perflo   ! Peak number of occupants per unit floor area
        real,    intent(in) :: hsesf    ! 
        real,    intent(in) :: hsequip(24) ! 
	
	real cswal(nwal)		!Specific heat of the wall [J/(m3.K)] 
	
	real csflo(nflo)		!Specific heat of the floor [J/(m3.K)]
	real csrof(nrof)		!Specific heat of the roof [J/(m3.K)] 
	real csgrd(ngrd)		!Specific heat of the ground [J/(m3.K)]
	
	real kwal(nwal+1)		!Thermal conductivity in each layers of the walls (face) [W/(m.K)]
	real kflo(nflo+1)		!Thermal diffusivity in each layers of the floors (face) [W/(m.K)]
	real krof(nrof+1)		!Thermal diffusivity in each layers of the roof (face) [W/(m.K)]
	real kgrd(ngrd+1)		!Thermal diffusivity in each layers of the ground (face) [W/(m.K)]
	
	real dzwal(nwal)		!Layer sizes of walls [m]
	real dzflo(nflo)		!Layer sizes of floors [m]
	real dzrof(nrof)		!Layer sizes of roof [m]
	real dzgrd(ngrd)		!Layer sizes of ground [m]
	
	real latent                      !latent heat of evaporation [J/Kg]	


	real rs				!external short wave radiation [W/m2]
	real rl				!external long wave radiation [W/m2]
	real rswal(4,nzcanm)		!short wave radiation reaching the exterior walls [W/m2]
        real rlwal(4,nzcanm)		!long wave radiation reaching the walls [W/m2]	
	real rhoout(nzcanm)		!exterior air density [kg/m3]
	real tout(nzcanm)		!external temperature [K]
	real humout(nzcanm)		!absolute humidity [Kgwater/Kgair]
	real press(nzcanm)		!external air pressure [Pa]
	
	real hswalout(4,nzcanm)	        !outside walls sensible heat flux [W/m2]
	real hswinout(4,nzcanm)	        !outside window sensible heat flux [W/m2]
	real hsrof			!Sensible heat flux at the roof [W/m2]
	
	real rair			!ideal gas constant  [J.kg-1.K-1]
	real sigma			!parameter (wall is not black body) [W/m2.K4]
	real cp				!specific heat of air [J/kg.K]
       
	
!Input-Output
!------------
	real tlev(nzcanm)		!temperature of the floors [K] 
	real shumlev(nzcanm)		!specific humidity of the floor [kg/kg]
	real twal(4,nwal,nzcanm)	!walls temperatures [K]
	real twin(4,nzcanm)		!windows temperatures [K]	
	real tflo(nflo,nzcanm-1)	!floor temperatures [K]
	real tgrd(ngrd)		        !ground temperature [K]
	real trof(nrof)		        !roof temperature [K]
	real hsout(nzcanm)		!sensible heat emitted outside the floor [W]
	real hlout(nzcanm)		!latent heat emitted outside the floor [W]
        real consump(nzcanm)            !Consumption for the a.c. in each floor [W]
	real hsvent(nzcanm)		!sensible heat generated by natural ventilation [W]
	real hlvent(nzcanm)		!latent heat generated by natural ventilation [W] 
        real gsrof                      !heat flux flowing inside the roof [W/m˛]
        real gswal(4,nzcanm)             !heat flux flowing inside the floors [W/m˛]

! Local:
! -----
	integer swwal                   !swich for the physical coefficients calculation
	integer ilev			!index for rooms	
	integer iwal			!index for walls
	integer iflo			!index for floors
	integer ivw			!index for vertical walls
        integer igrd                    !index for ground
        integer irof                    !index for roof 
        real hseqocc(nzcanm)		!sensible heat generated by equipments and occupants [W]
	real hleqocc(nzcanm)		!latent heat generated by occupants [W]
        real hscond(nzcanm)		!sensible heat generated by wall conduction [W]
        real hslev(nzcanm)		!sensible heat flux generated inside the room [W]
        real hllev(nzcanm)		!latent heat flux generatd inside the room [W]
	real surwal(6,nzcanm)	        !Surface of the walls [m2]
	real surwal1D(6)	        !wall surfaces of a generic room [m2]
	real rsint(6)		        !short wave radiation reaching the indoor walls[W/m2]
	real rswalins(6,nzcanm)	        !internal short wave radiation for the building [W/m2]
	real twin1D(4)		        !temperature of windows for a particular room [K]
	real twal_int(6)		!temperature of the first internal layers of a room [K]
	real rlint(6)		        !internal wall long wave radiation [w/m2]
	real rlwalins(6,nzcanm)	        !internal long wave radiation for the building [W/m2]	
	real hrwalout(4,nzcanm)	        !external radiative flux to the walls [W/m2]
	real hrwalins(6,nzcanm)	        !inside radiative flux to the walls [W/m2] 
	real hrwinout(4,nzcanm)	        !external radiative flux to the window [W/m2]
	real hrwinins(4,nzcanm)	        !inside radiative flux to the window [W/m2] 
	real hrrof			!external radiative flux to the roof [W/m2]
	real hs
        real hsneed(nzcanm)		!sensible heat needed by the room [W]
	real hlneed(nzcanm)		!latent heat needed by the room [W]	
        real hswalins(6,nzcanm)	        !inside walls sensible heat flux [W/m2]
	real hswalins1D(6)
	real hswinins(4,nzcanm)	        !inside window sensible heat flux [W/m2]
	real hswinins1D(4)	
	real htot(2)			!total heat flux at the wall [W/m2]
	real twal1D(nwal)
	real tflo1D(nflo)	
        real tgrd1D(ngrd)
        real trof1D(nrof)
	real rswal1D(4)
	real Qb				!Overall heat capacity of the indoor air [J/K]
	real vollev			!volume of the room [m3]
	real rhoint			!density of the internal air [Kg/m3]
	real cpint			!specific heat of the internal air [J/kg.K]
        real humdry                     !specific humidiy of dry air [kg water/kg dry air]
	real radflux                    !Function to compute the total radiation budget
	real consumpbuild               !Energetic consumption for the entire building [KWh/s]
        real hsoutbuild                 !Total sensible heat ejected into the atmosphere[W]
                                        !by the air conditioning system and per building
        real nhourday                   !number of hours from midnight, local time
!--------------------------------------------
!Initialization
!--------------------------------------------

       do ilev=1,nzcanm
          hseqocc(ilev)=0.
          hleqocc(ilev)=0.
          hscond(ilev)=0.
          hslev(ilev)=0.
          hllev(ilev)=0.
       enddo	

!Calculation of the surfaces of the building 
!--------------------------------------------
	
       
	do ivw=1,6
	do ilev=1,nzcanm
	 surwal(ivw,ilev)=1.   !initialisation
	end do
	end do

	do ilev=1,nlev
	  do ivw=1,2
	   surwal(ivw,ilev)=dzlev*bw
	  end do
	  do ivw=3,4
	   surwal(ivw,ilev)=dzlev*bl
	  end do
	  do ivw=5,6 		
	   surwal(ivw,ilev)=bw*bl
	  end do 
	end do


! Calculation of the short wave radiations at the internal walls
! ---------------------------------------------------------------
	

	do ilev=1,nlev	
	  
	  do ivw=1,4
	    rswal1D(ivw)=rswal(ivw,ilev)
	  end do	

	  do ivw=1,6
	    surwal1D(ivw)=surwal(ivw,ilev)
	  end do 		
	
	  call int_rsrad(albwin,albins,pwin,rswal1D,&
                         surwal1D,bw,bl,dzlev,rsint)

	  do ivw=1,6
	    rswalins(ivw,ilev)=rsint(ivw)
	  end do
          
	end do !ilev
	
	 

! Calculation of the long wave radiation at the internal walls
!-------------------------------------------------------------


!Intermediate rooms
       
       if (nlev.gt.2) then
	do ilev=2,nlev-1

	  do ivw=1,4
	    twin1D(ivw)=twin(ivw,ilev)
	    twal_int(ivw)=twal(ivw,1,ilev)
	  end do
	    
	   twal_int(5)=tflo(nflo,ilev-1)
	   twal_int(6)=tflo(1,ilev)		
		 
	   call int_rlrad(emins,emwin,sigma,twal_int,twin1D,&
     			  pwin,bw,bl,dzlev,rlint)
	  
	  
	  do ivw=1,6
	    rlwalins(ivw,ilev)=rlint(ivw)
	  end do
	    
	end do	!ilev 
      end if	 
	

      if (nlev.ne.1) then  

!bottom room

	  do ivw=1,4
	    twin1D(ivw)=twin(ivw,1)
	    twal_int(ivw)=twal(ivw,1,1)
	  end do
	  
	  twal_int(5)=tgrd(ngrd)
	  twal_int(6)=tflo(1,1)		
	  
	  						  	   
	   call int_rlrad(emins,emwin,sigma,twal_int,twin1D,&
     			  pwin,bw,bl,dzlev,rlint)
	  
	  do ivw=1,6
	    rlwalins(ivw,1)=rlint(ivw)
	  end do	  
            
!top room
	 
	  do ivw=1,4
	    twin1D(ivw)=twin(ivw,nlev)
	    twal_int(ivw)=twal(ivw,1,nlev)
	  end do
	  
	  twal_int(5)=tflo(nflo,nlev-1)
	  twal_int(6)=trof(1)		
	  
					
	   call int_rlrad(emins,emwin,sigma,twal_int,twin1D,&
     			  pwin,bw,bl,dzlev,rlint)
	  
	  do ivw=1,6
	    rlwalins(ivw,nlev)=rlint(ivw)
	  end do
	  
      else   !Top <---> Bottom
	  
	  do ivw=1,4
	    twin1D(ivw)=twin(ivw,1)
	    twal_int(ivw)=twal(ivw,1,1)
	  end do
	  
	  twal_int(5)=tgrd(ngrd)      
      	  twal_int(6)=trof(1)
	  
	  call int_rlrad(emins,emwin,sigma,twal_int,twin1D, &
     			 pwin,bw,bl,dzlev,rlint)
     	  
	  do ivw=1,6
	    rlwalins(ivw,1)=rlint(ivw)
	  end do
	
      end if  
	

! Calculation of the radiative fluxes
! -----------------------------------

!External vertical walls and windows

        do ilev=1,nlev
	 do ivw=1,4	 
	 call radfluxs(radflux,albwal,rswal(ivw,ilev),     &
     	                    emwal,rlwal(ivw,ilev),sigma,   &
                            twal(ivw,nwal,ilev))
	
         hrwalout(ivw,ilev)=radflux
      	 						
	 hrwinout(ivw,ilev)=emwin*rlwal(ivw,ilev)- &
     	                    emwin*sigma*(twin(ivw,ilev)**4)
	 
	 
	 end do ! ivw
	end do  ! ilev
	
!Roof

        call radfluxs(radflux,albrof,rs,emrof,rl,sigma,trof(nrof))

        hrrof=radflux

!Internal walls for intermediate rooms

      if(nlev.gt.2) then
       
	do ilev=2,nlev-1
	 do ivw=1,4
         
	 call radfluxs(radflux,albins,rswalins(ivw,ilev),     &
     	                    emins,rlwalins(ivw,ilev),sigma,   &
                            twal(ivw,1,ilev))
	 
	 hrwalins(ivw,ilev)=radflux

	 end do !ivw						

	 call radfluxs(radflux,albins,rswalins(5,ilev), &
     	                      emins,rlwalins(5,ilev),sigma,&
                              tflo(nflo,ilev-1))

         hrwalins(5,ilev)=radflux

         call radfluxs(radflux,albins,rswalins(6,ilev), &
                              emins,rlwalins(6,ilev),sigma,&
                              tflo(1,ilev))
         hrwalins(6,ilev)=radflux

       end do !ilev

      end if 	


!Internal walls for the bottom and the top room	 
!
      if (nlev.ne.1) then 

!bottom floor

	 do ivw=1,4

	    call radfluxs(radflux,albins,rswalins(ivw,1),  &
     	                    emins,rlwalins(ivw,1),sigma,   &
                            twal(ivw,1,1))
	
            hrwalins(ivw,1)=radflux

	 end do
	
	
	  call radfluxs(radflux,albins,rswalins(5,1),&
                           emins,rlwalins(5,1),sigma,&    !bottom
                           tgrd(ngrd))

          hrwalins(5,1)=radflux

	   
          call radfluxs(radflux,albins,rswalins(6,1),&
     	                   emins,rlwalins(6,1),sigma,&
                           tflo(1,1))  
	 
          hrwalins(6,1)=radflux

!roof floor

         do ivw=1,4
   
          call radfluxs(radflux,albins,rswalins(ivw,nlev),     &
     	                        emins,rlwalins(ivw,nlev),sigma,&
                                twal(ivw,1,nlev))

	  hrwalins(ivw,nlev)=radflux

	 end do                                          !top

	
         call radfluxs(radflux,albins,rswalins(5,nlev),    &
     	                      emins,rlwalins(5,nlev),sigma,&
                              tflo(nflo,nlev-1))

         hrwalins(5,nlev)=radflux

	 call radfluxs(radflux,albins,rswalins(6,nlev), &
                              emins,rlwalins(6,nlev),sigma,&
                              trof(1))

         hrwalins(6,nlev)=radflux
      
      else       ! Top <---> Bottom room
      
	 do ivw=1,4

	    call radfluxs(radflux,albins,rswalins(ivw,1),&
     	                    emins,rlwalins(ivw,1),sigma, &
                            twal(ivw,1,1))

            hrwalins(ivw,1)=radflux

         end do
     
     	    call radfluxs(radflux,albins,rswalins(5,1),&
                           emins,rlwalins(5,1),sigma,  &
                           tgrd(ngrd))

            hrwalins(5,1)=radflux
     
     	    call radfluxs(radflux,albins,rswalins(6,nlev),     &
                                  emins,rlwalins(6,nlev),sigma,&
                                  trof(1))
            hrwalins(6,1)=radflux

      end if
      
		
!Windows

	 do ilev=1,nlev
	  do ivw=1,4
	     hrwinins(ivw,ilev)=emwin*rlwalins(ivw,ilev)-    &
                                emwin*sigma*(twin(ivw,ilev)**4)
	  end do
	 end do
	
		
! Calculation of the sensible heat fluxes
! ---------------------------------------

!Vertical fluxes for walls
	
	do ilev=1,nlev
         do ivw=1,4
		
               call hsinsflux (2,2,tlev(ilev),twal(ivw,1,ilev),hs)		
	       
               hswalins(ivw,ilev)=hs 
         
         end do ! ivw     
        end do ! ilev
       
      
!Vertical fluxes for windows

	do ilev=1,nlev

         do ivw=1,4
	 
	       call hsinsflux (2,1,tlev(ilev),twin(ivw,ilev),hs)
	       
               hswinins(ivw,ilev)=hs 
			
         end do ! ivw	
	
	end do !ilev      

!Horizontal fluxes
       
      if (nlev.gt.2) then
       
        do ilev=2,nlev-1
                
	       call hsinsflux (1,2,tlev(ilev),tflo(nflo,ilev-1),hs)

	       hswalins(5,ilev)=hs
            
	       call hsinsflux (1,2,tlev(ilev),tflo(1,ilev),hs)

	       hswalins(6,ilev)=hs

        end do ! ilev
       
      end if
       
      if (nlev.ne.1) then
       
       	        call hsinsflux (1,2,tlev(1),tgrd(ngrd),hs)

		hswalins(5,1)=hs				!Bottom room
		
		call hsinsflux (1,2,tlev(1),tflo(1,1),hs)

		hswalins(6,1)=hs				
	 
       	        call hsinsflux (1,2,tlev(nlev),tflo(nflo,nlev-1),hs)

		hswalins(5,nlev)=hs			        !Top room

		call hsinsflux (1,2,tlev(nlev),trof(1),hs)

		hswalins(6,nlev)=hs	       
      
      else  ! Bottom<--->Top 
      
                call hsinsflux (1,2,tlev(1),tgrd(ngrd),hs)
		
		hswalins(5,1)=hs
		
		call hsinsflux (1,2,tlev(nlev),trof(1),hs)
		
		hswalins(6,nlev)=hs
      
      end if


!Calculation of the temperature for the different surfaces 
! --------------------------------------------------------

! Vertical walls	
        
       swwal=1
       do ilev=1,nlev
        do ivw=1,4  

	   htot(1)=hswalins(ivw,ilev)+hrwalins(ivw,ilev)	
           htot(2)=hswalout(ivw,ilev)+hrwalout(ivw,ilev)
           gswal(ivw,ilev)=htot(2)

	   do iwal=1,nwal
	      twal1D(iwal)=twal(ivw,iwal,ilev)
	   end do
	  
	   call wall(swwal,nwal,dt,dzwal,kwal,cswal,htot,twal1D)
	
	   do iwal=1,nwal
	      twal(ivw,iwal,ilev)=twal1D(iwal)
	   end do
           
	end do ! ivw
       end do ! ilev
       
! Windows

       do ilev=1,nlev
        do ivw=1,4
       
         htot(1)=hswinins(ivw,ilev)+hrwinins(ivw,ilev)	
         htot(2)=hswinout(ivw,ilev)+hrwinout(ivw,ilev)	

         twin(ivw,ilev)=twin(ivw,ilev)+(dt/(cswin*thickwin))* &
                        (htot(1)+htot(2))
	
	end do ! ivw
       end do ! ilev   

! Horizontal floors


      if (nlev.gt.1) then
       swwal=1
       do ilev=1,nlev-1
 
          htot(1)=hrwalins(6,ilev)+hswalins(6,ilev)
          htot(2)=hrwalins(5,ilev+1)+hswalins(5,ilev+1)	

	  do iflo=1,nflo
	     tflo1D(iflo)=tflo(iflo,ilev)
	  end do
        
	  call wall(swwal,nflo,dt,dzflo,kflo,csflo,htot,tflo1D)
	
	 do iflo=1,nflo
	    tflo(iflo,ilev)=tflo1D(iflo)
	 end do

       end do ! ilev
      end if 
        

! Ground 	
        
	swwal=1

	htot(1)=0.	!Diriclet b.c. at the internal boundary    
	htot(2)=hswalins(5,1)+hrwalins(5,1)   
   
        do igrd=1,ngrd
           tgrd1D(igrd)=tgrd(igrd)
        end do

         call wall(swwal,ngrd,dt,dzgrd,kgrd,csgrd,htot,tgrd1D)

        do igrd=1,ngrd
           tgrd(igrd)=tgrd1D(igrd)
        end do

        
! Roof
        
      swwal=1    

      htot(1)=hswalins(6,nlev)+hrwalins(6,nlev)     	
      htot(2)=hsrof+hrrof     
      gsrof=htot(2)

      do irof=1,nrof
         trof1D(irof)=trof(irof)
      end do     
      
      call wall(swwal,nrof,dt,dzrof,krof,csrof,htot,trof1D)
 
      do irof=1,nrof
         trof(irof)=trof1D(irof)
      end do
      
! Calculation of the heat fluxes and of the temperature of the rooms
! ------------------------------------------------------------------

 	do ilev=1,nlev
	  	  
	 !Calculation of the heat generated by equipments and occupants
	 
	 call fluxeqocc(nhourday,bw,bl,perflo,hsesf,hsequip,hseqocc(ilev),hleqocc(ilev))

     	 !Calculation of the heat generated by natural ventilation
	
	  vollev=bw*bl*dzlev
          humdry=shumlev(ilev)/(1.-shumlev(ilev))
	  rhoint=(press(ilev))/(rair*(1.+0.61*humdry)*tlev(ilev))
	  cpint=cp*(1.+0.84*humdry)
          
 	  
	  call fluxvent(cpint,rhoint,vollev,tlev(ilev),tout(ilev),     &
                        latent,humout(ilev),rhoout(ilev),shumlev(ilev),&
                        beta,hsvent(ilev),hlvent(ilev))
	      
         !Calculation of the heat generated by conduction
	  
	   do iwal=1,6
	     hswalins1D(iwal)=hswalins(iwal,ilev)
	     surwal1D(iwal)=surwal(iwal,ilev)
	  end do
	  
	   do iwal=1,4
	     hswinins1D(iwal)=hswinins(iwal,ilev)
	   end do
	
	  call fluxcond(hswalins1D,hswinins1D,surwal1D,pwin,&
                        hscond(ilev))

	!Calculation of the heat generated inside the room
 	
	  call fluxroo(hseqocc(ilev),hleqocc(ilev),hsvent(ilev), &
               hlvent(ilev),hscond(ilev),hslev(ilev),hllev(ilev))

	  
	!Evolution of the temperature and of the specific humidity 

	  Qb=rhoint*cpint*vollev

        ! temperature regulation

          call regtemp(sw_cond,nhourday,dt,Qb,hslev(ilev),       &
                       tlev(ilev),timeon,timeoff,targtemp,gaptemp,hsneed(ilev))

        ! humidity regulation 

	  call reghum(sw_cond,nhourday,dt,vollev,rhoint,latent, &
                      hllev(ilev),shumlev(ilev),timeon,timeoff,&
                      targhum,gaphum,hlneed(ilev))
!
!performance of the air conditioning system for the test
!	
	        
          call air_cond(hsneed(ilev),hlneed(ilev),dt, &
                        hsout(ilev),hlout(ilev),consump(ilev), cop)
    	         	
 	  tlev(ilev)=tlev(ilev)+(dt/Qb)*(hslev(ilev)-hsneed(ilev))
          	  	  
	  shumlev(ilev)=shumlev(ilev)+(dt/(vollev*rhoint*latent))* &
                        (hllev(ilev)-hlneed(ilev))
           
	end do !ilev
        
        call consump_total(nzcanm,nlev,consumpbuild,hsoutbuild, &
                           hsout,consump)
                
      return
      end subroutine BEM

!====6=8===============================================================72
!====6=8===============================================================72

	subroutine wall(swwall,nz,dt,dz,k,cs,flux,temp)
	
!______________________________________________________________________

!The aim of this subroutine is to solve the 1D heat fiffusion equation
!for roof, walls and streets:
!
!	dT/dt=d/dz[K*dT/dz] where:
!
!	-T is the surface temperature(wall, street, roof)
!      	-Kz is the heat diffusivity inside the material.
!
!The resolution is done implicitly with a FV discretisation along the
!different layers of the material:

!	____________________________
!     n             *
!                   *
!                   *
!     	____________________________
!    i+2
!              	    I+1                 
!	____________________________        
!    i+1        
!                    I                ==>   [T(I,n+1)-T(I,n)]/DT= 
!	____________________________        [F(i+1)-F(i)]/DZI
!    i
!                   I-1               ==> A*T(n+1)=B where:
!	____________________________         
!   i-1              *                   * A is a TRIDIAGONAL matrix.
!                    *                   * B=T(n)+S takes into account the sources.
!                    *
!     1	____________________________

!________________________________________________________________

	implicit none
		
!Input:
!-----
	integer nz		!Number of layers inside the material
	real dt			!Time step
	real dz(nz)		!Layer sizes [m]
	real cs(nz)		!Specific heat of the material [J/(m3.K)] 
	real k(nz+1)		!Thermal conductivity in each layers (face) [W/(m.K)]
	real flux(2)		!Internal and external flux terms.

!Input-Output:
!-------------

	integer swwall          !swich for the physical coefficients calculation
	real temp(nz)		!Temperature at each layer

!Local:
!-----	

      real a(-1:1,nz)          !  a(-1,*) lower diagonal      A(i,i-1)
                               !  a(0,*)  principal diagonal  A(i,i)
                               !  a(1,*)  upper diagonal      A(i,i+1).
      
      real b(nz)	       !Coefficients of the second term.	
      real k1(20)
      real k2(20)
      real kc(20)
      save k1,k2,kc
      integer iz
        	
!________________________________________________________________
!
!Calculation of the coefficients
	
	if (swwall.eq.1) then
	
           if (nz.gt.20) then
              write(*,*) 'number of layers in the walls/roofs too big ',nz
              write(*,*) 'please decrease under of',20
              stop
           endif

	   call wall_coeff(nz,dt,dz,cs,k,k1,k2,kc)
	   swwall=0

	end if
 	
!Computation of the first value (iz=1) of A and B:
	
		 a(-1,1)=0.
		 a(0,1)=1+k2(1)
		 a(1,1)=-k2(1)

                 b(1)=temp(1)+flux(1)*kc(1)

!!
!!We can fixed the internal temperature	
!!
!!		 a(-1,1)=0.
!!		 a(0,1)=1
!!		 a(1,1)=0.		 	 
!!		 
!!		 b(1)=temp(1)
!!
!Computation of the internal values (iz=2,...,n-1) of A and B:

	do iz=2,nz-1
		a(-1,iz)=-k1(iz)
		a(0,iz)=1+k1(iz)+k2(iz)
     		a(1,iz)=-k2(iz)
		b(iz)=temp(iz)
	end do		

!Computation of the external value (iz=n) of A and B:
	
		a(-1,nz)=-k1(nz)
		a(0,nz)=1+k1(nz)
		a(1,nz)=0.
	
		b(nz)=temp(nz)+flux(2)*kc(nz)

!Resolution of the system A*T(n+1)=B

	call tridia(nz,a,b,temp)

        return
	end  subroutine wall	

!====6=8===============================================================72
!====6=8===============================================================72

	subroutine wall_coeff(nz,dt,dz,cs,k,k1,k2,kc)

	implicit none
	
!---------------------------------------------------------------------
!Input
!-----
	integer nz		!Number of layers inside the material
	real dt			!Time step
	real dz(nz)		!Layer sizes [m]
	real cs(nz)		!Specific heat of the material [J/(m3.K)] 
	real k(nz+1)		!Thermal diffusivity in each layers (face) [W/(m.K)]


!Input-Output
!------------

	real flux(2)		!Internal and external flux terms.


!Output
!------
        real k1(20)
        real k2(20)
        real kc(20)

!Local
!-----	
	integer iz
	real kf(nz)

!------------------------------------------------------------------

	do iz=2,nz
	 kc(iz)=dt/(dz(iz)*cs(iz))
	 kf(iz)=2*k(iz)/(dz(iz)+dz(iz-1))
	end do 
	
	kc(1)=dt/(dz(1)*cs(1))
        kf(1)=2*k(1)/(dz(1))

	do iz=1,nz
	 k1(iz)=kc(iz)*kf(iz)
	end do
	
	do iz=1,nz-1
	 k2(iz)=kc(iz)*kf(iz+1)*cs(iz)/cs(iz+1)
	end do

	return
	end subroutine wall_coeff

!====6=8===============================================================72  
!====6=8===============================================================72
	subroutine hsinsflux(swsurf,swwin,tin,tw,hsins)	
	
	implicit none
	
! --------------------------------------------------------------------
! This routine computes the internal sensible heat flux.
! The swsurf, makes rhe difference between a vertical and a 
! horizontal surface. 
! The values of the heat conduction coefficients hc are obtained from the book
! "Energy Simulation in Building Design". J.A. Clarke. 
! Adam Hilger, Bristol, 362 pp.
! --------------------------------------------------------------------
!Input
!----
	integer swsurf  !swich for the type of surface (horizontal/vertical) 
        integer swwin   !swich for the type of surface (window/wall)
	real tin	!Inside temperature [K]
	real tw		!Internal wall temperature [K]  	


!Output
!------
	real hsins	!internal sensible heat flux [W/m2]
!Local
!-----
	real hc		!heat conduction coefficient [W/°C.m2]
!--------------------------------------------------------------------

	if (swsurf.eq.2) then	!vertical surface
         if (swwin.eq.1) then
            hc=5.678*0.99        !window surface (smooth surface)
         else
            hc=5.678*1.09        !wall surface (rough surface)
         endif
	 hsins=hc*(tin-tw)	
	endif
	
	if (swsurf.eq.1)  then   !horizontal surface
         if (swwin.eq.1) then
           hc=5.678*0.99        !window surface (smooth surface)
         else
           hc=5.678*1.09        !wall surface (rough surface)
         endif
         hsins=hc*(tin-tw)
        endif 		

	return
	end subroutine hsinsflux
!====6=8===============================================================72  
!====6=8===============================================================72

	subroutine int_rsrad(albwin,albwal,pwin,rswal,&
                             surwal,bw,bl,zw,rsint)
	
! ------------------------------------------------------------------
	implicit none
! ------------------------------------------------------------------	

!Input
!-----
	real albwin		!albedo of the windows
	real albwal		!albedo of the internal wall					
	real rswal(4)		!incoming short wave radiation [W/m2]
        real surwal(6) 		!surface of the indoor walls [m2]
	real bw,bl		!width of the walls [m]
	real zw			!height of the wall [m]
	real pwin               !window proportion
	
!Output
!------
	real rsint(6)		!internal walls short wave radiation [W/m2]

!Local
!-----
	real transmit   !transmittance of the direct/diffused radiation
        real rstr	!solar radiation transmitted through the windows	
        real surtotwal  !total indoor surface of the walls in the room
	integer iw
	real b(6)	!second member for the system
	real a(6,6)	!matrix for the system

!-------------------------------------------------------------------


! Calculation of the solar radiation transmitted through windows
                    
            rstr = 0.
            do iw=1,4
               transmit=1.-albwin
               rstr = rstr+(surwal(iw)*pwin)*(transmit*rswal(iw))
            enddo

!We suppose that the radiation is spread isotropically within the
!room when it passes through the windows, so the flux [W/m˛] in every 
!wall is:

            surtotwal=0.
            do iw=1,6
               surtotwal=surtotwal+surwal(iw)
            enddo
            
            rstr=rstr/surtotwal
 		
!Computation of the short wave radiation reaching the internal walls
	
	    call algebra_short(rstr,albwal,albwin,bw,bl,zw,pwin,a,b)
		
	    call gaussjbem(a,6,b,6)
	
            do iw=1,6
               rsint(iw)=b(iw)
            enddo

	    return
	    end subroutine int_rsrad

!====6=8===============================================================72  
!====6=8===============================================================72

	subroutine int_rlrad(emwal,emwin,sigma,twal_int,twin,&
     			     pwin,bw,bl,zw,rlint)
	
! ------------------------------------------------------------------
	implicit none
! ------------------------------------------------------------------	

!Input
!-----

	real emwal	!emissivity of the internal walls
	real emwin	!emissivity of the window
	real sigma	!Stefan-Boltzmann constant [W/m2.K4]
	real twal_int(6)!temperature of the first internal layers of a room [K]
	real twin(4)	!temperature of the windows [K]
	real bw		!width of the wall
	real bl		!length of the wall
	real zw		!height of the wall
	real pwin       !window proportion	

!Output
!------

	real rlint(6)	!internal walls long wave radiation [W/m2]

!Local
!------
	
	real b(6)	!second member vector for the system
	real a(6,6)	!matrix for the system
        integer iw
!----------------------------------------------------------------

!Compute the long wave radiation reachs the internal walls

	call algebra_long(emwal,emwin,sigma,twal_int,twin,pwin,&
                          bw,bl,zw,a,b)
  			  
	call gaussjbem(a,6,b,6)

        do iw=1,6
           rlint(iw)=b(iw)
        enddo
            
	return
	end subroutine int_rlrad	

!====6=8===============================================================72  
!====6=8===============================================================72

	subroutine algebra_short(rstr,albwal,albwin,aw,bw,zw,pwin,a,b)
    
!--------------------------------------------------------------------
!This routine calculates the algebraic system that will be solved for 
!the computation of the total shortwave radiation that reachs every 
!indoor wall in a floor.
!Write the matrix system ax=b to solve
!
!     -Rs(1)+a(1,2)Rs(2)+.................+a(1,6)Rs(6)=-Rs=b(1)
!a(2,1)Rs(1)-      Rs(2)+.................+a(2,6)Rs(6)=-Rs=b(2)
!a(3,1)Rs(1)+a(3,2)Rs(3)-Rs(3)+...........+a(3,6)Rs(6)=-Rs=b(3)
!a(4,1)Rs(1)+.................-Rs(4)+.....+a(4,6)Rs(6)=-Rs=b(4)
!a(5,1)Rs(1)+.......................-Rs(5)+a(5,6)Rs(6)=-Rs=b(5)
!a(6,1)Rs(1)+....................................-R(6)=-Rs=b(6)
!
!This version suppose the albedo of the indoor walls is the same.
!--------------------------------------------------------------------
	implicit none
!--------------------------------------------------------------------

!Input
!-----
	real rstr	!solar radiation transmitted through the windows		
	real albwal	!albedo of the internal walls
	real albwin	!albedo of the windows.
	real bw		!length of the wall
	real aw		!width of the wall
	real zw		!height of the wall
	real fprl_int	!view factor
	real fnrm_int	!view factor
	real pwin       !window proportion
!Output
!------
	real a(6,6)		!Matrix for the system
	real b(6)		!Second member for the system
!Local
!-----
	integer iw,jw	
	real albm               !averaged albedo
!----------------------------------------------------------------

!Initialise the variables

	do iw=1,6
           b(iw)= 0.
	  do jw=1,6
           a(iw,jw)= 0. 
          enddo
        enddo 

!Calculation of the second member b

	do iw=1,6
	 b(iw)=-rstr
	end do	

!Calculation of the averaged albedo

	albm=pwin*albwin+(1-pwin)*albwal
	
!Calculation of the matrix a

            a(1,1)=-1.

            call fprl_ints(fprl_int,aw/bw,zw/bw)

            a(1,2)=albm*fprl_int

            call fnrm_ints(fnrm_int,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))

            a(1,3)=albm*(bw/aw)*fnrm_int

            a(1,4)=a(1,3)

            call fnrm_ints(fnrm_int,zw/aw,bw/aw,(bw*bw+zw*zw)/(aw*aw))

            a(1,5)=albwal*(bw/zw)*fnrm_int

            a(1,6)=a(1,5)


            a(2,1)=a(1,2)
            a(2,2)=-1.
            a(2,3)=a(1,3)
            a(2,4)=a(1,4)
            a(2,5)=a(1,5)
            a(2,6)=a(1,6)
 
	
            call fnrm_ints(fnrm_int,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))

            a(3,1)=albm*(aw/bw)*fnrm_int
	    a(3,2)=a(3,1)
	    a(3,3)=-1.

            call fprl_ints(fprl_int,zw/aw,bw/aw)

	    a(3,4)=albm*fprl_int

            call fnrm_ints(fnrm_int,zw/bw,aw/bw,(aw*aw+zw*zw)/(bw*bw))

	    a(3,5)=albwal*(aw/zw)*fnrm_int
            a(3,6)=a(3,5)
	

            a(4,1)=a(3,1)
            a(4,2)=a(3,2)
            a(4,3)=a(3,4)
            a(4,4)=-1.
            a(4,5)=a(3,5)
            a(4,6)=a(3,6)

            call fnrm_ints(fnrm_int,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw)) 

            a(5,1)=albm*(zw/bw)*fnrm_int
                   
            a(5,2)=a(5,1)

            call fnrm_ints(fnrm_int,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))

            a(5,3)=albm*(zw/aw)*fnrm_int
           	   
            a(5,4)=a(5,3)
            a(5,5)=-1.

            call fprl_ints(fprl_int,aw/zw,bw/zw)

            a(5,6)=albwal*fprl_int


            a(6,1)=a(5,1)
            a(6,2)=a(5,2)
            a(6,3)=a(5,3)
            a(6,4)=a(5,4)
            a(6,5)=a(5,6)
            a(6,6)=-1.
	
	return
	end subroutine algebra_short

!====6=8===============================================================72  
!====6=8===============================================================72

	subroutine algebra_long(emwal,emwin,sigma,twalint,twinint,&
     				pwin,aw,bw,zw,a,b)

!--------------------------------------------------------------------
!This routine computes the algebraic system that will be solved to 
!compute the longwave radiation that reachs the indoor
!walls in a floor. 
!Write the matrix system ax=b to solve
!
!a(1,1)Rl(1)+.............................+Rl(6)=b(1)
!a(2,1)Rl(1)+.................+Rl(5)+a(2,6)Rl(6)=b(2)
!a(3,1)Rl(1)+.....+Rl(3)+...........+a(3,6)Rl(6)=b(3)
!a(4,1)Rl(1)+...........+Rl(4)+.....+a(4,6)Rl(6)=b(4)
!      Rl(1)+.......................+a(5,6)Rl(6)=b(5)
!a(6,1)Rl(1)+Rl(2)+.................+a(6,6)Rl(6)=b(6)
!
!--------------------------------------------------------------------
        implicit none 
	
!--------------------------------------------------------------------

!Input
!-----

	real pwin       !window proportion 
	real emwal	!emissivity of the internal walls
	real emwin	!emissivity of the window
	real sigma	!Stefan-Boltzmann constant [W/m2.K4]
	real twalint(6) !temperature of the first internal layers of a room [K]
	real twinint(4)	!temperature of the windows [K]
	real aw		!width of the wall
	real bw		!length of the wall
	real zw		!height of the wall
	real fprl_int	!view factor
	real fnrm_int	!view factor	
        real fnrm_intx	!view factor
        real fnrm_inty	!view factor

!Output
!------
	real b(6)	!second member vector for the system
	real a(6,6)	!matrix for the system
!Local
!-----
	integer iw,jw
	real b_wall(6)	
	real b_wind(6)
	real emwal_av		!averadge emissivity of the wall
	real emwin_av		!averadge emissivity of the window
	real em_av		!averadge emissivity
        real twal_int(6)        !twalint 
	real twin(4)   		!twinint 
!------------------------------------------------------------------

!Initialise the variables
!-------------------------

	 do iw=1,6
            b(iw)= 0.
            b_wall(iw)=0.
            b_wind(iw)=0.
          do jw=1,6
            a(iw,jw)= 0. 
          enddo
         enddo

         do iw=1,6
            twal_int(iw)=twalint(iw)
         enddo

         do iw=1,4
            twin(iw)=twinint(iw)
         enddo
	 
!Calculation of the averadge emissivities
!-----------------------------------------

	emwal_av=(1-pwin)*emwal
	emwin_av=pwin*emwin
	em_av=emwal_av+emwin_av
	
!Calculation of the second term for the walls
!-------------------------------------------

            call fprl_ints(fprl_int,aw/zw,bw/zw)
            call fnrm_ints(fnrm_intx,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))
            call fnrm_ints(fnrm_inty,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw))

            b_wall(1)=(emwal*sigma*(twal_int(5)**4)*           &
     	         fprl_int)+                                    &
                 (sigma*(emwal_av*(twal_int(3)**4)+            &
                  emwal_av*(twal_int(4)**4))*                  &
                 (zw/aw)*fnrm_intx)+                           &
                 (sigma*(emwal_av*(twal_int(1)**4)+            &
                  emwal_av*(twal_int(2)**4))*                  & 
                 (zw/bw)*fnrm_inty)

            call fprl_ints(fprl_int,aw/zw,bw/zw)
            call fnrm_ints(fnrm_intx,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))
            call fnrm_ints(fnrm_inty,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw))
	
            b_wall(2)=(emwal*sigma*(twal_int(6)**4)*           &
              	   fprl_int)+                                  &
                  (sigma*(emwal_av*(twal_int(3)**4)+           &
                  emwal_av*(twal_int(4)**4))*                  & 
                 (zw/aw)*fnrm_intx)+                           &
                 (sigma*(emwal_av*(twal_int(1)**4)+            &
                 emwal_av*(twal_int(2)**4))*                   &
                 (zw/bw)*fnrm_inty)

            call fprl_ints(fprl_int,zw/aw,bw/aw)
            call fnrm_ints(fnrm_intx,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))
            call fnrm_ints(fnrm_inty,zw/bw,aw/bw,(aw*aw+zw*zw)/(bw*bw))

            b_wall(3)=(emwal_av*sigma*(twal_int(4)**4)*        &
        	  fprl_int)+                                   &
                 (sigma*(emwal_av*(twal_int(2)**4)+            &
                  emwal_av*(twal_int(1)**4))*                  &
                 (aw/bw)*fnrm_intx)+                           &
                 (sigma*(emwal*(twal_int(5)**4)+               &
                  emwal*(twal_int(6)**4))*                     &
                 (aw/zw)*fnrm_inty)

            call fprl_ints(fprl_int,zw/aw,bw/aw)
            call fnrm_ints(fnrm_intx,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))
            call fnrm_ints(fnrm_inty,zw/bw,aw/bw,(aw*aw+zw*zw)/(bw*bw))

            b_wall(4)=(emwal_av*sigma*(twal_int(3)**4)*        &
     	          fprl_int)+                                   &
                 (sigma*(emwal_av*(twal_int(2)**4)+            &
                  emwal_av*(twal_int(1)**4))*                  &
                 (aw/bw)*fnrm_intx)+                           &
                 (sigma*(emwal*(twal_int(5)**4)+               &
                  emwal*(twal_int(6)**4))*                     &
                 (aw/zw)*fnrm_inty)

            call fprl_ints(fprl_int,aw/bw,zw/bw)
            call fnrm_ints(fnrm_intx,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))
            call fnrm_ints(fnrm_inty,zw/aw,bw/aw,(bw*bw+zw*zw)/(aw*aw))
          
            b_wall(5)=(emwal_av*sigma*(twal_int(2)**4)*        &
     	          fprl_int)+                                   &
                 (sigma*(emwal_av*(twal_int(3)**4)+            &
                  emwal_av*(twal_int(4)**4))*                  &
                 (bw/aw)*fnrm_intx)+                           &
                 (sigma*(emwal*(twal_int(5)**4)+               &
                  emwal*(twal_int(6)**4))*                     &
                 (bw/zw)*fnrm_inty)

            call fprl_ints(fprl_int,aw/bw,zw/bw)
            call fnrm_ints(fnrm_intx,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))
            call fnrm_ints(fnrm_inty,zw/aw,bw/aw,(bw*bw+zw*zw)/(aw*aw))

            b_wall(6)=(emwal_av*sigma*(twal_int(1)**4)*        &
     	         fprl_int)+                                    &
                 (sigma*(emwal_av*(twal_int(3)**4)+            &
                  emwal_av*(twal_int(4)**4))*                  &
                 (bw/aw)*fnrm_intx)+                           &
                 (sigma*(emwal*(twal_int(5)**4)+               &
                 emwal*(twal_int(6)**4))*                      &
                 (bw/zw)*fnrm_inty)
	
!Calculation of the second term for the windows
!---------------------------------------------
            call fnrm_ints(fnrm_intx,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))
            call fnrm_ints(fnrm_inty,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw))

            b_wind(1)=(sigma*(emwin_av*(twin(3)**4)+          &
                  emwin_av*(twin(4)**4))*                     &
                 (zw/aw)*fnrm_intx)+                          &
                 (sigma*(emwin_av*(twin(1)**4)+               &
                  emwin_av*(twin(2)**4))*                     &
                 (zw/bw)*fnrm_inty)

            call fnrm_ints(fnrm_intx,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))
            call fnrm_ints(fnrm_inty,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw))

            b_wind(2)=(sigma*(emwin_av*(twin(3)**4)+          &
                  emwin_av*(twin(4)**4))*                     &
                 (zw/aw)*fnrm_intx)+                          &
                 (sigma*(emwin_av*(twin(1)**4)+               &
                  emwin_av*(twin(2)**4))*                     &
                 (zw/bw)*fnrm_inty)

            call fprl_ints(fprl_int,zw/aw,bw/aw)
            call fnrm_ints(fnrm_int,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))
          
            b_wind(3)=emwin_av*sigma*(twin(4)**4)*            &
                 fprl_int+(sigma*(emwin_av*                   &
                 (twin(2)**4)+emwin_av*(twin(1)**4))*         &
                 (aw/bw)*fnrm_int)

            call fprl_ints(fprl_int,zw/aw,bw/aw)
            call fnrm_ints(fnrm_int,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))

            b_wind(4)=emwin_av*sigma*(twin(3)**4)*            &
                 fprl_int+(sigma*(emwin_av*                   &
                  (twin(2)**4)+emwin_av*(twin(1)**4))*        &
                 (aw/bw)*fnrm_int)

            call fprl_ints(fprl_int,aw/bw,zw/bw)
            call fnrm_ints(fnrm_int,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))
          
            b_wind(5)=emwin_av*sigma*(twin(2)**4)*            &
                 fprl_int+(sigma*(emwin_av*                   &
                 (twin(3)**4)+emwin_av*(twin(4)**4))*         &
                 (bw/aw)*fnrm_int)
 
            call fprl_ints(fprl_int,aw/bw,zw/bw)
            call fnrm_ints(fnrm_int,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))

            b_wind(6)=emwin_av*sigma*(twin(1)**4)*            &
                 fprl_int+(sigma*(emwin_av*                   &
                 (twin(3)**4)+emwin_av*(twin(4)**4))*         &
                 (bw/aw)*fnrm_int)
     
!Calculation of the total b term
!-------------------------------

	do iw=1,6
	 b(iw)=b_wall(iw)+b_wind(iw)
	end do     


!Calculation of the matrix of the system
!----------------------------------------

         call fnrm_ints(fnrm_int,bw/aw,zw/aw,(bw*bw+zw*zw)/(aw*aw))         

         a(1,1)=(em_av-1.)*(zw/bw)*fnrm_int
     	        
         a(1,2)=a(1,1)

         call fnrm_ints(fnrm_int,aw/bw,zw/bw,(aw*aw+zw*zw)/(bw*bw))

         a(1,3)=(em_av-1.)*(zw/aw)*fnrm_int
         	 
         a(1,4)=a(1,3)

         call fprl_ints(fprl_int,aw/zw,bw/zw)

         a(1,5)=(emwal-1.)*fprl_int
         a(1,6)=1.

         a(2,1)=a(1,1)
         a(2,2)=a(1,2)
         a(2,3)=a(1,3)
         a(2,4)=a(1,4)
         a(2,5)=1.
         a(2,6)=a(1,5)

         call fnrm_ints(fnrm_int,bw/zw,aw/zw,(bw*bw+aw*aw)/(zw*zw))

         a(3,1)=(em_av-1.)*(aw/bw)*fnrm_int
     	        
         a(3,2)=a(3,1)
         a(3,3)=1.

         call fprl_ints(fprl_int,zw/aw,bw/aw) 

         a(3,4)=(em_av-1.)*fprl_int

         call fnrm_ints(fnrm_int,zw/bw,aw/bw,(aw*aw+zw*zw)/(bw*bw))

         a(3,5)=(emwal-1.)*(aw/zw)*fnrm_int
     	        
         a(3,6)=a(3,5)

         a(4,1)=a(3,1)
         a(4,2)=a(3,2)
         a(4,3)=a(3,4)
         a(4,4)=1.
         a(4,5)=a(3,5)
         a(4,6)=a(3,6)

         a(5,1)=1.

         call fprl_ints(fprl_int,aw/bw,zw/bw)

         a(5,2)=(em_av-1.)*fprl_int

         call fnrm_ints(fnrm_int,aw/zw,bw/zw,(aw*aw+bw*bw)/(zw*zw))

         a(5,3)=(em_av-1.)*(bw/aw)*fnrm_int
     	        
         a(5,4)=a(5,3)

         call fnrm_ints(fnrm_int,zw/aw,bw/aw,(bw*bw+zw*zw)/(aw*aw))

         a(5,5)=(emwal-1.)*(bw/zw)*fnrm_int
     	        
         a(5,6)=a(5,5)

         a(6,1)=a(5,2)
         a(6,2)=1.
         a(6,3)=a(5,3)
         a(6,4)=a(5,4)
         a(6,5)=a(5,5)
         a(6,6)=a(6,5)

      return
      end subroutine algebra_long

!====6=8===============================================================72 
!====6=8===============================================================72 


	subroutine fluxroo(hseqocc,hleqocc,hsvent,hlvent, &
                           hscond,hslev,hllev) 
	
!-----------------------------------------------------------------------
!This routine calculates the heat flux generated inside the room
!and the heat ejected to the atmosphere.
!----------------------------------------------------------------------	

	implicit none

!-----------------------------------------------------------------------

!Input
!-----
	real hseqocc		!sensible heat generated by equipments and occupants [W]
	real hleqocc		!latent heat generated by occupants [W]
	real hsvent		!sensible heat generated by natural ventilation [W]
	real hlvent		!latent heat generated by natural ventilation [W] 
	real hscond		!sensible heat generated by wall conduction 

!Output
!------
	real hslev		!sensible heat flux generated inside the room [W]
	real hllev		!latent heat flux generatd inside the room


!Calculation of the total sensible heat generated inside the room

	hslev=hseqocc+hsvent+hscond 
 
!Calculation of the total latent heat generated inside the room
	
	hllev=hleqocc+hlvent
        
	return
	end subroutine fluxroo

!====6=8===============================================================72 
!====6=8===============================================================72

	subroutine phirat(nhourday,rocc)

!----------------------------------------------------------------------
!This routine calculates the occupation ratio of a floor
!By now we suppose a constant value
!----------------------------------------------------------------------

        implicit none

!Input
!-----

	real nhourday	! number of hours from midnight (local time)
	
!Output
!------
	real rocc       !value between 0 and 1

!!TEST
        rocc=1.

	return
	end subroutine phirat

!====6=8===============================================================72 
!====6=8===============================================================72

	subroutine phiequ(nhourday,hsesf,hsequip,hsequ)

!----------------------------------------------------------------------
!This routine calculates the sensible heat gain from equipments
!----------------------------------------------------------------------
        implicit none
!Input
!-----

	real nhourday ! number of hours from midnight, Local time
        real, intent(in) :: hsesf
        real, intent(in), dimension(24) :: hsequip
	
!Output
!------
	real hsequ    !sensible heat gain from equipment [WmŻ2]

!---------------------------------------------------------------------	

        hsequ = hsequip(int(nhourday)+1) * hsesf
        
	end subroutine phiequ
!====6=8===============================================================72 
!====6=8===============================================================72

	subroutine fluxeqocc(nhourday,bw,bl,perflo,hsesf,hsequip,hseqocc,hleqocc)
	
	implicit none

!---------------------------------------------------------------------
!This routine calculates the sensible and the latent heat flux 
!generated by equipments and occupants
!---------------------------------------------------------------------	

!Input
!-----
	real bw			!Room width [m]
	real bl			!Room lengzh [m]
	real nhourday		!number of hours from the beginning of the day
        real, intent(in) :: perflo ! Peak number of occupants per unit floor area
        real, intent(in) :: hsesf
        real, intent(in), dimension(24) :: hsequip

!Output
!------
	real hseqocc		!sensible heat generated by equipments and occupants [W]
	real hleqocc		!latent heat generated by occupants [W]
!Local
!-----
	real Af			!Air conditioned floor area [m2]
	real rocc		!Occupation ratio of the floor [0,1]
        real hsequ		!Heat generated from equipments 

        real hsocc		!Sensible heat generated by a person [W/Person]
                                !Source Boundary Layer Climates,page 195 (book)
        parameter (hsocc=160.)

        real hlocc		!Latent heat generated by a person [W/Person]
                                !Source Boundary Layer Climates,page 225 (book)
        parameter (hlocc=1.96e6/86400.)

!------------------------------------------------------------------
!			Sensible heat flux
!			------------------

	 Af=bw*bl

	 call phirat(nhourday,rocc)

         call phiequ(nhourday,hsesf,hsequip,hsequ)

         hseqocc=Af*rocc*perflo*hsocc+Af*hsequ

!
!			Latent heat
!			-----------
!

         hleqocc=Af*rocc*perflo*hlocc

	return
	end subroutine fluxeqocc

!====6=8===============================================================72 
!====6=8===============================================================72
	
	subroutine fluxvent(cpint,rhoint,vollev,tlev,tout,latent,&
                            humout,rhoout,humlev,beta,hsvent,hlvent)
	
	implicit none

!---------------------------------------------------------------------
!This routine calculates the sensible and the latent heat flux 
!generated by natural ventilation
!---------------------------------------------------------------------

!Input
!-----
	real cpint		!specific heat of the indoor air [J/kg.K]
	real rhoint		!density of the indoor air [Kg/m3]	
	real vollev		!volume of the room [m3]
	real tlev		!Room temperature [K]
	real tout		!outside air temperature [K]
	real latent		!latent heat of evaporation [J/Kg]
	real humout		!outside absolute humidity [Kgwater/Kgair]
	real rhoout		!air density [kg/m3]
	real humlev		!Specific humidity of the indoor air [Kgwater/Kgair]
        real, intent(in) :: beta!Thermal efficiency of the heat exchanger 
	
!Output
!------
	real hsvent		!sensible heat generated by natural ventilation [W]
	real hlvent		!latent heat generated by natural ventilation [W] 

!Local
!-----       
        
!----------------------------------------------------------------------

!			Sensible heat flux
!			------------------
        
	hsvent=(1.-beta)*cpint*rhoint*(vollev/3600.)*  &
               (tout-tlev)
	
!			Latent heat flux
!			----------------
       
	hlvent=(1.-beta)*latent*rhoint*(vollev/3600.)* &
     	       (humout-humlev)


	return
	end subroutine fluxvent

!====6=8===============================================================72 
!====6=8===============================================================72
	
	subroutine fluxcond(hswalins,hswinins,surwal,pwin,hscond)
	
	implicit none

!---------------------------------------------------------------------
!This routine calculates the sensible heat flux generated by 
!wall conduction.
!---------------------------------------------------------------------

!Input
!-----
	real hswalins(6)	!sensible heat at the internal layers of the wall [W/m2]
	real hswinins(4)	!internal window sensible heat flux [W/m2]
	real surwal(6)	        !surfaces of the room walls [m2]
	real pwin               !window proportion	


!Output
!------
	
	real hscond		!sensible heat generated by wall conduction [W]
	
!Local
!-----

	integer ivw

!----------------------------------------------------------------------

	  hscond=0.

	do ivw=1,4
	   hscond=hscond+surwal(ivw)*(1-pwin)*hswalins(ivw)+ &
                  surwal(ivw)*pwin*hswinins(ivw)	         
	end do

	do ivw=5,6
    	   hscond=hscond+surwal(ivw)*hswalins(ivw)	
	end do
!           
!Finally we must change the sign in hscond to be proportional
!to the difference (Twall-Tindoor).
!
        hscond=(-1)*hscond 

	return
	end subroutine fluxcond

!====6=8===============================================================72 
!====6=8===============================================================72
	
	subroutine regtemp(swcond,nhourday,dt,Qb,hsroo,          &
                           tlev,timeon,timeoff,targtemp,gaptemp,hsneed)
	
	implicit none

!---------------------------------------------------------------------
!This routine calculates the sensible heat fluxes, 
!after anthropogenic regulation (air conditioning)
!---------------------------------------------------------------------

!Input:
!-----.
        integer swcond       !swich air conditioning
	real nhourday        !number of hours from the beginning of the day real
	real dt	             !time step [s]
	real Qb		     !overall heat capacity of the indoor air [J/K]
        real hsroo           !sensible heat flux generated inside the room [W]
        real tlev            !room air temperature [K]
        real, intent(in) :: timeon  ! Initial local time of A/C systems
        real, intent(in) :: timeoff ! Ending local time of A/C systems
        real, intent(in) :: targtemp! Target temperature of A/C systems
        real, intent(in) :: gaptemp ! Comfort range of indoor temperature
        

!Local:
!-----.

        real templev         !hipotetical room air temperature [K]
        real alpha           !variable to control the heating/cooling of 
                             !the air conditining system
!Output:
!-----.
	real hsneed	     !sensible heat extracted to the indoor air [W]
!---------------------------------------------------------------------
!initialize variables
!---------------------
        templev = 0.
        alpha   = 0.

        if (swcond.eq.0) then ! there is not air conditioning in the floor
            hsneed = 0.
            goto 100
        else
            if ((nhourday.ge.timeon).and.(nhourday.le.timeoff)) then
               templev=tlev+(dt/Qb)*hsroo
               goto 200
            else
               hsneed = 0.     ! air conditioning is switched off
               goto 100
            endif
        endif

200     continue

        if (abs(templev-targtemp).le.gaptemp) then
           hsneed = 0.
        else 
           if (templev.gt.(targtemp+gaptemp)) then
              hsneed=hsroo-(Qb/dt)*(targtemp+gaptemp-tlev)
              alpha=(abs(hsneed-hsroo)/Qb)
              if (alpha.gt.temp_rat) then
                  hsneed=hsroo+temp_rat*Qb
                  goto 100
              else
                  goto 100
              endif
           else 
              hsneed=hsroo-(Qb/dt)*(targtemp-gaptemp-tlev)
              alpha=(abs(hsneed-hsroo)/Qb)
              if (alpha.gt.temp_rat) then
                  hsneed=hsroo-temp_rat*Qb
                  goto 100
              else
                  goto 100
              endif
           endif
        endif 

100     continue
	return
	end subroutine regtemp
     
!====6=8==============================================================72
!====6=8==============================================================72
         
	 subroutine reghum(swcond,nhourday,dt,volroo,rhoint,latent, &
                           hlroo,shumroo,timeon,timeoff,targhum,gaphum,hlneed)

	 implicit none

!---------------------------------------------------------------------
!This routine calculates the latent heat fluxes, 
!after anthropogenic regulation (air conditioning)
!---------------------------------------------------------------------

!Input:
!-----.
        integer swcond    !swich air conditioning
	real nhourday     !number of hours from the beginning of the day real[h]
	real dt	          !time step [s]
	real volroo       !volume of the room [m3]
        real rhoint       !density of the internal air [Kg/m3]
        real latent       !latent heat of evaporation [J/Kg]
        real hlroo        !latent heat flux generated inside the room [W]
        real shumroo      !specific humidity of the indoor air [kg/kg]
        real, intent(in) :: timeon  ! Initial local time of A/C systems
        real, intent(in) :: timeoff ! Ending local time of A/C systems
        real, intent(in) :: targhum ! Target humidity of the A/C systems
        real, intent(in) :: gaphum  ! comfort range of the specific humidity

!Local:
!-----.

        real humlev       !hipotetical specific humidity of the indoor [kg/kg]
        real betha        !variable to control the drying/moistening of
                          !the air conditioning system
!Output:
!-----.
	real hlneed	  !latent heat extracted to the indoor air [W]
!------------------------------------------------------------------------
!initialize variables
!---------------------
        humlev = 0.
        betha  = 0.

        if (swcond.eq.0) then ! there is not air conditioning in the floor
            hlneed = 0.
            goto 100
        else
            if ((nhourday.ge.timeon).and.(nhourday.le.timeoff)) then
               humlev=shumroo+(dt/(latent*rhoint*volroo))*hlroo
               goto 200
            else
               hlneed = 0.     ! air conditioning is switched off
               goto 100
            endif
        endif

200     continue

        if (abs(humlev-targhum).le.gaphum) then
           hlneed = 0.
        else 
           if (humlev.gt.(targhum+gaphum)) then
              hlneed=hlroo-((latent*rhoint*volroo)/dt)* &
                          (targhum+gaphum-shumroo)
              betha=abs(hlneed-hlroo)/(latent*rhoint*volroo)
              if (betha.gt.hum_rat) then
                  hlneed=hlroo+hum_rat*(latent*rhoint*volroo)
                  goto 100
              else
                  goto 100
              endif
           else 
              hlneed=hlroo-((latent*rhoint*volroo)/dt)* &
                          (targhum-gaphum-shumroo)
              betha=abs(hlneed-hlroo)/(latent*rhoint*volroo)
              if (betha.gt.hum_rat) then
                  hlneed=hlroo-hum_rat*(latent*rhoint*volroo)
                  goto 100
              else
                  goto 100
              endif
           endif
        endif 
	
100     continue
	return
	end subroutine reghum

!====6=8==============================================================72
!====6=8==============================================================72
         
         subroutine air_cond(hsneed,hlneed,dt,hsout,hlout,consump,cop)

         implicit none

!
!Performance of the air conditioning system        
!
!INPUT/OUTPUT VARIABLES
         real, intent(in) :: cop
!
!INPUT/OUTPUT VARIABLES
!       
         real hsneed     !sensible heat that is necessary for cooling/heating
                         !the indoor air temperature [W] 
         real hlneed     !latent heat that is necessary for controling
                         !the humidity of the indoor air [W]
         real dt         !time step [s]
!
!OUTPUT VARIABLES
!
         real hsout      !sensible heat pumped out into the atmosphere [W]
         real hlout      !latent heat pumped out into the atmosphere [W]
         real consump    !Electrical consumption of the air conditioning system [W] 
                         
    
!
!Performance of the air conditioning system
!
         if (hsneed.gt.0) then         ! air conditioning is cooling 
                                       ! and the heat is pumped out into the atmosphere  
	  hsout=(1/cop)*(abs(hsneed)+abs(hlneed))+hsneed
          hlout=hlneed
          consump=(1./cop)*(abs(hsneed)+abs(hlneed))
!!        hsout=0.             
!!        hlout=0.

         else if(hsneed.eq.0.) then !air conditioning is not working to regulate the indoor temperature
               hlneed=0.       !no humidity regulation is considered 
               hsout=0.        !no output into the atmosphere (sensible heat) 
               hlout=0.        !no output into the atmosphere (latent heat)
               consump=0.      !no electrical consumption

              else  !! hsneed < 0. !air conditioning is heating 
               hlneed=0.           !no humidity regulation is considered
               hlout=0.            !no output into the atmosphere (latent heat) 
               consump=(1./cop)*(abs(hsneed)+abs(hlneed))
!
!!We have two possibilities 
! 
!!             hsout=(1./cop)*(abs(hsneed)+abs(hlneed)) !output into the atmosphere 
               hsout=0.                            !no output into the atmosphere                        
         end if

         return 
         end subroutine air_cond

!====6=8==============================================================72
!====6=8==============================================================72

        subroutine consump_total(nzcanm,nlev,consumpbuild,hsoutbuild, &
                                 hsout,consump)

        implicit none
        
!-----------------------------------------------------------------------
!Compute the total consumption in kWh/s (1kWh=3.6e+6 J) and sensible heat
!ejected into the atmosphere per building
!------------------------------------------------------------------------
!
!INPUT VARIABLES
!
!
        integer nzcanm            !Maximum number of vertical levels in the urban grid
        real hsout(nzcanm)        !sensible heat emitted outside the room [W]
        real consump(nzcanm)      !Electricity consumption for the a.c. in each floor[W]
!
!OUTPUT VARIABLES
!
	real consumpbuild         !Energetic consumption for the entire building[kWh/s]
        real hsoutbuild           !Total sensible heat ejected into the atmosphere
                                  !by the air conditioning systems per building [W]        
!
!LOCAL  VARIABLES
!
        integer ilev

!
!INPUT VARIABLES
!
        integer nlev     
        
!
!INITIALIZE VARIABLES
!
        consumpbuild=0.
        hsoutbuild=0.
!
        do ilev=1,nlev
           consumpbuild=consumpbuild+consump(ilev)
           hsoutbuild=hsoutbuild+hsout(ilev)
        enddo !ilev

        consumpbuild=consumpbuild/(3.6e+06)

        return 
        end subroutine consump_total
!====6=8==============================================================72
!====6=8==============================================================72
        subroutine tridia(n,a,b,x)

!     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!     +    by A. Clappier,     EPFL, CH 1015 Lausanne                  +
!     +                        phone: ++41-(0)21-693-61-60             +
!     +                        email:alain.clappier@epfl.ch            +
!     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

! ----------------------------------------------------------------------
!        Resolution of a * x = b    where a is a tridiagonal matrix
!
! ----------------------------------------------------------------------

        implicit none

! Input
        integer n
        real a(-1:1,n)           !  a(-1,*) lower diagonal      A(i,i-1)
                               !  a(0,*)  principal diagonal  A(i,i)
                               !  a(1,*)  upper diagonal      A(i,i+1)
        real b(n)

! Output
        real x(n)

! Local
        integer i

! ----------------------------------------------------------------------

        do i=n-1,1,-1
           b(i)=b(i)-a(1,i)*b(i+1)/a(0,i+1)
           a(0,i)=a(0,i)-a(1,i)*a(-1,i+1)/a(0,i+1)
        enddo

        do i=2,n
           b(i)=b(i)-a(-1,i)*b(i-1)/a(0,i-1)
        enddo

        do i=1,n
           x(i)=b(i)/a(0,i)
        enddo

        return
        end subroutine tridia    
!====6=8===============================================================72     
!====6=8===============================================================72     
      
       subroutine gaussjbem(a,n,b,np)

! ----------------------------------------------------------------------
! This routine solve a linear system of n equations of the form
!              A X = B
!  where  A is a matrix a(i,j)
!         B a vector and X the solution
! In output b is replaced by the solution     
! ----------------------------------------------------------------------

       implicit none

! ----------------------------------------------------------------------
! INPUT:
! ----------------------------------------------------------------------
       integer np
       real a(np,np)

! ----------------------------------------------------------------------
! OUTPUT:
! ----------------------------------------------------------------------
       real b(np)

! ----------------------------------------------------------------------
! LOCAL:
! ----------------------------------------------------------------------
      integer nmax
      parameter (nmax=150)

      real big,dum
      integer i,icol,irow
      integer j,k,l,ll,n
      integer ipiv(nmax)
      real pivinv

! ----------------------------------------------------------------------
! END VARIABLES DEFINITIONS
! ----------------------------------------------------------------------
       
       do j=1,n
          ipiv(j)=0.
       enddo
       
      do i=1,n
         big=0.
         do j=1,n
            if(ipiv(j).ne.1)then
               do k=1,n
                  if(ipiv(k).eq.0)then
                     if(abs(a(j,k)).ge.big)then
                        big=abs(a(j,k))
                        irow=j
                        icol=k
                     endif
                  elseif(ipiv(k).gt.1)then
                     pause 'singular matrix in gaussjbem'
                  endif
               enddo
            endif
         enddo
         
         ipiv(icol)=ipiv(icol)+1
         
         if(irow.ne.icol)then
            do l=1,n
               dum=a(irow,l)
               a(irow,l)=a(icol,l)
               a(icol,l)=dum
            enddo
            
            dum=b(irow)
            b(irow)=b(icol)
            b(icol)=dum
          
         endif
         
         if(a(icol,icol).eq.0)pause 'singular matrix in gaussjbem'
         
         pivinv=1./a(icol,icol)
         a(icol,icol)=1
         
         do l=1,n
            a(icol,l)=a(icol,l)*pivinv
         enddo
         
         b(icol)=b(icol)*pivinv
         
         do ll=1,n
            if(ll.ne.icol)then
               dum=a(ll,icol)
               a(ll,icol)=0.
               do l=1,n
                  a(ll,l)=a(ll,l)-a(icol,l)*dum
               enddo
               
               b(ll)=b(ll)-b(icol)*dum
               
            endif
         enddo
      enddo
      
      return
      end subroutine gaussjbem
         
!====6=8===============================================================72     
!====6=8===============================================================72     

      subroutine radfluxs(radflux,alb,rs,em,rl,sigma,twal)

      implicit none
!-------------------------------------------------------------------
!This function calculates the radiative fluxe at a surface
!-------------------------------------------------------------------

	
	real alb	!albedo of the surface
	real rs		!shor wave radiation
	real em		!emissivity of the surface
	real rl 	!lon wave radiation
	real sigma	!parameter (wall is not black body) [W/m2.K4]
	real twal	!wall temperature [K]
	real radflux
	
	 radflux=(1.-alb)*rs+em*rl-em*sigma*twal**4
	
      return
      end subroutine radfluxs

!====6=8==============================================================72 
!====6=8==============================================================72
!       
!       we define the view factors fprl and fnrm, which are the angle 
!       factors between two equal and parallel planes, fprl, and two 
!       equal and orthogonal planes, fnrm, respectively
!       
        subroutine fprl_ints(fprl_int,vx,vy)
        
        implicit none

	real vx,vy
	real fprl_int
        
	fprl_int=(2./(3.141592653*vx*vy))*                       &
             (log(sqrt((1.+vx*vx)*(1.+vy*vy)/(1.+vx*vx+vy*vy)))+ &
              (vy*sqrt(1.+vx*vx)*atan(vy/sqrt(1.+vx*vx)))+       &
              (vx*sqrt(1.+vy*vy)*atan(vx/sqrt(1.+vy*vy)))-       &
              vy*atan(vy)-vx*atan(vx))

        return
        end subroutine fprl_ints

!====6=8==============================================================72 
!====6=8==============================================================72
!       
!       we define the view factors fprl and fnrm, which are the angle 
!       factors between two equal and parallel planes, fprl, and two 
!       equal and orthogonal planes, fnrm, respectively
!       

        subroutine fnrm_ints(fnrm_int,wx,wy,wz)

        implicit none
        
	real wx,wy,wz
	real fnrm_int
	
        fnrm_int=(1./(3.141592653*wy))*(wy*atan(1./wy)+wx*atan(1./wx)- &
              (sqrt(wz)*atan(1./sqrt(wz)))+                            &
              (1./4.)*(log((1.+wx*wx)*(1.+wy*wy)/(1.+wz))+             &
              wy*wy*log(wy*wy*(1.+wz)/(wz*(1.+wy*wy)))+                &
              wx*wx*log(wx*wx*(1.+wz)/(wz*(1.+wx*wx)))))
        
        return
        end subroutine fnrm_ints

!====6=8==============================================================72 
!====6=8==============================================================72
END MODULE module_sf_bem
Back to Top