Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,15 @@ import itertools
|
|
| 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),
|
|
@@ -27,45 +23,37 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 27 |
else:
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
|
| 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,
|
| 36 |
-
if len(set(combo)) < num_caps:
|
| 37 |
-
continue # Avoid reuse unless necessary
|
| 38 |
-
|
| 39 |
total = sum(combo)
|
|
|
|
| 40 |
|
| 41 |
-
if
|
|
|
|
| 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"
|
| 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 |
-
|
| 58 |
-
|
| 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
|
| 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**"
|
|
@@ -80,59 +68,63 @@ def compute_reactive_power(voltage, current, power_factor):
|
|
| 80 |
power_results['reactive_power']
|
| 81 |
)
|
| 82 |
else:
|
| 83 |
-
return ("
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
cap_bank_design
|
| 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 "
|
| 95 |
-
|
| 96 |
|
| 97 |
-
with gr.Blocks(
|
|
|
|
| 98 |
gr.Markdown("""
|
| 99 |
-
|
| 100 |
-
|
| 101 |
""")
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
-
gr.Markdown(""
|
| 123 |
-
|
| 124 |
-
""
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
cap_suggestion = gr.HTML()
|
| 129 |
-
cap_message = gr.HTML()
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
inputs=[
|
| 134 |
-
outputs=[
|
| 135 |
)
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
|
|
|
|
| 5 |
# Available Capacitor Units (kVAR)
|
| 6 |
available_capacitors = [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:
|
| 10 |
apparent_power = math.sqrt(3) * voltage * current
|
| 11 |
real_power = apparent_power * power_factor / 1000
|
|
|
|
| 12 |
try:
|
| 13 |
reactive_power = math.sqrt((apparent_power / 1000) ** 2 - real_power ** 2)
|
| 14 |
except ValueError:
|
| 15 |
reactive_power = 0.0
|
|
|
|
| 16 |
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
|
|
|
| 17 |
return {
|
| 18 |
"apparent_power": round(apparent_power, 2),
|
| 19 |
"real_power": round(real_power, 2),
|
|
|
|
| 23 |
else:
|
| 24 |
return None
|
| 25 |
|
| 26 |
+
def design_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 |
+
for combo in itertools.combinations_with_replacement(available_capacitors, num_caps):
|
|
|
|
|
|
|
|
|
|
| 32 |
total = sum(combo)
|
| 33 |
+
error = abs(total - reactive_power)
|
| 34 |
|
| 35 |
+
if error < min_error:
|
| 36 |
+
min_error = error
|
| 37 |
best_combo = combo
|
| 38 |
+
|
| 39 |
+
if error == 0:
|
| 40 |
break
|
| 41 |
|
| 42 |
if best_combo:
|
| 43 |
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 44 |
total_kvar = sum(best_combo)
|
| 45 |
+
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 46 |
return {
|
| 47 |
"suggested_capacitors": suggested_capacitors,
|
| 48 |
"total_kvar": round(total_kvar, 2),
|
| 49 |
"message": message,
|
|
|
|
| 50 |
}
|
| 51 |
else:
|
| 52 |
+
return {"message": "Could not find a suitable combination."}
|
| 53 |
+
else:
|
| 54 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
def reactive_power_first(voltage, current, power_factor):
|
| 57 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 58 |
if power_results:
|
| 59 |
apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
|
|
|
|
| 68 |
power_results['reactive_power']
|
| 69 |
)
|
| 70 |
else:
|
| 71 |
+
return ("⚠️ Please enter valid Voltage and Current!", "", "", "", 0)
|
| 72 |
|
| 73 |
+
def finalize_capacitor_bank(reactive_power, num_caps):
|
| 74 |
+
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
| 75 |
+
if cap_bank_design and cap_bank_design.get("suggested_capacitors"):
|
|
|
|
| 76 |
suggested_capacitors_text = "<br>".join(
|
| 77 |
[f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 78 |
)
|
| 79 |
return suggested_capacitors_text, cap_bank_design['message']
|
| 80 |
else:
|
| 81 |
+
return "Could not find a suitable combination.", ""
|
|
|
|
| 82 |
|
| 83 |
+
with gr.Blocks() as iface:
|
| 84 |
+
gr.Markdown("# ⚡ Three-Phase Power Calculator - Reactive Power Compensation")
|
| 85 |
gr.Markdown("""
|
| 86 |
+
Step 1: Enter system parameters to calculate apparent and reactive power.
|
| 87 |
+
Step 2: Input number of capacitors to compute optimal configuration.
|
| 88 |
""")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
+
voltage = gr.Number(label="Enter Voltage (V)", value=415)
|
| 92 |
+
current = gr.Number(label="Enter Current (A)", value=250)
|
| 93 |
+
power_factor = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)
|
| 94 |
+
frequency = gr.Radio(label="Select Frequency", choices=[50, 60], value=50)
|
| 95 |
+
|
| 96 |
+
calc_btn = gr.Button("🔍 Calculate Power Parameters")
|
| 97 |
+
|
| 98 |
+
apparent_power_out = gr.HTML()
|
| 99 |
+
real_power_out = gr.HTML()
|
| 100 |
+
reactive_power_out = gr.HTML()
|
| 101 |
+
calculated_pf_out = gr.HTML()
|
| 102 |
+
reactive_value = gr.State() # Store the reactive power for reuse
|
| 103 |
+
|
| 104 |
+
calc_btn.click(
|
| 105 |
+
fn=reactive_power_first,
|
| 106 |
+
inputs=[voltage, current, power_factor],
|
| 107 |
+
outputs=[
|
| 108 |
+
apparent_power_out,
|
| 109 |
+
real_power_out,
|
| 110 |
+
reactive_power_out,
|
| 111 |
+
calculated_pf_out,
|
| 112 |
+
reactive_value
|
| 113 |
+
]
|
| 114 |
)
|
| 115 |
|
| 116 |
+
gr.Markdown("### ➕ Enter number of capacitors to compensate reactive power:")
|
| 117 |
+
num_caps_input = gr.Number(label="Number of Capacitors", precision=0)
|
| 118 |
+
finalize_btn = gr.Button("⚙️ Generate Capacitor Bank")
|
| 119 |
|
| 120 |
+
capacitor_out = gr.HTML()
|
| 121 |
+
total_comp_out = gr.HTML()
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
finalize_btn.click(
|
| 124 |
+
fn=finalize_capacitor_bank,
|
| 125 |
+
inputs=[reactive_value, num_caps_input],
|
| 126 |
+
outputs=[capacitor_out, total_comp_out]
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
iface.launch()
|