-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafo3.py
More file actions
288 lines (271 loc) · 12.8 KB
/
Copy pathTrafo3.py
File metadata and controls
288 lines (271 loc) · 12.8 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
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
from pylon import Bus, Generator, Branch, Case
#from networkx import DiGraph, shortest_path_length
from scipy.sparse import hstack, vstack, csc_matrix, csr_matrix
from scipy.sparse.linalg import spsolve, splu
from numpy import cos, array, sin,ones,zeros,pi, arccos,dot, eye,sqrt,conj, sum, rank, append
from numpy.linalg import solve, inv
from Line3 import Line3
from Bus3 import Bus3
# Constants
a=cos(2*pi/3)+1j*sin(2*pi/3)
A=array([1.0,a,a*a])# creating three 120-shifted unity voltage.
P=array([[1,1,1],[1,a,a*a],[1,a*a,a]])/3
Pinv=array([[1,1,1],[1,a*a,a],[1,a,a*a]])
class Trafo3(Line3):
""" A model for transformer based on:
Xiao, P.; Yu, D. & Yan, W. A unified three-phase transformer model
for distribution load flow calculations Power Systems,
IEEE Transactions on, 2006, 21, 153 - 159
"""
nmode='NEGATIVE MODE'
pmode='POSITIVE MODE'
def __init__(self,from_bus=Bus3(),to_bus=Bus3(),yt=0,base_mva= 1.0,base_kv=12.0,case_base_mva=1.0,case_base_kv=12.0,E_high= 12.47, E_low=4.16, type_connection='YgYg',type='Step-Down',ps=0, alpha=1.0,beta=1.0 ):
Line3.__init__(self,from_bus,to_bus)
# constants
self.YI=eye(3)*yt#
self.YII=array([[2.,-1.,-1.],[-1.,2.,-1.],[-1.,-1.,2.]])*yt/3.
self.YIII=array([[-1.,1.,0.],[0.,-1.,1.],[1.,0.,-1.]])*yt/sqrt(3.0)
self.YIV=array([[1.,-1.,0.],[-1.,2.,-1.],[0.,-1.,1.]])*yt/3.
self.YV=array([[-1.,1.,0.],[0.,-1.,1.],[0.,0.,0.]])*yt/sqrt(3.0)
self.YVI=array([[1.,0.,0.],[0.,1.,0.],[0.,0.,0.]])*yt
self.type=type
self.yt=yt
self.Yabc=self.YI.copy()# phase admitance matrix
self.Zabc=inv(self.Yabc) # phase impedance matrix
self.Z012=dot(dot(P,self.Zabc),Pinv)
self.type_connection=type_connection
self.base_mva=base_mva
self.base_kv=base_kv
self.E_high=E_high
self.E_low=E_low
self.E_from0=0.0 # set initial zero sequence voltage to 0.
#self.to_bus.base_kv=self.from_bus.base_kv*self.E_low*sqrt(3)/(self.E_high)
self.ps=ps # how much is secondary side voltage is shifted in radians
# It is important to remember that in the following models, YD connection
# will INCREASE phase angle by 30 degrees when we move from Y side to D side.
# this is a DEFAULT mode named POSITIVE MODE
# When I evaluate the 4 bus feeder test case provided by IEEE, I have found
# that It follow reverse convention. In other words, as we move from Y to D
# side, phase angle are DECREASED by 30 degrees.
# This is named NEGATIVE MODE
if self.type_connection=='YgYg': # tested. there are more to model
self.Ypp=self.YI.copy()
self.Yps=-self.YI.copy()
self.Ysp=-self.YI.copy()
self.Yss=self.YI.copy()
if self.type_connection=='YgY':
self.Ypp=self.YII.copy()
self.Yps=-self.YII.copy()
self.Ysp=-self.YII.copy()
self.Yss=self.YII.copy()
if self.type_connection=='YgD':
self.Ypp=self.YI.copy()
self.Yps=self.YIII.copy()
self.Ysp=self.YIII.transpose().copy()
self.Yss=self.YII.copy()
if self.type_connection=='YD':
self.Ypp=self.YII.copy()
self.Yps=self.YIII.copy()
self.Ysp=self.YIII.transpose().copy()
self.Yss=self.YII.copy()
if self.type_connection=='DYg':
self.Ypp=self.YII.copy()
self.Yps=self.YIII.copy()
self.Ysp=self.YIII.transpose().copy()
self.Yss=self.YI.copy()
if self.type_connection=='DD':
self.Ypp=self.YII.copy()
self.Yps=-self.YII.copy()
self.Ysp=-self.YII.transpose().copy()
self.Yss=self.YI.copy()
#print
if self.type_connection=='UV':
self.Ypp=self.YVI.copy()
self.Yps=self.YV.copy()
self.Ysp=self.Yps.transpose().copy()
self.Yss=self.YIV.copy()
if type=='Step-Up':
self.reverseMode()
self.calculateSubmatrices()
def calculateSubmatrices(self):
# additional submatrices to handle singularities
if self.type_connection!='YgYg':
self.Ysp1=self.Ysp.copy()
self.Ysp2=self.Ysp.copy()
self.Yss1=self.Yss.copy()
self.Yss2=self.Yss.copy()
self.Yps2=self.Yps.copy()
self.Ypp2=self.Ypp.copy()
self.Ysp1[2]=ones(3)
self.Yss1[2]=zeros(3)
self.Ysp2[2]=zeros(3)
self.Yps2[2]=ones(3)
self.Yss2[2]=ones(3)
self.Ypp2[2]=zeros(3) # used during voltage update i.e. forward sweep
#print hstack([self.Ypp,self.Yps])
#self.YT=vstack([hstack([self.Ypp,self.Yps]),hstack([self.Ysp,self.Yss])])
def reverseMode(self):
self.Yps=self.Yps.transpose().copy()
self.Ysp=self.Ysp.transpose().copy()
self.calculateSubmatrices()
def updateIto(self):
"""
Update injected secondary current of transformer a line of to-side of a line.
"""
self.I_to=self.to_bus.totalI.copy() # the minus sign is important because by definition it is an injected current to the secondary side.
self.I_tod=self.I_to.copy()
self.I_tod[2]=0 # I_tod is used for connection in which Ysp is singular
def updateIline(self):
self.I_line =self.I_from.copy()
return self.I_line
def updateIfromtemp(self):
self.updateIntEfrom()
# remember to update primary voltage before updating this primary current.
self.I_from_temp= dot(self.Ypp,self.E_temp)+dot(self.Yps,self.E_to)
if self.type_connection=='YgD':
# here the idea is that zero component primary current is caused only by
# primary current. Hence we substract zero componet of secondary voltage.
# hmm can we actually justify this logic?
#Ep=self.E_to -sum(self.E_to/3.0)
self.I_from_temp= dot(self.Ypp,self.E_temp)+dot(self.Yps,self.E_to)
#print 'updateIfromtemp', self.type_connectio
self.S_from=self.E_temp*conj(self.I_from)
self.I_from=self.I_from_temp
def updateBackward(self):
"""
Perform the backward sweep process.
"""
#1. Sum up line segement currents
self.updateIto()
#2. calculate intermediate primary voltage
self.updateIntEfrom()
#3. Calculate Power at primary side
# 3.a. Calculate primary current
self.updateIfromtemp()
#self.I_from_temp= dot(self.Ypp,self.E_temp)+dot(self.Yps,self.E_to)
self.calculateSfrom()
def updateForward(self):
# 1. Calculate primary current injaction
# update voltage at from_bus
self.updateEfrom()
self.E_from0=sum(self.E_from)/3.0
#self.I_from=conj(self.S_from/self.E_from)
self.I_fromd=self.I_from.copy()
self.I_fromd[2]=0
# 2. Calculate secondary voltage
# 2.a. if Yps is invertible
if self.type_connection=='YgYg':
#b=csr_matrix(self.I_to-dot(self.Ysp,self.from_bus.E))
b=csr_matrix(self.I_from-dot(self.Ypp,self.from_bus.E))
#a=csr_matrix(self.Yss)
a=csr_matrix(self.Yps) # This Yps is singular for other type of connection
self.E_to=spsolve(a,b)
# 2.b. if Yps is NOT invertibel i.e. singular
if self.type_connection!='YgYg':
# Calculate secondary voltage positive+negative segquence part
b=csr_matrix( self.I_fromd -dot(self.Ypp2,self.E_from))
a=csr_matrix(self.Yps2)
#self.E_to0=sum(self.to_bus.E)/3.
self.E_to_temp=spsolve(a,b) # this contains NO zero sequence part
# Calculate secondary voltage zero sequence part
self.E_to0=0 # this is a potential error
# a. when Yss is NOT singular ==> YgYg and DYg
if self.type_connection=='DYg': #or self.type_connection=='YgYg':
b=csr_matrix( -self.I_to -dot(self.Ysp,self.E_from))
a=csr_matrix(self.Yss)
self.E_to_temp=spsolve(a,b)
#self.E_to0=sum(self.E_to)/3.0 #this leads to nonconvergences
#print '================Update Forwad', self.type_connection
# in the rest of connection types, zero components of secondary
# voltage depend on downstream network situation. In situation
# where there is multigrounded system, it is NOT zero and therefore
# this assuption is wrong.
# It is unfortunate that this feature is not yet developed as it
# is very situational.
# however, in most of situation this assumption is correct.
if self.type_connection=='YgD' or self.type_connection=='YD' :
self.E_to0=sum(self.E_to)/3.0
## print 'find me!'
if self.type_connection=='UV':
## print 'UV Forward sweep'
b=csr_matrix( self.I_fromd -dot(self.Ypp2,self.E_from))
a=csr_matrix(self.Yps2)
self.E_to_temp=spsolve(a,b)
self.E_to0=sum(self.E_to)/3
# Sum all sequences
self.E_to=self.E_to_temp+self.E_to0
#print self.E_to
def updateIntEfrom(self):
if self.type_connection=='YgYg':
b=csr_matrix(-self.I_to - dot(self.Yss, self.E_to))
a=csr_matrix(self.Ysp)
self.E_temp=spsolve(a,b)
if self.type_connection!='YgYg':
# for other type of connection Ysp is singular. therefore some tricks
# have to be done.
# for definition of Yss1 and Ysp1 see self.__init__()
b=csr_matrix(-self.I_tod - dot(self.Yss1, self.E_to))
a=csr_matrix(self.Ysp1)
#self.E_temp=spsolve(a,b) # this contains no zero sequence components
#print"HHHHHHHHHHHHHHHHHHH"
# below zero sequence component is added. This is ok for Yg Delta connection
# what I do not understand at this moment is how about DD and DYg.
## if self.type_connection=='YgD':
## self.E_from0=sum(self.E_from)/3.0
## #print "****************YgD********************"
self.E_temp=spsolve(a,b) + self.E_from0
# this function should not update the from_bus.E because
# in current update only current is updated.
if self.type_connection=='UV':
A=csr_matrix(self.Ysp[0:2,0:2])
b=-self.I_tod - dot(self.Yss1, self.E_to)
B=csr_matrix(b[0:2])
## print B.shape
Epm=spsolve(A,B)
self.E_temp=append(Epm,0) +self.E_from0 # what is the value of E_from[2] here?
return self.E_temp# This is the intermediate primary voltage.
def calculateSfrom(self):
self.S_from=dot(self.E_temp, conj(self.I_from_temp) )
return self.S_from
def updateToBusBaseKV(self):
self.to_bus.base_kv=self.from_bus.base_kv*self.E_low/self.E_high
# this valid for transformer only. Line and regulator should
# overide this function.
def updateLineLoss(self):
Ep=self.E_from
Ip=self.I_from
Es=self.E_to
Is=self.I_to
self.line_loss=Ep*conj(Ip)-Es*conj(Is)
## def updateEto(self):
## #self.I_from=conj(self.S_from/self.E_from)
## self.I_fromd=conj(self.S_from/self.E_from)
## self.I_fromd[2]=0.
## if self.type_connection=='YgYg':
## #b=csr_matrix(self.I_to-dot(self.Ysp,self.from_bus.E))
## b=csr_matrix(self.I_from-dot(self.Ypp,self.from_bus.E))
## #a=csr_matrix(self.Yss)
## a=csr_matrix(self.Yps)
## self.E_to=spsolve(a,b)
##
## # there are three alternative for calculating zerosequence secondary
## # voltage
## # 1. for DYg connection
## elif self.type_connection=='DYg':
## b=csr_matrix( -self.I_tod -dot(self.Ysp2,self.E_from))
## a=csr_matrix(self.Yss2)
## self.E_to_temp=spsolve(a,b)
## self.E_to0=sum(self.E_to)/3.0
## self.E_to=spsolve(a,b) + self.E_to0
## print "Updating E_to"
## #b=csr_matrix(self.I_tod-dot(self.Ysp2,self.from_bus.E))
## else:
## # 2. The zero sequence voltage is a function of network grounding downstream.
## # here it is assume to be zero. This is valid for most of the case.
## self.E_to0=0
##
## b=csr_matrix(self.I_fromd-dot(self.Ypp2,self.from_bus.E))
## #a=csr_matrix(self.Yss2)
## a=csr_matrix(self.Yps2)
## self.E_to=spsolve(a,b) + self.E_to0
## return self.E_to