-
Notifications
You must be signed in to change notification settings - Fork 2
/
Graph.py
364 lines (228 loc) · 7.17 KB
/
Graph.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import networkx as nx
import matplotlib.pyplot as plt
class Graph:
def __init__(self,vertices=0):
self.nodes=vertices
# self.adjMatrix=[[-1]*self.nodes for i in range(self.nodes)]
self.adjMatrix=None
self.shorted=[]
self.rhs=[]
def draw_graph_old(self,list_elements):
g=nx.Graph()
g.clear()
for i in list_elements:
g.add_edge(i[0],i[1],r=i[2])
pos = nx.spring_layout(g, scale=1.25)
nx.draw(g,pos,with_labels=True,edge_color='black',width=3,linewidths=3,node_color='red',node_size=1700,font_size=25,font_color="yellow",font_weight="bold")
edge_labels = nx.get_edge_attributes(g,'r')
nx.draw_networkx_edge_labels(g, pos, edge_labels = edge_labels,font_size=20,font_color="gray",label_pos=0.3)
plt.savefig("out_file1.jpg")
g.clear()
plt.close()
def draw_graph_new(self,reduced):
reduced_elements=reduced
g=nx.Graph()
g.clear()
i=0
while(i<len(reduced_elements)):
s=reduced_elements[i][0]
d=reduced_elements[i][1]
reduced_elements[i][2]=[reduced_elements[i][2]]
j=i+1
while(j<len(reduced_elements)):
if((reduced_elements[j][0]==s and reduced_elements[j][1]==d) or (reduced_elements[j][1]==s and reduced_elements[j][0]==d)):
reduced_elements[i][2].append(reduced_elements[j][2])
del reduced_elements[j]
j=j+1
i=i+1
for i in reduced_elements:
g.add_edge(i[0],i[1],r=i[2])
pos = nx.spring_layout(g, scale=1.25)
nx.draw(g,pos,with_labels=True,edge_color='black',width=3,linewidths=3,node_color='red',node_size=1700,font_size=25,font_color="yellow",font_weight="bold")
edge_labels = nx.get_edge_attributes(g,'r')
nx.draw_networkx_edge_labels(g, pos, edge_labels = edge_labels,font_size=20,font_color="gray",label_pos=0.3)
plt.savefig("out_file2.jpg")
g.clear()
plt.close()
def draw_graph_final(self,final_current):
print(final_current)
i=0
while i<len(final_current):
s=final_current[i][0]
d=final_current[i][1]
final_current[i][2]=[abs(round(final_current[i][2],2))]
j=i+1
while(j<len(final_current)):
if(final_current[j][0]==s and final_current[j][1]==d):
final_current[i][2].append(abs(round(final_current[j][2],2)))
del final_current[j]
j=j+1
i=i+1
g=nx.Graph()
g.clear()
for i in final_current:
g.add_edge(i[0],i[1],c=i[2])
pos = nx.spring_layout(g, scale=1.25)
nx.draw(g,pos,with_labels=True,edge_color='blue',width=3,linewidths=3,node_color='red',node_size=1700,font_size=25,font_color="black",font_weight="bold")
edge_labels = nx.get_edge_attributes(g,'c')
nx.draw_networkx_edge_labels(g, pos, edge_labels = edge_labels,font_size=20,font_color="gray",label_pos=0.35)
plt.savefig("out_file3.jpg")
g.clear()
plt.close()
def setadjMatrix(self):
self.adjMatrix=[[-1]*self.nodes for i in range(self.nodes)]
self.rhs=[0.0]*self.nodes
def add_edge(self,node1,node2,resistance):
if(node1==node2):
return
self.adjMatrix[node1][node2]=resistance
self.adjMatrix[node2][node1]=resistance
#changing here
def gui_input(self,arrinput):
for i in range(len(arrinput)):
if self.adjMatrix[arrinput[i][0]][arrinput[i][1]]==-1:
self.add_edge(arrinput[i][0],arrinput[i][1],arrinput[i][2])
else:
r1=self.adjMatrix[arrinput[i][0]][arrinput[i][1]]
r2=arrinput[i][2]
equivalent_resistance=(1/r1)+(1/r2)
equivalent_resistance=1/equivalent_resistance
self.add_edge(arrinput[i][0],arrinput[i][1],equivalent_resistance)
def make_eqns(self,source,destination,netCurrent):
coeffs=[[0.0]*self.nodes for i in range(self.nodes)]
print(self.adjMatrix)
for z,i in enumerate(self.adjMatrix):
for j in i:
if(j!=-1 and coeffs[z][z]!=0.0):
coeffs[z][z]+=float(1/j)
elif(j!=-1 and coeffs[z][z]==0.0):
coeffs[z][z]=float(1/j)
for index,j in enumerate(i):
if(index!=z and self.adjMatrix[z][index]!=-1):
coeffs[z][index]=-float(1/self.adjMatrix[z][index])
self.rhs[source]=netCurrent
self.rhs[destination]=-1.0*netCurrent
#reducing 1 Variable
n=max(source,destination)
#removed variable of Node n by substituting Vn=0.0Volts
# newcoeffs=[[0.0]*(self.nodes-1) for i in range(self.nodes)]
newcoeffs=[]
for i,c in enumerate(coeffs):
newcoeffs.append(c[:n]+c[n+1:])
M = [newcoeffs[i] + [self.rhs[i]] for i in range(len(coeffs))][:-1]
print(M)
self.solver(M)
copy=[x[:] for x in M]
voltage=self.calc_voltage(copy,max(source,destination))
return abs(M[min(source, destination)][-1]),voltage
def zero_case(self,list):
replaced=[]
for index,i in enumerate(list):
resistance=i[2]
if(resistance==0):
a=min(i[0],i[1])
b=max(i[0],i[1])
replaced.append((a,b))
del list[index]
self.shorted=sorted(replaced,key=lambda x: x[1], reverse=True)
no_of_zeroes=len(self.shorted)
self.nodes=self.nodes-no_of_zeroes#changing total number of nodes
for (a,b) in self.shorted:
element=b
for index,l in enumerate(list):
if(l[0]==b):
l[0]=a
if(l[1]==b):
l[1]=a
if(l[0]>b):
l[0]=l[0]-1
if(l[1]>b):
l[1]=l[1]-1
return list[:]
def new_Source(self,source):
flag=False
for (a,b) in self.shorted:
if(source==b):
source=a
flag=True
break
if(not(flag)):
for index,(a,b) in enumerate(self.shorted):
if(source>b):
source=source-len(self.shorted[index:])
break
return source
def new_Destination(self,dest):
flag=False
for (a,b) in self.shorted:
if(dest==b):
dest=a
flag=True
break
if(not(flag)):
for index,(a,b) in enumerate(self.shorted):
if(dest>b):
dest=dest-len(self.shorted[index:])
break
return dest
def is_Shorted(self,source,destination):
return ((source,destination) in self.shorted)
def solver(self,m, eps = 1.0/(10**10)):
h=len(m)
w=len(m[0])
for y in range(0,h):
for y2 in range(y+1, h): # Eliminate column y
c = m[y2][y] / m[y][y]
for x in range(y, w):
m[y2][x] =m[y2][x]- m[y][x] * c
for y in range(h-1, 0-1, -1): # Backsubstitute
c = m[y][y]
for y2 in range(0,y):
for x in range(w-1, y-1, -1):
m[y2][x] = m[y2][x] - m[y][x] * m[y2][y] / c
m[y][y] /= c
for x in range(h, w): # Normalize row y
m[y][x] /= c
def calc_voltage(self,M,x):
volt=[0]*(len(M)+1)
j=0
for i in range(0,len(M)):
volt[j]=M[i][i]*M[i][len(M)]
j+=1
if j==x:
volt[j]=0
j+=1
return volt
def calc_current_node(self,volt,resistances):
t=len(volt)
x=len(resistances)
currents=[[]for i in range(t)]
fcurr=[[]for i in range(x)]
power=[[]for i in range(t)]
fpow=[[]for i in range(x)]
for i in range(t):
for j in range(t):
currents[i].append([])
power[i].append([])
for i in resistances:
volt_diff=volt[i[1]]-volt[i[0]]
cur=volt_diff/i[2]
currents[i[0]][i[1]].append(cur)
powr=cur*cur*i[2]
power[i[0]][i[1]].append(powr)
coef=0
coef1=0
for i in range(t):
for j in range(t):
if currents[i][j]!=[]:
for k in currents[i][j]:
fcurr[coef].append(i)
fcurr[coef].append(j)
fcurr[coef].append(k)
coef+=1
for k in power[i][j]:
fpow[coef1].append(i)
fpow[coef1].append(j)
fpow[coef1].append(k)
coef1+=1
return fcurr,fpow