Update app.py
Browse files
app.py
CHANGED
|
@@ -6,34 +6,14 @@ import itertools
|
|
| 6 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 7 |
|
| 8 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 9 |
-
"""
|
| 10 |
-
Calculates power parameters for a three-phase system.
|
| 11 |
-
|
| 12 |
-
Args:
|
| 13 |
-
voltage (float): Voltage in Volts.
|
| 14 |
-
current (float): Current in Amperes.
|
| 15 |
-
power_factor (float): Power factor (0.0 to 1.0).
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
dict: A dictionary containing the calculated apparent, real, and reactive power.
|
| 19 |
-
Returns None if input is invalid.
|
| 20 |
-
"""
|
| 21 |
if voltage > 0 and current > 0:
|
| 22 |
-
# Apparent Power (VA) for Three-Phase
|
| 23 |
apparent_power = math.sqrt(3) * voltage * current
|
| 24 |
-
|
| 25 |
-
# Real Power (kW)
|
| 26 |
-
real_power = apparent_power * power_factor / 1000 # W to kW
|
| 27 |
-
|
| 28 |
-
# Reactive Power (kVAR)
|
| 29 |
try:
|
| 30 |
reactive_power = math.sqrt((apparent_power / 1000) ** 2 - real_power ** 2)
|
| 31 |
except ValueError:
|
| 32 |
reactive_power = 0.0
|
| 33 |
-
|
| 34 |
-
# Calculated PF
|
| 35 |
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
|
| 36 |
-
|
| 37 |
return {
|
| 38 |
"apparent_power": round(apparent_power, 2),
|
| 39 |
"real_power": round(real_power, 2),
|
|
@@ -43,136 +23,94 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 43 |
else:
|
| 44 |
return None
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
def design_capacitor_bank(reactive_power, num_caps):
|
| 49 |
-
"""
|
| 50 |
-
Designs a capacitor bank to compensate for reactive power.
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
reactive_power (float): Reactive power to compensate for (kVAR).
|
| 54 |
-
num_caps (int): Number of capacitors to use.
|
| 55 |
-
|
| 56 |
-
Returns:
|
| 57 |
-
dict: A dictionary containing the suggested capacitor sizes, total compensation,
|
| 58 |
-
and a message indicating the result. Returns None if input is invalid.
|
| 59 |
-
"""
|
| 60 |
if reactive_power > 0 and num_caps > 0:
|
| 61 |
-
found = False
|
| 62 |
best_combo = None
|
| 63 |
min_error = float('inf')
|
| 64 |
-
|
| 65 |
-
# Generate combinations with repetition
|
| 66 |
for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
|
| 67 |
total = sum(combo)
|
| 68 |
error = abs(total - reactive_power)
|
| 69 |
-
|
| 70 |
-
# Find the combination closest to the required reactive power
|
| 71 |
if error < min_error:
|
| 72 |
min_error = error
|
| 73 |
best_combo = combo
|
| 74 |
-
|
| 75 |
if error == 0:
|
| 76 |
-
break
|
| 77 |
-
|
| 78 |
if best_combo:
|
| 79 |
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 80 |
total_kvar = sum(best_combo)
|
| 81 |
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 82 |
if abs(total_kvar - reactive_power) > 0.5:
|
| 83 |
message += ". Small mismatch detected. Fine-tuning may be required."
|
| 84 |
-
mismatch = True
|
| 85 |
-
else:
|
| 86 |
-
mismatch = False
|
| 87 |
return {
|
| 88 |
"suggested_capacitors": suggested_capacitors,
|
| 89 |
"total_kvar": round(total_kvar, 2),
|
| 90 |
-
"message": message
|
| 91 |
-
"mismatch": mismatch
|
| 92 |
}
|
| 93 |
-
|
| 94 |
-
return {"message": "Could not find a suitable combination with the given number of capacitors."}
|
| 95 |
-
else:
|
| 96 |
-
return None
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
def three_phase_power_calculator(voltage, current, power_factor, frequency):
|
| 100 |
-
"""
|
| 101 |
-
Main function to calculate power parameters and design capacitor bank.
|
| 102 |
-
|
| 103 |
-
Args:
|
| 104 |
-
voltage (float): Voltage in Volts.
|
| 105 |
-
current (float): Current in Amperes.
|
| 106 |
-
power_factor (float): Power factor (0.0 to 1.0).
|
| 107 |
-
frequency (int): Selected frequency.
|
| 108 |
|
| 109 |
-
|
| 110 |
-
tuple: Tuple containing output for the components
|
| 111 |
-
"""
|
| 112 |
-
# 1. Perform electrical calculations
|
| 113 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 114 |
|
| 115 |
if power_results:
|
| 116 |
-
apparent_power_out = f"Apparent Power
|
| 117 |
-
real_power_out = f"Real Power
|
| 118 |
-
reactive_power_out = f"Reactive Power
|
| 119 |
-
calculated_pf_out = f"Calculated Power Factor
|
| 120 |
reactive_power_value = power_results['reactive_power']
|
| 121 |
else:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
[f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 135 |
-
)
|
| 136 |
-
cap_bank_message = cap_bank_design['message']
|
| 137 |
-
total_kvar_out = f"{cap_bank_design['message']}"
|
| 138 |
-
else:
|
| 139 |
-
suggested_capacitors_text = "Could not find a suitable combination."
|
| 140 |
-
cap_bank_message = ""
|
| 141 |
-
total_kvar_out = ""
|
| 142 |
else:
|
| 143 |
-
suggested_capacitors_text = "
|
| 144 |
-
cap_bank_message = ""
|
| 145 |
total_kvar_out = ""
|
|
|
|
| 146 |
|
| 147 |
-
return
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
iface = gr.Interface(
|
| 153 |
fn=three_phase_power_calculator,
|
| 154 |
inputs=[
|
| 155 |
-
gr.Number(label="
|
| 156 |
-
gr.Number(label="
|
| 157 |
-
gr.Slider(label="Power Factor
|
| 158 |
-
gr.Radio(label="
|
|
|
|
| 159 |
],
|
| 160 |
outputs=[
|
| 161 |
-
gr.
|
| 162 |
-
gr.
|
| 163 |
-
gr.
|
| 164 |
-
gr.
|
| 165 |
-
gr.
|
| 166 |
-
gr.
|
| 167 |
-
gr.
|
| 168 |
],
|
| 169 |
-
title="
|
| 170 |
-
description="
|
| 171 |
allow_flagging=False,
|
| 172 |
examples=[
|
| 173 |
-
[400, 100, 0.8, 50],
|
| 174 |
-
[220, 50, 0.95, 60],
|
| 175 |
-
[415, 120, 0.75, 50]
|
| 176 |
]
|
| 177 |
)
|
| 178 |
|
|
|
|
| 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 |
for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
|
| 31 |
total = sum(combo)
|
| 32 |
error = abs(total - reactive_power)
|
|
|
|
|
|
|
| 33 |
if error < min_error:
|
| 34 |
min_error = error
|
| 35 |
best_combo = combo
|
|
|
|
| 36 |
if error == 0:
|
| 37 |
+
break
|
|
|
|
| 38 |
if best_combo:
|
| 39 |
suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
|
| 40 |
total_kvar = sum(best_combo)
|
| 41 |
message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
|
| 42 |
if abs(total_kvar - reactive_power) > 0.5:
|
| 43 |
message += ". Small mismatch detected. Fine-tuning may be required."
|
|
|
|
|
|
|
|
|
|
| 44 |
return {
|
| 45 |
"suggested_capacitors": suggested_capacitors,
|
| 46 |
"total_kvar": round(total_kvar, 2),
|
| 47 |
+
"message": message
|
|
|
|
| 48 |
}
|
| 49 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
def three_phase_power_calculator(voltage, current, power_factor, frequency, num_caps):
|
|
|
|
|
|
|
|
|
|
| 52 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 53 |
|
| 54 |
if power_results:
|
| 55 |
+
apparent_power_out = f"**Apparent Power:** {power_results['apparent_power']} VA"
|
| 56 |
+
real_power_out = f"**Real Power:** {power_results['real_power']} kW"
|
| 57 |
+
reactive_power_out = f"**Reactive Power:** {power_results['reactive_power']} kVAR"
|
| 58 |
+
calculated_pf_out = f"**Calculated Power Factor:** {power_results['calculated_pf']}"
|
| 59 |
reactive_power_value = power_results['reactive_power']
|
| 60 |
else:
|
| 61 |
+
return (
|
| 62 |
+
"⚠️ Please enter valid Voltage and Current!",
|
| 63 |
+
"", "", "", "", "", ""
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
cap_bank_design = design_capacitor_bank(reactive_power_value, num_caps)
|
| 67 |
+
if cap_bank_design:
|
| 68 |
+
suggested_capacitors_text = "<br>".join(
|
| 69 |
+
[f"🔹 Capacitor {i + 1}: **{cap}**" for i, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 70 |
+
)
|
| 71 |
+
total_kvar_out = f"{cap_bank_design['total_kvar']} kVAR"
|
| 72 |
+
cap_bank_message = cap_bank_design['message']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
+
suggested_capacitors_text = "⚠️ Could not find a suitable capacitor combination."
|
|
|
|
| 75 |
total_kvar_out = ""
|
| 76 |
+
cap_bank_message = ""
|
| 77 |
|
| 78 |
+
return (
|
| 79 |
+
apparent_power_out,
|
| 80 |
+
real_power_out,
|
| 81 |
+
reactive_power_out,
|
| 82 |
+
calculated_pf_out,
|
| 83 |
+
suggested_capacitors_text,
|
| 84 |
+
total_kvar_out,
|
| 85 |
+
cap_bank_message
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Gradio Interface
|
| 89 |
iface = gr.Interface(
|
| 90 |
fn=three_phase_power_calculator,
|
| 91 |
inputs=[
|
| 92 |
+
gr.Number(label="🔌 Voltage (V)", value=400),
|
| 93 |
+
gr.Number(label="⚡ Current (A)", value=100),
|
| 94 |
+
gr.Slider(label="💡 Power Factor", minimum=0.0, maximum=1.0, value=1.0, step=0.01),
|
| 95 |
+
gr.Radio(label="🔁 Frequency (Hz)", choices=[50, 60], value=50),
|
| 96 |
+
gr.Slider(label="🔢 Number of Capacitors", minimum=1, maximum=6, step=1, value=2)
|
| 97 |
],
|
| 98 |
outputs=[
|
| 99 |
+
gr.Markdown(label="Apparent Power"),
|
| 100 |
+
gr.Markdown(label="Real Power"),
|
| 101 |
+
gr.Markdown(label="Reactive Power"),
|
| 102 |
+
gr.Markdown(label="Calculated Power Factor"),
|
| 103 |
+
gr.Markdown(label="Suggested Capacitor Sizes"),
|
| 104 |
+
gr.Markdown(label="Total Compensation"),
|
| 105 |
+
gr.Markdown(label="Message")
|
| 106 |
],
|
| 107 |
+
title="⚙️ Three-Phase Power & Capacitor Bank Calculator",
|
| 108 |
+
description="Enter voltage, current, and power factor to calculate three-phase power parameters and design a capacitor bank.",
|
| 109 |
allow_flagging=False,
|
| 110 |
examples=[
|
| 111 |
+
[400, 100, 0.8, 50, 2],
|
| 112 |
+
[220, 50, 0.95, 60, 3],
|
| 113 |
+
[415, 120, 0.75, 50, 4]
|
| 114 |
]
|
| 115 |
)
|
| 116 |
|