File size: 4,431 Bytes
9a72859
63b6af6
03f694f
28b19b2
 
03f694f
63b6af6
 
 
 
 
faae5fa
92207ef
2ee00dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28b19b2
 
9a72859
 
28b19b2
faae5fa
03f694f
467105f
 
 
63b6af6
 
 
 
 
 
 
 
 
 
 
 
 
467105f
 
faae5fa
 
 
 
 
 
 
 
92207ef
 
 
 
 
 
63b6af6
467105f
 
 
 
63b6af6
faae5fa
 
 
 
 
 
92207ef
2ee00dd
 
 
 
 
 
 
 
 
92207ef
 
 
 
 
faae5fa
 
467105f
 
28b19b2
03f694f
e1a872a
faae5fa
92207ef
2ee00dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.")