Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,22 @@ import gradio as gr
|
|
| 2 |
import math
|
| 3 |
import itertools
|
| 4 |
|
|
|
|
| 5 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 6 |
|
|
|
|
| 7 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 8 |
if voltage > 0 and current > 0:
|
| 9 |
apparent_power = math.sqrt(3) * voltage * current
|
| 10 |
real_power = apparent_power * power_factor / 1000
|
|
|
|
| 11 |
try:
|
| 12 |
reactive_power = math.sqrt((apparent_power / 1000) ** 2 - real_power ** 2)
|
| 13 |
except ValueError:
|
| 14 |
reactive_power = 0.0
|
|
|
|
| 15 |
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
|
|
|
| 16 |
return {
|
| 17 |
"apparent_power": round(apparent_power, 2),
|
| 18 |
"real_power": round(real_power, 2),
|
|
@@ -26,114 +31,108 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 26 |
def design_unique_capacitor_bank(reactive_power, num_caps):
|
| 27 |
if reactive_power > 0 and num_caps > 0:
|
| 28 |
best_combo = None
|
| 29 |
-
min_error = float("inf")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Allow repeated use only when needed
|
| 35 |
-
extended = list(combo)
|
| 36 |
-
while len(extended) < num_caps:
|
| 37 |
-
extended.append(min(combo)) # repeat the smallest one
|
| 38 |
-
combo = tuple(extended)
|
| 39 |
|
| 40 |
total = sum(combo)
|
| 41 |
-
error = abs(total - reactive_power)
|
| 42 |
|
| 43 |
-
if
|
| 44 |
-
min_error = error
|
| 45 |
best_combo = combo
|
| 46 |
-
|
| 47 |
-
if error == 0:
|
| 48 |
break
|
| 49 |
|
| 50 |
if best_combo:
|
| 51 |
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 52 |
total_kvar = sum(best_combo)
|
| 53 |
-
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 54 |
-
if abs(total_kvar - reactive_power) > 0.5:
|
| 55 |
-
message += ". Small mismatch detected. Fine-tuning may be required."
|
| 56 |
-
mismatch = True
|
| 57 |
-
else:
|
| 58 |
-
mismatch = False
|
| 59 |
return {
|
| 60 |
"suggested_capacitors": suggested_capacitors,
|
| 61 |
"total_kvar": round(total_kvar, 2),
|
| 62 |
"message": message,
|
| 63 |
-
"mismatch":
|
| 64 |
}
|
| 65 |
else:
|
| 66 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
return None
|
| 68 |
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 73 |
if power_results:
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
return (
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
)
|
| 81 |
else:
|
| 82 |
-
return (
|
| 83 |
-
"⚠️ Please enter valid Voltage and Current!",
|
| 84 |
-
"", "", ""
|
| 85 |
-
)
|
| 86 |
|
| 87 |
|
| 88 |
-
def
|
| 89 |
-
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
if cap_bank_design:
|
| 94 |
-
suggested_caps = "<br>".join(
|
| 95 |
-
[f"🔹 Capacitor {i+1}: **{c}**" for i, c in enumerate(cap_bank_design["suggested_capacitors"])]
|
| 96 |
)
|
| 97 |
-
return
|
| 98 |
else:
|
| 99 |
-
return "No
|
| 100 |
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
with gr.Blocks(title="Capacitor Bank Designer") as app:
|
| 105 |
-
gr.Markdown("## ⚡ Three-Phase Power & Capacitor Bank Designer")
|
| 106 |
-
|
| 107 |
with gr.Row():
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
inputs=[
|
| 123 |
-
outputs=[
|
| 124 |
)
|
| 125 |
|
| 126 |
-
gr.Markdown("
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
num_caps = gr.Number(label="Number of Capacitors", value=3, precision=0)
|
| 130 |
-
bank_btn = gr.Button("Design Capacitor Bank")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
cap_message = gr.HTML()
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
app.launch()
|
|
|
|
| 2 |
import math
|
| 3 |
import itertools
|
| 4 |
|
| 5 |
+
# Available Capacitor Units (kVAR)
|
| 6 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 7 |
|
| 8 |
+
|
| 9 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 10 |
if voltage > 0 and current > 0:
|
| 11 |
apparent_power = math.sqrt(3) * voltage * current
|
| 12 |
real_power = apparent_power * power_factor / 1000
|
| 13 |
+
|
| 14 |
try:
|
| 15 |
reactive_power = math.sqrt((apparent_power / 1000) ** 2 - real_power ** 2)
|
| 16 |
except ValueError:
|
| 17 |
reactive_power = 0.0
|
| 18 |
+
|
| 19 |
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
| 20 |
+
|
| 21 |
return {
|
| 22 |
"apparent_power": round(apparent_power, 2),
|
| 23 |
"real_power": round(real_power, 2),
|
|
|
|
| 31 |
def design_unique_capacitor_bank(reactive_power, num_caps):
|
| 32 |
if reactive_power > 0 and num_caps > 0:
|
| 33 |
best_combo = None
|
|
|
|
| 34 |
|
| 35 |
+
for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
|
| 36 |
+
if len(set(combo)) < num_caps:
|
| 37 |
+
continue # Avoid reuse unless necessary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
total = sum(combo)
|
|
|
|
| 40 |
|
| 41 |
+
if abs(total - reactive_power) < 0.01:
|
|
|
|
| 42 |
best_combo = combo
|
|
|
|
|
|
|
| 43 |
break
|
| 44 |
|
| 45 |
if best_combo:
|
| 46 |
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 47 |
total_kvar = sum(best_combo)
|
| 48 |
+
message = f"✅ Exact Match! Total Compensation: {round(total_kvar, 2)} kVAR"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return {
|
| 50 |
"suggested_capacitors": suggested_capacitors,
|
| 51 |
"total_kvar": round(total_kvar, 2),
|
| 52 |
"message": message,
|
| 53 |
+
"mismatch": False
|
| 54 |
}
|
| 55 |
else:
|
| 56 |
+
return {
|
| 57 |
+
"message": "❌ No exact match found with the selected number of capacitors.",
|
| 58 |
+
"suggested_capacitors": [],
|
| 59 |
+
"total_kvar": 0,
|
| 60 |
+
"mismatch": True
|
| 61 |
+
}
|
| 62 |
return None
|
| 63 |
|
| 64 |
|
| 65 |
+
reactive_power_state = gr.State(0.0)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def compute_reactive_power(voltage, current, power_factor):
|
| 69 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 70 |
if power_results:
|
| 71 |
+
apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
|
| 72 |
+
real_power_out = f"Real Power: **{power_results['real_power']} kW**"
|
| 73 |
+
reactive_power_out = f"Reactive Power: **{power_results['reactive_power']} kVAR**"
|
| 74 |
+
calculated_pf_out = f"Calculated Power Factor: **{power_results['calculated_pf']}**"
|
| 75 |
return (
|
| 76 |
+
apparent_power_out,
|
| 77 |
+
real_power_out,
|
| 78 |
+
reactive_power_out,
|
| 79 |
+
calculated_pf_out,
|
| 80 |
+
power_results['reactive_power']
|
| 81 |
)
|
| 82 |
else:
|
| 83 |
+
return ("Invalid Input", "", "", "", 0.0)
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
+
def process_capacitor_bank(reactive_power, num_caps):
|
| 87 |
+
cap_bank_design = design_unique_capacitor_bank(reactive_power, num_caps)
|
| 88 |
+
if cap_bank_design and not cap_bank_design['mismatch']:
|
| 89 |
+
suggested_capacitors_text = "<br>".join(
|
| 90 |
+
[f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
+
return suggested_capacitors_text, cap_bank_design['message']
|
| 93 |
else:
|
| 94 |
+
return "<span style='color:red'>No exact match found.</span>", cap_bank_design['message']
|
| 95 |
|
| 96 |
|
| 97 |
+
with gr.Blocks(title="Three-Phase Power Calculator") as app:
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
# ⚡ Three-Phase Power Calculator & Capacitor Bank Designer
|
| 100 |
+
Compute power parameters and precisely design your capacitor bank with no mismatch.
|
| 101 |
+
""")
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
with gr.Row():
|
| 104 |
+
voltage_input = gr.Number(label="Voltage (V)", value=415)
|
| 105 |
+
current_input = gr.Number(label="Current (A)", value=250)
|
| 106 |
+
pf_input = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)
|
| 107 |
+
|
| 108 |
+
freq_input = gr.Radio(label="Select Frequency (Hz)", choices=[50, 60], value=50)
|
| 109 |
+
compute_btn = gr.Button("Compute Power")
|
| 110 |
+
|
| 111 |
+
app_output = gr.HTML()
|
| 112 |
+
real_output = gr.HTML()
|
| 113 |
+
reactive_output = gr.HTML()
|
| 114 |
+
pf_output = gr.HTML()
|
| 115 |
+
|
| 116 |
+
compute_btn.click(
|
| 117 |
+
compute_reactive_power,
|
| 118 |
+
inputs=[voltage_input, current_input, pf_input],
|
| 119 |
+
outputs=[app_output, real_output, reactive_output, pf_output, reactive_power_state]
|
| 120 |
)
|
| 121 |
|
| 122 |
+
gr.Markdown("""
|
| 123 |
+
### Capacitor Bank Design (Only exact match results are accepted)
|
| 124 |
+
""")
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
num_caps_input = gr.Number(label="Enter Number of Capacitors", value=2)
|
| 127 |
+
cap_btn = gr.Button("Design Capacitor Bank")
|
| 128 |
+
cap_suggestion = gr.HTML()
|
| 129 |
cap_message = gr.HTML()
|
| 130 |
|
| 131 |
+
cap_btn.click(
|
| 132 |
+
process_capacitor_bank,
|
| 133 |
+
inputs=[reactive_power_state, num_caps_input],
|
| 134 |
+
outputs=[cap_suggestion, cap_message]
|
| 135 |
+
)
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
app.launch()
|