fpowerkit 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fpowerkit/__init__.py +1 -0
- fpowerkit/grid.py +487 -0
- fpowerkit-0.0.1.dist-info/LICENSE +165 -0
- fpowerkit-0.0.1.dist-info/METADATA +103 -0
- fpowerkit-0.0.1.dist-info/RECORD +7 -0
- fpowerkit-0.0.1.dist-info/WHEEL +5 -0
- fpowerkit-0.0.1.dist-info/top_level.txt +1 -0
fpowerkit/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .grid import *
|
fpowerkit/grid.py
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import zipfile
|
|
2
|
+
from io import BytesIO, TextIOWrapper
|
|
3
|
+
from typing import Any, TextIO, Iterable, Optional
|
|
4
|
+
|
|
5
|
+
from feasytools import TimeFunc, ConstFunc, makeFunc, FloatLike
|
|
6
|
+
from gurobipy import Model, GRB, quicksum, Var, Constr
|
|
7
|
+
|
|
8
|
+
FloatVar = Optional[float]
|
|
9
|
+
Qs = quicksum
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _float2func(v: FloatLike) -> TimeFunc:
|
|
13
|
+
if isinstance(v, (float, int)):
|
|
14
|
+
return ConstFunc(v)
|
|
15
|
+
else:
|
|
16
|
+
assert isinstance(v, TimeFunc)
|
|
17
|
+
return v
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _tfv(s: FloatVar): return "<unsolved>" if s is None else str(s)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Bus:
|
|
24
|
+
"""母线 Bus"""
|
|
25
|
+
ID: str
|
|
26
|
+
V: FloatVar # pu
|
|
27
|
+
Pd: TimeFunc # pu
|
|
28
|
+
Qd: TimeFunc # pu
|
|
29
|
+
ShadowPrice: FloatVar # 该母线上的发电机的影子价格,元/pu power
|
|
30
|
+
|
|
31
|
+
def __init__(self, id: str, p_pu: FloatLike, q_pu: FloatLike):
|
|
32
|
+
self.ID = id
|
|
33
|
+
self.V = None
|
|
34
|
+
self.Pd = _float2func(p_pu)
|
|
35
|
+
self.Qd = _float2func(q_pu)
|
|
36
|
+
self.ShadowPrice = None
|
|
37
|
+
|
|
38
|
+
def __str__(self):
|
|
39
|
+
return f"Bus[ID={self.ID}, V={_tfv(self.V)}, Pd={self.Pd}, Qd={self.Qd}]"
|
|
40
|
+
|
|
41
|
+
def __repr__(self):
|
|
42
|
+
return self.__str__()
|
|
43
|
+
|
|
44
|
+
def str_t(self, _t: int):
|
|
45
|
+
return f"Bus[ID={self.ID}, V={_tfv(self.V)}, Pd={self.Pd(_t)}, Qd={self.Qd(_t)}]"
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def load(fp: TextIO, Sb: float = 1.0, loop: str = "0,0"):
|
|
49
|
+
ret = loop.split(",")
|
|
50
|
+
assert len(ret) == 2
|
|
51
|
+
loop_period = int(ret[0])
|
|
52
|
+
loop_times = int(ret[1])
|
|
53
|
+
data = fp.readlines()
|
|
54
|
+
hl = data[0].split(',')
|
|
55
|
+
if hl[0].strip() != "time": raise ValueError("Bad format of bus CSV file")
|
|
56
|
+
tl = [int(x.strip()) for x in hl[1:]]
|
|
57
|
+
if loop_period > 0 and loop_times > 0:
|
|
58
|
+
tl_old = tl.copy()
|
|
59
|
+
for i in range(loop_times - 1):
|
|
60
|
+
tl.extend([x + loop_period * (i + 1) for x in tl_old])
|
|
61
|
+
else:
|
|
62
|
+
tl_old = tl
|
|
63
|
+
|
|
64
|
+
def parseline(ln: str,i:int):
|
|
65
|
+
ret = ln.split(',')
|
|
66
|
+
if len(ret) != len(tl_old) + 1: raise ValueError(f"Bad line length at line {i} in bus CSV file")
|
|
67
|
+
p: 'list[float]' = []
|
|
68
|
+
q: 'list[float]' = []
|
|
69
|
+
for x in ret[1:]:
|
|
70
|
+
p0, q0 = x.strip().split('|')
|
|
71
|
+
p.append(float(p0) / Sb)
|
|
72
|
+
q.append(float(q0) / Sb)
|
|
73
|
+
if loop_period > 0 and loop_times > 0:
|
|
74
|
+
p = p * loop_times
|
|
75
|
+
q = q * loop_times
|
|
76
|
+
return ret[0].strip(), makeFunc(tl, p), makeFunc(tl, q)
|
|
77
|
+
|
|
78
|
+
return [Bus(*parseline(ln,i)) for i,ln in enumerate(data[1:],start=1)]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class Line:
|
|
82
|
+
"""线路"""
|
|
83
|
+
ID: str
|
|
84
|
+
fBus: str
|
|
85
|
+
tBus: str
|
|
86
|
+
R: float # pu
|
|
87
|
+
X: float # pu
|
|
88
|
+
P: FloatVar # pu
|
|
89
|
+
Q: FloatVar # pu
|
|
90
|
+
I: FloatVar # pu
|
|
91
|
+
|
|
92
|
+
def __init__(self, id: str, fbus: str, tbus: str, r_pu: float, x_pu: float):
|
|
93
|
+
'''
|
|
94
|
+
初始化
|
|
95
|
+
id: 线路ID
|
|
96
|
+
fbus: 起始母线ID
|
|
97
|
+
tbus: 终止母线ID
|
|
98
|
+
r_pu: 电阻, pu
|
|
99
|
+
x_pu: 电抗, pu
|
|
100
|
+
'''
|
|
101
|
+
self.ID = id
|
|
102
|
+
self.fBus = fbus
|
|
103
|
+
self.tBus = tbus
|
|
104
|
+
self.R = r_pu
|
|
105
|
+
self.X = x_pu
|
|
106
|
+
self.I = None
|
|
107
|
+
self.P = None
|
|
108
|
+
self.Q = None
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def pair(self):
|
|
112
|
+
'''(self.fBus, self.tBus)的语法糖'''
|
|
113
|
+
return (self.fBus, self.tBus)
|
|
114
|
+
|
|
115
|
+
def __str__(self) -> str:
|
|
116
|
+
return f"Line[ID={self.ID}, fBus={self.fBus}, tBus={self.tBus}, R={self.R}, X={self.X}, I={_tfv(self.I)}, P={_tfv(self.P)}, Q={_tfv(self.Q)}]"
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
return self.__str__()
|
|
120
|
+
|
|
121
|
+
def str_t(self, _t: int) -> str:
|
|
122
|
+
return self.__str__()
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def load(fp: TextIO, Zb: float = 1.0):
|
|
126
|
+
data = fp.readlines()
|
|
127
|
+
head = [x.strip() for x in data[0].split(',')]
|
|
128
|
+
if head != ["id", "fBus", "tBus", "R", "X"]:
|
|
129
|
+
raise ValueError("线路CSV文件格式错误: 列必须为id,fBus,tBus,R,X, 且区分大小写")
|
|
130
|
+
|
|
131
|
+
def parseline(ln: str):
|
|
132
|
+
(id, f, t, r, x) = (x.strip() for x in ln.split(','))
|
|
133
|
+
return id, f, t, float(r) / Zb, float(x) / Zb
|
|
134
|
+
|
|
135
|
+
return [Line(*parseline(ln)) for ln in data[1:]]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class Generator:
|
|
139
|
+
'''发电机(火电/风电/光伏通用型)'''
|
|
140
|
+
ID: str
|
|
141
|
+
BusID: str
|
|
142
|
+
Pmin: TimeFunc #pu
|
|
143
|
+
Pmax: TimeFunc #pu
|
|
144
|
+
P: FloatVar #pu
|
|
145
|
+
Qmin: TimeFunc #pu
|
|
146
|
+
Qmax: TimeFunc #pu
|
|
147
|
+
Q: FloatVar #pu
|
|
148
|
+
CostA: TimeFunc #二次成本:元/(pu Power·h)**2
|
|
149
|
+
CostB: TimeFunc #线性成本:元/pu Power·h
|
|
150
|
+
CostC: TimeFunc #开机固定成本:元
|
|
151
|
+
|
|
152
|
+
def __init__(self, id: str, busid: str, pmin_pu: FloatLike, pmax_pu: FloatLike,
|
|
153
|
+
qmin_pu: FloatLike, qmax_pu: FloatLike, costA: FloatLike, costB: FloatLike, costC: FloatLike):
|
|
154
|
+
'''
|
|
155
|
+
初始化
|
|
156
|
+
id: 发电机ID
|
|
157
|
+
busid: 发电机所在母线ID
|
|
158
|
+
pmin_pu: 最小有功出力, pu
|
|
159
|
+
pmax_pu: 最大有功出力, pu
|
|
160
|
+
qmin_pu: 最小无功出力, pu
|
|
161
|
+
qmax_pu: 最大无功出力, pu
|
|
162
|
+
costA: 二次成本系数, 元/(pu Power·h)**2
|
|
163
|
+
costB: 线性成本系数, 元/pu Power·h
|
|
164
|
+
costC: 开机固定成本, 元
|
|
165
|
+
'''
|
|
166
|
+
self.ID = id
|
|
167
|
+
self.BusID = busid
|
|
168
|
+
self.P = None
|
|
169
|
+
self.Q = None
|
|
170
|
+
self.Pmin = _float2func(pmin_pu)
|
|
171
|
+
self.Pmax = _float2func(pmax_pu)
|
|
172
|
+
self.Qmin = _float2func(qmin_pu)
|
|
173
|
+
self.Qmax = _float2func(qmax_pu)
|
|
174
|
+
self.CostA = _float2func(costA)
|
|
175
|
+
self.CostB = _float2func(costB)
|
|
176
|
+
self.CostC = _float2func(costC)
|
|
177
|
+
self.CostShadow = None
|
|
178
|
+
|
|
179
|
+
def __str__(self) -> str:
|
|
180
|
+
return f"Generator[ID={self.ID}, Bus={self.BusID}, P={_tfv(self.P)}, Q={_tfv(self.Q)}, Pmin={self.Pmin}, Pmax={self.Pmax}, Qmin={self.Qmin}, Qmax={self.Qmax}, CostA={self.CostA}, CostB={self.CostB}, CostC={self.CostC}]"
|
|
181
|
+
|
|
182
|
+
def __repr__(self) -> str:
|
|
183
|
+
return self.__str__()
|
|
184
|
+
|
|
185
|
+
def str_t(self, _t: int) -> str:
|
|
186
|
+
return f"Generator[ID={self.ID}, Bus={self.BusID}, P={_tfv(self.P)}, Q={_tfv(self.Q)}, Pmin={self.Pmin(_t)}, Pmax={self.Pmax(_t)}, Qmin={self.Qmin(_t)}, Qmax={self.Qmax(_t)}, CostA={self.CostA(_t)}, CostB={self.CostB(_t)}, CostC={self.CostC(_t)}]"
|
|
187
|
+
|
|
188
|
+
def Cost(self, _t: int, secondary: bool = True) -> FloatVar:
|
|
189
|
+
'''
|
|
190
|
+
获取当前发电功率下的发电成本, 元/h
|
|
191
|
+
_t: 时间
|
|
192
|
+
secondary: True表示使用二次函数成本模型, False表示使用一次函数成本模型
|
|
193
|
+
'''
|
|
194
|
+
if self.P is None: return None
|
|
195
|
+
ret = self.CostB(_t) * self.P + self.CostC(_t)
|
|
196
|
+
if secondary: ret += self.CostA(_t) * self.P ** 2
|
|
197
|
+
return ret
|
|
198
|
+
|
|
199
|
+
def CostPerPUPower(self, _t: int, secondary: bool = True) -> FloatVar:
|
|
200
|
+
'''
|
|
201
|
+
获取当前发电功率下的单位发电成本, 元/pu·h
|
|
202
|
+
_t: 时间
|
|
203
|
+
secondary: True表示使用二次函数成本模型, False表示使用一次函数成本模型
|
|
204
|
+
'''
|
|
205
|
+
if self.P is None or self.P == 0: return None
|
|
206
|
+
ret = self.CostB(_t) * self.P + self.CostC(_t)
|
|
207
|
+
if secondary: ret += self.CostA(_t) * self.P ** 2
|
|
208
|
+
return ret / self.P
|
|
209
|
+
|
|
210
|
+
@staticmethod
|
|
211
|
+
def load(fp: TextIO, SbkW: float, Sb: float = 1.0):
|
|
212
|
+
'''
|
|
213
|
+
从文件中加载发电机数据
|
|
214
|
+
fp: 文件指针
|
|
215
|
+
SbkW: 基准功率, kW, 用于电价单位转换: 元/kWh->元/pu Power·h
|
|
216
|
+
Sb: 基准功率, 单位不确定(取决于info.txt), 要求文件中的功率除以Sb后为标幺值
|
|
217
|
+
'''
|
|
218
|
+
data = fp.readlines()
|
|
219
|
+
hl = data[0].split(',')
|
|
220
|
+
if hl[0].strip() != "time": raise ValueError("发电机CSV文件格式错误")
|
|
221
|
+
tl = [int(x.strip()) for x in hl[1:]]
|
|
222
|
+
|
|
223
|
+
def parseline(ln: str):
|
|
224
|
+
ret = ln.split(',')
|
|
225
|
+
if len(ret) != len(tl) + 1: raise ValueError("发电机CSV文件行长度错误")
|
|
226
|
+
pmin: 'list[float]' = []
|
|
227
|
+
qmin: 'list[float]' = []
|
|
228
|
+
pmax: 'list[float]' = []
|
|
229
|
+
qmax: 'list[float]' = []
|
|
230
|
+
for x in ret[1:]:
|
|
231
|
+
pmin0, pmax0, qmin0, qmax0 = x.strip().split('|')
|
|
232
|
+
pmin.append(float(pmin0) / Sb)
|
|
233
|
+
pmax.append(float(pmax0) / Sb)
|
|
234
|
+
qmin.append(float(qmin0) / Sb)
|
|
235
|
+
qmax.append(float(qmax0) / Sb)
|
|
236
|
+
(id, busid, ca, cb, cc) = (x.strip() for x in ret[0].strip().split("|"))
|
|
237
|
+
#ca:元/(kWh)**2;cb:元/kWh;cc:元
|
|
238
|
+
return (id, busid, makeFunc(tl, pmin), makeFunc(tl, pmax),
|
|
239
|
+
makeFunc(tl, qmin), makeFunc(tl, qmax), float(ca) * SbkW * SbkW, float(cb) * SbkW, float(cc))
|
|
240
|
+
|
|
241
|
+
return [Generator(*parseline(ln)) for ln in data[1:]]
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
BusID = str
|
|
245
|
+
LineID = str
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class Grid:
|
|
249
|
+
Sb: float #MVA
|
|
250
|
+
Ub: float #kV
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def Sb_MVA(self) -> float:
|
|
254
|
+
return self.Sb
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def Sb_kVA(self) -> float:
|
|
258
|
+
return self.Sb * 1000
|
|
259
|
+
|
|
260
|
+
@property
|
|
261
|
+
def Zb(self) -> float:
|
|
262
|
+
'''Zb, unit = Ohm'''
|
|
263
|
+
return self.Ub ** 2 / self.Sb
|
|
264
|
+
|
|
265
|
+
@property
|
|
266
|
+
def Ib(self) -> float:
|
|
267
|
+
'''Ib, unit = kA'''
|
|
268
|
+
return self.Sb / (self.Ub * (3 ** 0.5))
|
|
269
|
+
|
|
270
|
+
def __init__(self, Sb_MVA: float, Ub_kV: float, buses: 'Iterable[Bus]',
|
|
271
|
+
lines: 'Iterable[Line]', gens: 'Iterable[Generator]', holdShadowPrice: bool = False):
|
|
272
|
+
'''
|
|
273
|
+
初始化
|
|
274
|
+
Sb_MVA: 基准功率, MVA
|
|
275
|
+
Ub_kV: 基准电压, kV
|
|
276
|
+
buses: 母线列表
|
|
277
|
+
lines: 线路列表
|
|
278
|
+
gens: 发电机列表
|
|
279
|
+
holdShadowPrice: 当本次影子价格求解失败时,是否保留上次影子价格
|
|
280
|
+
'''
|
|
281
|
+
self.Sb = Sb_MVA
|
|
282
|
+
self.Ub = Ub_kV
|
|
283
|
+
self._buses = {bus.ID: bus for bus in buses}
|
|
284
|
+
self._lines = {line.ID: line for line in lines}
|
|
285
|
+
self._ladjfb: 'dict[str, list[Line]]' = {bus.ID: [] for bus in buses}
|
|
286
|
+
self._ladjtb: 'dict[str, list[Line]]' = {bus.ID: [] for bus in buses}
|
|
287
|
+
self._bnames = list(self._buses.keys())
|
|
288
|
+
self._gens = {gen.ID: gen for gen in gens}
|
|
289
|
+
self._gatb: 'dict[str, list[Generator]]' = {bus.ID: [] for bus in buses}
|
|
290
|
+
for line in self._lines.values():
|
|
291
|
+
if not line.fBus in self._bnames: raise ValueError(f"母线{line.fBus}未定义")
|
|
292
|
+
if not line.tBus in self._bnames: raise ValueError(f"母线{line.tBus}未定义")
|
|
293
|
+
self._ladjfb[line.fBus].append(line)
|
|
294
|
+
self._ladjtb[line.tBus].append(line)
|
|
295
|
+
for gen in self._gens.values():
|
|
296
|
+
assert gen.BusID in self._bnames
|
|
297
|
+
self._gatb[gen.BusID].append(gen)
|
|
298
|
+
self.__holdShadowPrice = holdShadowPrice
|
|
299
|
+
|
|
300
|
+
def AddGen(self, g: Generator):
|
|
301
|
+
self._gens[g.ID] = g
|
|
302
|
+
self._gatb[g.BusID].append(g)
|
|
303
|
+
|
|
304
|
+
@property
|
|
305
|
+
def BusNames(self) -> 'list[str]':
|
|
306
|
+
return self._bnames
|
|
307
|
+
|
|
308
|
+
def Bus(self, id: str) -> 'Bus':
|
|
309
|
+
return self._buses[id]
|
|
310
|
+
|
|
311
|
+
@property
|
|
312
|
+
def Buses(self):
|
|
313
|
+
return self._buses.values()
|
|
314
|
+
|
|
315
|
+
def LinesOfFBus(self, busid: str) -> 'list[Line]':
|
|
316
|
+
return self._ladjfb[busid]
|
|
317
|
+
|
|
318
|
+
def LinesOfTBus(self, busid: str) -> 'list[Line]':
|
|
319
|
+
return self._ladjtb[busid]
|
|
320
|
+
|
|
321
|
+
def Line(self, id: str) -> 'Line':
|
|
322
|
+
return self._lines[id]
|
|
323
|
+
|
|
324
|
+
@property
|
|
325
|
+
def Lines(self):
|
|
326
|
+
return self._lines.values()
|
|
327
|
+
|
|
328
|
+
def Gen(self, id: str) -> 'Generator':
|
|
329
|
+
return self._gens[id]
|
|
330
|
+
|
|
331
|
+
@property
|
|
332
|
+
def GenNames(self) -> 'list[str]':
|
|
333
|
+
return list(self._gens.keys())
|
|
334
|
+
|
|
335
|
+
@property
|
|
336
|
+
def Gens(self):
|
|
337
|
+
return self._gens.values()
|
|
338
|
+
|
|
339
|
+
def GensAtBus(self, busid: str) -> 'list[Generator]':
|
|
340
|
+
return self._gatb[busid]
|
|
341
|
+
|
|
342
|
+
def __str__(self):
|
|
343
|
+
b = '\n '.join(map(str, self._buses.values()))
|
|
344
|
+
l = '\n '.join(map(str, self._lines.values()))
|
|
345
|
+
g = '\n '.join(map(str, self._gens.values()))
|
|
346
|
+
return f"Buses:\n {b}\nLines:\n {l}\nGenerators:\n {g}"
|
|
347
|
+
|
|
348
|
+
def __repr__(self):
|
|
349
|
+
return self.__str__()
|
|
350
|
+
|
|
351
|
+
def str_t(self, _t: int):
|
|
352
|
+
b = '\n '.join(v.str_t(_t) for v in self._buses.values())
|
|
353
|
+
l = '\n '.join(v.str_t(_t) for v in self._lines.values())
|
|
354
|
+
g = '\n '.join(v.str_t(_t) for v in self._gens.values())
|
|
355
|
+
return f"At time {_t}:\nBuses:\n {b}\nLines:\n {l}\nGenerators:\n {g}"
|
|
356
|
+
|
|
357
|
+
@staticmethod
|
|
358
|
+
def fromFile(zip_name: str, holdShadowPrice: bool = False):
|
|
359
|
+
zf = zipfile.ZipFile(zip_name, "r")
|
|
360
|
+
with TextIOWrapper(BytesIO(zf.read("info.txt"))) as fp:
|
|
361
|
+
info: dict[str, Any] = {}
|
|
362
|
+
for item in [x.split(':') for x in fp.readlines()]:
|
|
363
|
+
if len(item) != 2: raise ValueError("电网信息文件错误")
|
|
364
|
+
info[item[0].strip()] = item[1].strip()
|
|
365
|
+
Sb = float(info.pop("S_base_MVA"))
|
|
366
|
+
Ub = float(info.pop("U_base_kV"))
|
|
367
|
+
Zb = Ub * Ub / Sb
|
|
368
|
+
with TextIOWrapper(BytesIO(zf.read("buses.csv"))) as fp:
|
|
369
|
+
buses_unit = info.pop("buses_unit")
|
|
370
|
+
buses_loop = info.pop("buses_loop", "0,0")
|
|
371
|
+
if buses_unit == "pu":
|
|
372
|
+
buses = Bus.load(fp, 1, buses_loop)
|
|
373
|
+
elif buses_unit in ["kVA", "kvar", "kW"]:
|
|
374
|
+
buses = Bus.load(fp, Sb * 1000, buses_loop)
|
|
375
|
+
elif buses_unit in ["MVA", "Mvar", "MW"]:
|
|
376
|
+
buses = Bus.load(fp, Sb, buses_loop)
|
|
377
|
+
else:
|
|
378
|
+
raise ValueError(f"无效的功率单位{buses_unit}")
|
|
379
|
+
with TextIOWrapper(BytesIO(zf.read("lines.csv"))) as fp:
|
|
380
|
+
lines_unit = info.pop("lines_unit")
|
|
381
|
+
if lines_unit == "pu":
|
|
382
|
+
lines = Line.load(fp)
|
|
383
|
+
elif lines_unit == "ohm":
|
|
384
|
+
lines = Line.load(fp, Zb)
|
|
385
|
+
else:
|
|
386
|
+
raise ValueError(f"无效的线路阻抗单位{lines_unit}")
|
|
387
|
+
with TextIOWrapper(BytesIO(zf.read("gens.csv"))) as fp:
|
|
388
|
+
gens_unit = info.pop("gens_unit")
|
|
389
|
+
if gens_unit == "pu":
|
|
390
|
+
gens = Generator.load(fp, Sb * 1000)
|
|
391
|
+
elif gens_unit in ["kVA", "kvar", "kW"]:
|
|
392
|
+
gens = Generator.load(fp, Sb * 1000, Sb * 1000)
|
|
393
|
+
elif gens_unit in ["MVA", "Mvar", "MW"]:
|
|
394
|
+
gens = Generator.load(fp, Sb * 1000, Sb)
|
|
395
|
+
else:
|
|
396
|
+
raise ValueError(f"无效的功率单位{gens_unit}")
|
|
397
|
+
return Grid(Sb, Ub, buses, lines, gens, holdShadowPrice)
|
|
398
|
+
|
|
399
|
+
def solve(self, _t: int, /, *, max_v_pu:float = 1.1, min_v_pu: float = 0.9) -> 'tuple[bool, float]':
|
|
400
|
+
'''求解_t时刻的最优解, 返回(是否求解成功, 目标函数最优值)'''
|
|
401
|
+
model = Model("model")
|
|
402
|
+
max_v = max_v_pu ** 2
|
|
403
|
+
min_v = min_v_pu ** 2
|
|
404
|
+
max_l = (0.866 / self.Ib) ** 2 # 中国10kV配网限制功率15MVA,对应电流0.866kA
|
|
405
|
+
# ---------------添加变量---------------
|
|
406
|
+
# pg0[k]: 发电机有功
|
|
407
|
+
# qg0[k]: 发电机无功
|
|
408
|
+
# --> pg[j]: 节点所有发电机的有功
|
|
409
|
+
# --> qg[j]: 节点所有发电机的无功
|
|
410
|
+
# v[j]: 节点电压平方
|
|
411
|
+
# l[i,j]: 线路电流平方
|
|
412
|
+
# P[i,j] 线路有功
|
|
413
|
+
# Q[i,j]: 线路无功
|
|
414
|
+
pg0: dict[str, Var] = {g.ID: model.addVar(name=f"pg_{g.ID}", vtype='C', lb=g.Pmin(_t), ub=g.Pmax(_t)) for g in
|
|
415
|
+
self.Gens}
|
|
416
|
+
qg0: dict[str, Var] = {g.ID: model.addVar(name=f"qg_{g.ID}", vtype='C', lb=g.Qmin(_t), ub=g.Qmax(_t)) for g in
|
|
417
|
+
self.Gens}
|
|
418
|
+
pg: dict[str, list[Var]] = {bus.ID: [] for bus in self.Buses}
|
|
419
|
+
qg: dict[str, list[Var]] = {bus.ID: [] for bus in self.Buses}
|
|
420
|
+
for g in self.Gens:
|
|
421
|
+
pg[g.BusID].append(pg0[g.ID])
|
|
422
|
+
qg[g.BusID].append(qg0[g.ID])
|
|
423
|
+
|
|
424
|
+
v = {bus.ID: model.addVar(name=f"v_{bus.ID}", vtype='C', lb=min_v, ub=max_v) for bus in self.Buses}
|
|
425
|
+
l = {line.ID: model.addVar(name=f"l_{line.ID}", vtype='C', lb=0, ub=max_l) for line in self.Lines}
|
|
426
|
+
P = {line.ID: model.addVar(name=f"P_{line.ID}", vtype='C', lb=-GRB.INFINITY, ub=GRB.INFINITY) for line in
|
|
427
|
+
self.Lines}
|
|
428
|
+
Q = {line.ID: model.addVar(name=f"Q_{line.ID}", vtype='C', lb=-GRB.INFINITY, ub=GRB.INFINITY) for line in
|
|
429
|
+
self.Lines}
|
|
430
|
+
Pcons: dict[str, Constr] = {}
|
|
431
|
+
Qcons: dict[str, Constr] = {}
|
|
432
|
+
|
|
433
|
+
# ---------------添加约束---------------
|
|
434
|
+
# 功率平衡约束
|
|
435
|
+
for bus in self.Buses:
|
|
436
|
+
flow_in = self.LinesOfTBus(bus.ID)
|
|
437
|
+
flow_out = self.LinesOfFBus(bus.ID)
|
|
438
|
+
j = bus.ID
|
|
439
|
+
Pcons[j] = model.addConstr(Qs(P[ln.ID] - ln.R * l[ln.ID] for ln in flow_in) + Qs(pg[j]) == Qs(
|
|
440
|
+
P[ln.ID] for ln in flow_out) + bus.Pd(_t), f"Pcons_{j}")
|
|
441
|
+
Qcons[j] = model.addConstr(Qs(Q[ln.ID] - ln.X * l[ln.ID] for ln in flow_in) + Qs(qg[j]) == Qs(
|
|
442
|
+
Q[ln.ID] for ln in flow_out) + bus.Qd(_t), f"Qcons_{j}")
|
|
443
|
+
|
|
444
|
+
# 电压和电流平衡约束
|
|
445
|
+
for line in self.Lines:
|
|
446
|
+
i, j = line.pair
|
|
447
|
+
lid = line.ID
|
|
448
|
+
model.addConstr(
|
|
449
|
+
v[j] == v[i] - 2 * (line.R * P[lid] + line.X * Q[lid]) + (line.R ** 2 + line.X ** 2) * l[lid],
|
|
450
|
+
f"ΔU2_cons_{lid}")
|
|
451
|
+
model.addConstr(P[lid] ** 2 + Q[lid] ** 2 <= l[lid] * v[i], f"SoC_cons_{lid}")
|
|
452
|
+
|
|
453
|
+
# 配网源节点电压固定为1pu
|
|
454
|
+
model.addConstr(v['B1'] == 1, 'v_B1==1')
|
|
455
|
+
|
|
456
|
+
# 线性发电成本目标函数
|
|
457
|
+
model.setObjective(
|
|
458
|
+
quicksum(g.CostA(_t) * pg0[g.ID] ** 2 + g.CostB(_t) * pg0[g.ID] + g.CostC(_t) for g in self.Gens),
|
|
459
|
+
GRB.MINIMIZE)
|
|
460
|
+
model.setParam('OutputFlag', 0) # 关闭输出
|
|
461
|
+
model.setParam('QCPDual', 1) # 启用二次约束规划对偶
|
|
462
|
+
model.update()
|
|
463
|
+
model.optimize()
|
|
464
|
+
if model.Status != GRB.Status.OPTIMAL:
|
|
465
|
+
return False, 0
|
|
466
|
+
|
|
467
|
+
for bus in self.Buses:
|
|
468
|
+
j = bus.ID
|
|
469
|
+
bus.V = v[j].X ** 0.5
|
|
470
|
+
try:
|
|
471
|
+
sp = Pcons[j].Pi
|
|
472
|
+
except:
|
|
473
|
+
sp = None if not self.__holdShadowPrice else bus.ShadowPrice
|
|
474
|
+
bus.ShadowPrice = sp
|
|
475
|
+
|
|
476
|
+
for line in self.Lines:
|
|
477
|
+
lid = line.ID
|
|
478
|
+
line.I = l[lid].X ** 0.5
|
|
479
|
+
line.P = P[lid].X
|
|
480
|
+
line.Q = Q[lid].X
|
|
481
|
+
|
|
482
|
+
for gen in self.Gens:
|
|
483
|
+
j = gen.ID
|
|
484
|
+
gen.P = pg0[j].X
|
|
485
|
+
gen.Q = qg0[j].X
|
|
486
|
+
|
|
487
|
+
return True, model.ObjVal
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fpowerkit
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A Distflow PDN solver based on Gurobi.
|
|
5
|
+
Author: fmy_xfk
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: feasytools >=0.0.6
|
|
13
|
+
Requires-Dist: gurobipy >=9.1.0
|
|
14
|
+
|
|
15
|
+
# FPowerKit 配电网运算组件
|
|
16
|
+
- 依赖于feasytools和gurobipy: `pip install feasytools,gurobipy`(请自行安装Gurobi并申请许可证)
|
|
17
|
+
- 包含电网的描述(含母线、发电机、线路等)和基于gurobipy的配电网求解
|
|
18
|
+
- 内含IEEE 33节点配电网DistFlow模型
|
|
19
|
+
- 优化目标为“发电成本最小”。发电成本模型为二次函数$f(x)=ax^2+bx+c$。
|
|
20
|
+
|
|
21
|
+
## 使用
|
|
22
|
+
|
|
23
|
+
输入以下命令开始仿真:
|
|
24
|
+
```bash
|
|
25
|
+
python main.py -g cases/ieee33.zip
|
|
26
|
+
```
|
|
27
|
+
`ieee33`描述了IEEE 33节点配电网模型。换成`nodes3`则是一个极简版3节点辐射形配电网模型,结构如下:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
G0 G1
|
|
31
|
+
| |
|
|
32
|
+
B0->B1->B2
|
|
33
|
+
| |
|
|
34
|
+
L1 L2
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
您也换成自己的`.zip`文件来仿真,例如`python main.py -g path/to/your_zipfile.zip`
|
|
38
|
+
|
|
39
|
+
## 文件格式
|
|
40
|
+
若要配置您自己的`.zip`文件来仿真,您需要确保该zip压缩文件**有且仅有**如下文件(区分大小写),不能包含任何文件夹:
|
|
41
|
+
```
|
|
42
|
+
<your_file>.zip
|
|
43
|
+
info.txt
|
|
44
|
+
buses.csv
|
|
45
|
+
lines.csv
|
|
46
|
+
gens.csv
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
具体范例参见`PowerKit/cases/nodes3.zip`。
|
|
50
|
+
|
|
51
|
+
#### info.txt
|
|
52
|
+
该文件示例如下:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
S_base_MVA: 10
|
|
56
|
+
U_base_kV: 10
|
|
57
|
+
buses_loop: 86400, 8
|
|
58
|
+
buses_unit: MVA
|
|
59
|
+
lines_unit: ohm
|
|
60
|
+
gens_unit: MVA
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`S_base_MVA`表示复功率标幺值,单位为MVA;
|
|
64
|
+
`U_base_kV`表示电压标幺值,单位为kV;
|
|
65
|
+
`buses_loop`表示buses.csv中功率的循环周期(秒)和循环次数;
|
|
66
|
+
`buses_unit`表示buses.csv中功率值的单位,可选pu(标幺值),kVA,MVA;
|
|
67
|
+
`lines_unit`表示lines.csv中功率值的单位,可选pu(标幺值),ohm;
|
|
68
|
+
`gens_unit`表示gens.csv中功率值的单位,可选pu(标幺值),kVA,MVA;
|
|
69
|
+
|
|
70
|
+
#### buses.csv
|
|
71
|
+
该文件示例如下:
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
time,0
|
|
75
|
+
B0,0|0
|
|
76
|
+
B1,10.2|3.2
|
|
77
|
+
B2,3.8|0.7
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
首行表示时间线,内容用英文逗号分隔。第1个内容必定是小写`time`,从第2个内容开始为整数,表示从这一**秒**开始的负载功率;
|
|
81
|
+
从第2行开始,每行的第1个内容表示母线名称,从第二个内容开始表示复功率,以`有功功率|无功功率`的形式出现,单位为`info.txt`中的指定值。
|
|
82
|
+
|
|
83
|
+
#### gens.csv
|
|
84
|
+
该文件示例如下:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
time,0
|
|
88
|
+
G0|B0|0.002|0.3|5,0|100|-100|100
|
|
89
|
+
G1|B1|0.0025|0.4|15,0|100|-100|100
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
首行表示时间线,内容用英文逗号分隔。第1个内容必定是小写`time`,从第2个内容开始为整数,表示从这一**秒**开始的发电功率最值;
|
|
93
|
+
从第2行开始,每行的第1个内容为`发电机ID|所在母线|成本系数a|成本系数b|成本系数c`,从第二个内容开始表示发电功率最值,以`最小有功功率|最大有功功率|最小无功功率|最大无功功率`的形式出现,单位为`info.txt`中的指定值。
|
|
94
|
+
|
|
95
|
+
#### lines.csv
|
|
96
|
+
该文件示例如下:
|
|
97
|
+
```
|
|
98
|
+
id,fBus,tBus,R,X
|
|
99
|
+
L0-1,B0,B1,0.08,0.2
|
|
100
|
+
L1-2,B1,B2,0.06,0.12
|
|
101
|
+
```
|
|
102
|
+
首行内容必须是`id,fBus,tBus,R,X`,区分大小写;
|
|
103
|
+
从第2行开始,每行的内容为`线路名称,起始母线,终止母线,线路电阻,线路电抗`。请注意线路功率的正方向为“起始母线->终止母线”,填反有可能需要在仿真时修改边界条件。
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
fpowerkit/__init__.py,sha256=VKE-l3DVMQ9vsFLEftZ9YQPId9vPuKUrpt3n7MLzHds,19
|
|
2
|
+
fpowerkit/grid.py,sha256=mDhbPO6pnemWLaV4JmNGrkWozEptMPO4sykIM5BwjD0,18733
|
|
3
|
+
fpowerkit-0.0.1.dist-info/LICENSE,sha256=6n0EnHcF3BOvwgLdGOGCfzSE-CEv0_p7gvxKDDY0Msk,7816
|
|
4
|
+
fpowerkit-0.0.1.dist-info/METADATA,sha256=NB4o17eI7VwVKae_xZgcoFqq9iIvNvJzWyyYFHzxS_8,3603
|
|
5
|
+
fpowerkit-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
6
|
+
fpowerkit-0.0.1.dist-info/top_level.txt,sha256=zWN-TLewJw6rY8yliyNorFN-fevcIM1yX8dT9t88Cic,10
|
|
7
|
+
fpowerkit-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fpowerkit
|