-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculations.py
332 lines (273 loc) · 12.9 KB
/
calculations.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# *****************************************
# Author: Emanuel A.
# Date: 09/22/2024
# Project: Power-Model
#
# Purpose: Functions for calculations.
# *****************************************
# Imports
import math
import cmath
import numpy as np
# *****************************************
# Utility Functions for Phasors
#
# *****************************************
def polar_to_rect(magnitude, angle_deg):
"""Converts polar form (magnitude, angle) to rectangular form (complex number)."""
angle_rad = math.radians(angle_deg)
return cmath.rect(magnitude, angle_rad)
# end polar_to_rect()
def rect_to_polar(complex_number):
"""Converts rectangular form (complex number) to polar form (magnitude, angle)."""
magnitude = abs(complex_number)
angle_rad = cmath.phase(complex_number)
angle_deg = math.degrees(angle_rad)
return magnitude, angle_deg
# end rect_to_polar()
# *****************************************
# Wye Connection Calculations
#
# *****************************************
def wye_calculations(voltage_phase=None, voltage_line=None, current_phase=None, current_line=None, R=None, power=None):
# Count how many parameters are provided
provided_params = sum(param is not None for param in [voltage_phase, voltage_line, current_phase, current_line, R, power])
if provided_params != 2:
return {"error": "Please input exactly 2 variables"}
# Ensure all provided values are greater than 0
if any(x is not None and x <= 0 for x in [voltage_phase, voltage_line, current_phase, current_line, R, power]):
return "Error: All input values must be greater than zero."
# Base angles for phasor representation (phase shift of 120 degrees)
phase_angles = [0, 120, 240] # degrees for 3 phases
if power and voltage_phase:
voltage_line = math.sqrt(3) * voltage_phase
current_phase = power / (3* voltage_phase)
current_line = current_phase
R = voltage_phase / current_phase
elif power and voltage_line:
voltage_phase = voltage_line / math.sqrt(3)
current_phase = power / (3* voltage_phase)
current_line = current_phase
R = voltage_phase / current_phase
elif power and current_phase:
voltage_phase = power / (3 * current_phase)
voltage_line = math.sqrt(3) * voltage_phase
current_line = current_phase
R = voltage_phase / current_phase
elif power and current_line:
current_phase = current_line
voltage_phase = power / (3 * current_phase)
voltage_line = math.sqrt(3) * voltage_phase
R = voltage_phase / current_phase
elif power and R:
current_phase = math.sqrt(power / (3 * R))
voltage_phase = current_phase * R
voltage_line = math.sqrt(3) * voltage_phase
current_line = current_phase
elif voltage_phase and current_phase:
power = 3 * voltage_phase * current_phase
voltage_line = math.sqrt(3) * voltage_phase
current_line = current_phase
R = voltage_phase / current_phase
elif voltage_phase and current_line:
current_phase = current_line
power = 3 * voltage_phase * current_phase
voltage_line = math.sqrt(3) * voltage_phase
R = voltage_phase / current_phase
elif voltage_phase and R:
current_phase = voltage_phase / R
power = 3 * voltage_phase * current_phase
voltage_line = math.sqrt(3) * voltage_phase
current_line = current_phase
elif voltage_line and current_phase:
voltage_phase = voltage_line / math.sqrt(3)
power = 3 * voltage_phase * current_phase
current_line = current_phase
R = voltage_phase / current_phase
elif voltage_line and current_line:
current_phase = current_line
voltage_phase = voltage_line / math.sqrt(3)
power = 3 * voltage_phase * current_phase
R = voltage_phase / current_phase
elif voltage_line and R:
voltage_phase = voltage_line / math.sqrt(3)
current_phase = voltage_phase / R
current_line = current_phase
power = 3 * voltage_phase * current_phase
elif current_phase and R:
voltage_phase = current_phase * R
voltage_line = math.sqrt(3) * voltage_phase
current_line = current_phase
power = 3 * voltage_phase * current_phase
elif current_line and R:
current_phase = current_line
voltage_phase = current_phase * R
voltage_line = math.sqrt(3) * voltage_phase
power = 3 * voltage_phase * current_phase
# Phasor calculations
phase_voltages = [polar_to_rect(voltage_phase, angle) for angle in phase_angles]
line_voltages = [polar_to_rect(voltage_line, angle + 30) for angle in phase_angles]
phase_currents = [polar_to_rect(current_phase, angle) for angle in phase_angles] if current_phase else []
line_currents = phase_currents if current_phase else []
return {
"voltage_phase": [rect_to_polar(vp) for vp in phase_voltages],
"voltage_line": [rect_to_polar(vl) for vl in line_voltages],
"current_phase": [rect_to_polar(cp) for cp in phase_currents] if current_phase else None,
"current_line": [rect_to_polar(cl) for cl in line_currents] if line_currents else None,
"power": power,
"resistance": R
}
# end wye_calculations()
# *****************************************
# Delta Connection Calculations
#
# *****************************************
def delta_calculations(voltage_phase=None, voltage_line=None, current_phase=None, current_line=None, R=None, power=None):
# Count how many parameters are provided
provided_params = sum(param is not None for param in [voltage_phase, voltage_line, current_phase, current_line, R, power])
if provided_params != 2:
return {"error": "Please input exactly 2 variables"}
# Ensure all provided values are greater than 0
if any(x is not None and x <= 0 for x in [voltage_phase, voltage_line, current_phase, current_line, R, power]):
return "Error: All input values must be greater than zero."
# Base angles for phasor representation (phase shift of 120 degrees)
phase_angles = [0, 120, 240] # degrees for 3 phases
if power and voltage_phase:
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_phase = power / (3 * voltage_phase)
current_line = math.sqrt(3) * current_phase
R = voltage_phase / current_phase
elif power and voltage_line:
voltage_phase = voltage_line # In delta, voltage_phase = voltage_line
current_phase = power / (3 * voltage_phase)
current_line = math.sqrt(3) * current_phase
R = voltage_phase / current_phase
elif power and current_phase:
voltage_phase = power / (3 * current_phase)
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_line = math.sqrt(3) * current_phase
R = voltage_phase / current_phase
elif power and current_line:
current_phase = current_line / math.sqrt(3)
voltage_phase = power / (3 * current_phase)
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
R = voltage_phase / current_phase
elif power and R:
current_phase = math.sqrt(power / (3 * R))
voltage_phase = current_phase * R
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_line = math.sqrt(3) * current_phase
elif voltage_phase and current_phase:
power = 3 * voltage_phase * current_phase
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_line = math.sqrt(3) * current_phase
R = voltage_phase / current_phase
elif voltage_phase and current_line:
current_phase = current_line / math.sqrt(3)
power = 3 * voltage_phase * current_phase
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
R = voltage_phase / current_phase
elif voltage_phase and R:
current_phase = voltage_phase / R
power = 3 * voltage_phase * current_phase
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_line = math.sqrt(3) * current_phase
elif voltage_line and current_phase:
voltage_phase = voltage_line # In delta, voltage_phase = voltage_line
power = 3 * voltage_phase * current_phase
current_line = math.sqrt(3) * current_phase
R = voltage_phase / current_phase
elif voltage_line and current_line:
current_phase = current_line / math.sqrt(3)
voltage_phase = voltage_line # In delta, voltage_phase = voltage_line
power = 3 * voltage_phase * current_phase
R = voltage_phase / current_phase
elif voltage_line and R:
voltage_phase = voltage_line # In delta, voltage_phase = voltage_line
current_phase = voltage_phase / R
current_line = math.sqrt(3) * current_phase
power = 3 * voltage_phase * current_phase
elif current_phase and R:
voltage_phase = current_phase * R
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
current_line = math.sqrt(3) * current_phase
power = 3 * voltage_phase * current_phase
elif current_line and R:
current_phase = current_line / math.sqrt(3)
voltage_phase = current_phase * R
voltage_line = voltage_phase # In delta, voltage_phase = voltage_line
power = 3 * voltage_phase * current_phase
# Phasor calculations
phase_voltages = [polar_to_rect(voltage_phase, angle) for angle in phase_angles]
line_currents = [polar_to_rect(current_line, angle - 30) for angle in phase_angles]
phase_currents = [polar_to_rect(current_phase, angle) for angle in phase_angles]
return {
"voltage_phase": [rect_to_polar(vp) for vp in phase_voltages],
"voltage_line": [rect_to_polar(vl) for vl in phase_voltages], # Same in delta
"current_phase": [rect_to_polar(cp) for cp in phase_currents] if current_phase else None,
"current_line": [rect_to_polar(cl) for cl in line_currents] if line_currents else None,
"power": power,
"resistance": R
}
# end delta_calculations()
# *****************************************
# Time Series Generation for Plotting
# *****************************************
def generate_time_series(frequency=60, period_count=5, time_steps=500):
"""
Generates time values and phase angles for phasors over several periods of a 3-phase system.
frequency: Frequency of the AC system (default: 60Hz).
period_count: Number of periods to plot (default: 5 periods).
time_steps: Number of points per period (default: 500 points).
Returns:
- time array
- angles for Phase A, Phase B, and Phase C
"""
# Time array from 0 to 'period_count' periods
total_time = period_count * (1 / frequency)
time = np.linspace(0, total_time, time_steps)
# Phase angles (120 degrees apart for a balanced 3-phase system)
theta_A = 2 * np.pi * frequency * time # Phase A (0°)
theta_B = theta_A - (2 * np.pi / 3) # Phase B (120° behind A)
theta_C = theta_A + (2 * np.pi / 3) # Phase C (120° ahead of A)
return time, theta_A, theta_B, theta_C
def calculate_3phase_voltages(voltage_magnitude, time, theta_A, theta_B, theta_C):
"""
Calculates the 3-phase voltages over time based on the phase angles.
voltage_magnitude: The amplitude of the phase/line voltage.
time: Time array.
theta_A, theta_B, theta_C: Phase angles for Phases A, B, and C.
Returns:
- Voltage A
- Voltage B
- Voltage C
"""
voltage_A = voltage_magnitude * np.cos(theta_A)
voltage_B = voltage_magnitude * np.cos(theta_B)
voltage_C = voltage_magnitude * np.cos(theta_C)
return voltage_A, voltage_B, voltage_C
def calculate_3phase_currents(current_magnitude, time, theta_A, theta_B, theta_C):
"""
Calculates the 3-phase currents over time based on the phase angles.
current_magnitude: The amplitude of the phase/line current.
time: Time array.
theta_A, theta_B, theta_C: Phase angles for Phases A, B, and C.
Returns:
- Current A
- Current B
- Current C
"""
current_A = current_magnitude * np.cos(theta_A)
current_B = current_magnitude * np.cos(theta_B)
current_C = current_magnitude * np.cos(theta_C)
return current_A, current_B, current_C
# ************************************
# Uncomment to test calculations
#
# ************************************
# Test wye calculations with voltage and resistance
# result = wye_calculations(voltage_phase=120, R=10)
# print(result) # Should calculate current, power, and voltage line
# # Test delta calculations with power and current
# result = delta_calculations(power=500, current_phase=10)
# print(result) # Should calculate voltage, resistance, and current line