-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
293 lines (265 loc) · 14.7 KB
/
calc.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
def lvsolve(slackV,L1P,L1Q,L2P,L2Q,L3P,L3Q,phaseconfig):
import numpy as np
import VUFer
reload(VUFer)
# this is a solution based on Berg et al and the nodal method
# it accepts a phaseconfig array describing the phase configuration at each branch point along the main feeder.
# e.g. [3,0,0] means that all three loads are connected to
# It has been validated as having close agreement with PSCAD within 0.0181% of load voltage magnitudes (e.g. with Fixed apparent power loads, real power values sampled from a gamma distribution* at 0.9 power factor. Unbalanced: Selected randomly with a bias towards phase 1 (40%,30%,30%). Totals on each phase were 25, 16 and 19. ).
nonodes=len(L1P)
vsource= np.array([slackV,slackV *(-0.5-0.8660254037844387j),slackV *(-0.5+0.8660254037844387j)]).reshape(3,)
Iload=np.zeros((1,3),dtype=complex)
Vload=np.zeros((1,3),dtype=complex)
# Initialise working array
nodedata = np.zeros((3,13,nonodes),dtype=complex)
S= np.ones((3,nonodes),dtype=complex)
S[0,:] = L1P + L1Q*1j
S[1,:] = L2P + L2Q*1j
S[2,:] = L3P + L3Q*1j
S[S==0]=0.0000001 # set any zeros to very small number to avoid divide by 0 warning and subsequent errors
for node in range(0,nonodes):
nodedata[:,11,node] = S[:,node] #
if np.any(phaseconfig[node,:]==2):
z=np.asarray(np.where(phaseconfig[node,:]==0))
y=np.asarray(np.where(phaseconfig[node,:]==2))
S[y,node]=S[y,node]+S[z,node]
S[z,node] =0.0000001
if np.any(phaseconfig[node,:]==3):
y=np.asarray(np.where(phaseconfig[node,:]==3))
S[y,node]=np.sum(S[:,node])
for r in [0,1,2]:
if r == y:
pass
else:
S[r,node] =0.0000001
# # impedances: for 32 nodes
# zlinea=0.00154+0.000694j # line impedance of each phase
# zlineb=0.003+0.000703j # line impedance of each phase
# zservice = 0.0255+0.00123j
# zna=0.00154+0.000694j# neutral impedance
# znb=0.003+0.00015j# neutral impedance
#impedances for 20 nodes
zlinea=0.00246+0.00111j# line impedance of each phase
zlineb=0.0048+0.001125j# line impedance of each phase
zservice = 0.0255+0.00123j#
zna=0.00246+0.00111j# neutral impedance
znb=0.0048+0.00024j# neutral impedance
# admittances:
ylinea = 1/zlinea
ylineb = 1/zlineb
yservice = 1/zservice
yna = 1/zna
ynb = 1/znb
# put initial values working array
for node in range(0,nonodes):
nodedata[:,7,node]=vsource
if node >(nonodes/2)-1:
nodedata[0,1,node] = ylineb
nodedata[0,2,node] = ynb
else:
nodedata[0,1,node] = ylinea
nodedata[0,2,node] = yna
nodedata[1,1,node] = yservice
nodedata[1,2,node] = yservice
if np.any(phaseconfig[node,:]==2):
nodedata[:,3,node] = 1/(nodedata[:,7,node]*np.conj(nodedata[:,7,node]/nodedata[:,11,node])+[2*zservice])
nodedata[:,0,node] = 1/(nodedata[:,7,node]*np.conj(nodedata[:,7,node]/S[:,node])+[2*zservice])
z=np.asarray(np.where(phaseconfig[node,:]==0))
w=np.asarray(np.where(phaseconfig[node,:]==2))
x=np.asarray(np.where(phaseconfig[node,:]==1))
Y1 = 1/((nodedata[w,7,node]*np.conj(nodedata[w,7,node]/nodedata[w,11,node])) + [2*zservice])
Y2 = 1/((nodedata[z,7,node]*np.conj(nodedata[z,7,node]/nodedata[z,11,node])) + [2*zservice])
nodedata[w,0,node] = Y1 + Y2
nodedata[z,0,node] = 0
nodedata[w,12,node] = 1/(nodedata[w,7,node]*np.conj(nodedata[w,7,node]/nodedata[w,11,node]))
nodedata[z,12,node] = 1/(nodedata[w,7,node]*np.conj(nodedata[w,7,node]/nodedata[z,11,node]))
nodedata[x,12,node] = 1/(nodedata[x,7,node]*np.conj(nodedata[x,7,node]/nodedata[x,11,node]))
elif np.any(phaseconfig[node,:]==3):
w=np.asarray(np.where(phaseconfig[node,:]==3))
z=np.asarray(np.where(phaseconfig[node,:]!=3))
nodedata[:,3,node] = 1/(nodedata[w,7,node]*np.conj(nodedata[w,7,node]/nodedata[:,11,node])+[2*zservice])
nodedata[w,0,node] = 1/(nodedata[w,7,node]*np.conj(nodedata[w,7,node]/S[w,node])+[2*zservice])
nodedata[z,0,node] = 0
nodedata[:,12,node] = 1/(nodedata[w,7,node]*np.conj(nodedata[w,7,node]/nodedata[:,11,node]))
else:
nodedata[:,3,node] = 1/(nodedata[:,7,node]*np.conj(nodedata[:,7,node]/nodedata[:,11,node])+[2*zservice])
nodedata[:,0,node] = 1/(nodedata[:,7,node]*np.conj(nodedata[:,7,node]/nodedata[:,11,node])+[2*zservice])
nodedata[:,12,node] = 1/(nodedata[:,7,node]*np.conj(nodedata[:,7,node]/nodedata[:,11,node]))
nodedata[:,4,node] = S[:,node]
# Yloadcalc = The Ys used in the calculations, includes 'dummy' very high values to represent open circuits for certain phase configurations
# Yloadserv the 3 Ys associated with the input S values combined with the service cable impedance
# Yload - the 3 Ys associated with the input S values alone
#Program structure
# 1. set up dpns ('driving point network' - see Berg-Hawkins-Pleines Paper
# 2. calc vs dpns
# 3. calc load vs
# 4 recalc yload using calculated V and given S
# repeat
Vabsprev = np.ones((3,1,nonodes),dtype=complex)
n=0
for r in range(1,150):#150
for k in range(0,nonodes): # 1. set up dpns
node = nonodes - k - 1 # from nonodes to 1
if node != nonodes-1:
# combine line Ys to to - node dpn - star to mesh transformations
# remove node1
A= nodedata[0,1,node+1] # Line Admittance
B= nodedata[0,5,node+1] # Ydpn1
C= nodedata[0,6,node+1] # YDdpn12
D = nodedata[2,6,node+1] # YDdpn31
E = nodedata[1,6,node+1] # YDdpn23
F = nodedata[1,5,node+1] # Ydpn2
G = nodedata[2,5,node+1] # Ydpn3
D1 = A+B+C+D
nodedata[2,6,node] = A*D/D1 # =Yli*YDdpn31/sum
nodedata[0,6,node] = A*C/D1 # =Yli*YDdpn12/sum
nodedata[0,5,node] = A*B/D1 # =Yli*Ydpn1/sum
nodedata[1,6,node] = C*D/D1 + E # =YDdpn12*YDdpn31/sum + YDdpn23
nodedata[1,5,node] = B*C/D1 + F # =Ydpn1*YDdpn12/sum + Ydpn2
nodedata[2,5,node] = B*D/D1 + G # =Ydpn1*YDdpn31/sum + Ydpn3
# remove node1
B= nodedata[0,5,node]
C= nodedata[0,6,node]
D = nodedata[2,6,node]
E = nodedata[1,6,node]
F = nodedata[1,5,node]
G = nodedata[2,5,node]
# remove node2
D2 = C+F+E+A
nodedata[1,6,node] = A*E/D2
nodedata[0,6,node] = C*A/D2
nodedata[1,5,node] = A*F/D2
nodedata[0,5,node] = F*C/D2 + B
nodedata[2,6,node] = C*E/D2 + D
nodedata[2,5,node] = F*E/D2 + G
B= nodedata[0,5,node] #Ydpn1
C= nodedata[0,6,node] #YDdpn12
D = nodedata[2,6,node] #YDdpn31
E = nodedata[1,6,node] #YDdpn23
F = nodedata[1,5,node] #Ydpn2
G = nodedata[2,5,node] #Ydpn3
# remove node3
D3 = D+E+G+A
nodedata[2,6,node] = A*D/D3
nodedata[1,6,node] = A*E/D3
nodedata[2,5,node] = A*G/D3
nodedata[0,5,node] = G*D/D3 + B
nodedata[1,5,node] = G*E/D3 + F
nodedata[0,6,node] = D*E/D3 + C
# remove nodeN
B= nodedata[0,5,node]
C= nodedata[0,6,node]
D = nodedata[2,6,node]
E = nodedata[1,6,node]
F = nodedata[1,5,node]
G = nodedata[2,5,node]
H = nodedata[0,2,node+1]#
H = nodedata[0,2,node+1]
D4 = B+F+G+H
nodedata[0,5,node] = H*B/D4
nodedata[1,5,node] = H*F/D4
nodedata[2,5,node] = H*G/D4
nodedata[0,6,node] = B*F/D4 + C
nodedata[1,6,node] = F*G/D4 + E
nodedata[2,6,node] = B*G/D4 + D
# add dpn addmittances to Y addmittances of current node (as in parallel)
nodedata[:,5,node] = nodedata[:,0,node] + nodedata[:,5,node]
#print 'node:'+repr(node)
else:
nodedata[:,6,node] = 0 # No delta admittances at end node
nodedata[:,5,node] = nodedata[:,0,node] #Ydpn admittances = Y admittances of end node
for node in range(0,nonodes):
# 2. calc v dpns - Nodal Analysis
if node < nonodes-1:
Y = np.array([[nodedata[0,5,node]+nodedata[0,1,node],-nodedata[0,6,node],-nodedata[2,6,node],-nodedata[0,5,node]],
[-nodedata[0,6,node],nodedata[1,5,node]+nodedata[0,1,node],-nodedata[1,6,node],-nodedata[1,5,node]],
[-nodedata[2,6,node],-nodedata[1,6,node],nodedata[2,5,node]+nodedata[0,1,node],-nodedata[2,5,node]],
[-nodedata[0,5,node],-nodedata[1,5,node],-nodedata[2,5,node],nodedata[0,2,node]+np.sum(nodedata[:,5,node])]])
Iin = np.append(nodedata[:,7,node],nodedata[2,10,node])*np.append([nodedata[0,1,node]]*3,nodedata[0,2,node]) #Iin = Vin*Yin [Zline Zneut]
V = np.dot(np.linalg.inv(Y),Iin) ## reverted back (from above line) 28/3/2014
nodedata[:,7,node+1]= V[0:3] ###
nodedata[2,10,node+1]= V[3]
else:
Y = np.array([[nodedata[0,5,node]+nodedata[0,1,node],-nodedata[0,6,node],-nodedata[2,6,node],-nodedata[0,5,node]],
[-nodedata[0,6,node],nodedata[1,5,node]+nodedata[0,1,node],-nodedata[1,6,node],-nodedata[1,5,node]],
[-nodedata[2,6,node],-nodedata[1,6,node],nodedata[2,5,node]+nodedata[0,1,node],-nodedata[2,5,node]],
[-nodedata[0,5,node],-nodedata[1,5,node],-nodedata[2,5,node],nodedata[0,2,node]+np.sum(nodedata[:,5,node])]])
Iin = np.append(nodedata[:,7,node],nodedata[2,10,node])*np.append([nodedata[0,1,node]]*3,nodedata[0,2,node])
V = np.dot(np.linalg.inv(Y),Iin)
# get phase currents
Vtrunkn = V[0:3]-V[3]
Iphase = Vtrunkn*nodedata[:,0,node]
#get load currents (current dividers where unbalanced phase configuration)
if np.any(phaseconfig[node,:]==2):
z=np.asarray(np.where(phaseconfig[node,:]==0))
w=np.asarray(np.where(phaseconfig[node,:]==2))
x=np.asarray(np.where(phaseconfig[node,:]==1))
Vload = Vload.reshape((1,3))
Iload[:,x] = Iphase[x]
Iload[:,w]= nodedata[w,3,node]*(Iphase[w])/(nodedata[w,3,node]+nodedata[z,3,node])
Iload[:,z] = nodedata[z,3,node]*(Iphase[w])/(nodedata[w,3,node]+nodedata[z,3,node])
Vload[:,x] = (1/nodedata[x,12,node])*Vtrunkn[x]/((1/nodedata[x,12,node])+2*zservice)
Vload[:,w] = (1/nodedata[w,12,node])*Vtrunkn[w]/((1/nodedata[w,12,node])+2*zservice)
Vload[:,z] = (1/nodedata[z,12,node])*Vtrunkn[w]/((1/nodedata[z,12,node])+2*zservice)
elif np.any(phaseconfig[node,:]==3):
w=np.asarray(np.where(phaseconfig[node,:]==3))
Iload = nodedata[:,3,node]*Iphase[w]/(np.sum(nodedata[:,3,node]))
Vload = nodedata[:,11,node]/np.conj(Iload[:])
Vload = Iload[:]/nodedata[:,12,node]
Vload = (1/nodedata[:,12,node])*Vtrunkn[w]/((1/nodedata[:,12,node])+2*zservice)
else:
Iload = Iphase.reshape((1,3))
Vload = Iload[:]/nodedata[:,12,node]
Vload = (1/nodedata[:,12,node])*Vtrunkn/((1/nodedata[:,12,node])+2*zservice)
nodedata[:,8,node] = Iload ###
nodedata[:,9,node] = Vload ###
nodedata[0,10,node]= V[3]
if node < nonodes-1:
nodedata[1,10,node]=np.sum(Iphase) + nodedata[1,10,node+1]#np.sum(nodedata[:,8,node]) + nodedata[1,10,node+1]
else:
nodedata[1,10,node]=np.sum(Iphase)#np.sum(nodedata[:,8,node])
# Check for convergence of voltage values:
if np.all(np.abs((np.absolute(nodedata[:,9,:])-np.absolute(Vabsprev[:,0,:]))) <0.0001):#0.0001
for node in range(0,nonodes):
nodedata[2,1,node] = VUFer.VUFer(nodedata[0,7,node],nodedata[1,7,node],nodedata[2,7,node])
print 'converged in ' + str(r+1) + ' iterations'
# print nodedata.dtype
return nodedata
else:
Vabsprev[:,0,:] = nodedata[:,9,:]
n=n+1
###print 'iteration' + str(r)
if r > 148:
##print "did not converge"
return 'ERROR'
# 3 recalc yload (only gets here if no convergence)
for node in range(0,nonodes):
if np.any(phaseconfig[node,:]==2):
z=np.asarray(np.where(phaseconfig[node,:]==0))
w=np.asarray(np.where(phaseconfig[node,:]==2))
x=np.asarray(np.where(phaseconfig[node,:]==1))
nodedata[:,12,node] = 1/(nodedata[:,9,node]*np.conj(nodedata[:,9,node]/nodedata[:,11,node]))
Vtrunkn = nodedata[:,7,node] - nodedata[0,10,node]
nodedata[:,3,node] = ((nodedata[:,12,node])/(1 + 2*nodedata[:,12,node]*zservice))
Y1 = 1/((nodedata[w,9,node]*np.conj(nodedata[w,9,node]/nodedata[w,11,node])) + [2*zservice])
Y2 = 1/((nodedata[z,9,node]*np.conj(nodedata[z,9,node]/nodedata[z,11,node])) + [2*zservice])
nodedata[w,0,node] = nodedata[w,3,node] + nodedata[z,3,node]
nodedata[z,0,node] = 0
nodedata[x,0,node] = nodedata[x,3,node]
elif np.any(phaseconfig[node,:]==3):
w=np.asarray(np.where(phaseconfig[node,:]==3))
z=np.asarray(np.where(phaseconfig[node,:]!=3))
Vtrunkn = nodedata[:,7,node]-nodedata[0,10,node]
nodedata[:,12,node] = 1/(nodedata[:,9,node]*np.conj(nodedata[:,9,node]/nodedata[:,11,node]))
nodedata[:,3,node] = ((nodedata[:,12,node])/(1 + 2*nodedata[:,12,node]*zservice))
nodedata[z,0,node] = 0
nodedata[w,0,node] = np.sum(nodedata[:,3,node])
else:
nodedata[:,12,node] = 1/(nodedata[:,9,node]*np.conj(nodedata[:,9,node]/nodedata[:,11,node]))
nodedata[:,3,node] = ((nodedata[:,12,node])/(1 + 2*nodedata[:,12,node]*zservice))
nodedata[:,0,node] = nodedata[:,3,node]
# WORKING ARRAY STRUCTURE (nodedata):
# 0 1 2 3 4 5 6 7 8 9 10 11 12
#0 [ Yloadcalc1 Yli Yli-n Yloadserv1 Sload1 Zdpn1 ZDdpn12 vsource1 iload1 Vload1 Vneut Sloadact1 Yload1
#1 Yloadcalc2 Yserv Yserv-n Yloadserv2 Sload2 Zdpn2 ZDdpn23 vsource2 iload2 Vload2 Ineut Sloadact2 Yload2
#2 Yloadcalc3 0 0 Yloadserv3 Sload3 Zdpn3 ZDdpn31 vsource3 iload3 Vload3 VsourceN Sloadact3 Yload3]