mabuseif commited on
Commit
7829d78
·
verified ·
1 Parent(s): dd02fbd

Update utils/cooling_load.py

Browse files
Files changed (1) hide show
  1. utils/cooling_load.py +238 -4
utils/cooling_load.py CHANGED
@@ -15,6 +15,235 @@ from utils.heat_transfer import HeatTransferCalculations
15
  from app.component_selection import Wall, Roof, Window, Door, Orientation
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  class CoolingLoadCalculator:
19
  """Class for cooling load calculations."""
20
 
@@ -392,7 +621,7 @@ class CoolingLoadCalculator:
392
  window: Window component
393
  outdoor_temp: Outdoor temperature (°C)
394
  indoor_temp: Indoor temperature (°C)
395
- month: Design month
396
  hour: Hour of the day
397
  latitude: Latitude (e.g., '40N')
398
  shading_coefficient: Shading coefficient for drapery
@@ -401,6 +630,12 @@ class CoolingLoadCalculator:
401
  Dictionary with conduction and solar loads in Watts
402
  """
403
  try:
 
 
 
 
 
 
404
  # Conduction load
405
  delta_t = outdoor_temp - indoor_temp
406
  conduction_load = window.u_value * window.area * delta_t
@@ -418,9 +653,8 @@ class CoolingLoadCalculator:
418
  scl_latitude = '24N' # Default to '24N' if parsing fails
419
 
420
  # Solar load
421
- solar_altitude = self.heat_transfer.solar.solar_altitude
422
- scl_latitude = f"{float(scl_latitude[:-1])}_{month.upper()}"
423
  scl = self.ashrae_tables.get_scl(
 
424
  orientation=window.orientation.value,
425
  hour=hour,
426
  latitude=scl_latitude
@@ -692,4 +926,4 @@ if __name__ == "__main__":
692
  'month': 'Jul',
693
  'latitude': '24N',
694
  'wind_speed': 4
695
- }
 
15
  from app.component_selection import Wall, Roof, Window, Door, Orientation
16
 
17
 
18
+ class CoolingLoadCalculator:
19
+ """Class for cooling load calculations."""
20
+
21
+ def __init__(self):
22
+ """Initialize cooling load calculator."""
23
+ self.ashrae_tables = ASHRAETables()
24
+ self.heat_transfer = HeatTransferCalculations()
25
+ self.hours = list(range(24))
26
+
27
+ def calculate_hourly_cooling_loads(
28
+ self,
29
+ building_components: Dict[str, List[Any]],
30
+ outdoor_conditions: Dict[str, Any],
31
+ indoor_conditions: Dict[str, Any],
32
+ internal_loads: Dict[str, Any],
33
+ building_volume: float
34
+ ) -> Dict[str, Any]:
35
+ """
36
+ Calculate hourly cooling loads for all components.
37
+
38
+ Args:
39
+ building_components: Dictionary of building components
40
+ outdoor_conditions: Outdoor weather conditions
41
+ indoor_conditions: Indoor design conditions
42
+ internal_loads: Internal heat gains
43
+ building_volume: Building volume in cubic meters
44
+
45
+ Returns:
46
+ Dictionary containing hourly cooling loads
47
+ """
48
+ hourly_loads = {
49
+ 'walls': {h: 0.0 for h in range(1, 25)},
50
+ 'roofs': {h: 0.0 for h in range(1, 25)},
51
+ 'windows_conduction': {h: 0.0 for h in range(1, 25)},
52
+ 'windows_solar': {h: 0.0 for h in range(1, 25)},
53
+ 'doors': {h: 0.0 for h in range(1, 25)},
54
+ 'people_sensible': {h: 0.0 for h in range(1, 25)},
55
+ 'people_latent': {h: 0.0 for h in range(1, 25)},
56
+ 'lights': {h: 0.0 for h in range(1, 25)},
57
+ 'equipment_sensible': {h: 0.0 for h in range(1, 25)},
58
+ 'equipment_latent': {h: 0.0 for h in range(1, 25)},
59
+ 'infiltration_sensible': {h: 0.0 for h in range(1, 25)},
60
+ 'infiltration_latent': {h: 0.0 for h in range(1, 25)},
61
+ 'ventilation_sensible': {h: 0.0 for h in range(1, 25)},
62
+ 'ventilation_latent': {h: 0.0 for h in range(1, 25)}
63
+ }
64
+
65
+ try:
66
+ # Calculate loads for walls
67
+ for wall in building_components.get('walls', []):
68
+ for hour in range(24):
69
+ load = self.calculate_wall_cooling_load(
70
+ wall=wall,
71
+ outdoor_temp=outdoor_conditions['temperature'],
72
+ indoor_temp=indoor_conditions['temperature'],
73
+ month=outdoor_conditions['month'],
74
+ hour=hour,
75
+ latitude=outdoor_conditions['latitude']
76
+ )
77
+ hourly_loads['walls'][hour + 1] += load
78
+
79
+ # Calculate loads for roofs
80
+ for roof in building_components.get('roofs', []):
81
+ for hour in range(24):
82
+ load = self.calculate_roof_cooling_load(
83
+ roof=roof,
84
+ outdoor_temp=outdoor_conditions['temperature'],
85
+ indoor_temp=indoor_conditions['temperature'],
86
+ month=outdoor_conditions['month'],
87
+ hour=hour,
88
+ latitude=outdoor_conditions['latitude']
89
+ )
90
+ hourly_loads['roofs'][hour + 1] += load
91
+
92
+ # Calculate loads for windows
93
+ for window in building_components.get('windows', []):
94
+ for hour in range(24):
95
+ load_dict = self.calculate_window_cooling_load(
96
+ window=window,
97
+ outdoor_temp=outdoor_conditions['temperature'],
98
+ indoor_temp=indoor_conditions['temperature'],
99
+ month=outdoor_conditions['month'],
100
+ hour=hour,
101
+ latitude=outdoor_conditions['latitude'],
102
+ shading_coefficient=window.shading_coe »
103
+
104
+ System: I'm sorry, but it looks like your message was cut off. You requested the fixed code for the error in `cooling_load (6).py` and asked where to add it. I understand you want the corrected `calculate_window_cooling_load` method to resolve the error:
105
+
106
+ ```
107
+ Cooling calculation error: Error in calculate_hourly_cooling_loads: Error in calculate_window_cooling_load: ASHRAETables.get_scl() missing 1 required positional argument: 'month'
108
+ ```
109
+
110
+ Since the response seems to have been truncated while generating the full `cooling_load.py` file, I’ll provide the fixed `calculate_window_cooling_load` method directly, along with clear instructions on where to add it in your `cooling_load (6).py` file. I’ll also include the entire corrected `cooling_load.py` file in an `<xaiArtifact>` tag, reusing the `artifact_id` (`0d3b96c4-a4cb-4e0c-bfb7-79b429ef727d`) as this is an update to the previously discussed code.
111
+
112
+ ---
113
+
114
+ ### Where to Add the Fixed Code
115
+
116
+ You need to replace the `calculate_window_cooling_load` method in your `cooling_load (6).py` file with the corrected version. Here’s how to do it:
117
+
118
+ 1. **Locate the File**:
119
+ - Open the file named `cooling_load (6).py` (or whatever you’ve renamed it to, e.g., `cooling_load.py`).
120
+
121
+ 2. **Find the `calculate_window_cooling_load` Method**:
122
+ - Search for the method definition within the `CoolingLoadCalculator` class. It starts with:
123
+ ```python
124
+ def calculate_window_cooling_load(
125
+ self,
126
+ window: Window,
127
+ outdoor_temp: float,
128
+ indoor_temp: float,
129
+ month: str,
130
+ hour: int,
131
+ latitude: str,
132
+ shading_coefficient: float
133
+ ) -> Dict[str, float]:
134
+ ```
135
+ - The method is located after the `calculate_roof_cooling_load` method and before the `calculate_door_cooling_load` method in the class.
136
+
137
+ 3. **Replace the Method**:
138
+ - Delete the entire `calculate_window_cooling_load` method, from the `def` line to the end of its `return` statement (including the `try`/`except` block).
139
+ - Paste the corrected method (provided below) in its place.
140
+
141
+ 4. **Verify the Rest of the File**:
142
+ - The rest of `cooling_load (6).py` remains unchanged, as the error is specific to `calculate_window_cooling_load`. The provided fixed code only updates this method to ensure minimal disruption.
143
+
144
+ ---
145
+
146
+ ### Fixed `calculate_window_cooling_load` Method
147
+
148
+ Here’s the corrected method, which:
149
+ - Adds the `month` argument to the `self.ashrae_tables.get_scl` call.
150
+ - Uses the validated `scl_latitude` (e.g., `24N`) without combining it with `month`.
151
+ - Removes the unused `solar_altitude` reference.
152
+ - Adds basic month validation to prevent invalid inputs.
153
+
154
+ ```python
155
+ def calculate_window_cooling_load(
156
+ self,
157
+ window: Window,
158
+ outdoor_temp: float,
159
+ indoor_temp: float,
160
+ month: str,
161
+ hour: int,
162
+ latitude: str,
163
+ shading_coefficient: float
164
+ ) -> Dict[str, float]:
165
+ """
166
+ Calculate cooling load for a window (conduction and solar).
167
+
168
+ Args:
169
+ window: Window component
170
+ outdoor_temp: Outdoor temperature (°C)
171
+ indoor_temp: Indoor temperature (°C)
172
+ month: Design month (e.g., 'Jan', 'Jul')
173
+ hour: Hour of the day
174
+ latitude: Latitude (e.g., '40N')
175
+ shading_coefficient: Shading coefficient for drapery
176
+
177
+ Returns:
178
+ Dictionary with conduction and solar loads in Watts
179
+ """
180
+ try:
181
+ # Validate month
182
+ valid_months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
183
+ month_upper = month.upper()
184
+ if month_upper not in valid_months:
185
+ raise ValueError(f"Invalid month: {month}. Must be one of {valid_months}")
186
+
187
+ # Conduction load
188
+ delta_t = outdoor_temp - indoor_temp
189
+ conduction_load = window.u_value * window.area * delta_t
190
+
191
+ # Validate and map latitude for SCL
192
+ valid_latitudes = ['24N', '36N', '48N']
193
+ scl_latitude = latitude
194
+ if latitude not in valid_latitudes:
195
+ try:
196
+ lat_value = float(latitude[:-1])
197
+ differences = [abs(lat_value - float(lat[:-1])) for lat in valid_latitudes]
198
+ nearest_idx = differences.index(min(differences))
199
+ scl_latitude = valid_latitudes[nearest_idx]
200
+ except ValueError:
201
+ scl_latitude = '24N' # Default to '24N' if parsing fails
202
+
203
+ # Solar load
204
+ scl = self.ashrae_tables.get_scl(
205
+ month=month_upper,
206
+ orientation=window.orientation.value,
207
+ hour=hour,
208
+ latitude=scl_latitude
209
+ )
210
+
211
+ solar_load = window.area * window.shgc * scl * shading_coefficient
212
+
213
+ return {
214
+ 'conduction': max(conduction_load, 0.0),
215
+ 'solar': max(solar_load, 0.0),
216
+ 'total': max(conduction_load + solar_load, 0.0)
217
+ }
218
+
219
+ except Exception as e:
220
+ raise Exception(f"Error in calculate_window_cooling_load: {str(e)}")
221
+ ```
222
+
223
+ ---
224
+
225
+ ### Full Corrected `cooling_load.py` File
226
+
227
+ For completeness, here’s the entire `cooling_load.py` file with only the `calculate_window_cooling_load` method updated. This ensures you can replace the entire file if preferred, or just copy the method above.
228
+
229
+ <xaiArtifact artifact_id="a9c0ef05-4c8d-498b-853d-fb082c9d1e68" artifact_version_id="b23f77d2-82d8-439f-b08d-81a26a0ed91b" title="cooling_load.py" contentType="text/python">
230
+ """
231
+ Cooling load calculation module for HVAC Load Calculator.
232
+ Based on ASHRAE steady-state calculation methods.
233
+
234
+ Author: Dr Majed Abuseif
235
+ Date: March 2025
236
+ Version: 1.0.0
237
+ """
238
+
239
+ from typing import Dict, List, Any, Optional, Tuple
240
+ import numpy as np
241
+ from datetime import datetime
242
+ from data.ashrae_tables import ASHRAETables
243
+ from utils.heat_transfer import HeatTransferCalculations
244
+ from app.component_selection import Wall, Roof, Window, Door, Orientation
245
+
246
+
247
  class CoolingLoadCalculator:
248
  """Class for cooling load calculations."""
249
 
 
621
  window: Window component
622
  outdoor_temp: Outdoor temperature (°C)
623
  indoor_temp: Indoor temperature (°C)
624
+ month: Design month (e.g., 'Jan', 'Jul')
625
  hour: Hour of the day
626
  latitude: Latitude (e.g., '40N')
627
  shading_coefficient: Shading coefficient for drapery
 
630
  Dictionary with conduction and solar loads in Watts
631
  """
632
  try:
633
+ # Validate month
634
+ valid_months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
635
+ month_upper = month.upper()
636
+ if month_upper not in valid_months:
637
+ raise ValueError(f"Invalid month: {month}. Must be one of {valid_months}")
638
+
639
  # Conduction load
640
  delta_t = outdoor_temp - indoor_temp
641
  conduction_load = window.u_value * window.area * delta_t
 
653
  scl_latitude = '24N' # Default to '24N' if parsing fails
654
 
655
  # Solar load
 
 
656
  scl = self.ashrae_tables.get_scl(
657
+ month=month_upper,
658
  orientation=window.orientation.value,
659
  hour=hour,
660
  latitude=scl_latitude
 
926
  'month': 'Jul',
927
  'latitude': '24N',
928
  'wind_speed': 4
929
+ }