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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
"""
Shared calculation functions module for HVAC Load Calculator.
This module implements common heat transfer calculations used in both cooling and heating load calculations.
"""

from typing import Dict, List, Any, Optional, Tuple
import math
import numpy as np
import pandas as pd
import os

# Import data models and utilities
from data.building_components import Wall, Roof, Floor, Window, Door, Orientation
from utils.psychrometrics import Psychrometrics

# Define constants
STEFAN_BOLTZMANN_CONSTANT = 5.67e-8  # W/(m²·K⁴)
SOLAR_CONSTANT = 1367  # W/m²
EARTH_TILT_ANGLE = 23.45  # degrees


class HeatTransfer:
    """Class for shared heat transfer calculations."""
    
    @staticmethod
    def conduction_heat_transfer(u_value: float, area: float, delta_t: float) -> float:
        """
        Calculate conduction heat transfer through a building component.
        
        Args:
            u_value: U-value of the component in W/(m²·K)
            area: Area of the component in m²
            delta_t: Temperature difference across the component in K (or °C)
            
        Returns:
            Heat transfer rate in W
        """
        return u_value * area * delta_t
    
    @staticmethod
    def convection_heat_transfer(h_c: float, area: float, delta_t: float) -> float:
        """
        Calculate convection heat transfer.
        
        Args:
            h_c: Convection heat transfer coefficient in W/(m²·K)
            area: Surface area in m²
            delta_t: Temperature difference between surface and fluid in K (or °C)
            
        Returns:
            Heat transfer rate in W
        """
        return h_c * area * delta_t
    
    @staticmethod
    def radiation_heat_transfer(emissivity: float, area: float, t_surface: float, t_surroundings: float) -> float:
        """
        Calculate radiation heat transfer.
        
        Args:
            emissivity: Surface emissivity (0-1)
            area: Surface area in m²
            t_surface: Surface temperature in K
            t_surroundings: Surroundings temperature in K
            
        Returns:
            Heat transfer rate in W
        """
        return emissivity * STEFAN_BOLTZMANN_CONSTANT * area * (t_surface**4 - t_surroundings**4)
    
    @staticmethod
    def infiltration_heat_transfer(flow_rate: float, delta_t: float, density: float = 1.2, specific_heat: float = 1006) -> float:
        """
        Calculate sensible heat transfer due to infiltration or ventilation.
        
        Args:
            flow_rate: Volumetric flow rate in m³/s
            delta_t: Temperature difference between indoor and outdoor air in K (or °C)
            density: Air density in kg/m³ (default: 1.2 kg/m³)
            specific_heat: Specific heat capacity of air in J/(kg·K) (default: 1006 J/(kg·K))
            
        Returns:
            Heat transfer rate in W
        """
        return flow_rate * density * specific_heat * delta_t
    
    @staticmethod
    def infiltration_latent_heat_transfer(flow_rate: float, delta_w: float, density: float = 1.2, latent_heat: float = 2501000) -> float:
        """
        Calculate latent heat transfer due to infiltration or ventilation.
        
        Args:
            flow_rate: Volumetric flow rate in m³/s
            delta_w: Humidity ratio difference between indoor and outdoor air in kg/kg
            density: Air density in kg/m³ (default: 1.2 kg/m³)
            latent_heat: Latent heat of vaporization in J/kg (default: 2501000 J/kg)
            
        Returns:
            Heat transfer rate in W
        """
        return flow_rate * density * latent_heat * delta_w
    
    @staticmethod
    def air_exchange_rate_to_flow_rate(ach: float, volume: float) -> float:
        """
        Convert air changes per hour to volumetric flow rate.
        
        Args:
            ach: Air changes per hour (1/h)
            volume: Room or building volume in m³
            
        Returns:
            Volumetric flow rate in m³/s
        """
        return ach * volume / 3600
    
    @staticmethod
    def flow_rate_to_air_exchange_rate(flow_rate: float, volume: float) -> float:
        """
        Convert volumetric flow rate to air changes per hour.
        
        Args:
            flow_rate: Volumetric flow rate in m³/s
            volume: Room or building volume in m³
            
        Returns:
            Air changes per hour (1/h)
        """
        return flow_rate * 3600 / volume
    
    @staticmethod
    def crack_method_infiltration(crack_length: float, coefficient: float, pressure_difference: float, exponent: float = 0.65) -> float:
        """
        Calculate infiltration using the crack method.
        
        Args:
            crack_length: Length of cracks in m
            coefficient: Flow coefficient in m³/(s·m·Pa^n)
            pressure_difference: Pressure difference in Pa
            exponent: Flow exponent (default: 0.65)
            
        Returns:
            Infiltration flow rate in m³/s
        """
        return coefficient * crack_length * pressure_difference**exponent
    
    @staticmethod
    def wind_pressure_difference(wind_speed: float, wind_coefficient: float, density: float = 1.2) -> float:
        """
        Calculate pressure difference due to wind.
        
        Args:
            wind_speed: Wind speed in m/s
            wind_coefficient: Wind pressure coefficient (dimensionless)
            density: Air density in kg/m³ (default: 1.2 kg/m³)
            
        Returns:
            Pressure difference in Pa
        """
        return 0.5 * density * wind_speed**2 * wind_coefficient
    
    @staticmethod
    def stack_pressure_difference(height: float, indoor_temp: float, outdoor_temp: float, 
                                 neutral_plane_height: float = None, gravity: float = 9.81) -> float:
        """
        Calculate pressure difference due to stack effect.
        
        Args:
            height: Height from reference level in m
            indoor_temp: Indoor temperature in K
            outdoor_temp: Outdoor temperature in K
            neutral_plane_height: Height of neutral pressure plane in m (default: half of height)
            gravity: Acceleration due to gravity in m/s² (default: 9.81 m/s²)
            
        Returns:
            Pressure difference in Pa
        """
        if neutral_plane_height is None:
            neutral_plane_height = height / 2
        
        # Calculate pressure difference
        return gravity * (height - neutral_plane_height) * (outdoor_temp - indoor_temp) / outdoor_temp
    
    @staticmethod
    def combined_pressure_difference(wind_pd: float, stack_pd: float) -> float:
        """
        Calculate combined pressure difference from wind and stack effects.
        
        Args:
            wind_pd: Pressure difference due to wind in Pa
            stack_pd: Pressure difference due to stack effect in Pa
            
        Returns:
            Combined pressure difference in Pa
        """
        # Simple quadrature combination
        return math.sqrt(wind_pd**2 + stack_pd**2)
    
    @staticmethod
    def solar_declination(day_of_year: int) -> float:
        """
        Calculate solar declination angle.
        
        Args:
            day_of_year: Day of the year (1-365)
            
        Returns:
            Solar declination angle in degrees
        """
        return EARTH_TILT_ANGLE * math.sin(2 * math.pi * (day_of_year - 81) / 365)
    
    @staticmethod
    def solar_hour_angle(solar_time: float) -> float:
        """
        Calculate solar hour angle.
        
        Args:
            solar_time: Solar time in hours (0-24)
            
        Returns:
            Solar hour angle in degrees
        """
        return 15 * (solar_time - 12)
    
    @staticmethod
    def solar_altitude(latitude: float, declination: float, hour_angle: float) -> float:
        """
        Calculate solar altitude angle.
        
        Args:
            latitude: Latitude in degrees
            declination: Solar declination angle in degrees
            hour_angle: Solar hour angle in degrees
            
        Returns:
            Solar altitude angle in degrees
        """
        # Convert angles to radians
        lat_rad = math.radians(latitude)
        decl_rad = math.radians(declination)
        hour_rad = math.radians(hour_angle)
        
        # Calculate solar altitude
        sin_altitude = (math.sin(lat_rad) * math.sin(decl_rad) + 
                       math.cos(lat_rad) * math.cos(decl_rad) * math.cos(hour_rad))
        
        return math.degrees(math.asin(sin_altitude))
    
    @staticmethod
    def solar_azimuth(latitude: float, declination: float, hour_angle: float, altitude: float) -> float:
        """
        Calculate solar azimuth angle.
        
        Args:
            latitude: Latitude in degrees
            declination: Solar declination angle in degrees
            hour_angle: Solar hour angle in degrees
            altitude: Solar altitude angle in degrees
            
        Returns:
            Solar azimuth angle in degrees (0° = South, positive westward)
        """
        # Convert angles to radians
        lat_rad = math.radians(latitude)
        decl_rad = math.radians(declination)
        hour_rad = math.radians(hour_angle)
        alt_rad = math.radians(altitude)
        
        # Calculate solar azimuth
        cos_azimuth = ((math.sin(decl_rad) * math.cos(lat_rad) - 
                       math.cos(decl_rad) * math.sin(lat_rad) * math.cos(hour_rad)) / 
                      math.cos(alt_rad))
        
        # Limit cos_azimuth to [-1, 1] to avoid domain errors
        cos_azimuth = max(-1.0, min(1.0, cos_azimuth))
        
        # Calculate azimuth angle
        azimuth = math.degrees(math.acos(cos_azimuth))
        
        # Adjust for morning hours (negative hour angle)
        if hour_angle < 0:
            azimuth = -azimuth
        
        # Convert to compass bearing (0° = South, positive westward)
        return azimuth
    
    @staticmethod
    def incident_angle(surface_tilt: float, surface_azimuth: float, 
                      solar_altitude: float, solar_azimuth: float) -> float:
        """
        Calculate angle of incidence on a surface.
        
        Args:
            surface_tilt: Surface tilt angle from horizontal in degrees (0° = horizontal, 90° = vertical)
            surface_azimuth: Surface azimuth angle in degrees (0° = South, positive westward)
            solar_altitude: Solar altitude angle in degrees
            solar_azimuth: Solar azimuth angle in degrees (0° = South, positive westward)
            
        Returns:
            Angle of incidence in degrees
        """
        # Convert angles to radians
        surf_tilt_rad = math.radians(surface_tilt)
        surf_azim_rad = math.radians(surface_azimuth)
        solar_alt_rad = math.radians(solar_altitude)
        solar_azim_rad = math.radians(solar_azimuth)
        
        # Calculate cosine of incident angle
        cos_incident = (math.cos(solar_alt_rad) * math.cos(solar_azim_rad - surf_azim_rad) * 
                       math.sin(surf_tilt_rad) + math.sin(solar_alt_rad) * math.cos(surf_tilt_rad))
        
        # Limit cos_incident to [-1, 1] to avoid domain errors
        cos_incident = max(-1.0, min(1.0, cos_incident))
        
        # Calculate incident angle
        incident_angle = math.degrees(math.acos(cos_incident))
        
        return incident_angle
    
    @staticmethod
    def direct_normal_irradiance(altitude: float, atmospheric_extinction: float = 0.14) -> float:
        """
        Calculate direct normal irradiance.
        
        Args:
            altitude: Solar altitude angle in degrees
            atmospheric_extinction: Atmospheric extinction coefficient (default: 0.14)
            
        Returns:
            Direct normal irradiance in W/m²
        """
        if altitude <= 0:
            return 0
        
        # Calculate air mass
        air_mass = 1 / math.sin(math.radians(altitude))
        
        # Calculate direct normal irradiance
        return SOLAR_CONSTANT * math.exp(-atmospheric_extinction * air_mass)
    
    @staticmethod
    def diffuse_horizontal_irradiance(direct_normal: float, altitude: float, 
                                    clearness_factor: float = 1.0) -> float:
        """
        Calculate diffuse horizontal irradiance.
        
        Args:
            direct_normal: Direct normal irradiance in W/m²
            altitude: Solar altitude angle in degrees
            clearness_factor: Sky clearness factor (default: 1.0)
            
        Returns:
            Diffuse horizontal irradiance in W/m²
        """
        if altitude <= 0:
            return 0
        
        # Calculate diffuse horizontal irradiance
        c = 0.095 + 0.04 * math.sin(math.radians(altitude))
        return c * direct_normal * clearness_factor
    
    @staticmethod
    def global_horizontal_irradiance(direct_normal: float, diffuse_horizontal: float, 
                                   altitude: float) -> float:
        """
        Calculate global horizontal irradiance.
        
        Args:
            direct_normal: Direct normal irradiance in W/m²
            diffuse_horizontal: Diffuse horizontal irradiance in W/m²
            altitude: Solar altitude angle in degrees
            
        Returns:
            Global horizontal irradiance in W/m²
        """
        if altitude <= 0:
            return 0
        
        # Calculate global horizontal irradiance
        return direct_normal * math.sin(math.radians(altitude)) + diffuse_horizontal
    
    @staticmethod
    def irradiance_on_tilted_surface(direct_normal: float, diffuse_horizontal: float, 
                                   global_horizontal: float, incident_angle: float, 
                                   surface_tilt: float, ground_reflectance: float = 0.2) -> float:
        """
        Calculate solar irradiance on a tilted surface.
        
        Args:
            direct_normal: Direct normal irradiance in W/m²
            diffuse_horizontal: Diffuse horizontal irradiance in W/m²
            global_horizontal: Global horizontal irradiance in W/m²
            incident_angle: Angle of incidence in degrees
            surface_tilt: Surface tilt angle from horizontal in degrees
            ground_reflectance: Ground reflectance (albedo) (default: 0.2)
            
        Returns:
            Total irradiance on tilted surface in W/m²
        """
        # Convert angles to radians
        incident_rad = math.radians(incident_angle)
        tilt_rad = math.radians(surface_tilt)
        
        # Calculate direct component
        if incident_angle < 90:
            direct_component = direct_normal * math.cos(incident_rad)
        else:
            direct_component = 0
        
        # Calculate diffuse component (isotropic model)
        diffuse_component = diffuse_horizontal * (1 + math.cos(tilt_rad)) / 2
        
        # Calculate ground-reflected component
        reflected_component = global_horizontal * ground_reflectance * (1 - math.cos(tilt_rad)) / 2
        
        # Calculate total irradiance
        return direct_component + diffuse_component + reflected_component
    
    @staticmethod
    def solar_heat_gain(irradiance: float, area: float, shgc: float, 
                       incident_angle_modifier: float = 1.0) -> float:
        """
        Calculate solar heat gain through a window.
        
        Args:
            irradiance: Solar irradiance on window surface in W/m²
            area: Window area in m²
            shgc: Solar heat gain coefficient at normal incidence
            incident_angle_modifier: Incident angle modifier (default: 1.0)
            
        Returns:
            Solar heat gain in W
        """
        return irradiance * area * shgc * incident_angle_modifier
    
    @staticmethod
    def incident_angle_modifier(incident_angle: float, glazing_layers: int = 1) -> float:
        """
        Calculate incident angle modifier for windows.
        
        Args:
            incident_angle: Angle of incidence in degrees
            glazing_layers: Number of glazing layers (default: 1)
            
        Returns:
            Incident angle modifier (dimensionless)
        """
        if incident_angle >= 90:
            return 0
        
        # Calculate incident angle modifier
        if glazing_layers == 1:
            # Single glazing
            return 1 - 0.0018 * incident_angle
        else:
            # Multiple glazing
            return 1 - 0.00259 * incident_angle
    
    @staticmethod
    def shading_coefficient_to_shgc(sc: float) -> float:
        """
        Convert shading coefficient to solar heat gain coefficient.
        
        Args:
            sc: Shading coefficient
            
        Returns:
            Solar heat gain coefficient
        """
        return 0.87 * sc
    
    @staticmethod
    def shgc_to_shading_coefficient(shgc: float) -> float:
        """
        Convert solar heat gain coefficient to shading coefficient.
        
        Args:
            shgc: Solar heat gain coefficient
            
        Returns:
            Shading coefficient
        """
        return shgc / 0.87