-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
405 lines (370 loc) · 12.9 KB
/
main.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#coding=utf-8
#determinant of matrix
def det(l):
n=len(l)
if n>2:
i=1
t=0
sum=float(0)
while t<=n-1:
d={}
t1=1
while t1<=n-1:
m=0
d[t1]=[]
while m<=n-1:
if m==t:
u=0
else:
d[t1].append(l[t1][m])
m+=1
t1+=1
l1=[d[x] for x in d]
sum=sum+i*(l[0][t])*(det(l1))
i=i*(-1)
t+=1
return sum
else:
return(l[0][0]*l[1][1]-l[0][1]*l[1][0])
print('Analysis type:')
print('FOR NODAL ANALYSIS-1 \nFOR MESH ANALYSIS-2')
atype=int(input('\nAnalysis type:'))
print('\n')
if atype==1:
print('NODAL ANALYSIS:')
n=int(input('no. of nodes:'))
i=1
l1=[]
if n==1:
ii=float(input('\nCurrent flowing through the circuit='))
rr=float(input('net resistance connected between node and neutral='))
vv=ii*rr
print('Voltage at node:',vv)
#for 2 node system
if n==2:
cw=1
while(i<=n):
print('\nFOR NODE',i,'-')
print('total no. of resistors connected to node ',i,'=',)
r=int(input())
if r==0:
finalsum=0
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
else:
if cw==1:
print('\nFOR RESISTANCE CONNECTED BETWEEN NODES')
r_mid=float(input('value of resistance connected between node 1 and node 2:'))
cw=2
if r_mid==0:
c_mid=0
else:
c_mid=1/r_mid
print('\nNOW, FOR RESISTANCES CONNECTED BETWEEN NODES AND NEUTRAL')
sum=0
for j in range(1,r):
print('r',j,':')
rnode=float(input())
#solving in terms of conductances
if rnode==0:
c=0
else:
c=1/rnode
sum=sum+c
finalsum=sum+c_mid
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
#taking input of currents
print('\nAT NODE 1- ')
II=float(input('net incoming current:'))
Io=float(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=float(input('net incoming current:'))
Io2=float(input('net outgoing current:'))
IO2=-Io2
I2=IO2+II2
matrix0=[[l1[0],-c_mid],[-c_mid,l1[1]]]
matrix1=[[I1,-c_mid],[I2,l1[1]]]
matrix2=[[l1[0],I1],[-c_mid,I2]]
#print(matrix0)
#print(matrix1)
#print(matrix2)
#calculating node voltage
a=det(matrix0)
b=det(matrix1)
c=det(matrix2)
V1=b/a
V2=c/a
print('\nNode 1 Voltage:',V1,'V')
print('Node 2 Voltage:',V2,'V')
#for 3 node system
elif n==3:
qw=1
i=1
while(i<=n):
if qw==1:
print('\nFOR RESISTANCE CONNECTED BETWEEN NODES')
r_12=float(input('value of resistance connected between node 1 and node 2:'))
r_23=float(input('value of resistance connected between node 2 and node 3:'))
r_13=float(input('value of resistance connected between node 1 and node 3:'))
qw=2
#problem-common resistance between nodes has to be input again and again for every loop
if r_12==0:
c_12=0
else:
c_12=1/r_12
if r_23==0:
c_23=0
else:
c_23=1/r_23
if r_13==0:
c_13=0
else:
c_13=1/r_13
print('\nFOR NODE',i,'-')
print('total no. of resistors connected between node',i,' and neutral-',)
r=int(input())
if r==0:
if i==1:
sum=0
finalsum=sum+c_12+c_13
if i==2:
sum=0
finalsum=sum+c_23+c_12
if i==3:
sum=0
finalsum=sum+c_13+c_23
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
else:
#print('\nNOW, FOR RESISTANCES CONNECTED BETWEEN NODES AND NEUTRAL')
sum=0
for j in range(1,r+1):
print('r',j,':')
rnode=float(input())
#solving in terms of conductances
c=1/rnode
sum=sum+c
if i==1:
finalsum=sum+c_12+c_13
elif i==2:
finalsum=sum+c_23+c_12
elif i==3:
finalsum=sum+c_13+c_23
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
#taking input of currents
print('\nAT NODE 1- ')
II=float(input('net incoming current:'))
Io=float(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=float(input('net incoming current:'))
Io2=float(input('net outgoing current:'))
IO2=-Io2
I2=IO2+II2
print('\nAT NODE 3- ')
II3=float(input('net incoming current:'))
Io3=float(input('net outgoing current:'))
IO3=-Io3
I3=IO3+II3
matrix0=[[l1[0],-c_12,-c_13],[-c_12,l1[1],-c_23],[-c_13,-c_23,l1[2]]]
matrix1=[[I1,-c_12,-c_13],[I2,l1[1],-c_23],[I3,-c_23,l1[2]]]
matrix2=[[l1[0],I1,-c_13],[-c_12,I2,-c_23],[-c_13,I3,l1[2]]]
matrix3=[[l1[0],-c_12,I1],[-c_12,l1[1],I2],[-c_13,-c_23,I3]]
#print(matrix0)
#print(matrix1)
#print(matrix2)
#print(matrix3)
#calculating node voltage
a=det(matrix0)
b=det(matrix1)
c=det(matrix2)
d=det(matrix3)
V1=b/a
V2=c/a
V3=d/a
print('\nNode 1 Voltage:',V1,'V')
print('Node 2 Voltage:',V2,'V')
print('Node 3 Voltage:',V3,'V')
#for 4-node system
elif n==4:
qw=1
i=1
while(i<=n):
if qw==1:
print('\nFOR RESISTANCE CONNECTED BETWEEN NODES')
r_12=float(input('value of resistance connected between node 1 and node 2:'))
r_23=float(input('value of resistance connected between node 2 and node 3:'))
r_13=float(input('value of resistance connected between node 1 and node 3:'))
r_14=float(input('value of resistance connected between node 1 and node 4:'))
r_24=float(input('value of resistance connected between node 2 and node 4:'))
r_34=float(input('value of resistance connected between node 3 and node 4:'))
qw=2
#problem-common resistance between nodes has to be input again and again for every loop
if r_12==0:
c_12=0
else:
c_12=1/r_12
if r_23==0:
c_23=0
else:
c_23=1/r_23
if r_13==0:
c_13=0
else:
c_13=1/r_13
if r_14==0:
c_14=0
else:
c_14=1/r_14
if r_24==0:
c_24=0
else:
c_24=1/r_24
if r_34==0:
c_34=0
else:
c_34=1/r_34
print('\nFOR NODE',i,'-')
print('total no. of resistors connected between node',i,' and neutral-',)
r=int(input())
if r==0:
sum=0
if i==1:
finalsum=sum+c_12+c_13+c_14
elif i==2:
finalsum=sum+c_23+c_12+c_24
elif i==3:
finalsum=sum+c_14+c_24+c_34
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
#print('\nNOW, FOR RESISTANCES CONNECTED BETWEEN NODES AND NEUTRAL')
else:
sum=0
for j in range(1,r+1):
print('r',j,':')
rnode=float(input())
#solving in terms of conductances
c=1/rnode
sum=sum+c
if i==1:
finalsum=sum+c_12+c_13+c_14
elif i==2:
finalsum=sum+c_23+c_12+c_24
elif i==3:
finalsum=sum+c_13+c_23+c_34
elif i==4:
finalsum=sum+c_14+c_24+c_34
l1.append(finalsum)
#print(finalsum)
#print(l1)
i=i+1
#taking input of currents
print('\nAT NODE 1- ')
II=float(input('net incoming current:'))
Io=float(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=float(input('net incoming current:'))
Io2=float(input('net outgoing current:'))
IO2=-Io2
I2=IO2+II2
print('\nAT NODE 3- ')
II3=float(input('net incoming current:'))
Io3=float(input('net outgoing current:'))
IO3=-Io3
I3=IO3+II3
print('\nAT NODE 4- ')
II4=float(input('net incoming current:'))
Io4=float(input('net outgoing current:'))
IO4=-Io4
I4=IO4+II4
matrix0=[[l1[0],-c_12,-c_13,-c_14],[-c_12,l1[1],-c_23,-c_24],[-c_13,-c_23,l1[2],-c_34],[-c_14,-c_24,-c_34,l1[3]]]
matrix1=[[I1,-c_12,-c_13,-c_14],[I2,l1[1],-c_23,-c_24],[I3,-c_23,l1[2],-c_34],[I4,-c_24,-c_34,l1[3]]]
matrix2=[[l1[0],I1,-c_13,-c_14],[-c_12,I2,-c_23,-c_24],[-c_13,I3,l1[2],-c_34],[-c_14,I4,-c_34,l1[3]]]
matrix3=[[l1[0],-c_12,I1,-c_14],[-c_12,l1[1],I2,-c_24],[-c_13,-c_23,I3,-c_34],[-c_14,-c_24,I4,l1[3]]]
matrix4=[[l1[0],-c_12,-c_13,I1],[-c_12,l1[1],-c_23,I2],[-c_13,-c_23,l1[2],I3],[-c_14,-c_24,-c_34,I4]]
#print(matrix0)
#print(matrix1)
#print(matrix2)
#print(matrix3)
#calculating node voltage
a=det(matrix0)
b=det(matrix1)
c=det(matrix2)
d=det(matrix3)
e=det(matrix4)
V1=b/a
V2=c/a
V3=d/a
V4=e/a
print('\nNode 1 Voltage:',V1,'V')
print('Node 2 Voltage:',V2,'V')
print('Node 3 Voltage:',V3,'V')
print('Node 4 Voltage:',V4,'V')
elif atype==2:
print ("MESH ANALYSIS\n")
ch=int(input("No. of meshes(1,2 or 3):"))
chx='C'
if ch==1:
v=float(input('Net Voltage:'))
R=float(input('Net resistance:'))
i=v/R
l=1
print('Current in circuit:',i)
if ch==3:
chx=input('Circuit Type(a or b):')
r=[]
V=[]
a=1
if ch==2:
l=14
a1=7
elif chx=='b':
l=15
a1=9
elif chx=='a':
l=19
a1=10
for i in range(1,l):
if a<=a1:
print('R',i,':')
r.append(float(input()))
a=a+1
else:
print('V',i-a1,':')
V.append(float(input()))
if ch==2:
d=[[r[0]+r[1]+r[2]+r[6],-r[6]],[-r[6],r[3]+r[4]+r[5]+r[6]]]
d1=[[V[0]+V[1]+V[2],-r[6]],[V[3]+V[4]+V[5],r[3]+r[4]+r[5]+r[6]]]
d2=[[r[0]+r[1]+r[2]+r[6],V[0]+V[1]+V[2]],[-r[6],V[3]+V[4]+V[5]]]
print ("Ï1=",(format((det(d1)/det(d)),'.4f',)),"A\nI2=",(format((det(d2)/det(d)), '.4f')),"A")
elif chx=='b':
d=[[r[0]+r[1]+r[2]+r[3],-r[6],-r[7]],[-r[6],r[2]+r[6]+r[3]+r[8],-r[8]],[-r[7],-r[8],r[4]+r[7]+r[8]+r[5]]]
d1=[[V[0]+V[1],-r[6],-r[7]],[V[2]+V[3],r[2]+r[6]+r[3]+r[8],-r[8]],[V[4],-r[8],r[4]+r[7]+r[8]+r[5]]]
d2=[[r[0]+r[1]+r[2]+r[3],V[0]+V[1],-r[7]],[-r[6],V[2]+V[3],-r[8]],[-r[7],V[4],r[4]+r[7]+r[8]+r[5]]]
d3=[[r[0]+r[1]+r[2]+r[3],-r[6],V[0]+V[1]],[-r[6],r[2]+r[6]+r[3]+r[8],V[2]+V[3]],[-r[7],-r[8],V[4]]]
print( "Ï1=",(format((det(d1)/det(d)),'.4f')),"A\nI2=",(format((det(d2)/det(d)), '.4f')),"A\nI3=",(format((det(d3)/det(d)), '.4f')),"A")
elif chx=='a':
d=[[r[0]+r[1]+r[2]+r[8],-r[8],0],[-r[8],r[3]+r[9]+r[8]+r[4],-r[9]],[0,-r[9],r[5]+r[6]+r[7]+r[9]]]
d1=[[V[0]+V[1]+V[2],-r[8],0],[V[3]+V[4],r[3]+r[9]+r[8]+r[4],-r[9]],[V[5]+V[6]+V[7],-r[9],r[5]+r[6]+r[7]+r[9]]]
d2=[[r[0]+r[1]+r[2]+r[8],V[0]+V[1]+V[2],0],[-r[8],V[3]+V[4],-r[9]],[0,V[5]+V[6]+V[7],r[5]+r[6]+r[7]+r[9]]]
d3=[[r[0]+r[1]+r[2]+r[8],-r[8],V[0]+V[1]+V[2]],[-r[8],r[3]+r[9]+r[8]+r[4],V[3]+V[4]],[0,-r[9],V[5]+V[6]+V[7]]]
print( "Ï1=",(format((det(d1)/det(d)),'.4f',)),"A\nI2=",(format((det(d2)/det(d)), '.4f')),"A\nI3=",(format((det(d3)/det(d)), '.4f')),"A")
else:
print("ERROR , PLEASE ENTER CHOICE CORRECTLY ie 1 for nodal and 2 for mesh")