Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import gradio as gr
|
|
| 2 |
import math
|
| 3 |
import itertools
|
| 4 |
|
| 5 |
-
|
| 6 |
-
available_capacitors = [30,25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 7 |
|
| 8 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 9 |
if voltage > 0 and current > 0:
|
|
@@ -14,7 +13,6 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 14 |
except ValueError:
|
| 15 |
reactive_power = 0.0
|
| 16 |
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
| 17 |
-
|
| 18 |
return {
|
| 19 |
"apparent_power": round(apparent_power, 2),
|
| 20 |
"real_power": round(real_power, 2),
|
|
@@ -24,12 +22,21 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 24 |
else:
|
| 25 |
return None
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
if reactive_power > 0 and num_caps > 0:
|
| 29 |
best_combo = None
|
| 30 |
-
min_error = float(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
|
| 33 |
total = sum(combo)
|
| 34 |
error = abs(total - reactive_power)
|
| 35 |
|
|
@@ -45,81 +52,88 @@ def design_capacitor_bank(reactive_power, num_caps):
|
|
| 45 |
total_kvar = sum(best_combo)
|
| 46 |
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 47 |
if abs(total_kvar - reactive_power) > 0.5:
|
| 48 |
-
message += "
|
|
|
|
|
|
|
|
|
|
| 49 |
return {
|
| 50 |
"suggested_capacitors": suggested_capacitors,
|
| 51 |
"total_kvar": round(total_kvar, 2),
|
| 52 |
-
"message": message
|
|
|
|
| 53 |
}
|
| 54 |
else:
|
| 55 |
-
return {"message": "
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
def
|
|
|
|
| 60 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 61 |
if power_results:
|
|
|
|
| 62 |
return (
|
| 63 |
-
f"
|
| 64 |
-
f"
|
| 65 |
-
f"
|
| 66 |
-
f"
|
| 67 |
-
round(power_results['reactive_power'], 2)
|
| 68 |
)
|
| 69 |
else:
|
| 70 |
-
return (
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
def step2_design_bank(reactive_power, num_caps):
|
| 73 |
-
try:
|
| 74 |
-
num_caps = int(num_caps)
|
| 75 |
-
except ValueError:
|
| 76 |
-
return "⚠️ Please enter a valid number.", "", ""
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if cap_bank_design:
|
| 80 |
-
|
| 81 |
-
[f"🔹 Capacitor {i+1}: {
|
| 82 |
-
)
|
| 83 |
-
return (
|
| 84 |
-
suggested_capacitors,
|
| 85 |
-
f"{cap_bank_design['total_kvar']} kVAR",
|
| 86 |
-
cap_bank_design['message']
|
| 87 |
)
|
|
|
|
| 88 |
else:
|
| 89 |
-
return
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
gr.Markdown("## ⚡ Three-Phase Power Calculator with Capacitor Bank Design")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
-
voltage = gr.Number(label="
|
| 96 |
-
current = gr.Number(label="
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
calc_btn = gr.Button("
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
app.launch()
|
|
|
|
| 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:
|
|
|
|
| 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),
|
|
|
|
| 22 |
else:
|
| 23 |
return None
|
| 24 |
|
| 25 |
+
|
| 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 |
+
# Try all unique combinations of sizes with up to num_caps elements
|
| 32 |
+
for combo in itertools.combinations(available_capacitors, min(num_caps, len(available_capacitors))):
|
| 33 |
+
if len(combo) < num_caps:
|
| 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 |
|
|
|
|
| 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": mismatch
|
| 64 |
}
|
| 65 |
else:
|
| 66 |
+
return {"message": "Could not find a suitable combination."}
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
|
| 70 |
+
def calculate_and_store_reactive_power(voltage, current, power_factor, frequency):
|
| 71 |
+
global stored_reactive_power
|
| 72 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 73 |
if power_results:
|
| 74 |
+
stored_reactive_power = power_results["reactive_power"]
|
| 75 |
return (
|
| 76 |
+
f"Apparent Power: **{power_results['apparent_power']} VA**",
|
| 77 |
+
f"Real Power: **{power_results['real_power']} kW**",
|
| 78 |
+
f"Reactive Power: **{power_results['reactive_power']} kVAR**",
|
| 79 |
+
f"Calculated Power Factor: **{power_results['calculated_pf']}**"
|
|
|
|
| 80 |
)
|
| 81 |
else:
|
| 82 |
+
return (
|
| 83 |
+
"⚠️ Please enter valid Voltage and Current!",
|
| 84 |
+
"", "", ""
|
| 85 |
+
)
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
def design_bank_from_user_input(num_caps):
|
| 89 |
+
global stored_reactive_power
|
| 90 |
+
if stored_reactive_power <= 0:
|
| 91 |
+
return "Please calculate reactive power first.", "", ""
|
| 92 |
+
cap_bank_design = design_unique_capacitor_bank(stored_reactive_power, num_caps)
|
| 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 suggested_caps, cap_bank_design["total_kvar"], cap_bank_design["message"]
|
| 98 |
else:
|
| 99 |
+
return "No suitable combination found.", "", ""
|
| 100 |
+
|
| 101 |
|
| 102 |
+
stored_reactive_power = 0
|
|
|
|
| 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 |
+
voltage = gr.Number(label="Voltage (V)", value=415)
|
| 109 |
+
current = gr.Number(label="Current (A)", value=250)
|
| 110 |
+
power_factor = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)
|
| 111 |
+
frequency = gr.Radio(label="Frequency", choices=[50, 60], value=50)
|
| 112 |
+
|
| 113 |
+
calc_btn = gr.Button("Calculate Reactive Power")
|
| 114 |
+
|
| 115 |
+
app_power = gr.HTML()
|
| 116 |
+
real_power = gr.HTML()
|
| 117 |
+
reactive_power = gr.HTML()
|
| 118 |
+
pf_result = gr.HTML()
|
| 119 |
+
|
| 120 |
+
calc_btn.click(
|
| 121 |
+
fn=calculate_and_store_reactive_power,
|
| 122 |
+
inputs=[voltage, current, power_factor, frequency],
|
| 123 |
+
outputs=[app_power, real_power, reactive_power, pf_result]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
gr.Markdown("---")
|
| 127 |
+
gr.Markdown("### Enter Number of Capacitors to Suggest a Bank")
|
| 128 |
+
|
| 129 |
+
num_caps = gr.Number(label="Number of Capacitors", value=3, precision=0)
|
| 130 |
+
bank_btn = gr.Button("Design Capacitor Bank")
|
| 131 |
+
|
| 132 |
+
cap_result = gr.HTML()
|
| 133 |
+
cap_total = gr.HTML()
|
| 134 |
+
cap_message = gr.HTML()
|
| 135 |
+
|
| 136 |
+
bank_btn.click(fn=design_bank_from_user_input, inputs=[num_caps], outputs=[cap_result, cap_total, cap_message])
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
app.launch()
|