hvac / app.py
awais0300's picture
Update app.py
df0f425 verified
import os
import numpy as np
import pandas as pd
import gradio as gr
import matplotlib.pyplot as plt
from fpdf import FPDF
from groq import Groq
# Set up Groq API
api_key = "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"
client = Groq(api_key=api_key)
# HVAC Load Calculation Logic
def calculate_load(room_size, insulation_type, climate, occupants, outdoor_air, dust_efficiency, design_temp,
material, infiltration, internal_loads, lighting_cooling_factor, heat_gain_occupant,
space_per_occupant, sensible_heat_factor, solar_heat_gain, shading_coefficients, cooling_load_temp_diff,
is_commercial=False):
try:
# R-values for selected materials (per inch)
material_r_values = {
"Fiberglass Insulation (Batt or Roll)": 3.7,
"Expanded Polystyrene (EPS)": 4.0,
"Extruded Polystyrene (XPS)": 5.0,
"Spray Foam Insulation": 6.0,
"Wood (Softwood)": 1.41,
"Concrete (6\" thick)": 1.11,
"Brick": 0.80,
"Double Glazed Glass": 2.0,
"Mineral Wool Insulation": 3.5,
"Cellulose Insulation": 3.6,
}
# Get R-value based on selected material
thermal_resistance = material_r_values.get(material, 1)
# Placeholder calculations (replace with ASHRAE-compliant formulas)
base_load = room_size * 10 # Base load
insulation_factor = 0.8 if insulation_type == "Good" else 1.2
climate_factor = 1.5 if climate == "Hot" else 0.8
occupant_factor = occupants * heat_gain_occupant
# Ventilation load
ventilation_load = outdoor_air * dust_efficiency * design_temp
# Infiltration load
infiltration_load = room_size * infiltration
# Internal loads
internal_load = internal_loads * room_size
# Lighting load
lighting_load = room_size * lighting_cooling_factor
# Solar heat gain
solar_load = solar_heat_gain * shading_coefficients
# Wall load
wall_load = room_size * cooling_load_temp_diff * thermal_resistance
# Total load in BTU/hr
total_load_btu = (
base_load * insulation_factor * climate_factor +
occupant_factor + ventilation_load + infiltration_load +
internal_load + lighting_load + solar_load + wall_load
)
# Convert to kW
total_load_kw = total_load_btu * 0.000293071
# Calculate tonnage
required_tonnage = total_load_btu / 12000
# Commercial building load: increase size factor for larger spaces
if is_commercial:
total_load_btu *= 1.5 # Scale load for commercial buildings
required_tonnage = total_load_btu / 12000
# Maintenance recommendations based on usage
maintenance_recommendation = ""
if room_size > 1000:
maintenance_recommendation = "Regular HVAC maintenance is recommended, check air filters and ducts."
# Energy savings tips based on input
energy_savings_tips = "Consider upgrading insulation or using energy-efficient HVAC systems for better performance."
# Determine recommendation
recommendation = "AC is required" if climate == "Hot" else "Heater is required"
return total_load_btu, total_load_kw, required_tonnage, recommendation, maintenance_recommendation, energy_savings_tips, {
"Base Load": base_load,
"Ventilation Load": ventilation_load,
"Infiltration Load": infiltration_load,
"Internal Load": internal_load,
"Lighting Load": lighting_load,
"Solar Load": solar_load,
"Wall Load": wall_load,
}
except Exception as e:
return f"Error in calculation: {e}", None, None, None, None, None, {}
# Visualization Function
def generate_plot(load_components):
try:
plt.figure(figsize=(10, 6))
plt.bar(load_components.keys(), load_components.values(), color='skyblue')
plt.xlabel("Load Components")
plt.ylabel("Load (BTU/hr)")
plt.title("HVAC Load Breakdown")
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
plot_path = "load_components.png"
plt.savefig(plot_path)
plt.close()
return plot_path
except Exception as e:
return f"Error in generating plot: {e}"
# Gradio Interface Function
def app(room_size, insulation_type, climate, occupants, outdoor_air, dust_efficiency, design_temp,
material, infiltration, internal_loads, lighting_cooling_factor, heat_gain_occupant,
space_per_occupant, sensible_heat_factor, solar_heat_gain, shading_coefficients, cooling_load_temp_diff,
hours_of_usage, cost_per_kwh, is_commercial):
try:
total_load_btu, total_load_kw, required_tonnage, recommendation, maintenance_recommendation, energy_savings_tips, load_components = calculate_load(
room_size, insulation_type, climate, occupants, outdoor_air, dust_efficiency, design_temp,
material, infiltration, internal_loads, lighting_cooling_factor, heat_gain_occupant,
space_per_occupant, sensible_heat_factor, solar_heat_gain, shading_coefficients, cooling_load_temp_diff,
is_commercial
)
# Calculate energy consumption and cost
energy_consumed_kwh = total_load_kw * hours_of_usage
total_cost = energy_consumed_kwh * cost_per_kwh
plot_path = generate_plot(load_components)
# Generate a report PDF
report_pdf = FPDF()
report_pdf.add_page()
report_pdf.set_font("Arial", size=12)
report_pdf.cell(200, 10, txt="HVAC Load Calculation Report", ln=True, align="C")
report_pdf.ln(10)
report_pdf.multi_cell(0, 10, txt=f"Room Size: {room_size} sq ft\n"
f"Insulation Type: {insulation_type}\n"
f"Climate: {climate}\n"
f"Number of Occupants: {occupants}\n"
f"Recommended Tonnage: {required_tonnage:.2f} tons\n"
f"Total HVAC Load: {total_load_btu:.2f} BTU/hr ({total_load_kw:.2f} kW)\n"
f"Energy Consumed: {energy_consumed_kwh:.2f} kWh\n"
f"Total Cost: ${total_cost:.2f}\n"
f"Maintenance Recommendation: {maintenance_recommendation}\n"
f"Energy Savings Tips: {energy_savings_tips}")
report_pdf.output("hvac_report.pdf")
return (
f"Total HVAC Load: {total_load_btu:.2f} BTU/hr ({total_load_kw:.2f} kW)\n"
f"Recommended Tonnage: {required_tonnage:.2f} tons\n"
f"Recommendation: {recommendation}\n"
f"Energy Consumed: {energy_consumed_kwh:.2f} kWh\n"
f"Total Cost: ${total_cost:.2f}\n"
f"Maintenance Recommendation: {maintenance_recommendation}\n"
f"Energy Savings Tips: {energy_savings_tips}",
plot_path,
"hvac_report.pdf"
)
except Exception as e:
return f"Error: {e}", None, None
# Gradio Interface
interface = gr.Interface(
fn=app,
inputs=[
gr.Number(label="Room Size (sq ft)"),
gr.Radio(["Good", "Poor"], label="Insulation Type"),
gr.Radio(["Hot", "Cold"], label="Climate"),
gr.Number(label="Number of Occupants"),
gr.Number(label="Outdoor Air (CFM)", value=0),
gr.Number(label="ASHRAE Dust Spot Efficiency (%)", value=100),
gr.Number(label="Design Temperature (°C)", value=24),
gr.Dropdown(
label="Building Material",
choices=["Fiberglass Insulation (Batt or Roll)", "Expanded Polystyrene (EPS)", "Extruded Polystyrene (XPS)",
"Spray Foam Insulation", "Wood (Softwood)", "Concrete (6\" thick)", "Brick", "Double Glazed Glass",
"Mineral Wool Insulation", "Cellulose Insulation"]
),
gr.Number(label="Infiltration (CFM)"),
gr.Number(label="Internal Loads (W/m²)"),
gr.Number(label="Lighting Cooling Factor (W/m²)"),
gr.Number(label="Heat Gain per Occupant (W)"),
gr.Number(label="Space per Occupant (sq ft)"),
gr.Number(label="Sensible Heat Factor for Occupants"),
gr.Number(label="Solar Heat Gain (W/m²)"),
gr.Number(label="Shading Coefficients"),
gr.Number(label="Cooling Load Temperature Difference (°C)"),
gr.Number(label="Hours of Usage (hrs/day)", value=5),
gr.Number(label="Cost per kWh ($)", value=0.12),
gr.Checkbox(label="Is Commercial Building?", value=False)
],
outputs=[
gr.Textbox(label="Results"),
gr.Image(label="Load Breakdown Plot"),
gr.File(label="Download HVAC Report")
]
)
interface.launch()