mabuseif commited on
Commit
35bc1fb
·
verified ·
1 Parent(s): 65c9c7f

Update utils/heat_transfer.py

Browse files
Files changed (1) hide show
  1. utils/heat_transfer.py +40 -14
utils/heat_transfer.py CHANGED
@@ -2,6 +2,7 @@
2
  Heat transfer calculation module for HVAC Load Calculator.
3
  This module provides enhanced calculations for conduction, convection, radiation,
4
  infiltration, and solar geometry, with improved modularity and error handling.
 
5
  """
6
 
7
  from typing import Optional, Tuple
@@ -106,7 +107,7 @@ class SolarCalculations:
106
  """
107
  self.validate_angle(latitude, "Latitude", -90, 90)
108
  self.validate_angle(declination, "Declination", -90, 90)
109
- self.validate_angle(hour_angle, "Hour angle", -180, 180)
110
  self.validate_angle(altitude, "Altitude", 0, 90)
111
 
112
  lat_rad = math.radians(latitude)
@@ -236,6 +237,7 @@ class HeatTransferCalculations:
236
  def conduction_heat_transfer(self, u_value: float, area: float, delta_t: float) -> float:
237
  """
238
  Calculate heat transfer by conduction.
 
239
 
240
  Args:
241
  u_value: Overall heat transfer coefficient in W/(m²·K)
@@ -253,6 +255,7 @@ class HeatTransferCalculations:
253
  def convection_heat_transfer(self, h: float, area: float, delta_t: float) -> float:
254
  """
255
  Calculate heat transfer by convection.
 
256
 
257
  Args:
258
  h: Convective heat transfer coefficient in W/(m²·K)
@@ -270,6 +273,7 @@ class HeatTransferCalculations:
270
  def radiation_heat_transfer(self, emissivity: float, area: float, t_surface: float, t_surroundings: float) -> float:
271
  """
272
  Calculate heat transfer by radiation using Stefan-Boltzmann law.
 
273
 
274
  Args:
275
  emissivity: Surface emissivity (0-1)
@@ -293,6 +297,7 @@ class HeatTransferCalculations:
293
  def thermal_lag_factor(self, thermal_mass: float, time_constant: float, time_step: float) -> float:
294
  """
295
  Calculate thermal lag factor for transient heat transfer.
 
296
 
297
  Args:
298
  thermal_mass: Thermal mass in J/K
@@ -311,41 +316,52 @@ class HeatTransferCalculations:
311
 
312
  return math.exp(-time_step / time_constant)
313
 
314
- def infiltration_heat_transfer(self, flow_rate: float, delta_t: float) -> float:
315
  """
316
  Calculate sensible heat transfer due to infiltration or ventilation.
 
317
 
318
  Args:
319
  flow_rate: Air flow rate in m³/s
320
  delta_t: Temperature difference in °C
 
 
 
321
 
322
  Returns:
323
  Sensible heat transfer rate in W
324
  """
325
  self.validate_inputs(delta_t, flow_rate=flow_rate)
326
- rho = 1.2 # Air density in kg/m³
327
- cp = 1005 # Specific heat of air in J/(kg·K)
328
- return flow_rate * rho * cp * delta_t
 
329
 
330
- def infiltration_latent_heat_transfer(self, flow_rate: float, delta_w: float) -> float:
331
  """
332
  Calculate latent heat transfer due to infiltration or ventilation.
 
333
 
334
  Args:
335
  flow_rate: Air flow rate in m³/s
336
  delta_w: Humidity ratio difference in kg/kg
 
 
 
337
 
338
  Returns:
339
  Latent heat transfer rate in W
340
  """
341
- self.validate_inputs(0, flow_rate=flow_rate)
342
- rho = 1.2 # Air density in kg/m³
343
- h_fg = 2501000 # Latent heat of vaporization in J/kg
 
344
  return flow_rate * rho * h_fg * delta_w
345
 
346
  def wind_pressure_difference(self, wind_speed: float, wind_coefficient: float = 0.4) -> float:
347
  """
348
  Calculate pressure difference due to wind.
 
349
 
350
  Args:
351
  wind_speed: Wind speed in m/s
@@ -359,12 +375,13 @@ class HeatTransferCalculations:
359
  if not 0 <= wind_coefficient <= 1:
360
  raise ValueError(f"Wind coefficient {wind_coefficient} must be between 0 and 1")
361
 
362
- rho = 1.2 # Air density in kg/m³
363
  return 0.5 * wind_coefficient * rho * wind_speed**2
364
 
365
  def stack_pressure_difference(self, height: float, indoor_temp: float, outdoor_temp: float) -> float:
366
  """
367
  Calculate pressure difference due to stack effect.
 
368
 
369
  Args:
370
  height: Height difference in m
@@ -380,7 +397,7 @@ class HeatTransferCalculations:
380
  raise ValueError("Temperatures must be positive in Kelvin")
381
 
