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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import math
|
| 3 |
+
import itertools
|
| 4 |
+
import ezdxf
|
| 5 |
+
import os
|
| 6 |
+
import groq
|
| 7 |
+
from ezdxf import zoom
|
| 8 |
+
from ezdxf.enums import TextEntityAlignment
|
| 9 |
+
|
| 10 |
+
# Initialize Groq client
|
| 11 |
+
client = groq.Client(api_key=os.getenv("GROQ_API_KEY"))
|
| 12 |
+
|
| 13 |
+
# Available Capacitor Units (kVAR)
|
| 14 |
+
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 15 |
+
|
| 16 |
+
# Prompt Groq for explanation (optional)
|
| 17 |
+
def ask_groq(prompt):
|
| 18 |
+
try:
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model="llama3-8b-8192",
|
| 21 |
+
messages=[{"role": "user", "content": prompt}]
|
| 22 |
+
)
|
| 23 |
+
return response.choices[0].message.content
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Groq Error: {str(e)}"
|
| 26 |
+
|
| 27 |
+
def calculate_power_parameters(voltage, current, power_factor):
|
| 28 |
+
if voltage > 0 and current > 0:
|
| 29 |
+
apparent_power = math.sqrt(3) * voltage * current
|
| 30 |
+
real_power = apparent_power * power_factor / 1000
|
| 31 |
+
try:
|
| 32 |
+
reactive_power = math.sqrt((apparent_power / 1000) ** 2 - real_power ** 2)
|
| 33 |
+
except ValueError:
|
| 34 |
+
reactive_power = 0.0
|
| 35 |
+
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
| 36 |
+
return {
|
| 37 |
+
"apparent_power": round(apparent_power, 2),
|
| 38 |
+
"real_power": round(real_power, 2),
|
| 39 |
+
"reactive_power": round(reactive_power, 2),
|
| 40 |
+
"calculated_pf": round(calculated_pf, 2)
|
| 41 |
+
}
|
| 42 |
+
else:
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
def design_capacitor_bank(reactive_power, num_caps):
|
| 46 |
+
if reactive_power > 0 and num_caps > 0:
|
| 47 |
+
best_combo = None
|
| 48 |
+
min_error = float('inf')
|
| 49 |
+
|
| 50 |
+
# Allow repetition freely to match reactive power
|
| 51 |
+
combos = itertools.combinations_with_replacement(available_capacitors, num_caps)
|
| 52 |
+
for combo in combos:
|
| 53 |
+
total = sum(combo)
|
| 54 |
+
error = abs(total - reactive_power)
|
| 55 |
+
if error < min_error:
|
| 56 |
+
min_error = error
|
| 57 |
+
best_combo = combo
|
| 58 |
+
if error == 0:
|
| 59 |
+
break
|
| 60 |
+
|
| 61 |
+
if best_combo:
|
| 62 |
+
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 63 |
+
total_kvar = sum(best_combo)
|
| 64 |
+
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 65 |
+
return {
|
| 66 |
+
"suggested_capacitors": suggested_capacitors,
|
| 67 |
+
"total_kvar": round(total_kvar, 2),
|
| 68 |
+
"message": message,
|
| 69 |
+
"combo": best_combo
|
| 70 |
+
}
|
| 71 |
+
else:
|
| 72 |
+
return {"message": "Could not find a suitable combination."}
|
| 73 |
+
else:
|
| 74 |
+
return None
|
| 75 |
+
|
| 76 |
+
def create_dxf_capacitor_bank(capacitors):
|
| 77 |
+
doc = ezdxf.new()
|
| 78 |
+
msp = doc.modelspace()
|
| 79 |
+
x = 0
|
| 80 |
+
y = 0
|
| 81 |
+
row_width = 15 # Distance between capacitors in a row
|
| 82 |
+
row_height = 20
|
| 83 |
+
max_in_row = 5
|
| 84 |
+
|
| 85 |
+
for idx, cap in enumerate(capacitors):
|
| 86 |
+
label = f"{cap} kVAR"
|
| 87 |
+
# Draw rectangle for capacitor
|
| 88 |
+
points = [(x, y), (x + 10, y), (x + 10, y + 10), (x, y + 10), (x, y)]
|
| 89 |
+
msp.add_lwpolyline(points, close=True, dxfattribs={'color': 3}) # Color 3 = Green
|
| 90 |
+
|
| 91 |
+
# Add Text with more control
|
| 92 |
+
text = msp.add_text(label, dxfattribs={
|
| 93 |
+
'height': 2.5,
|
| 94 |
+
'color': 4, # color Cyan
|
| 95 |
+
'style': 'STANDARD', # You can define text styles in DXF
|
| 96 |
+
'halign': TextEntityAlignment.CENTER, # Horizontal alignment
|
| 97 |
+
'valign': TextEntityAlignment.BOTTOM, # Vertical alignment
|
| 98 |
+
})
|
| 99 |
+
text.dxf.insert = (x + 5, y + 5) # Position at center of rectangle
|
| 100 |
+
|
| 101 |
+
x += row_width
|
| 102 |
+
if (idx + 1) % max_in_row == 0: # Move to the next row
|
| 103 |
+
x = 0
|
| 104 |
+
y += row_height
|
| 105 |
+
|
| 106 |
+
# Add a title
|
| 107 |
+
title_text = msp.add_text("Capacitor Bank Layout", dxfattribs={'height': 5, 'color': 1})
|
| 108 |
+
title_text.dxf.insert = (0, y + 30)
|
| 109 |
+
|
| 110 |
+
# Zoom to extents
|
| 111 |
+
zoom.extents(msp, factor=1.1) # Add a small padding
|
| 112 |
+
|
| 113 |
+
output_path = "capacitor_bank_layout.dxf"
|
| 114 |
+
doc.saveas(output_path)
|
| 115 |
+
return output_path
|
| 116 |
+
|
| 117 |
+
def reactive_power_first(voltage, current, power_factor):
|
| 118 |
+
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 119 |
+
if power_results:
|
| 120 |
+
apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
|
| 121 |
+
real_power_out = f"Real Power: **{power_results['real_power']} kW**"
|
| 122 |
+
reactive_power_out = f"Reactive Power: **{power_results['reactive_power']} kVAR**"
|
| 123 |
+
calculated_pf_out = f"Calculated Power Factor: **{power_results['calculated_pf']}**"
|
| 124 |
+
return (
|
| 125 |
+
apparent_power_out,
|
| 126 |
+
real_power_out,
|
| 127 |
+
reactive_power_out,
|
| 128 |
+
calculated_pf_out,
|
| 129 |
+
power_results['reactive_power']
|
| 130 |
+
)
|
| 131 |
+
else:
|
| 132 |
+
return ("⚠️ Please enter valid Voltage and Current!", "", "", "", 0)
|
| 133 |
+
|
| 134 |
+
def finalize_capacitor_bank(reactive_power, num_caps):
|
| 135 |
+
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
| 136 |
+
if cap_bank_design and cap_bank_design.get("suggested_capacitors"):
|
| 137 |
+
suggested_capacitors_text = "<br>".join(
|
| 138 |
+
[f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 139 |
+
)
|
| 140 |
+
dxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])
|
| 141 |
+
return suggested_capacitors_text, cap_bank_design['message'], dxf_path
|
| 142 |
+
else:
|
| 143 |
+
return "Could not find a suitable combination.", "", None
|
| 144 |
+
|
| 145 |
+
with gr.Blocks() as iface:
|
| 146 |
+
gr.Markdown("# ⚡ Three-Phase Power Calculator - Reactive Power Compensation")
|
| 147 |
+
gr.Markdown("""
|
| 148 |
+
Step 1: Enter system parameters to calculate apparent and reactive power.<br>
|
| 149 |
+
Step 2: Input number of capacitors to compute optimal configuration.<br>
|
| 150 |
+
Step 3: Download AutoCAD (.dxf) layout.
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
voltage = gr.Number(label="Enter Voltage (V)", value=415)
|
| 155 |
+
current = gr.Number(label="Enter Current (A)", value=250)
|
| 156 |
+
power_factor = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)
|
| 157 |
+
frequency = gr.Radio(label="Select Frequency", choices=[50, 60], value=50)
|
| 158 |
+
|
| 159 |
+
calc_btn = gr.Button("🔍 Calculate Power Parameters")
|
| 160 |
+
|
| 161 |
+
apparent_power_out = gr.HTML()
|
| 162 |
+
real_power_out = gr.HTML()
|
| 163 |
+
reactive_power_out = gr.HTML()
|
| 164 |
+
calculated_pf_out = gr.HTML()
|
| 165 |
+
reactive_value = gr.Number(visible=False)
|
| 166 |
+
|
| 167 |
+
calc_btn.click(
|
| 168 |
+
fn=reactive_power_first,
|
| 169 |
+
inputs=[voltage, current, power_factor],
|
| 170 |
+
outputs=[
|
| 171 |
+
apparent_power_out,
|
| 172 |
+
real_power_out,
|
| 173 |
+
reactive_power_out,
|
| 174 |
+
calculated_pf_out,
|
| 175 |
+
reactive_value
|
| 176 |
+
]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
gr.Markdown("### ➕ Enter number of capacitors to compensate reactive power:")
|
| 180 |
+
num_caps_input = gr.Number(label="Number of Capacitors", precision=0)
|
| 181 |
+
finalize_btn = gr.Button("⚙️ Generate Capacitor Bank")
|
| 182 |
+
|
| 183 |
+
capacitor_out = gr.HTML()
|
| 184 |
+
total_comp_out = gr.HTML()
|
| 185 |
+
dxf_file = gr.File(label="📥 Download AutoCAD File")
|
| 186 |
+
|
| 187 |
+
finalize_btn.click(
|
| 188 |
+
fn=finalize_capacitor_bank,
|
| 189 |
+
inputs=[reactive_value, num_caps_input],
|
| 190 |
+
outputs=[capacitor_out, total_comp_out, dxf_file]
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
if __name__ == "__main__":
|
| 194 |
+
iface.launch()
|