fpowerkit 0.3.7__tar.gz → 0.4.0__tar.gz
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-0.3.7 → fpowerkit-0.4.0}/PKG-INFO +1 -1
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/bus.py +13 -28
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/cases.py +42 -12
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/ess.py +15 -24
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/gen.py +15 -27
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/grid.py +89 -53
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/island.py +1 -2
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/pvwind.py +12 -21
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/utils.py +66 -2
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/pyproject.toml +1 -1
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/.gitignore +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/LICENSE +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/README.md +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/desc.md +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/__init__.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/line.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/lpwrapper.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solbase.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solcmb.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solcvx.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/soldist.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/soldss.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solldf.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solldf2.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/fpowerkit/solnt.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/main.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/test.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/test314t.py +0 -0
- {fpowerkit-0.3.7 → fpowerkit-0.4.0}/test_cmb.py +0 -0
|
@@ -1,30 +1,29 @@
|
|
|
1
1
|
import math
|
|
2
|
-
from typing import Optional
|
|
2
|
+
from typing import Optional, Any, Tuple
|
|
3
3
|
from xml.etree.ElementTree import Element
|
|
4
4
|
from feasytools import TimeFunc
|
|
5
5
|
from .utils import *
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
class Bus:
|
|
8
|
+
class Bus(PositionBase):
|
|
9
9
|
Pd: TimeFunc # pu
|
|
10
10
|
Qd: TimeFunc # pu
|
|
11
11
|
ShadowPrice: FloatVar # Shadow price of generators on this bus,$/pu power
|
|
12
|
-
Lat: NFloat
|
|
13
|
-
Lon: NFloat
|
|
14
12
|
|
|
15
|
-
def __init__(self, id: str, pd_pu: FloatLike, qd_pu: FloatLike,
|
|
13
|
+
def __init__(self, id: str, pd_pu: FloatLike, qd_pu: FloatLike, x: float, y: float, proj:Optional[Any] = None,
|
|
16
14
|
v_pu: NFloat = None, min_v_pu: float = 0.0, max_v_pu: float = float('inf'), theta: NFloat = None):
|
|
17
15
|
'''
|
|
18
16
|
Initialize
|
|
19
17
|
id: Bus ID
|
|
20
18
|
pd_pu: Active power demand, pu
|
|
21
19
|
qd_pu: Reactive power demand, pu
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
x: X coordinate (for plotting)
|
|
21
|
+
y: Y coordinate (for plotting)
|
|
24
22
|
v_pu: Voltage, pu. None for unfixed voltage
|
|
25
23
|
min_v_pu: Minimal voltage, pu
|
|
26
24
|
max_v_pu: Maximal voltage, pu
|
|
27
25
|
'''
|
|
26
|
+
super().__init__(x, y, proj)
|
|
28
27
|
self._id = id
|
|
29
28
|
self._v = v_pu
|
|
30
29
|
self._fixed_v = v_pu is not None
|
|
@@ -33,8 +32,6 @@ class Bus:
|
|
|
33
32
|
self.Pd = Float2Func(pd_pu)
|
|
34
33
|
self.Qd = Float2Func(qd_pu)
|
|
35
34
|
self.ShadowPrice = None
|
|
36
|
-
self.Lat = lat
|
|
37
|
-
self.Lon = lon
|
|
38
35
|
self._t = theta
|
|
39
36
|
self._fixed_t = self._t is not None
|
|
40
37
|
|
|
@@ -135,33 +132,21 @@ class Bus:
|
|
|
135
132
|
|
|
136
133
|
def __repr__(self):
|
|
137
134
|
return f"Bus(id='{self.ID}', v_pu={self.V}, pd_pu={self.Pd}, qd_pu={self.Qd}, " + \
|
|
138
|
-
f"
|
|
135
|
+
f"x={self.x}, y={self.y}, min_v_pu={self.MinV}, max_v_pu={self.MaxV})"
|
|
139
136
|
|
|
140
137
|
def __str__(self):
|
|
141
138
|
return repr(self)
|
|
142
139
|
|
|
143
140
|
def str_t(self, _t: int, /):
|
|
144
141
|
return f"Bus(id='{self.ID}', v_pu={FVstr(self.V)}, pd_pu={self.Pd(_t)}, qd_pu={self.Qd(_t)}, " + \
|
|
145
|
-
f"
|
|
146
|
-
|
|
147
|
-
@property
|
|
148
|
-
def position(self) -> 'tuple[NFloat, NFloat]':
|
|
149
|
-
'''Return the position of the bus, (lat, lon)'''
|
|
150
|
-
return self.Lat, self.Lon
|
|
151
|
-
|
|
152
|
-
@property
|
|
153
|
-
def LonLat(self) -> 'tuple[NFloat, NFloat]':
|
|
154
|
-
return self.Lon, self.Lat
|
|
142
|
+
f"x={self.x}, y={self.y}, min_v_pu={self.MinV}, max_v_pu={self.MaxV})"
|
|
155
143
|
|
|
156
144
|
@staticmethod
|
|
157
|
-
def fromXML(node:
|
|
145
|
+
def fromXML(node: Element, Sb_MVA: float, Ub_kV: float, proj:Optional[Any] = None) -> 'Bus':
|
|
158
146
|
id = node.attrib["ID"]
|
|
159
147
|
p = ReadFloatLike(node.find("Pd"), Sb_MVA, Ub_kV)
|
|
160
148
|
q = ReadFloatLike(node.find("Qd"), Sb_MVA, Ub_kV)
|
|
161
|
-
|
|
162
|
-
if lat is not None: lat = float(lat)
|
|
163
|
-
lon = node.attrib.get("Lon", None)
|
|
164
|
-
if lon is not None: lon = float(lon)
|
|
149
|
+
x, y = PositionBase.load_pos_from_xml(node, proj)
|
|
165
150
|
if "V" in node.attrib:
|
|
166
151
|
v = ReadConst(node.attrib["V"], Sb_MVA, Ub_kV)
|
|
167
152
|
minv = maxv = v
|
|
@@ -169,13 +154,13 @@ class Bus:
|
|
|
169
154
|
minv = ReadConst(node.attrib.get("MinV",'0.8pu'), Sb_MVA, Ub_kV)
|
|
170
155
|
maxv = ReadConst(node.attrib.get("MaxV",'1.2pu'), Sb_MVA, Ub_kV)
|
|
171
156
|
v = None
|
|
172
|
-
return Bus(id, p, q,
|
|
157
|
+
return Bus(id, p, q, x, y, proj, v, minv, maxv)
|
|
173
158
|
|
|
174
159
|
def toXMLNode(self, Ub_kV:Optional[float] = None,
|
|
175
160
|
Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
|
|
176
161
|
e = Element("bus", {"ID": self.ID})
|
|
177
|
-
if self.
|
|
178
|
-
if self.
|
|
162
|
+
if self.x is not None: e.attrib["x"] = f"{self.x:.6f}"
|
|
163
|
+
if self.y is not None: e.attrib["y"] = f"{self.y:.6f}"
|
|
179
164
|
if self.V is not None and self.FixedV:
|
|
180
165
|
e.attrib["V"] = str(self.V * Ub_kV)+"kV" if Ub_kV else str(self.V)
|
|
181
166
|
else:
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from typing import List, Tuple
|
|
1
2
|
from feasytools import SegFunc, ConstFunc
|
|
2
3
|
from .grid import GeneratorModel, Grid, Bus, Line
|
|
3
4
|
|
|
@@ -274,22 +275,44 @@ def _get_buses(
|
|
|
274
275
|
for j in range(24):
|
|
275
276
|
load_fluc.append((ls[j][0], ls[j][1]))
|
|
276
277
|
if grid_repeat == 1:
|
|
277
|
-
B = [Bus(
|
|
278
|
-
|
|
279
|
-
|
|
278
|
+
B = [Bus(
|
|
279
|
+
"b" + str(p[0]),
|
|
280
|
+
_create_load(p[1] / Sb_KVA, load_fluc).repeat(load_repeat, load_period),
|
|
281
|
+
_create_load(p[2] / Sb_KVA, load_fluc).repeat(load_repeat, load_period),
|
|
282
|
+
(i % 10) * 50, (i // 10) * 50,
|
|
283
|
+
min_v_pu=0.9, max_v_pu=1.1
|
|
284
|
+
) for i, p in enumerate(LOAD)]
|
|
280
285
|
else:
|
|
281
286
|
B = []
|
|
282
287
|
for i in range(grid_repeat):
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
288
|
+
pre = len(LOAD) * i
|
|
289
|
+
B.extend([Bus(
|
|
290
|
+
f"b{i}_{p[0]}",
|
|
291
|
+
_create_load(p[1] / Sb_KVA, load_fluc).repeat(load_repeat, load_period),
|
|
292
|
+
_create_load(p[2] / Sb_KVA, load_fluc).repeat(load_repeat, load_period),
|
|
293
|
+
((pre + j) % 10) * 50, ((pre + j) // 10) * 50,
|
|
294
|
+
min_v_pu=0.9, max_v_pu=1.1
|
|
295
|
+
) for j, p in enumerate(LOAD)])
|
|
286
296
|
else:
|
|
287
297
|
if grid_repeat == 1:
|
|
288
|
-
B = [Bus(
|
|
298
|
+
B = [Bus(
|
|
299
|
+
f"b{p[0]}",
|
|
300
|
+
p[1] / Sb_KVA,
|
|
301
|
+
p[2] / Sb_KVA,
|
|
302
|
+
(i % 10) * 50, (i // 10) * 50,
|
|
303
|
+
min_v_pu=0.9, max_v_pu=1.1
|
|
304
|
+
) for i, p in enumerate(LOAD)]
|
|
289
305
|
else:
|
|
290
306
|
B = []
|
|
291
307
|
for i in range(grid_repeat):
|
|
292
|
-
|
|
308
|
+
pre = len(LOAD) * i
|
|
309
|
+
B.extend([Bus(
|
|
310
|
+
f"b{i}_{p[0]}",
|
|
311
|
+
p[1] / Sb_KVA,
|
|
312
|
+
p[2] / Sb_KVA,
|
|
313
|
+
((pre + j) % 10) * 50, ((pre + j) // 10) * 50,
|
|
314
|
+
min_v_pu=0.9, max_v_pu=1.1
|
|
315
|
+
) for j, p in enumerate(LOAD)])
|
|
293
316
|
B[0].fixV(1.0)
|
|
294
317
|
B[0].Pd = ConstFunc(0)
|
|
295
318
|
B[0].Qd = ConstFunc(0)
|
|
@@ -297,7 +320,7 @@ def _get_buses(
|
|
|
297
320
|
B[0].MinV = 0
|
|
298
321
|
return B
|
|
299
322
|
|
|
300
|
-
def _get_lines(Zb:float, LINE:
|
|
323
|
+
def _get_lines(Zb:float, LINE:List[Tuple[int, int, int, float, float]], grid_repeat:int):
|
|
301
324
|
if grid_repeat == 1:
|
|
302
325
|
L = [Line("l"+str(p[0]), "b"+str(p[1]), "b"+str(p[2]), p[3]/Zb, p[4]/Zb) for p in LINE]
|
|
303
326
|
else:
|
|
@@ -308,11 +331,18 @@ def _get_lines(Zb:float, LINE:'list[tuple[int,int,int,float,float]]', grid_repea
|
|
|
308
331
|
|
|
309
332
|
def _get_gens(default_gen:GeneratorModel, gen_pos:'list[int]', grid_repeat:int):
|
|
310
333
|
if grid_repeat == 1:
|
|
311
|
-
G = [default_gen.toGenerator(
|
|
334
|
+
G = [default_gen.toGenerator(
|
|
335
|
+
"g" + p, "b" + p, (i % 10) * 50, (i // 10) * 50
|
|
336
|
+
) for i, p in enumerate(map(str, gen_pos))]
|
|
312
337
|
else:
|
|
313
338
|
G = []
|
|
314
339
|
for i in range(grid_repeat):
|
|
315
|
-
|
|
340
|
+
pre = len(gen_pos) * i
|
|
341
|
+
G.extend([
|
|
342
|
+
default_gen.toGenerator(
|
|
343
|
+
f"g{i}_{p}", f"b{i}_{p}", ((pre + j) % 10) * 50, ((pre + j) // 10) * 50
|
|
344
|
+
) for j, p in enumerate(gen_pos)
|
|
345
|
+
])
|
|
316
346
|
return G
|
|
317
347
|
|
|
318
348
|
class PDNCases:
|
|
@@ -353,7 +383,7 @@ class PDNCases:
|
|
|
353
383
|
_get_buses(Sb_MVA*1000, grid_repeat, _IEEE69_LOAD, changeable_load, load_fluc, load_repeat, load_period),
|
|
354
384
|
_get_lines(Ub_kV**2/Sb_MVA, _IEEE69_LINE, grid_repeat),
|
|
355
385
|
_get_gens(default_gen, gen_pos, grid_repeat),
|
|
356
|
-
[],[]
|
|
386
|
+
[], []
|
|
357
387
|
)
|
|
358
388
|
|
|
359
389
|
__all__ = ["PDNCases"]
|
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
import math
|
|
2
|
-
from typing import Optional
|
|
2
|
+
from typing import Any, Optional
|
|
3
3
|
from enum import IntEnum, Enum
|
|
4
4
|
from xml.etree.ElementTree import Element
|
|
5
5
|
from feasytools import RangeList
|
|
6
6
|
from .utils import *
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
class ESSPolicy(Enum):
|
|
9
10
|
Manual = "Manual" # Charging/discharging manually
|
|
10
11
|
Time = "Time" # Charging/discharging at given period
|
|
11
12
|
Price = "Price" # Charging at low price, discharging at high price
|
|
12
13
|
ByOptim = "Optim" # Charging according to solver optimization
|
|
13
14
|
|
|
15
|
+
|
|
14
16
|
class ESSManualState(IntEnum):
|
|
15
17
|
Idle = 0
|
|
16
18
|
Charge = 1
|
|
17
19
|
Discharge = 2
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
|
|
22
|
+
class ESS(PositionBase):
|
|
20
23
|
'''Model of an Energy Storage System'''
|
|
21
24
|
def __init__(self, id: str, busid: str, cap_puh: float, ec:float, ed:float, pc_max:float, pd_max:float,
|
|
22
|
-
pf:float, policy:ESSPolicy,
|
|
23
|
-
|
|
25
|
+
pf:float, policy:ESSPolicy, x:float, y:float, proj:Optional[Any] = None,ctime:Optional[RangeList]=None,
|
|
26
|
+
dtime:Optional[RangeList]=None, cprice:NFloat=None, dprice:NFloat=None, init_elec_puh: float=0.0):
|
|
24
27
|
'''
|
|
25
28
|
Initialize
|
|
26
29
|
id: ESS ID
|
|
@@ -34,9 +37,8 @@ class ESS:
|
|
|
34
37
|
policy: ESSPolicy
|
|
35
38
|
c_cond: Charging condition
|
|
36
39
|
d_cond: Discharging condition
|
|
37
|
-
Lat: Latitude of the ESS
|
|
38
|
-
Lon: Longitude of the ESS
|
|
39
40
|
'''
|
|
41
|
+
super().__init__(x, y, proj)
|
|
40
42
|
self._id = id
|
|
41
43
|
self._bus = busid
|
|
42
44
|
self._cap = cap_puh
|
|
@@ -45,8 +47,6 @@ class ESS:
|
|
|
45
47
|
self._pc_max = pc_max
|
|
46
48
|
self._pd_max = pd_max
|
|
47
49
|
self._pf = pf
|
|
48
|
-
self.Lat = Lat
|
|
49
|
-
self.Lon = Lon
|
|
50
50
|
self._elec = init_elec_puh
|
|
51
51
|
self._policy = policy
|
|
52
52
|
self._ctime = ctime
|
|
@@ -223,19 +223,11 @@ class ESS:
|
|
|
223
223
|
def PF(self, pf: float):
|
|
224
224
|
assert -1 <= pf <= 1, "Power factor must be in [-1, 1]"
|
|
225
225
|
self._pf = pf
|
|
226
|
-
|
|
227
|
-
def position(self):
|
|
228
|
-
'''Return the position of the ESS'''
|
|
229
|
-
return (self.Lat, self.Lon)
|
|
230
|
-
|
|
231
|
-
def LonLat(self):
|
|
232
|
-
'''Return the position of the ESS'''
|
|
233
|
-
return (self.Lon, self.Lat)
|
|
234
226
|
|
|
235
227
|
def __repr__(self) -> str:
|
|
236
228
|
return (f"ESS(id='{self.ID}', busid='{self.BusID}', "+
|
|
237
229
|
f"pc_max={self.MaxPc:.6f}, pd_max={self.MaxPd:.6f}, pf={self.PF:.6f}, "+
|
|
238
|
-
f"ec={self.EC:.6f}, ed={self.ED:.6f},
|
|
230
|
+
f"ec={self.EC:.6f}, ed={self.ED:.6f}, x={self.x:.6f}, y={self.y:.6f})")
|
|
239
231
|
|
|
240
232
|
def __str__(self) -> str:
|
|
241
233
|
return repr(self)
|
|
@@ -244,7 +236,7 @@ class ESS:
|
|
|
244
236
|
return repr(self)
|
|
245
237
|
|
|
246
238
|
@staticmethod
|
|
247
|
-
def fromXML(node: 'Element', Sb_MVA: float, Ub_kV: float) -> 'ESS':
|
|
239
|
+
def fromXML(node: 'Element', Sb_MVA: float, Ub_kV: float, proj:Optional[Any] = None) -> 'ESS':
|
|
248
240
|
'''
|
|
249
241
|
Load ESS from XML node
|
|
250
242
|
node: XML node
|
|
@@ -270,11 +262,10 @@ class ESS:
|
|
|
270
262
|
dtime = None
|
|
271
263
|
cprice = ReadConst(node.attrib["cprice"], Sb_MVA, Ub_kV) if "cprice" in node.attrib else None
|
|
272
264
|
dprice = ReadConst(node.attrib["dprice"], Sb_MVA, Ub_kV) if "dprice" in node.attrib else None
|
|
273
|
-
|
|
274
|
-
lon = float(node.attrib["Lon"]) if "Lon" in node.attrib else None
|
|
265
|
+
x, y = PositionBase.load_pos_from_xml(node, proj)
|
|
275
266
|
init = ReadConst(node.attrib["init_elec"], Sb_MVA, Ub_kV) if "init_elec" in node.attrib else 0.0
|
|
276
267
|
return ESS(id, busid, cap, ec, ed, pc_max, pd_max, pf, policy,
|
|
277
|
-
ctime, dtime, cprice, dprice,
|
|
268
|
+
x, y, proj, ctime, dtime, cprice, dprice, init)
|
|
278
269
|
|
|
279
270
|
def toXMLNode(self, Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
|
|
280
271
|
'''Convert to XML node'''
|
|
@@ -284,10 +275,10 @@ class ESS:
|
|
|
284
275
|
"ec": str(self.EC),
|
|
285
276
|
"ed": str(self.ED),
|
|
286
277
|
"pf": str(self.PF),
|
|
287
|
-
"policy": self._policy.value
|
|
278
|
+
"policy": self._policy.value,
|
|
279
|
+
"x": f"{self.x:.6f}",
|
|
280
|
+
"y": f"{self.y:.6f}",
|
|
288
281
|
})
|
|
289
|
-
if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
|
|
290
|
-
if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
|
|
291
282
|
if self._ctime is not None:
|
|
292
283
|
e.append(self._ctime.toXMLNode("ctime"))
|
|
293
284
|
if self._dtime is not None:
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
from copy import deepcopy
|
|
2
|
-
from typing import Optional, Union
|
|
2
|
+
from typing import Any, Optional, Union
|
|
3
3
|
from xml.etree.ElementTree import Element
|
|
4
4
|
from feasytools import TimeFunc
|
|
5
5
|
from .utils import *
|
|
6
6
|
|
|
7
7
|
NFloatOrFunc = Union[None, float, TimeFunc]
|
|
8
8
|
|
|
9
|
-
class Generator:
|
|
9
|
+
class Generator(PositionBase):
|
|
10
10
|
Pmin: Optional[TimeFunc] #pu
|
|
11
11
|
Pmax: Optional[TimeFunc] #pu
|
|
12
12
|
Qmin: Optional[TimeFunc] #pu
|
|
@@ -17,9 +17,9 @@ class Generator:
|
|
|
17
17
|
|
|
18
18
|
def __init__(self, id: str, busid: str,
|
|
19
19
|
costA: FloatLike, costB: FloatLike, costC: FloatLike,
|
|
20
|
+
x: float, y: float, proj:Optional[Any] = None,
|
|
20
21
|
pmin_pu: NFloatLike = None, pmax_pu: NFloatLike = None,
|
|
21
22
|
qmin_pu: NFloatLike = None, qmax_pu: NFloatLike = None,
|
|
22
|
-
Lat: NFloat = None, Lon: NFloat = None,
|
|
23
23
|
P: NFloatLike = None, Q: NFloatLike = None, active: bool = True
|
|
24
24
|
):
|
|
25
25
|
'''
|
|
@@ -42,10 +42,9 @@ class Generator:
|
|
|
42
42
|
Or:
|
|
43
43
|
Q: Reactive power output, pu. None for unfixed power
|
|
44
44
|
'''
|
|
45
|
+
super().__init__(x, y, proj)
|
|
45
46
|
self._id = id
|
|
46
47
|
self._bus = busid
|
|
47
|
-
self.Lat = Lat
|
|
48
|
-
self.Lon = Lon
|
|
49
48
|
if P is not None:
|
|
50
49
|
self._p = Float2Func(P)
|
|
51
50
|
self._fixed_p = True
|
|
@@ -140,15 +139,6 @@ class Generator:
|
|
|
140
139
|
self._q = None
|
|
141
140
|
self._fixed_q = False
|
|
142
141
|
|
|
143
|
-
@property
|
|
144
|
-
def position(self) -> 'tuple[NFloat, NFloat]':
|
|
145
|
-
'''Position of the generator, (Lat, Lon)'''
|
|
146
|
-
return (self.Lat, self.Lon)
|
|
147
|
-
|
|
148
|
-
@property
|
|
149
|
-
def LonLat(self) -> 'tuple[NFloat, NFloat]':
|
|
150
|
-
return self.Lon, self.Lat
|
|
151
|
-
|
|
152
142
|
@property
|
|
153
143
|
def ID(self) -> str:
|
|
154
144
|
'''Name of the generator'''
|
|
@@ -162,7 +152,7 @@ class Generator:
|
|
|
162
152
|
def __repr__(self) -> str:
|
|
163
153
|
return (f"Generator(id='{self.ID}', busid='{self.BusID}', P={self.P}, Q={self.Q}, " +
|
|
164
154
|
f"pmin_pu={self.Pmin}, pmax_pu={self.Pmax}, qmin_pu={self.Qmin}, qmax_pu={self.Qmax}, " +
|
|
165
|
-
f"costA={self.CostA}, costB={self.CostB}, costC={self.CostC},
|
|
155
|
+
f"costA={self.CostA}, costB={self.CostB}, costC={self.CostC}, x={self.x}, y={self.y})")
|
|
166
156
|
|
|
167
157
|
def __str__(self) -> str:
|
|
168
158
|
return repr(self)
|
|
@@ -176,7 +166,8 @@ class Generator:
|
|
|
176
166
|
qmax = self.Qmax(_t) if isinstance(self.Qmax, TimeFunc) else None
|
|
177
167
|
return (f"Generator(id='{self.ID}', busid='{self.BusID}', P={p}, Q={q}, " +
|
|
178
168
|
f"pmin_pu={pmin}, pmax_pu={pmax}, qmin_pu={qmin}, qmax_pu={qmax}, " +
|
|
179
|
-
f"costA={self.CostA(_t)}, costB={self.CostB(_t)}, costC={self.CostC(_t)},
|
|
169
|
+
f"costA={self.CostA(_t)}, costB={self.CostB(_t)}, costC={self.CostC(_t)}, " +
|
|
170
|
+
f"x={self.x}, y={self.y})")
|
|
180
171
|
|
|
181
172
|
def Cost(self, _t: int, /, secondary: bool = True) -> FloatVar:
|
|
182
173
|
'''
|
|
@@ -204,7 +195,7 @@ class Generator:
|
|
|
204
195
|
return ret / p
|
|
205
196
|
|
|
206
197
|
@staticmethod
|
|
207
|
-
def fromXML(node: 'Element', Sb_MVA: float, Ub_kV: float):
|
|
198
|
+
def fromXML(node: 'Element', Sb_MVA: float, Ub_kV: float, proj:Optional[Any] = None) -> 'Generator':
|
|
208
199
|
id = node.attrib["ID"]
|
|
209
200
|
busid = node.attrib["Bus"]
|
|
210
201
|
pmin = ReadNFloatLike(node.find("Pmin"), Sb_MVA, Ub_kV)
|
|
@@ -216,19 +207,16 @@ class Generator:
|
|
|
216
207
|
cc = ReadFloatLike(node.find("CostC"), Sb_MVA, Ub_kV)
|
|
217
208
|
p = ReadNFloatLike(node.find("P"), Sb_MVA, Ub_kV)
|
|
218
209
|
q = ReadNFloatLike(node.find("Q"), Sb_MVA, Ub_kV)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
lon = node.attrib.get("Lon", None)
|
|
222
|
-
if lon is not None: lon = float(lon)
|
|
223
|
-
return Generator(id, busid, ca, cb, cc, pmin, pmax, qmin, qmax, lat, lon, p, q)
|
|
210
|
+
x, y = PositionBase.load_pos_from_xml(node, proj)
|
|
211
|
+
return Generator(id, busid, ca, cb, cc, x, y, proj, pmin, pmax, qmin, qmax, p, q)
|
|
224
212
|
|
|
225
213
|
def toXMLNode(self, Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
|
|
226
214
|
e = Element("gen", {
|
|
227
215
|
"ID": self.ID,
|
|
228
216
|
"Bus": self.BusID,
|
|
217
|
+
"x": f"{self.x:.6f}",
|
|
218
|
+
"y": f"{self.y:.6f}",
|
|
229
219
|
})
|
|
230
|
-
if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
|
|
231
|
-
if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
|
|
232
220
|
if Sb_MVA is not None and Sb_kVA is not None:
|
|
233
221
|
raise ValueError("Provide only ONE of Sb_MVA or Sb_kVA.")
|
|
234
222
|
if Sb_MVA is not None:
|
|
@@ -278,13 +266,13 @@ class GeneratorModel:
|
|
|
278
266
|
self.CostB = Float2Func(costB)
|
|
279
267
|
self.CostC = Float2Func(costC)
|
|
280
268
|
|
|
281
|
-
def toGenerator(self, id:str, busid:str):
|
|
269
|
+
def toGenerator(self, id:str, busid:str, x:float, y:float, proj:Optional[Any] = None) -> Generator:
|
|
282
270
|
return Generator(id, busid, deepcopy(self.CostA), deepcopy(self.CostB),
|
|
283
|
-
deepcopy(self.CostC), deepcopy(self.Pmin), deepcopy(self.Pmax),
|
|
271
|
+
deepcopy(self.CostC), x, y, proj, deepcopy(self.Pmin), deepcopy(self.Pmax),
|
|
284
272
|
deepcopy(self.Qmin), deepcopy(self.Qmax))
|
|
285
273
|
|
|
286
274
|
@staticmethod
|
|
287
|
-
def fromXML(node:
|
|
275
|
+
def fromXML(node: Element, Sb_MVA: float, Ub_kV: float):
|
|
288
276
|
pmin = ReadFloatLike(node.find("Pmin"), Sb_MVA, Ub_kV)
|
|
289
277
|
pmax = ReadFloatLike(node.find("Pmax"), Sb_MVA, Ub_kV)
|
|
290
278
|
qmin = ReadFloatLike(node.find("Qmin"), Sb_MVA, Ub_kV)
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import gzip
|
|
2
|
-
import zipfile
|
|
3
1
|
from xml.etree.ElementTree import Element, ElementTree
|
|
4
|
-
from enum import
|
|
5
|
-
from typing import Iterable, Optional
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Any, Dict, Iterable, List, Optional
|
|
6
4
|
from feasytools import ConstFunc
|
|
7
5
|
from .utils import *
|
|
8
6
|
from .bus import *
|
|
@@ -14,16 +12,16 @@ from .ess import *
|
|
|
14
12
|
BusID = str
|
|
15
13
|
LineID = str
|
|
16
14
|
|
|
17
|
-
class PQSaveMode(
|
|
15
|
+
class PQSaveMode(Enum):
|
|
18
16
|
PU = 0
|
|
19
17
|
KVA = 1
|
|
20
18
|
MVA = 2
|
|
21
19
|
|
|
22
|
-
class USaveMode(
|
|
20
|
+
class USaveMode(Enum):
|
|
23
21
|
PU = 0
|
|
24
22
|
KV = 1
|
|
25
23
|
|
|
26
|
-
class ZSaveMode(
|
|
24
|
+
class ZSaveMode(Enum):
|
|
27
25
|
PU = 0
|
|
28
26
|
OHM = 1
|
|
29
27
|
|
|
@@ -50,9 +48,15 @@ class Grid:
|
|
|
50
48
|
'''Ib, unit = kA'''
|
|
51
49
|
return self.Sb / (self.Ub * (3 ** 0.5))
|
|
52
50
|
|
|
53
|
-
def __init__(self, Sb_MVA: float, Ub_kV: float,
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
def __init__(self, Sb_MVA: float, Ub_kV: float,
|
|
52
|
+
buses: Iterable['Bus'],
|
|
53
|
+
lines: Iterable['Line'],
|
|
54
|
+
gens: Iterable['Generator'],
|
|
55
|
+
pvws: Optional[Iterable['PVWind']] = None,
|
|
56
|
+
esss: Optional[Iterable['ESS']] = None,
|
|
57
|
+
cprice: FloatLike = 0.4, dprice: FloatLike = 0.5,
|
|
58
|
+
holdShadowPrice: bool = False,
|
|
59
|
+
proj:Optional[Any] = None):
|
|
56
60
|
'''
|
|
57
61
|
Initialize
|
|
58
62
|
Sb_MVA: base power, MVA
|
|
@@ -65,27 +69,37 @@ class Grid:
|
|
|
65
69
|
cprice: charge price of the whole grid, $/puh
|
|
66
70
|
dprice: discharge price of the whole grid, $/puh
|
|
67
71
|
holdShadowPrice: whether to hold the shadow price of the buses of the last time when failed to solve this time
|
|
72
|
+
proj: projection info of pyproj.Proj, optional
|
|
68
73
|
'''
|
|
69
74
|
self.Sb = Sb_MVA
|
|
70
75
|
self.Ub = Ub_kV
|
|
71
76
|
self._busl = list(buses)
|
|
72
77
|
self._buses = {bus.ID: bus for bus in self._busl}
|
|
73
|
-
|
|
78
|
+
for b in self._buses.values():
|
|
79
|
+
b._proj = proj
|
|
80
|
+
self._lines = {line.ID: line for line in lines}
|
|
74
81
|
self._gens = {gen.ID: gen for gen in gens}
|
|
82
|
+
for g in self._gens.values():
|
|
83
|
+
g._proj = proj
|
|
75
84
|
if pvws is None: pvws = []
|
|
76
85
|
self._pvws = {pvw.ID: pvw for pvw in pvws}
|
|
86
|
+
for p in self._pvws.values():
|
|
87
|
+
p._proj = proj
|
|
77
88
|
if esss is None: esss = []
|
|
78
89
|
self._esss = {ess.ID: ess for ess in esss}
|
|
90
|
+
for e in self._esss.values():
|
|
91
|
+
e._proj = proj
|
|
79
92
|
self._cp = Float2Func(cprice)
|
|
80
93
|
self._dp = Float2Func(dprice)
|
|
81
|
-
self._holdShadowPrice = holdShadowPrice
|
|
94
|
+
self._holdShadowPrice = holdShadowPrice
|
|
95
|
+
self._proj = proj
|
|
82
96
|
|
|
83
97
|
self._bnames = list(self._buses.keys())
|
|
84
|
-
self._ladjfb: '
|
|
85
|
-
self._ladjtb: '
|
|
86
|
-
self._gatb: '
|
|
87
|
-
self._patb: '
|
|
88
|
-
self._eatb: '
|
|
98
|
+
self._ladjfb: 'Dict[str, List[Line]]' = {bus.ID: [] for bus in self._buses.values()}
|
|
99
|
+
self._ladjtb: 'Dict[str, List[Line]]' = {bus.ID: [] for bus in self._buses.values()}
|
|
100
|
+
self._gatb: 'Dict[str, List[Generator]]' = {bus.ID: [] for bus in self._buses.values()}
|
|
101
|
+
self._patb: 'Dict[str, List[PVWind]]' = {bus.ID: [] for bus in self._buses.values()}
|
|
102
|
+
self._eatb: 'Dict[str, List[ESS]]' = {bus.ID: [] for bus in self._buses.values()}
|
|
89
103
|
for line in self._lines.values():
|
|
90
104
|
if not line.fBus in self._bnames: raise ValueError(f"Bus {line.fBus} undefined")
|
|
91
105
|
if not line.tBus in self._bnames: raise ValueError(f"Bus {line.tBus} undefined")
|
|
@@ -110,24 +124,26 @@ class Grid:
|
|
|
110
124
|
return self._dp
|
|
111
125
|
|
|
112
126
|
def AddGen(self, g: Generator):
|
|
113
|
-
'''Add a generator'''
|
|
127
|
+
'''Add a generator. The generator's projection info will be set to the grid's projection info'''
|
|
114
128
|
self._gens[g.ID] = g
|
|
115
129
|
assert g.BusID in self._bnames, f"Bus {g.BusID} undefined"
|
|
116
130
|
self._gatb[g.BusID].append(g)
|
|
131
|
+
g._proj = self._proj
|
|
117
132
|
|
|
118
133
|
def DelGen(self, id: str):
|
|
119
134
|
'''Delete a generator'''
|
|
120
135
|
g = self._gens.pop(id)
|
|
121
136
|
self._gatb[g.BusID].remove(g)
|
|
122
137
|
|
|
123
|
-
def AddBus(self, b: Bus):
|
|
124
|
-
'''Add a bus'''
|
|
138
|
+
def AddBus(self, b: 'Bus'):
|
|
139
|
+
'''Add a bus. The bus's projection info will be set to the grid's projection info'''
|
|
125
140
|
self._buses[b.ID] = b
|
|
126
141
|
self._bnames.append(b.ID)
|
|
127
142
|
self._ladjfb[b.ID] = []
|
|
128
143
|
self._ladjtb[b.ID] = []
|
|
129
144
|
self._gatb[b.ID] = []
|
|
130
145
|
self._patb[b.ID] = []
|
|
146
|
+
b._proj = self._proj
|
|
131
147
|
|
|
132
148
|
def DelBus(self, id: str):
|
|
133
149
|
'''Delete a bus and all related lines, generators and PVWinds'''
|
|
@@ -156,20 +172,23 @@ class Grid:
|
|
|
156
172
|
self._ladjfb[l.fBus].remove(l)
|
|
157
173
|
self._ladjtb[l.tBus].remove(l)
|
|
158
174
|
|
|
159
|
-
def AddPVWind(self, p: PVWind):
|
|
160
|
-
'''Add a PVWind'''
|
|
175
|
+
def AddPVWind(self, p: 'PVWind'):
|
|
176
|
+
'''Add a PVWind. The PVWind's projection info will be set to the grid's projection info'''
|
|
161
177
|
self._pvws[p.ID] = p
|
|
162
178
|
assert p.BusID in self._bnames, f"Bus {p.BusID} undefined"
|
|
163
179
|
self._patb[p.BusID].append(p)
|
|
180
|
+
p._proj = self._proj
|
|
164
181
|
|
|
165
182
|
def DelPVWind(self, id: str):
|
|
166
183
|
'''Delete a PVWind'''
|
|
167
184
|
p = self._pvws.pop(id)
|
|
168
185
|
self._patb[p.BusID].remove(p)
|
|
169
186
|
|
|
170
|
-
def AddESS(self, e: ESS):
|
|
171
|
-
'''Add an ESS'''
|
|
187
|
+
def AddESS(self, e: 'ESS'):
|
|
188
|
+
'''Add an ESS. The ESS's projection info will be set to the grid's projection info'''
|
|
172
189
|
self._esss[e.ID] = e
|
|
190
|
+
self._eatb[e.BusID].append(e)
|
|
191
|
+
e._proj = self._proj
|
|
173
192
|
|
|
174
193
|
def ApplyAllESS(self, dur: int):
|
|
175
194
|
'''Apply all ESSs'''
|
|
@@ -177,7 +196,7 @@ class Grid:
|
|
|
177
196
|
e.Apply(dur)
|
|
178
197
|
|
|
179
198
|
@property
|
|
180
|
-
def BusNames(self) ->
|
|
199
|
+
def BusNames(self) -> List[str]:
|
|
181
200
|
return self._bnames
|
|
182
201
|
|
|
183
202
|
def Bus(self, id: str) -> 'Bus':
|
|
@@ -287,6 +306,12 @@ class Grid:
|
|
|
287
306
|
@property
|
|
288
307
|
def Buses(self):
|
|
289
308
|
return self._buses.values()
|
|
309
|
+
|
|
310
|
+
def CreateKDTreeOfBusPos(self):
|
|
311
|
+
"""Create a KDTree of bus positions for fast spatial queries.
|
|
312
|
+
The index of the buses is the same as that of in self.BusNames."""
|
|
313
|
+
from scipy.spatial import KDTree
|
|
314
|
+
return KDTree([self.Bus(bn).pos for bn in self.BusNames])
|
|
290
315
|
|
|
291
316
|
def LinesOfFBus(self, busid: str, only_active:bool = True) -> 'Iterable[Line]':
|
|
292
317
|
if only_active:
|
|
@@ -315,14 +340,14 @@ class Grid:
|
|
|
315
340
|
return self._gens[id]
|
|
316
341
|
|
|
317
342
|
@property
|
|
318
|
-
def GenNames(self) ->
|
|
343
|
+
def GenNames(self) -> List[str]:
|
|
319
344
|
return list(self._gens.keys())
|
|
320
345
|
|
|
321
346
|
@property
|
|
322
347
|
def Gens(self):
|
|
323
348
|
return self._gens.values()
|
|
324
349
|
|
|
325
|
-
def GensAtBus(self, busid: str) ->
|
|
350
|
+
def GensAtBus(self, busid: str) -> List[Generator]:
|
|
326
351
|
return self._gatb[busid]
|
|
327
352
|
|
|
328
353
|
@property
|
|
@@ -333,7 +358,7 @@ class Grid:
|
|
|
333
358
|
return self._pvws[id]
|
|
334
359
|
|
|
335
360
|
@property
|
|
336
|
-
def PVWindNames(self) ->
|
|
361
|
+
def PVWindNames(self) -> List[str]:
|
|
337
362
|
return list(self._pvws.keys())
|
|
338
363
|
|
|
339
364
|
@property
|
|
@@ -343,7 +368,7 @@ class Grid:
|
|
|
343
368
|
def ESS(self, id: str) -> 'ESS':
|
|
344
369
|
return self._esss[id]
|
|
345
370
|
|
|
346
|
-
def ESSsAtBus(self, busid: str) -> '
|
|
371
|
+
def ESSsAtBus(self, busid: str) -> 'List[ESS]':
|
|
347
372
|
return self._eatb[busid]
|
|
348
373
|
|
|
349
374
|
def __repr__(self):
|
|
@@ -362,12 +387,13 @@ class Grid:
|
|
|
362
387
|
return f"Ub={self.Ub}kV, Sb={self.Sb_MVA}MVA\nAt time {_t}:\nBuses:\n {b}\nLines:\n {l}\nGenerators:\n {g}"
|
|
363
388
|
|
|
364
389
|
@staticmethod
|
|
365
|
-
def fromFile(file_name:str, holdShadowPrice: bool = False):
|
|
366
|
-
return Grid.fromFileXML(file_name, holdShadowPrice)
|
|
390
|
+
def fromFile(file_name:str, holdShadowPrice: bool = False, external_proj:Optional[Projector] = None):
|
|
391
|
+
return Grid.fromFileXML(file_name, holdShadowPrice, external_proj)
|
|
367
392
|
|
|
368
393
|
@staticmethod
|
|
369
|
-
def fromFileXML(xml_name: str, holdShadowPrice: bool = False):
|
|
394
|
+
def fromFileXML(xml_name: str, holdShadowPrice: bool = False, external_proj:Optional[Projector] = None):
|
|
370
395
|
if xml_name.lower().endswith(".xml.gz"):
|
|
396
|
+
import gzip
|
|
371
397
|
fh = gzip.open(xml_name)
|
|
372
398
|
rt = ElementTree(file=fh).getroot()
|
|
373
399
|
fh.close()
|
|
@@ -385,6 +411,15 @@ class Grid:
|
|
|
385
411
|
Ub, unit = ReadVal(rt.attrib["Ub"])
|
|
386
412
|
if unit == "kV": pass
|
|
387
413
|
else: raise ValueError(f"Invalid base value unit: {unit}")
|
|
414
|
+
|
|
415
|
+
if external_proj is None:
|
|
416
|
+
try:
|
|
417
|
+
proj = Projector.fromXML(rt)
|
|
418
|
+
except:
|
|
419
|
+
proj = None
|
|
420
|
+
else:
|
|
421
|
+
proj = external_proj
|
|
422
|
+
|
|
388
423
|
#Read grid model
|
|
389
424
|
bm = rt.attrib.get("model","")
|
|
390
425
|
if bm != "": # No base model
|
|
@@ -397,52 +432,52 @@ class Grid:
|
|
|
397
432
|
from .cases import _get_buses, _get_lines, _get_gens, _DEFAULT_LOAD_SCALE, _DEFAULT_GENERATOR, _DEFAULT_GEN_POS
|
|
398
433
|
if bm.lower() == "ieee33":
|
|
399
434
|
from .cases import _IEEE33_LOAD, _IEEE33_LINE
|
|
400
|
-
buses:
|
|
401
|
-
lines:
|
|
402
|
-
gens:
|
|
435
|
+
buses:List[Bus] = _get_buses(Sb * 1000, grpt, _IEEE33_LOAD, not fixed, _DEFAULT_LOAD_SCALE, lrpt, 86400)
|
|
436
|
+
lines:List[Line] = _get_lines(Ub * Ub / Sb, _IEEE33_LINE, grpt)
|
|
437
|
+
gens:List[Generator] = _get_gens(_DEFAULT_GENERATOR, _DEFAULT_GEN_POS, grpt)
|
|
403
438
|
elif bm.lower() == "ieee69":
|
|
404
439
|
from .cases import _IEEE69_LOAD, _IEEE69_LINE
|
|
405
|
-
buses:
|
|
406
|
-
lines:
|
|
407
|
-
gens:
|
|
440
|
+
buses:List[Bus] = _get_buses(Sb * 1000, grpt, _IEEE69_LOAD, not fixed, _DEFAULT_LOAD_SCALE, lrpt, 86400)
|
|
441
|
+
lines:List[Line] = _get_lines(Ub * Ub / Sb, _IEEE69_LINE, grpt)
|
|
442
|
+
gens:List[Generator] = _get_gens(_DEFAULT_GENERATOR, _DEFAULT_GEN_POS, grpt)
|
|
408
443
|
else:
|
|
409
444
|
raise ValueError(f"Undefined base model: {bm}")
|
|
410
445
|
else:
|
|
411
|
-
buses:
|
|
412
|
-
lines:
|
|
413
|
-
gens:
|
|
414
|
-
pvws:
|
|
415
|
-
esss:
|
|
446
|
+
buses:List[Bus] = []
|
|
447
|
+
lines:List[Line] = []
|
|
448
|
+
gens:List[Generator] = []
|
|
449
|
+
pvws:List[PVWind] = []
|
|
450
|
+
esss:List[ESS] = []
|
|
416
451
|
#Read buses, lines and generators
|
|
417
|
-
cp = 0
|
|
418
|
-
dp = 0
|
|
452
|
+
cp = 0; dp = 0
|
|
419
453
|
|
|
420
|
-
gen_models:
|
|
454
|
+
gen_models:Dict[str, GeneratorModel] = {}
|
|
421
455
|
for e in rt:
|
|
422
456
|
if e.tag == "bus":
|
|
423
|
-
buses.append(Bus.fromXML(e, Sb, Ub))
|
|
457
|
+
buses.append(Bus.fromXML(e, Sb, Ub, proj))
|
|
424
458
|
elif e.tag == "line":
|
|
425
459
|
lines.append(Line.fromXML(e, Ub * Ub / Sb))
|
|
426
460
|
elif e.tag == "gen" or e.tag == "generator":
|
|
427
461
|
bm = e.attrib.get("model","")
|
|
428
462
|
if bm == "": # No base model
|
|
429
|
-
gens.append(Generator.fromXML(e, Sb, Ub))
|
|
463
|
+
gens.append(Generator.fromXML(e, Sb, Ub, proj))
|
|
430
464
|
else:
|
|
431
465
|
if bm not in gen_models: raise ValueError(f"Undefined generator model: {bm}")
|
|
432
|
-
|
|
466
|
+
x, y = PositionBase.load_pos_from_xml(e, proj)
|
|
467
|
+
gens.append(gen_models[bm].toGenerator(e.attrib["ID"], e.attrib["Bus"], x, y, proj))
|
|
433
468
|
elif e.tag == "genmodel" or e.tag == "generatormodel":
|
|
434
469
|
gen_models[e.attrib["name"]] = GeneratorModel.fromXML(e, Sb, Ub)
|
|
435
470
|
elif e.tag == "pv" or e.tag == "wind":
|
|
436
|
-
pvws.append(PVWind.fromXML(e, Sb, Ub))
|
|
471
|
+
pvws.append(PVWind.fromXML(e, Sb, Ub, proj))
|
|
437
472
|
elif e.tag == "ess":
|
|
438
|
-
esss.append(ESS.fromXML(e, Sb, Ub))
|
|
473
|
+
esss.append(ESS.fromXML(e, Sb, Ub, proj))
|
|
439
474
|
elif e.tag == "cprice":
|
|
440
475
|
cp = ReadFloatLike(e, Sb, Ub)
|
|
441
476
|
elif e.tag == "dprice":
|
|
442
477
|
dp = ReadFloatLike(e, Sb, Ub)
|
|
443
478
|
else:
|
|
444
479
|
raise ValueError(f"Unknown XML node: {e.tag}")
|
|
445
|
-
return Grid(Sb, Ub, buses, lines, gens, pvws, esss, cp, dp, holdShadowPrice)
|
|
480
|
+
return Grid(Sb, Ub, buses, lines, gens, pvws, esss, cp, dp, holdShadowPrice, proj)
|
|
446
481
|
|
|
447
482
|
def YMat(self):
|
|
448
483
|
'''(NumPy required) Return the admittance matrix of the grid'''
|
|
@@ -463,7 +498,7 @@ class Grid:
|
|
|
463
498
|
PQmode: PQSaveMode = PQSaveMode.MVA,
|
|
464
499
|
Umode: USaveMode = USaveMode.PU,
|
|
465
500
|
Zmode: ZSaveMode = ZSaveMode.OHM
|
|
466
|
-
)->
|
|
501
|
+
) -> Element:
|
|
467
502
|
e = Element('grid', {
|
|
468
503
|
"Sb": f"{self.Sb_MVA}MVA",
|
|
469
504
|
"Ub": f"{self.Ub}kV",
|
|
@@ -505,6 +540,7 @@ class Grid:
|
|
|
505
540
|
if path.lower().endswith(".xml"):
|
|
506
541
|
et.write(path, encoding="utf-8")
|
|
507
542
|
elif path.lower().endswith(".xml.gz"):
|
|
543
|
+
import zipfile
|
|
508
544
|
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z:
|
|
509
545
|
with z.open("data.xml", "w") as f:
|
|
510
546
|
et.write(f, encoding="utf-8")
|
|
@@ -97,6 +97,5 @@ class Island:
|
|
|
97
97
|
def hasName(self, name: str) -> bool:
|
|
98
98
|
"""Check if the island has a bus, generator, line, PVW or ESS with the given name."""
|
|
99
99
|
for s in chain(self.Buses, self.Gens, self.Lines, self.PVWs, self.ESSs):
|
|
100
|
-
if s in name:
|
|
101
|
-
return True
|
|
100
|
+
if s in name: return True
|
|
102
101
|
return False
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import math
|
|
2
2
|
from xml.etree.ElementTree import Element
|
|
3
|
-
from typing import Optional
|
|
3
|
+
from typing import Any, Optional
|
|
4
4
|
from .utils import *
|
|
5
5
|
|
|
6
|
-
class PVWind:
|
|
6
|
+
class PVWind(PositionBase):
|
|
7
7
|
'''
|
|
8
8
|
Model of PV Panel and Wind Turbine
|
|
9
9
|
Such generators are modeled as no operation cost.
|
|
@@ -11,7 +11,8 @@ class PVWind:
|
|
|
11
11
|
Reactive power output is calculated from the power factor.
|
|
12
12
|
Not all the power output is used, the maximum power abondon rate is given.
|
|
13
13
|
'''
|
|
14
|
-
def __init__(self, id: str, busid:str, p: FloatLike, pf: float,
|
|
14
|
+
def __init__(self, id: str, busid:str, p: FloatLike, pf: float,
|
|
15
|
+
cc:float, tag: str, x:float, y:float, proj:Optional[Any] = None):
|
|
15
16
|
'''
|
|
16
17
|
Initialize
|
|
17
18
|
id: PV Panel ID
|
|
@@ -20,9 +21,10 @@ class PVWind:
|
|
|
20
21
|
pf: Power factor, cos(phi). Reactive output Q = P * sqrt(1 - PF**2)
|
|
21
22
|
cc: curtail cost, $/puh
|
|
22
23
|
tag: "PV" or "Wind", indicating the type of the generator
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
x: X coordinate of the PV Panel
|
|
25
|
+
y: Y coordinate of the PV Panel
|
|
25
26
|
'''
|
|
27
|
+
super().__init__(x, y, proj)
|
|
26
28
|
self._id = id
|
|
27
29
|
self._bus = busid
|
|
28
30
|
self._p = Float2Func(p)
|
|
@@ -32,8 +34,6 @@ class PVWind:
|
|
|
32
34
|
self._cr:FloatVar = None
|
|
33
35
|
self._pr:FloatVar = None
|
|
34
36
|
self._qr:FloatVar = None
|
|
35
|
-
self.Lat = Lat
|
|
36
|
-
self.Lon = Lon
|
|
37
37
|
|
|
38
38
|
@property
|
|
39
39
|
def ID(self):
|
|
@@ -69,14 +69,6 @@ class PVWind:
|
|
|
69
69
|
'''Reactive power output, pu'''
|
|
70
70
|
return self.P(_t) * self.tanPhi
|
|
71
71
|
|
|
72
|
-
def position(self):
|
|
73
|
-
'''Return the position of the PV Panel'''
|
|
74
|
-
return (self.Lat, self.Lon)
|
|
75
|
-
|
|
76
|
-
def LonLat(self):
|
|
77
|
-
'''Return the position of the PV Panel'''
|
|
78
|
-
return (self.Lon, self.Lat)
|
|
79
|
-
|
|
80
72
|
def __repr__(self) -> str:
|
|
81
73
|
return (f"PVPanel(id='{self.ID}', busid='{self.BusID}', P={self.P}, PF={self.PF})")
|
|
82
74
|
|
|
@@ -132,7 +124,7 @@ class PVWind:
|
|
|
132
124
|
return self._qr
|
|
133
125
|
|
|
134
126
|
@staticmethod
|
|
135
|
-
def fromXML(node:
|
|
127
|
+
def fromXML(node: Element, Sb_MVA: float, Ub_kV: float, proj:Optional[Any] = None) -> 'PVWind':
|
|
136
128
|
'''
|
|
137
129
|
Load PV Panel from XML node
|
|
138
130
|
node: XML node
|
|
@@ -143,9 +135,8 @@ class PVWind:
|
|
|
143
135
|
pf = float(node.attrib["pf"])
|
|
144
136
|
cc = ReadConst(node.attrib["cc"], Sb_MVA, Ub_kV) if "cc" in node.attrib else 0
|
|
145
137
|
tag = node.tag
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return PVWind(id, busid, p, pf, cc, tag, lat, lon)
|
|
138
|
+
x, y = PositionBase.load_pos_from_xml(node, proj)
|
|
139
|
+
return PVWind(id, busid, p, pf, cc, tag, x, y, proj)
|
|
149
140
|
|
|
150
141
|
def toXMLNode(self, Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
|
|
151
142
|
'''Convert to XML node'''
|
|
@@ -153,9 +144,9 @@ class PVWind:
|
|
|
153
144
|
"ID": self.ID,
|
|
154
145
|
"Bus": self.BusID,
|
|
155
146
|
"pf": str(self.PF),
|
|
147
|
+
"x": f"{self.x:.6f}",
|
|
148
|
+
"y": f"{self.y:.6f}"
|
|
156
149
|
})
|
|
157
|
-
if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
|
|
158
|
-
if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
|
|
159
150
|
if Sb_MVA is not None and Sb_kVA is not None:
|
|
160
151
|
raise ValueError("Provide only ONE of Sb_MVA or Sb_kVA.")
|
|
161
152
|
if Sb_MVA is not None:
|
|
@@ -1,15 +1,77 @@
|
|
|
1
|
-
from typing import Callable, Optional, Tuple, Union, List, Dict
|
|
1
|
+
from typing import Callable, Optional, Tuple, Union, List, Dict, Any
|
|
2
2
|
from feasytools import TimeFunc, SegFunc, ConstFunc
|
|
3
3
|
from xml.etree.ElementTree import Element
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
FloatVar = Optional[float]
|
|
7
7
|
NFloat = Optional[float]
|
|
8
|
-
FloatLike = Union[int, float, TimeFunc,
|
|
8
|
+
FloatLike = Union[int, float, TimeFunc, List[Tuple[int, float]]]
|
|
9
9
|
NFloatLike = Union[None, FloatLike]
|
|
10
10
|
FloatVals = Union[int, float]
|
|
11
11
|
|
|
12
12
|
|
|
13
|
+
class Projector:
|
|
14
|
+
def __init__(self, proj_param:str, offset_x:float = 0.0, offset_y:float = 0.0):
|
|
15
|
+
from pyproj import Proj
|
|
16
|
+
self._proj = Proj(projparams=proj_param)
|
|
17
|
+
self._offset_x = offset_x
|
|
18
|
+
self._offset_y = offset_y
|
|
19
|
+
|
|
20
|
+
def __call__(self, v1: float, v2: float, inverse: bool = False) -> Tuple[float, float]:
|
|
21
|
+
if inverse:
|
|
22
|
+
return self.convXY2LL(v1, v2)
|
|
23
|
+
else:
|
|
24
|
+
return self.convLL2XY(v1, v2)
|
|
25
|
+
|
|
26
|
+
def convLL2XY(self, lon: float, lat: float) -> Tuple[float, float]:
|
|
27
|
+
x, y = self._proj(lon, lat)
|
|
28
|
+
return x + self._offset_x, y + self._offset_y
|
|
29
|
+
|
|
30
|
+
def convXY2LL(self, x: float, y: float) -> Tuple[float, float]:
|
|
31
|
+
lon, lat = self._proj(x - self._offset_x, y - self._offset_y, inverse=True)
|
|
32
|
+
return lon, lat
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def fromXML(node: Element):
|
|
36
|
+
proj_str = node.get("projection","!")
|
|
37
|
+
net_offset = tuple(map(float, node.get("netOffset", "0,0").split(",")))
|
|
38
|
+
return Projector(proj_str, net_offset[0], net_offset[1])
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class PositionBase:
|
|
42
|
+
def __init__(self, x: float, y: float, proj:Optional[Projector] = None):
|
|
43
|
+
self.x = x
|
|
44
|
+
self.y = y
|
|
45
|
+
self._proj = proj
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def pos(self) -> Tuple[float, float]:
|
|
49
|
+
return (self.x, self.y)
|
|
50
|
+
|
|
51
|
+
position = pos
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def LonLat(self) -> Tuple[float, float]:
|
|
55
|
+
assert self._proj is not None, "No projector defined for this object."
|
|
56
|
+
return self._proj(self.x, self.y, inverse=True)
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def load_pos_from_xml(node: Element, proj:Optional[Projector] = None) -> Tuple[float, float]:
|
|
60
|
+
if proj is None:
|
|
61
|
+
x = float(node.attrib["x"])
|
|
62
|
+
y = float(node.attrib["y"])
|
|
63
|
+
else:
|
|
64
|
+
x = node.attrib.get("x")
|
|
65
|
+
y = node.attrib.get("y")
|
|
66
|
+
if x is None or y is None:
|
|
67
|
+
lon = node.attrib["Lon"]
|
|
68
|
+
lat = node.attrib["Lat"]
|
|
69
|
+
x, y = proj(float(lon), float(lat))
|
|
70
|
+
else:
|
|
71
|
+
x = float(x); y = float(y)
|
|
72
|
+
return x, y
|
|
73
|
+
|
|
74
|
+
|
|
13
75
|
class VarBank:
|
|
14
76
|
def __init__(self):
|
|
15
77
|
try:
|
|
@@ -222,6 +284,8 @@ def ReadFloatLike(e: Optional[Element], sb_mva:float, ub_kv:float) -> FloatLike:
|
|
|
222
284
|
return r
|
|
223
285
|
|
|
224
286
|
__all__ = [
|
|
287
|
+
'Projector',
|
|
288
|
+
'PositionBase',
|
|
225
289
|
'VarBank',
|
|
226
290
|
'Float2Func',
|
|
227
291
|
'Func2Elem',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|