File size: 4,223 Bytes
616007f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
import streamlit as st
import math
import itertools

# Title
st.title("โšก Three-Phase Power Calculator - Reactive Power Compensation Panel")

# Sidebar
st.sidebar.header("๐Ÿ”ง Input Parameters")

voltage = st.sidebar.number_input("Enter Voltage (V)", min_value=0.0, step=0.1)
current = st.sidebar.number_input("Enter Current (A)", min_value=0.0, step=0.1)
power_factor = st.sidebar.slider("Power Factor (optional, default = 1)", 0.0, 1.0, 1.0, step=0.01)
frequency = st.sidebar.selectbox("Select Frequency", [50, 60], index=0)  # Frequency in Hz

# Available Capacitor Units (kVAR)
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]

# Main Tabs
tab1, tab2, tab3 = st.tabs(["๐Ÿ“Š Calculation", "๐Ÿงฎ Capacitor Bank Design", "๐Ÿ“‹ About"])

with tab1:
    st.header("๐Ÿ“Š Electrical Calculation")

    if st.button("Calculate Power Parameters"):
        if voltage > 0 and current > 0:
            # Apparent Power (VA) for Three-Phase
            apparent_power = math.sqrt(3) * voltage * current

            # Real Power (kW)
            real_power = apparent_power * power_factor / 1000  # W to kW

            # Reactive Power (kVAR)
            try:
                reactive_power = math.sqrt((apparent_power / 1000)**2 - real_power**2)
            except ValueError:
                reactive_power = 0.0

            # Calculated PF
            calculated_pf = real_power * 1000 / apparent_power if apparent_power > 0 else 0

            st.subheader("๐Ÿ”Ž Results:")
            st.success(f"Apparent Power: **{round(apparent_power, 2)} VA**")
            st.success(f"Real Power: **{round(real_power, 2)} kW**")
            st.success(f"Reactive Power: **{round(reactive_power, 2)} kVAR**")
            st.info(f"Calculated Power Factor: **{round(calculated_pf, 2)}**")

            st.session_state['reactive_power'] = reactive_power

        else:
            st.warning("โš ๏ธ Please enter valid Voltage and Current!")

with tab2:
    st.header("๐Ÿงฎ Capacitor Bank Designer")

    if 'reactive_power' in st.session_state and st.session_state['reactive_power'] > 0:
        reactive_power = st.session_state['reactive_power']

        st.success(f"Target Compensation: **{round(reactive_power,2)} kVAR**")

        num_caps = st.number_input("How many capacitors do you want to use?", min_value=1, step=1)

        if num_caps > 0:
            if st.button("Find Capacitor Combination"):
                # Try to find a combination
                found = False
                best_combo = None
                min_error = float('inf')

                # Generate combinations with repetition
                for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
                    total = sum(combo)
                    error = abs(total - reactive_power)

                    # Find the combination closest to the required reactive power
                    if error < min_error:
                        min_error = error
                        best_combo = combo

                        if error == 0:
                            break  # exact match found

                if best_combo:
                    st.subheader("๐Ÿ› ๏ธ Suggested Capacitor Sizes:")
                    for idx, cap in enumerate(best_combo, start=1):
                        st.write(f"๐Ÿ”น Capacitor {idx}: **{cap} kVAR**")
                    
                    total_kvar = sum(best_combo)
                    st.success(f"โœ… Total Compensation: **{round(total_kvar,2)} kVAR**")

                    if abs(total_kvar - reactive_power) > 0.5:
                        st.warning("โš ๏ธ Small mismatch detected. Fine-tuning may be required.")
                else:
                    st.error("โŒ Could not find a suitable combination with the given number of capacitors.")

    else:
        st.info("Please first calculate in ๐Ÿ“Š Calculation tab.")

with tab3:
    st.header("๐Ÿ“‹ About This Tool")
    st.markdown("""
    This app is designed for engineers and manufacturers to:
    - Calculate three-phase system parameters
    - Estimate reactive power
    - Design capacitor banks based on user preferences
    ---
    **Developed by:** Muhendis & ChatGPT ๐Ÿš€
    """)