MuhammadSajid's picture
Update app.py
2ee00dd verified
import streamlit as st
import math
# Title
st.title("Single-Phase Power Calculator - Reactive Power Compensation Panel")
st.markdown("This tool calculates:")
st.markdown("- Apparent Power (VA)")
st.markdown("- Real Power (kW)")
st.markdown("- Power Factor")
st.markdown("- Reactive Power (kVAR)")
st.markdown("- Capacitor Rating Recommendation to Compensate Reactive Power")
st.markdown("- Capacitor kVAR Contribution")
st.markdown("- Capacitor Bank Combination Suggestion")
# Suggest capacitor bank combination using standard sizes
def suggest_capacitor_bank(target_kvar, available_units=[25, 20, 15, 10, 5, 2.5]):
result = []
remaining = target_kvar
for unit in available_units:
count = int(remaining // unit)
if count > 0:
result.append((unit, count))
remaining -= unit * count
if remaining > 0:
result.append(("Remaining uncompensated", round(remaining, 2)))
return result
# Inputs
voltage = st.number_input("Enter Voltage (V)", min_value=0.0, step=0.1)
current = st.number_input("Enter Current (A)", min_value=0.0, step=0.1)
power_factor = st.slider("Power Factor (optional, default = 1)", 0.0, 1.0, 1.0, step=0.01)
frequency = st.selectbox("Select Frequency", [50, 60], index=0) # Frequency in Hz
# Calculate button
if st.button("Calculate"):
if voltage > 0 and current > 0:
# Apparent Power (VA)
apparent_power = voltage * current
# Real Power (kW)
real_power = voltage * current * power_factor / 1000 # Convert W to kW
# Reactive Power (kVAR) using Pythagorean relationship
try:
reactive_power = math.sqrt((apparent_power / 1000)**2 - real_power**2) # in kVAR
except ValueError:
reactive_power = 0.0 # Handle rounding errors that cause sqrt of negative
# Calculated PF (just for verification)
calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0
# Capacitor sizing formula: Q_c = V^2 * (2 * pi * f * C) / 1000
# Rearranged to solve for C: C = Q_c * 1000 / (V^2 * 2 * pi * f)
if reactive_power > 0:
required_capacitance = reactive_power * 1000 / (voltage**2 * 2 * math.pi * frequency)
required_capacitance_uF = required_capacitance * 1e6 # Convert to microfarads (uF)
else:
required_capacitance_uF = 0.0
# Capacitor Reactive Power (kVAR) from the capacitance
if required_capacitance_uF > 0:
capacitor_reactive_power = (voltage**2 * 2 * math.pi * frequency * required_capacitance) / 1000000
else:
capacitor_reactive_power = 0.0
# Display results
st.subheader("Results (Single-Phase):")
st.write(f"🔌 Apparent Power: **{round(apparent_power, 2)} VA**")
st.write(f"⚡ Real Power: **{round(real_power, 2)} kW**")
st.write(f"📐 Power Factor: **{round(calculated_pf, 2)}**")
st.write(f"⚛️ Reactive Power: **{round(reactive_power, 2)} kVAR**")
# Display the capacitor recommendation
if reactive_power > 0:
st.subheader("Recommended Capacitor Size:")
st.write(f"To compensate the **{round(reactive_power, 2)} kVAR** of reactive power:")
st.write(f"💡 Required Capacitor Size: **{round(required_capacitance_uF, 2)} µF**")
# Suggest Capacitor Bank Combination
bank_recommendation = suggest_capacitor_bank(reactive_power)
st.subheader("🧮 Capacitor Bank Recommendation:")
for item in bank_recommendation:
if isinstance(item[0], (int, float)):
st.write(f"🔸 {item[1]} x {item[0]} kVAR capacitor(s)")
else:
st.warning(f"⚠️ {item[1]} kVAR is not compensated (no exact match)")
# Display Capacitor Reactive Power (kVAR)
if required_capacitance_uF > 0:
st.subheader("Capacitor Reactive Power Contribution:")
st.write(f"With a **{round(required_capacitance_uF, 2)} µF** capacitor at {voltage} V and {frequency} Hz,")
st.write(f"the capacitor will provide **{round(capacitor_reactive_power, 2)} kVAR** of reactive power.")
else:
st.write("No compensation needed as reactive power is zero or negative.")
else:
st.warning("Please enter valid voltage and current values.")