382
  g = 9.81 # Gravitational acceleration in m/s²
383
- rho = 1.2 # Air density in kg/m³
384
  delta_t = abs(indoor_temp - outdoor_temp)
385
  t_avg = (indoor_temp + outdoor_temp) / 2
386
  return rho * g * height * delta_t / t_avg
@@ -388,6 +405,7 @@ class HeatTransferCalculations:
388
  def combined_pressure_difference(self, wind_pd: float, stack_pd: float) -> float:
389
  """
390
  Calculate combined pressure difference from wind and stack effects.
 
391
 
392
  Args:
393
  wind_pd: Wind pressure difference in Pa
@@ -404,6 +422,7 @@ class HeatTransferCalculations:
404
  pressure_difference: float) -> float:
405
  """
406
  Calculate infiltration flow rate using crack method.
 
407
 
408
  Args:
409
  crack_length: Total crack length in m
@@ -427,6 +446,7 @@ class HeatTransferCalculations:
427
  surface_absorptivity: float, surface_resistance: float) -> float:
428
  """
429
  Calculate sol-air temperature for a surface.
 
430
 
431
  Args:
432
  outdoor_temp: Outdoor air temperature in °C
@@ -453,6 +473,7 @@ class HeatTransferCalculations:
453
  shading_coefficient: float = 1.0) -> float:
454
  """
455
  Calculate solar heat gain through a surface.
 
456
 
457
  Args:
458
  irradiance: Solar irradiance on surface in W/m²
@@ -498,11 +519,16 @@ if __name__ == "__main__":
498
  u_value = 0.5 # W/(m²·K)
499
  area = 20.0 # m²
500
  delta_t = 10.0 # °C
 
 
501
  conduction = heat_transfer_calculator.conduction_heat_transfer(u_value, area, delta_t)
502
  print(f"Conduction Heat Transfer: {conduction:.2f} W")
503
 
 
 
 
 
 
504
  # Example psychrometric calculation
505
- temp = 25.0 # °C
506
- rh = 50.0 # %
507
- humidity_ratio = heat_transfer_calculator.psychrometrics.humidity_ratio(temp, rh)
508
  print(f"Humidity Ratio: {humidity_ratio:.6f} kg/kg")
 
2
  Heat transfer calculation module for HVAC Load Calculator.
3
  This module provides enhanced calculations for conduction, convection, radiation,
4
  infiltration, and solar geometry, with improved modularity and error handling.
5
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Sections 18.3-18.4.
6
  """
7
 
8
  from typing import Optional, Tuple
 
107
  """
108
  self.validate_angle(latitude, "Latitude", -90, 90)
109
  self.validate_angle(declination, "Declination", -90, 90)
