MuhammadSajid commited on
Commit
876635a
·
verified ·
1 Parent(s): c68c888

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+ import itertools
4
+
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
+ """
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
+ and calculated power factor. 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),
40
+ "reactive_power": round(reactive_power, 2),
41
+ "calculated_pf": round(calculated_pf, 2),
42
+ }
43
+ else:
44
+ return None
45
+
46
+ def design_capacitor_bank(reactive_power, num_caps):
47
+ """
48
+ Designs a capacitor bank to compensate for reactive power.
49
+
50
+ Args:
51
+ reactive_power (float): Reactive power to compensate for (kVAR).
52
+ num_caps (int): Number of capacitors to use.
53
+
54
+ Returns:
55
+ dict: A dictionary containing the suggested capacitor sizes, total compensation,
56
+ and a message indicating the result. Returns None if input is invalid.
57
+ """
58
+ if reactive_power > 0 and num_caps > 0:
59
+ found = False
60
+ best_combo = None
61
+ min_error = float('inf')
62
+
63
+ # Generate combinations with repetition
64
+ for combo in itertools.combinations_with_replacement(available_capacitors, int(num_caps)):
65
+ total = sum(combo)
66
+ error = abs(total - reactive_power)
67
+
68
+ # Find the combination closest to the required reactive power
69
+ if error < min_error:
70
+ min_error = error
71
+ best_combo = combo
72
+
73
+ if error == 0:
74
+ break # exact match found
75
+
76
+ if best_combo:
77
+ suggested_capacitors = [f"{cap} kVAR" for cap in best_combo]
78
+ total_kvar = sum(best_combo)
79
+ message = f"Total Compensation: {round(total_kvar, 2)} kVAR"
80
+ if abs(total_kvar - reactive_power) > 0.5:
81
+ message += ". Small mismatch detected. Fine-tuning may be required."
82
+ mismatch = True
83
+ else:
84
+ mismatch = False
85
+ return {
86
+ "suggested_capacitors": suggested_capacitors,
87
+ "total_kvar": round(total_kvar, 2),
88
+ "message": message,
89
+ "mismatch": mismatch
90
+ }
91
+ else:
92
+ return {"message": "Could not find a suitable combination with the given number of capacitors."}
93
+ else:
94
+ return None
95
+ def three_phase_power_calculator(voltage, current, power_factor, frequency):
96
+ """
97
+ Main function to calculate power parameters and design capacitor bank.
98
+
99
+ Args:
100
+ voltage (float): Voltage in Volts.
101
+ current (float): Current in Amperes.
102
+ power_factor (float): Power factor (0.0 to 1.0).
103
+ frequency (int): Selected frequency.
104
+
105
+ Returns:
106
+ tuple: Tuple containing output for the four output components
107
+ """
108
+ # 1. Perform electrical calculations
109
+ power_results = calculate_power_parameters(voltage, current, power_factor)
110
+
111
+ if power_results:
112
+ apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
113
+ real_power_out = f"Real Power: **{power_results['real_power']} kW**"
114
+ reactive_power_out = f"Reactive Power: **{power_results['reactive_power']} kVAR**"
115
+ calculated_pf_out = f"Calculated Power Factor: **{power_results['calculated_pf']}**"
116
+ reactive_power_value = power_results['reactive_power']
117
+ else:
118
+ apparent_power_out = "⚠️ Please enter valid Voltage and Current!"
119
+ real_power_out = ""
120
+ reactive_power_out = ""
121
+ calculated_pf_out = ""
122
+ reactive_power_value = 0
123
+
124
+ # 2. Design capacitor bank (if reactive power is available)
125
+ if reactive_power_value > 0:
126
+ num_caps = 2 # default value
127
+ cap_bank_design = design_capacitor_bank(reactive_power_value, num_caps)
128
+ if cap_bank_design:
129
+ suggested_capacitors_text = "<br>".join(
130
+ [f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
131
+ )
132
+ cap_bank_message = cap_bank_design['message']
133
+ total_kvar = cap_bank_design['total_kvar']
134
+ if 'mismatch' in cap_bank_design and cap_bank_design['mismatch']:
135
+ total_kvar_out = f"<span style='color:red;'>{cap_bank_design['message']}</span>"
136
+ else:
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 = "Please first calculate power parameters."
144
+ cap_bank_message = ""
145
+ total_kvar_out = ""
146
+
147
+ return apparent_power_out, real_power_out, reactive_power_out, calculated_pf_out, suggested_capacitors_text, total_kvar_out, cap_bank_message
148
+
149
+ def about_tab():
150
+ """
151
+ Function for About tab
152
+ """
153
+ about_text = """
154
+ This app is designed for engineers and manufacturers to:
155
+ - Calculate three-phase system parameters
156
+ - Estimate reactive power
157
+ - Design capacitor banks based on user preferences
158
+ ---
159
+ **Developed by:** Muhendis & ChatGPT 🚀
160
+ """
161
+ return about_text
162
+
163
+ # Create Gradio interface
164
+ iface = gr.Interface(
165
+ fn=three_phase_power_calculator,
166
+ inputs=[
167
+ gr.Number(label="Enter Voltage (V)", value=400, precision=2), # Added default value
168
+ gr.Number(label="Enter Current (A)", value=100, precision=2), # Added default value
169
+ gr.Slider(label="Power Factor (optional, default = 1)", minimum=0.0, maximum=1.0, value=1.0, step=0.01),
170
+ gr.Radio(label="Select Frequency", choices=[50, 60], default=50),
171
+ ],
172
+ outputs=[
173
+ gr.HTML(label="Apparent Power"),
174
+ gr.HTML(label="Real Power"),
175
+ gr.HTML(label="Reactive Power"),
176
+ gr.HTML(label="Calculated Power Factor"),
177
+ gr.HTML(label="Suggested Capacitor Sizes"),
178
+ gr.HTML(label="Total Compensation"),
179
+ gr.HTML(label="Message")
180
+ ],
181
+ title="⚡ Three-Phase Power Calculator - Reactive Power Compensation Panel",
182
+ description="Calculate power parameters and design capacitor banks for three-phase systems.",
183
+ allow_flagging=False,
184
+ examples=[
185
+ [400, 100, 0.8, 50], # Example 1
186
+ [220, 50, 0.95, 60], # Example 2
187
+ [415, 120, 0.75, 50]
188
+ ]
189
+ )
190
+
191
+ if __name__ == "__main__":
192
+ iface.launch()