Spaces:
Sleeping
Sleeping
Update utils/solar.py
Browse files- utils/solar.py +44 -64
utils/solar.py
CHANGED
@@ -3,7 +3,7 @@ from typing import List, Dict, Any, Optional, Tuple
|
|
3 |
import math
|
4 |
from datetime import datetime
|
5 |
from app.materials_library import MaterialLibrary, GlazingMaterial, Material
|
6 |
-
from utils.
|
7 |
from app.m_c_data import DEFAULT_WINDOW_PROPERTIES
|
8 |
import logging
|
9 |
|
@@ -67,73 +67,45 @@ class SolarCalculations:
|
|
67 |
logger.info("Initialized SolarCalculations with MaterialLibrary and project-specific libraries.")
|
68 |
|
69 |
@staticmethod
|
70 |
-
def
|
71 |
-
"""Calculate
|
72 |
|
73 |
Args:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
Returns:
|
79 |
-
|
80 |
|
81 |
References:
|
82 |
-
ASHRAE Handbook—Fundamentals, Chapter
|
83 |
"""
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
@staticmethod
|
89 |
-
def
|
90 |
-
"""Calculate
|
91 |
|
92 |
Args:
|
93 |
-
|
94 |
-
surface_type: ComponentType enum (WALL, ROOF, FLOOR, WINDOW, SKYLIGHT).
|
95 |
|
96 |
Returns:
|
97 |
-
float:
|
98 |
|
99 |
References:
|
100 |
-
ASHRAE Handbook—Fundamentals, Chapter
|
101 |
"""
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
else: # WINDOW, SKYLIGHT
|
108 |
-
h_o = DEFAULT_WINDOW_PROPERTIES["h_o"]
|
109 |
-
return max(h_o, 5.0) # Minimum for stability
|
110 |
|
111 |
-
|
112 |
-
def calculate_sol_air_temperature(T_out: float, I_t: float, absorptivity: float, emissivity: float,
|
113 |
-
h_o: float, dew_point: float, total_sky_cover: float = 0.5) -> float:
|
114 |
-
"""Calculate sol-air temperature for opaque surfaces.
|
115 |
-
|
116 |
-
Args:
|
117 |
-
T_out: Outdoor dry-bulb temperature (°C).
|
118 |
-
I_t: Total incident solar radiation (W/m²).
|
119 |
-
absorptivity: Surface absorptivity (0 to 1).
|
120 |
-
emissivity: Surface emissivity (0 to 1).
|
121 |
-
h_o: Outdoor convective heat transfer coefficient (W/m²·K).
|
122 |
-
dew_point: Dew point temperature (°C).
|
123 |
-
total_sky_cover: Cloud cover fraction (0 to 1, default 0.5).
|
124 |
-
|
125 |
-
Returns:
|
126 |
-
float: Sol-air temperature (°C).
|
127 |
-
|
128 |
-
References:
|
129 |
-
ASHRAE Handbook—Fundamentals, Chapter 26, Eq. 13.
|
130 |
-
"""
|
131 |
-
sigma = 5.67e-8 # Stefan-Boltzmann constant
|
132 |
-
T_sky = SolarCalculations.calculate_sky_temperature(T_out, dew_point, total_sky_cover)
|
133 |
-
T_sol_air = T_out + (absorptivity * I_t - emissivity * sigma * ((T_out + 273.15)**4 - (T_sky + 273.15)**4)) / h_o
|
134 |
-
return T_sol_air
|
135 |
-
|
136 |
-
def get_surface_parameters(self, component: Any, building_info: Dict, wind_speed: float = 4.0) -> Tuple[float, float, float, Optional[float], float]:
|
137 |
"""
|
138 |
Determine surface parameters (tilt, azimuth, h_o, emissivity, absorptivity) for a component.
|
139 |
Uses pre-calculated values stored in the component dictionary from components.py.
|
@@ -141,8 +113,7 @@ class SolarCalculations:
|
|
141 |
Args:
|
142 |
component: Component dictionary with surface_tilt, surface_azimuth, absorptivity/emissivity or shgc,
|
143 |
component_type, and optionally fenestration.
|
144 |
-
building_info: Building information (not used since parameters are pre-calculated).
|
145 |
-
wind_speed: Wind speed (m/s, default 4.0).
|
146 |
|
147 |
Returns:
|
148 |
Tuple[float, float, float, Optional[float], float]: Surface tilt (°), surface azimuth (°),
|
@@ -166,8 +137,9 @@ class SolarCalculations:
|
|
166 |
}
|
167 |
component_type = type_map.get(component_type, ComponentType.WALL)
|
168 |
|
169 |
-
#
|
170 |
-
|
|
|
171 |
|
172 |
# Default parameters
|
173 |
if component_type == ComponentType.ROOF:
|
@@ -204,15 +176,19 @@ class SolarCalculations:
|
|
204 |
# Apply defaults
|
205 |
if component_type == ComponentType.ROOF:
|
206 |
surface_tilt = 0.0
|
|
|
207 |
surface_azimuth = 0.0
|
208 |
elif component_type == ComponentType.SKYLIGHT:
|
209 |
surface_tilt = 0.0
|
|
|
210 |
surface_azimuth = 0.0
|
211 |
elif component_type == ComponentType.FLOOR:
|
212 |
surface_tilt = 180.0
|
|
|
213 |
surface_azimuth = 0.0
|
214 |
else: # WALL, WINDOW
|
215 |
surface_tilt = 90.0
|
|
|
216 |
surface_azimuth = 0.0
|
217 |
|
218 |
if component_type in [ComponentType.WALL, ComponentType.ROOF]:
|
@@ -261,7 +237,8 @@ class SolarCalculations:
|
|
261 |
"""Calculate solar angles, sol-air temperature, and solar heat gain for hourly data with GHI > 0.
|
262 |
|
263 |
Args:
|
264 |
-
hourly_data (List[Dict]): Hourly weather data containing month, day, hour, GHI, DNI, DHI, dry_bulb
|
|
|
265 |
latitude (float): Latitude in degrees.
|
266 |
longitude (float): Longitude in degrees.
|
267 |
timezone (float): Timezone offset in hours.
|
@@ -318,9 +295,9 @@ class SolarCalculations:
|
|
318 |
dni = record.get("direct_normal_radiation", ghi * 0.7) # Fallback: estimate DNI
|
319 |
dhi = record.get("diffuse_horizontal_radiation", ghi * 0.3) # Fallback: estimate DHI
|
320 |
outdoor_temp = record.get("dry_bulb")
|
321 |
-
dew_point = record.get("dew_point", outdoor_temp - 5.0)
|
322 |
-
wind_speed = record.get("wind_speed", 4.0)
|
323 |
-
total_sky_cover = record.get("total_sky_cover", 0.5)
|
324 |
|
325 |
if None in [month, day, hour, outdoor_temp]:
|
326 |
logger.error(f"Missing required weather data for {month}/{day}/{hour}")
|
@@ -379,7 +356,7 @@ class SolarCalculations:
|
|
379 |
try:
|
380 |
# Get surface parameters
|
381 |
surface_tilt, surface_azimuth, h_o, emissivity, absorptivity = \
|
382 |
-
self.get_surface_parameters(comp, building_info
|
383 |
|
384 |
# For windows/skylights, get SHGC from component
|
385 |
shgc = comp.get('shgc', 0.7)
|
@@ -393,7 +370,7 @@ class SolarCalculations:
|
|
393 |
|
394 |
logger.info(f" Component {comp.get('name', 'unknown_component')} at {month}/{day}/{hour}: "
|
395 |
f"surface_tilt={surface_tilt:.2f}, surface_azimuth={surface_azimuth:.2f}, "
|
396 |
-
f"cos_theta={cos_theta:.2f}")
|
397 |
|
398 |
# Calculate total incident radiation (I_t)
|
399 |
view_factor = (1 - math.cos(math.radians(surface_tilt))) / 2
|
@@ -409,9 +386,12 @@ class SolarCalculations:
|
|
409 |
}
|
410 |
|
411 |
# Calculate sol-air temperature for opaque surfaces
|
412 |
-
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
415 |
comp_result["sol_air_temp"] = round(T_sol_air, 2)
|
416 |
logger.info(f"Sol-air temp for {comp_result['component_id']} at {month}/{day}/{hour}: {T_sol_air:.2f}°C")
|
417 |
|
|
|
3 |
import math
|
4 |
from datetime import datetime
|
5 |
from app.materials_library import MaterialLibrary, GlazingMaterial, Material
|
6 |
+
from utils.ctf_calculations import ComponentType, CTFCalculator
|
7 |
from app.m_c_data import DEFAULT_WINDOW_PROPERTIES
|
8 |
import logging
|
9 |
|
|
|
67 |
logger.info("Initialized SolarCalculations with MaterialLibrary and project-specific libraries.")
|
68 |
|
69 |
@staticmethod
|
70 |
+
def day_of_year(month: int, day: int, year: int) -> int:
|
71 |
+
"""Calculate day of the year (n) from month, day, and year, accounting for leap years.
|
72 |
|
73 |
Args:
|
74 |
+
month (int): Month of the year (1-12).
|
75 |
+
day (int): Day of the month (1-31).
|
76 |
+
year (int): Year.
|
77 |
|
78 |
Returns:
|
79 |
+
int: Day of the year (1-365 or 366 for leap years).
|
80 |
|
81 |
References:
|
82 |
+
ASHRAE Handbook—Fundamentals, Chapter 18.
|
83 |
"""
|
84 |
+
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
85 |
+
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
|
86 |
+
days_in_month[1] = 29
|
87 |
+
return sum(days_in_month[:month-1]) + day
|
88 |
|
89 |
@staticmethod
|
90 |
+
def equation_of_time(n: int) -> float:
|
91 |
+
"""Calculate Equation of Time (EOT) in minutes using Spencer's formula.
|
92 |
|
93 |
Args:
|
94 |
+
n (int): Day of the year (1-365 or 366).
|
|
|
95 |
|
96 |
Returns:
|
97 |
+
float: Equation of Time in minutes.
|
98 |
|
99 |
References:
|
100 |
+
ASHRAE Handbook—Fundamentals, Chapter 18.
|
101 |
"""
|
102 |
+
B = (n - 1) * 360 / 365
|
103 |
+
B_rad = math.radians(B)
|
104 |
+
EOT = 229.2 * (0.000075 + 0.001868 * math.cos(B_rad) - 0.032077 * math.sin(B_rad) -
|
105 |
+
0.014615 * math.cos(2 * B_rad) - 0.04089 * math.sin(2 * B_rad))
|
106 |
+
return EOT
|
|
|
|
|
|
|
107 |
|
108 |
+
def get_surface_parameters(self, component: Any, building_info: Dict) -> Tuple[float, float, float, Optional[float], float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
"""
|
110 |
Determine surface parameters (tilt, azimuth, h_o, emissivity, absorptivity) for a component.
|
111 |
Uses pre-calculated values stored in the component dictionary from components.py.
|
|
|
113 |
Args:
|
114 |
component: Component dictionary with surface_tilt, surface_azimuth, absorptivity/emissivity or shgc,
|
115 |
component_type, and optionally fenestration.
|
116 |
+
building_info (Dict): Building information (not used since parameters are pre-calculated).
|
|
|
117 |
|
118 |
Returns:
|
119 |
Tuple[float, float, float, Optional[float], float]: Surface tilt (°), surface azimuth (°),
|
|
|
137 |
}
|
138 |
component_type = type_map.get(component_type, ComponentType.WALL)
|
139 |
|
140 |
+
# Get dynamic h_o using wind speed from component or default
|
141 |
+
wind_speed = component.get('wind_speed', 4.0) # Default from session state or component
|
142 |
+
h_o = CTFCalculator.calculate_h_o(wind_speed, component_type)
|
143 |
|
144 |
# Default parameters
|
145 |
if component_type == ComponentType.ROOF:
|
|
|
176 |
# Apply defaults
|
177 |
if component_type == ComponentType.ROOF:
|
178 |
surface_tilt = 0.0
|
179 |
+
h_o = CTFCalculator.calculate_h_o(wind_speed, component_type)
|
180 |
surface_azimuth = 0.0
|
181 |
elif component_type == ComponentType.SKYLIGHT:
|
182 |
surface_tilt = 0.0
|
183 |
+
h_o = CTFCalculator.calculate_h_o(wind_speed, component_type)
|
184 |
surface_azimuth = 0.0
|
185 |
elif component_type == ComponentType.FLOOR:
|
186 |
surface_tilt = 180.0
|
187 |
+
h_o = CTFCalculator.calculate_h_o(wind_speed, component_type)
|
188 |
surface_azimuth = 0.0
|
189 |
else: # WALL, WINDOW
|
190 |
surface_tilt = 90.0
|
191 |
+
h_o = CTFCalculator.calculate_h_o(wind_speed, component_type)
|
192 |
surface_azimuth = 0.0
|
193 |
|
194 |
if component_type in [ComponentType.WALL, ComponentType.ROOF]:
|
|
|
237 |
"""Calculate solar angles, sol-air temperature, and solar heat gain for hourly data with GHI > 0.
|
238 |
|
239 |
Args:
|
240 |
+
hourly_data (List[Dict]): Hourly weather data containing month, day, hour, GHI, DNI, DHI, dry_bulb,
|
241 |
+
dew_point, wind_speed, total_sky_cover.
|
242 |
latitude (float): Latitude in degrees.
|
243 |
longitude (float): Longitude in degrees.
|
244 |
timezone (float): Timezone offset in hours.
|
|
|
295 |
dni = record.get("direct_normal_radiation", ghi * 0.7) # Fallback: estimate DNI
|
296 |
dhi = record.get("diffuse_horizontal_radiation", ghi * 0.3) # Fallback: estimate DHI
|
297 |
outdoor_temp = record.get("dry_bulb")
|
298 |
+
dew_point = record.get("dew_point", outdoor_temp - 5.0) # Default
|
299 |
+
wind_speed = record.get("wind_speed", 4.0) # Default
|
300 |
+
total_sky_cover = record.get("total_sky_cover", 0.5) # Default
|
301 |
|
302 |
if None in [month, day, hour, outdoor_temp]:
|
303 |
logger.error(f"Missing required weather data for {month}/{day}/{hour}")
|
|
|
356 |
try:
|
357 |
# Get surface parameters
|
358 |
surface_tilt, surface_azimuth, h_o, emissivity, absorptivity = \
|
359 |
+
self.get_surface_parameters(comp, building_info)
|
360 |
|
361 |
# For windows/skylights, get SHGC from component
|
362 |
shgc = comp.get('shgc', 0.7)
|
|
|
370 |
|
371 |
logger.info(f" Component {comp.get('name', 'unknown_component')} at {month}/{day}/{hour}: "
|
372 |
f"surface_tilt={surface_tilt:.2f}, surface_azimuth={surface_azimuth:.2f}, "
|
373 |
+
f"cos_theta={cos_theta:.2f}, h_o={h_o:.2f}")
|
374 |
|
375 |
# Calculate total incident radiation (I_t)
|
376 |
view_factor = (1 - math.cos(math.radians(surface_tilt))) / 2
|
|
|
386 |
}
|
387 |
|
388 |
# Calculate sol-air temperature for opaque surfaces
|
389 |
+
ifsentence = comp.get('type', '').lower() in ['walls', 'roofs']
|
390 |
+
if ifsentence:
|
391 |
+
T_sol_air = CTFCalculator.calculate_sol_air_temperature(
|
392 |
+
outdoor_temp, I_t, absorptivity, emissivity or 0.9,
|
393 |
+
h_o, dew_point, total_sky_cover
|
394 |
+
)
|
395 |
comp_result["sol_air_temp"] = round(T_sol_air, 2)
|
396 |
logger.info(f"Sol-air temp for {comp_result['component_id']} at {month}/{day}/{hour}: {T_sol_air:.2f}°C")
|
397 |
|