110
+ self.validate_angle(hour_angle, "Hour angle", -180ublisher: https://www.w3schools.com/howto/tryit.asp?filename=trycss_form_checkbox
111
  self.validate_angle(altitude, "Altitude", 0, 90)
112
 
113
  lat_rad = math.radians(latitude)
 
237
  def conduction_heat_transfer(self, u_value: float, area: float, delta_t: float) -> float:
238
  """
239
  Calculate heat transfer by conduction.
240
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Equation 18.1.
241
 
242
  Args:
243
  u_value: Overall heat transfer coefficient in W/(m²·K)
 
255
  def convection_heat_transfer(self, h: float, area: float, delta_t: float) -> float:
256
  """
257
  Calculate heat transfer by convection.
258
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Section 18.3.
259
 
260
  Args:
261
  h: Convective heat transfer coefficient in W/(m²·K)
 
273
  def radiation_heat_transfer(self, emissivity: float, area: float, t_surface: float, t_surroundings: float) -> float:
274
  """
275
  Calculate heat transfer by radiation using Stefan-Boltzmann law.
276
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Section 18.3.
277
 
278
  Args:
279
  emissivity: Surface emissivity (0-1)
 
297
  def thermal_lag_factor(self, thermal_mass: float, time_constant: float, time_step: float) -> float:
298
  """
299
  Calculate thermal lag factor for transient heat transfer.
300
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Section 18.5.
301
 
302
  Args:
303
  thermal_mass: Thermal mass in J/K
 
316
 
317
  return math.exp(-time_step / time_constant)
318
 
319
+ def infiltration_heat_transfer(self, flow_rate: float, delta_t: float, t_db: float, rh: float, p_atm: float = 101325) -> float:
320
  """
321
  Calculate sensible heat transfer due to infiltration or ventilation.
322
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Equation 18.5.
323
 
324
  Args:
325
  flow_rate: Air flow rate in m³/s
326
  delta_t: Temperature difference in °C
327
+ t_db: Dry-bulb temperature in °C (for air properties)
328
+ rh: Relative humidity in % (for air properties)
329
+ p_atm: Atmospheric pressure in Pa (default: 101325 Pa)
330
 
331
  Returns:
332
  Sensible heat transfer rate in W
333
  """
334
  self.validate_inputs(delta_t, flow_rate=flow_rate)
335
+ w = self.psychrometrics.humidity_ratio(t_db, rh, p_atm)
336
+ rho = self.psychrometrics.density(t_db, w, p_atm)
337
+ c_p = 1006 + w * 1860 # Specific heat of moist air in J/(kg·K)
338
+ return flow_rate * rho * c_p * delta_t
339
 
340
+ def infiltration_latent_heat_transfer(self, flow_rate: float, delta_w: float, t_db: float, rh: float, p_atm: float = 101325) -> float:
341
  """
342
  Calculate latent heat transfer due to infiltration or ventilation.
343
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Equation 18.6.
344
 
345
  Args:
346
  flow_rate: Air flow rate in m³/s
347
  delta_w: Humidity ratio difference in kg/kg
348
+ t_db: Dry-bulb temperature in °C (for air properties)
349
+ rh: Relative humidity in % (for air properties)
350
+ p_atm: Atmospheric pressure in Pa (default: 101325 Pa)
351
 
352
  Returns:
353
  Latent heat transfer rate in W
354
  """
355
+ self.validate_inputs(t_db, flow_rate=flow_rate)
356
+ w = self.psychrometrics.humidity_ratio(t_db, rh, p_atm)
357
+ rho = self.psychrometrics.density(t_db, w, p_atm)
358
+ h_fg = 2501000 + 1840 * t_db # Latent heat of vaporization in J/kg
359
  return flow_rate * rho * h_fg * delta_w
360
 
361
  def wind_pressure_difference(self, wind_speed: float, wind_coefficient: float = 0.4) -> float:
362
  """
363
  Calculate pressure difference due to wind.
364
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 16, Section 16.3.
365
 
366
  Args:
367
  wind_speed: Wind speed in m/s
 
375
  if not 0 <= wind_coefficient <= 1:
376
  raise ValueError(f"Wind coefficient {wind_coefficient} must be between 0 and 1")
377
 
378
+ rho = 1.2 # Air density in kg/m³ (simplified for wind calculations)
379
  return 0.5 * wind_coefficient * rho * wind_speed**2
380
 
381
  def stack_pressure_difference(self, height: float, indoor_temp: float, outdoor_temp: float) -> float:
382
  """
383
  Calculate pressure difference due to stack effect.
384
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 16, Section 16.3.
385
 
386
  Args:
387
  height: Height difference in m
 
397
  raise ValueError("Temperatures must be positive in Kelvin")
398
 
399
  g = 9.81 # Gravitational acceleration in m/s²
400
+ rho = 1.2 # Air density in kg/m³ (simplified for stack calculations)
401
  delta_t = abs(indoor_temp - outdoor_temp)
402
  t_avg = (indoor_temp + outdoor_temp) / 2
403
  return rho * g * height * delta_t / t_avg
 
405
  def combined_pressure_difference(self, wind_pd: float, stack_pd: float) -> float:
406
  """
407
  Calculate combined pressure difference from wind and stack effects.
408
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 16, Section 16.3.
409
 
410
  Args:
411
  wind_pd: Wind pressure difference in Pa
 
422
  pressure_difference: float) -> float:
423
  """
424
  Calculate infiltration flow rate using crack method.
425
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 16, Section 16.4.
426
 
427
  Args:
428
  crack_length: Total crack length in m
 
446
  surface_absorptivity: float, surface_resistance: float) -> float:
447
  """
448
  Calculate sol-air temperature for a surface.
449
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Section 18.4.
450
 
451
  Args:
452
  outdoor_temp: Outdoor air temperature in °C
 
473
  shading_coefficient: float = 1.0) -> float:
474
  """
475
  Calculate solar heat gain through a surface.
476
+ Reference: ASHRAE Handbook—Fundamentals (2017), Chapter 18, Section 18.4.
477
 
478
  Args:
479
  irradiance: Solar irradiance on surface in W/m²
 
519
  u_value = 0.5 # W/(m²·K)
520
  area = 20.0 # m²
521
  delta_t = 10.0 # °C
522
+ t_db = 25.0 # °C
523
+ rh = 50.0 # %
524
  conduction = heat_transfer_calculator.conduction_heat_transfer(u_value, area, delta_t)
525
  print(f"Conduction Heat Transfer: {conduction:.2f} W")
526
 
527
+ # Example infiltration calculation
528
+ flow_rate = 0.05 # m³/s
529
+ infiltration = heat_transfer_calculator.infiltration_heat_transfer(flow_rate, delta_t, t_db, rh)
530
+ print(f"Infiltration Heat Transfer: {infiltration:.2f} W")
531
+
532
  # Example psychrometric calculation
533
+ humidity_ratio = heat_transfer_calculator.psychrometrics.humidity_ratio(t_db, rh)
 
 
534
  print(f"Humidity Ratio: {humidity_ratio:.6f} kg/kg")