forked from PasanBhanu/loadflow-forward-backward-sweep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.py
93 lines (77 loc) · 2.4 KB
/
Functions.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
import cmath
import numpy
def maxValueFromExcelColumns(rows, column):
maxValue = 0
for row in rows:
if (row[column] > maxValue):
maxValue = row[column]
return maxValue
# Array : [array, value]
def searchArray(array, value):
for row in array:
if (row[0] == value):
return row
return 0
# Array : [array, from, to]
def searchDoubleArray(arr, start, end):
for row in arr:
if (row[0] == start and row[2] == end):
return row
return 0
def printMatrix(matrix):
s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
return '\n'.join(table)
def printMatrixPolar(matrix):
s = [[str(str(abs(e)) + ' ∠ ' + str(numpy.degrees(cmath.phase(e)))) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
return '\n'.join(table)
def convertToPolar(value, roundFactor):
polarStr = str(round(abs(value),roundFactor)) + ' ∠ ' + str(round(numpy.degrees(cmath.phase(value)),roundFactor))
return polarStr
# ---- Backward Sweep ---- #
# Get destination of the given edge
def getDestinationNodeId_BS(arr, node):
for row in arr:
if (row[2] == node):
return row[0]
return 0
def getDestinationNode_BS(arr, node):
for row in arr:
if (row[2] == node):
return row
return 0
# Get previously connected nodes for a given node
def getConnectedNodesId_BS(arr, node):
connectedNodes = []
for row in arr:
if (row[0] == node):
connectedNodes.append(row[2])
return connectedNodes
def getConnectedNodes_BS(arr, node):
connectedNodes = []
for row in arr:
if (row[0] == node):
connectedNodes.append(row)
return connectedNodes
def getCalculatedTotalCurrentsAtNode(arr, node):
for row in arr:
if (row[0] == node):
return row[1]
return 0
# ---- Foward Sweep ---- #
# Get previously connected nodes for a given node
def getPreviousNodeId_FS(arr, node):
for row in arr:
if (row[2] == node):
return row[0]
return 0
def getPreviousNode_FS(arr, node):
for row in arr:
if (row[2] == node):
return row
return 0