-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_analysis.py
301 lines (273 loc) · 8.92 KB
/
node_analysis.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
#determinant of matrix
def det(l):
n=len(l)
if n>2:
i=1
t=0
sum=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=[]
#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 cw==1:
print('\nFOR RESISTANCE CONNECTED BETWEEN NODES')
r_mid=int(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=int(input())
if rnode==0:
c=0
else:
c=1/rnode
sum=sum+c
finalsum=sum+c_mid
l1.append(finalsum)
i=i+1
print('\nAT NODE 1- ')
II=int(input('net incoming current:'))
Io=int(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=int(input('net incoming current:'))
Io2=int(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]]
a=det(matrix0)
b=det(matrix1)
c=det(matrix2)
V1=a/b
V2=a/c
print('\nNode 1 Voltage:',V1)
print('Node 2 Voltage:',V2)
#calculating node voltage
#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=int(input('value of resistance connected between node 1 and node 2:'))
r_23=int(input('value of resistance connected between node 2 and node 3:'))
r_13=int(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())
#print('\nNOW, FOR RESISTANCES CONNECTED BETWEEN NODES AND NEUTRAL')
sum=0
for j in range(1,r+1):
print('r',j,':')
rnode=int(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=int(input('net incoming current:'))
Io=int(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=int(input('net incoming current:'))
Io2=int(input('net outgoing current:'))
IO2=-Io2
I2=IO2+II2
print('\nAT NODE 3- ')
II3=int(input('net incoming current:'))
Io3=int(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)
print('Node 2 Voltage:',V2)
print('Node 3 Voltage:',V3)
#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=int(input('value of resistance connected between node 1 and node 2:'))
r_23=int(input('value of resistance connected between node 2 and node 3:'))
r_13=int(input('value of resistance connected between node 1 and node 3:'))
r_14=int(input('value of resistance connected between node 1 and node 4:'))
r_24=int(input('value of resistance connected between node 2 and node 4:'))
r_34=int(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())
#print('\nNOW, FOR RESISTANCES CONNECTED BETWEEN NODES AND NEUTRAL')
sum=0
for j in range(1,r+1):
print('r',j,':')
rnode=int(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=int(input('net incoming current:'))
Io=int(input('net outgoing current:'))
IO=-Io
I1=IO+II
print('\nAT NODE 2- ')
II2=int(input('net incoming current:'))
Io2=int(input('net outgoing current:'))
IO2=-Io2
I2=IO2+II2
print('\nAT NODE 3- ')
II3=int(input('net incoming current:'))
Io3=int(input('net outgoing current:'))
IO3=-Io3
I3=IO3+II3
print('\nAT NODE 4- ')
II4=int(input('net incoming current:'))
Io4=int(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]]
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)
print('Node 2 Voltage:',V2)
print('Node 3 Voltage:',V3)
print('Node 4 Voltage:',V4)
#print(matrix0)
#print(matrix1)
#print(matrix2)
#print(matrix3)
#calculating node voltage
elif atype==2:
print(a)