Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
182 lines (125 loc) · 3.49 KB
/
Copy pathmain.py
File metadata and controls
182 lines (125 loc) · 3.49 KB
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
import os
print("CURRENT FOLDER =", os.getcwd())
print("FILES =", os.listdir())
print("MAIN FILE STARTED")
from tkinter import Tk, filedialog
import sys
from parser import parse_raw
from network_builder import build_graph
from sld_drawer import draw_sld
import sld_drawer
print("SLD FILE =", sld_drawer.__file__)
from networkx.algorithms.community import greedy_modularity_communities
if len(sys.argv) > 1:
raw_file = sys.argv[1]
else:
root = Tk()
root.withdraw()
raw_file = filedialog.askopenfilename(
title="Select RAW File",
filetypes=[("RAW Files","*.raw *.RAW")]
)
print("RAW FILE RECEIVED =", raw_file)
with open(raw_file, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
data = parse_raw(lines)
import subprocess
project_dir = os.path.dirname(
os.path.abspath(__file__)
)
result = subprocess.run(
[
"python",
os.path.join(
project_dir,
"newton_raphson_solver.py"
),
raw_file
],
cwd=project_dir,
capture_output=True,
text=True
)
print("RETURN CODE =", result.returncode)
print("STDOUT =====================")
print(result.stdout)
print("STDERR =====================")
print(result.stderr)
print("RETURN CODE =", result.returncode)
slack_bus = None
for bus in data.buses:
if data.bus_types[bus] == 3:
slack_bus = bus
break
print("SLACK BUS =", slack_bus)
G = build_graph(data)
print("\nSUMMARY")
print("TOTAL BUSES =", len(data.buses))
print("TOTAL BRANCHES =", len(data.branches))
print("TOTAL GENERATORS =", len(data.generators))
print("TOTAL LOADS =", len(data.loads))
print("TOTAL TRANSFORMERS =", len(data.transformers))
print("\nGRAPH")
print("NODES =", G.number_of_nodes())
print("EDGES =", G.number_of_edges())
print("\nFIRST 10 BUS NAMES\n")
for bus_no, bus_name in list(data.bus_names.items())[:10]:
print(bus_no, "=", bus_name)
print("\nAREA STATISTICS\n")
from networkx.algorithms.community import greedy_modularity_communities
communities = list(greedy_modularity_communities(G))
for i, community in enumerate(communities):
print(f"AREA {i+1} : {len(community)} BUSES")
print(
"BUSES =",
sorted(list(community))
)
print()
print("\nINTER AREA LINKS\n")
node_to_area = {}
for area_no, community in enumerate(communities, start=1):
for node in community:
node_to_area[node] = area_no
links = {}
for u, v in G.edges():
area_u = node_to_area[u]
area_v = node_to_area[v]
if area_u != area_v:
pair = tuple(sorted((area_u, area_v)))
links[pair] = links.get(pair, 0) + 1
for pair, count in sorted(links.items()):
print(
f"AREA {pair[0]} ↔ AREA {pair[1]} : {count} Links"
)
# INTER AREA LINKS wala code
print("\nTOTAL GENERATORS =", len(data.generators))
print("TOTAL LOADS =", len(data.loads))
print("TOTAL TRANSFORMERS =", len(data.transformers))
print("\nLAST 10 GENERATORS")
print(data.generators[-10:])
print("\nLAST 10 LOADS")
print(data.loads[-10:])
print(type(data.generators[0]))
print(data.generators[:5])
print("\nFIRST 5 BRANCHES")
for b in data.branches[:5]:
print(b)
print("\nGENERATORS")
for g in data.generators[:10]:
print(g)
print("\nLOADS")
for l in data.loads[:10]:
print(l)
print("\nTRANSFORMERS")
for t in data.transformers[:5]:
print(t)
import os
json_file = os.path.splitext(raw_file)[0] + "_nr_results.json"
draw_sld(
G,
data.generators,
data.loads,
data.transformers,
slack_bus,
json_file
)