fpowerkit 0.2.2__tar.gz → 0.3.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.2.2 → fpowerkit-0.3.0}/PKG-INFO +2 -2
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/README.md +21 -38
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/__init__.py +0 -1
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/cases.py +1 -1
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/ess.py +1 -1
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/grid.py +20 -8
- fpowerkit-0.3.0/fpowerkit/island.py +91 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/line.py +6 -1
- fpowerkit-0.3.0/fpowerkit/solbase.py +85 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/solcmb.py +24 -15
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/soldist.py +28 -104
- fpowerkit-0.3.0/fpowerkit/soldss.py +125 -0
- fpowerkit-0.3.0/fpowerkit/solnt.py +166 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/utils.py +1 -2
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit.egg-info/PKG-INFO +2 -2
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit.egg-info/SOURCES.txt +1 -1
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit.egg-info/requires.txt +1 -1
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/setup.py +2 -2
- fpowerkit-0.2.2/fpowerkit/solbase.py +0 -30
- fpowerkit-0.2.2/fpowerkit/soldss.py +0 -110
- fpowerkit-0.2.2/fpowerkit/solgcal.py +0 -132
- fpowerkit-0.2.2/fpowerkit/solnt.py +0 -156
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/LICENSE +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/bus.py +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/gen.py +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit/pvwind.py +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit.egg-info/dependency_links.txt +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/fpowerkit.egg-info/top_level.txt +0 -0
- {fpowerkit-0.2.2 → fpowerkit-0.3.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: fpowerkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A Distflow PDN solver based on Gurobi.
|
|
5
5
|
Author: fmy_xfk
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -10,7 +10,7 @@ Requires-Python: >=3.8
|
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
11
|
License-File: LICENSE
|
|
12
12
|
Requires-Dist: feasytools>=0.0.21
|
|
13
|
-
Requires-Dist: numpy>=1.
|
|
13
|
+
Requires-Dist: numpy>=1.19.0
|
|
14
14
|
Dynamic: author
|
|
15
15
|
Dynamic: classifier
|
|
16
16
|
Dynamic: description
|
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
# FPowerKit 配电网运算组件
|
|
2
2
|
**本仓库已上线PyPI**,您可以通过`pip install fpowerkit`直接以Python包的形式安装使用本仓库的代码!
|
|
3
3
|
|
|
4
|
+
## 简介
|
|
5
|
+
- FPowerKit是一个配电网求解组件,它包含电网的描述(含母线、发电机、线路、光伏、风机和储能等)和多种内置和外部求解方案:
|
|
6
|
+
+ (内置) 改进DistFlow配电网最优潮流
|
|
7
|
+
+ (内置) Newton-Raphson潮流计算
|
|
8
|
+
+ (外接) OpenDSS配电网潮流计算
|
|
9
|
+
+ 最优潮流+潮流计算核验的混合方案
|
|
10
|
+
- 对于最优潮流模型,优化目标为“发电成本最小”。发电成本模型为二次函数$f(x)=ax^2+bx+c$或者一次函数$f(x)=bx+c$。
|
|
11
|
+
- 依赖于feasytools和numpy: `pip install feasytools numpy`
|
|
12
|
+
- 内含IEEE 33节点配电网和IEEE 69节点配电网,可通过以下方式快速创建:
|
|
13
|
+
```py
|
|
14
|
+
from fpowerkit import PDNCases
|
|
15
|
+
grid_obj33 = PDNCases.IEEE33()
|
|
16
|
+
grid_obj69 = PDNCases.IEEE69()
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 重要信息目录
|
|
20
|
+
+ [FPowerKit文件格式](docs/xml_file.md)
|
|
21
|
+
+ [在命令行中使用](docs/cmd.md)
|
|
22
|
+
+ [从代码创建和求解电网](docs/develop.md)
|
|
23
|
+
|
|
24
|
+
## 引用说明
|
|
4
25
|
这个项目是[V2Sim](https://gitee.com/fmy_xfk/v2sim)的附属项目。V2Sim的论文请见如下链接
|
|
5
26
|
|
|
6
27
|
- 正式版:https://ieeexplore.ieee.org/document/10970754
|
|
@@ -22,41 +43,3 @@
|
|
|
22
43
|
```
|
|
23
44
|
|
|
24
45
|
IEEE33和IEEE69节点的配网数据从公开渠道收集。除配网数据以外,本仓库代码均遵循LGPL3.0协议使用。
|
|
25
|
-
|
|
26
|
-
## 简介
|
|
27
|
-
- 依赖于feasytools和numpy: `pip install feasytools numpy`(请自行安装Gurobi并申请许可证)
|
|
28
|
-
- 包含电网的描述(含母线、发电机、线路、光伏、风机和储能等)和改进配电网DistFlow模型求解。
|
|
29
|
-
- 优化目标为“发电成本最小”。发电成本模型为二次函数$f(x)=ax^2+bx+c$或者一次函数$f(x)=bx+c$。
|
|
30
|
-
- 内含IEEE 33节点配电网和IEEE 69节点配电网,可通过以下方式快速创建:
|
|
31
|
-
```py
|
|
32
|
-
from fpowerkit import PDNCases
|
|
33
|
-
grid_obj33 = PDNCases.IEEE33()
|
|
34
|
-
grid_obj69 = PDNCases.IEEE69()
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## 使用
|
|
38
|
-
|
|
39
|
-
克隆本仓库,并输入以下命令开始求解:
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
python main.py -g cases/33base.xml
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
请注意上述命令不适用于pip安装的版本。
|
|
46
|
-
|
|
47
|
-
`33base`描述了IEEE 33节点配电网模型。换成`3nodes`则是一个极简版3节点辐射形配电网模型,结构如下:
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
G0 G1
|
|
51
|
-
| |
|
|
52
|
-
B0->B1->B2
|
|
53
|
-
| |
|
|
54
|
-
L1 L2
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
您也换成自己的配电网文件来求解,例如`python main.py -g path/to/your_case.xml`
|
|
58
|
-
|
|
59
|
-
## 配电网文件格式
|
|
60
|
-
[XML格式](docs/xml_file.md)
|
|
61
|
-
|
|
62
|
-
ZIP格式已经不再支持。
|
|
@@ -76,7 +76,7 @@ class ESS:
|
|
|
76
76
|
'''Calculate reactive power'''
|
|
77
77
|
return P * math.sqrt(1 - self._pf**2)
|
|
78
78
|
|
|
79
|
-
def GetLoad(self, t: int, cprice:float, dprice:float) -> tuple[float, float]:
|
|
79
|
+
def GetLoad(self, t: int, cprice:float, dprice:float) -> 'tuple[float, float]':
|
|
80
80
|
'''
|
|
81
81
|
Get the power of the ESS. Return p+jq.
|
|
82
82
|
p > 0 means the ESS is charging, and thus the ESS is treated as a load.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
from collections import deque
|
|
1
2
|
from enum import IntEnum
|
|
2
3
|
import gzip
|
|
3
4
|
from xml.etree.ElementTree import ElementTree
|
|
4
5
|
import zipfile
|
|
5
|
-
from
|
|
6
|
-
from typing import Any, Iterable
|
|
6
|
+
from typing import Iterable
|
|
7
7
|
|
|
8
8
|
import numpy as np
|
|
9
9
|
from .bus import *
|
|
@@ -52,7 +52,7 @@ class Grid:
|
|
|
52
52
|
return self.Sb / (self.Ub * (3 ** 0.5))
|
|
53
53
|
|
|
54
54
|
def __init__(self, Sb_MVA: float, Ub_kV: float, buses: 'Iterable[Bus]', lines: 'Iterable[Line]',
|
|
55
|
-
gens: 'Iterable[Generator]', pvws: 'Iterable[PVWind]', esss: 'Iterable[ESS]',
|
|
55
|
+
gens: 'Iterable[Generator]', pvws: 'Optional[Iterable[PVWind]]' = None, esss: 'Optional[Iterable[ESS]]' = None,
|
|
56
56
|
cprice: FloatLike = 0, dprice: FloatLike = 0, holdShadowPrice: bool = False):
|
|
57
57
|
'''
|
|
58
58
|
Initialize
|
|
@@ -73,7 +73,9 @@ class Grid:
|
|
|
73
73
|
self._buses = {bus.ID: bus for bus in self._busl}
|
|
74
74
|
self._lines = {line.ID: line for line in lines}
|
|
75
75
|
self._gens = {gen.ID: gen for gen in gens}
|
|
76
|
+
if pvws is None: pvws = []
|
|
76
77
|
self._pvws = {pvw.ID: pvw for pvw in pvws}
|
|
78
|
+
if esss is None: esss = []
|
|
77
79
|
self._esss = {ess.ID: ess for ess in esss}
|
|
78
80
|
self._cp = Float2Func(cprice)
|
|
79
81
|
self._dp = Float2Func(dprice)
|
|
@@ -287,11 +289,17 @@ class Grid:
|
|
|
287
289
|
def Buses(self):
|
|
288
290
|
return self._buses.values()
|
|
289
291
|
|
|
290
|
-
def LinesOfFBus(self, busid: str) -> '
|
|
291
|
-
|
|
292
|
+
def LinesOfFBus(self, busid: str, only_active:bool = True) -> 'Iterable[Line]':
|
|
293
|
+
if only_active:
|
|
294
|
+
return (l for l in self._ladjfb[busid] if l.active)
|
|
295
|
+
else:
|
|
296
|
+
return self._ladjfb[busid]
|
|
292
297
|
|
|
293
|
-
def LinesOfTBus(self, busid: str) -> '
|
|
294
|
-
|
|
298
|
+
def LinesOfTBus(self, busid: str, only_active:bool = True) -> 'Iterable[Line]':
|
|
299
|
+
if only_active:
|
|
300
|
+
return (l for l in self._ladjtb[busid] if l.active)
|
|
301
|
+
else:
|
|
302
|
+
return self._ladjtb[busid]
|
|
295
303
|
|
|
296
304
|
def Line(self, id: str) -> 'Line':
|
|
297
305
|
return self._lines[id]
|
|
@@ -299,6 +307,10 @@ class Grid:
|
|
|
299
307
|
@property
|
|
300
308
|
def Lines(self):
|
|
301
309
|
return self._lines.values()
|
|
310
|
+
|
|
311
|
+
@property
|
|
312
|
+
def ActiveLines(self):
|
|
313
|
+
return (l for l in self._lines.values() if l.active)
|
|
302
314
|
|
|
303
315
|
def Gen(self, id: str) -> 'Generator':
|
|
304
316
|
return self._gens[id]
|
|
@@ -437,7 +449,7 @@ class Grid:
|
|
|
437
449
|
'''Return the admittance matrix of the grid'''
|
|
438
450
|
n = len(self._buses)
|
|
439
451
|
Y = np.zeros((n, n), dtype=complex)
|
|
440
|
-
for l in self.
|
|
452
|
+
for l in self.ActiveLines:
|
|
441
453
|
i = self._bnames.index(l.fBus)
|
|
442
454
|
j = self._bnames.index(l.tBus)
|
|
443
455
|
y = 1 / l.Z
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from collections import deque
|
|
2
|
+
from .grid import *
|
|
3
|
+
|
|
4
|
+
class IslandResult(IntEnum):
|
|
5
|
+
Undefined = -1
|
|
6
|
+
OK = 0 # The island is solved without overflow
|
|
7
|
+
OverFlow = 1 # The island is solved but overflow
|
|
8
|
+
Failed = 2 # No island is solved
|
|
9
|
+
|
|
10
|
+
class Island:
|
|
11
|
+
@staticmethod
|
|
12
|
+
def from_grid(g: Grid) -> 'list[Island]':
|
|
13
|
+
"""Create a list of islands from the grid. An island is a set of buses that are connected to each other."""
|
|
14
|
+
q: 'deque[BusID]' = deque()
|
|
15
|
+
visited: 'set[BusID]' = set()
|
|
16
|
+
islands: 'list[set[BusID]]' = []
|
|
17
|
+
for bus in g.BusNames:
|
|
18
|
+
if bus in visited: continue
|
|
19
|
+
q.append(bus)
|
|
20
|
+
island: 'set[BusID]' = set()
|
|
21
|
+
while len(q) > 0:
|
|
22
|
+
b = q.popleft()
|
|
23
|
+
if b in visited: continue
|
|
24
|
+
visited.add(b)
|
|
25
|
+
island.add(b)
|
|
26
|
+
for line in g.LinesOfFBus(b):
|
|
27
|
+
if line.tBus in visited: continue
|
|
28
|
+
q.append(line._tBus)
|
|
29
|
+
for line in g.LinesOfTBus(b):
|
|
30
|
+
if line.fBus in visited: continue
|
|
31
|
+
q.append(line.fBus)
|
|
32
|
+
islands.append(island)
|
|
33
|
+
return [Island(g, island) for island in islands]
|
|
34
|
+
|
|
35
|
+
def __init__(self, g: Grid, buses: Iterable[BusID]):
|
|
36
|
+
self.grid = g
|
|
37
|
+
self.Buses: 'set[str]' = set()
|
|
38
|
+
self.Gens: 'set[str]' = set()
|
|
39
|
+
self.Lines: 'set[str]' = set()
|
|
40
|
+
self.PVWs: 'set[str]' = set()
|
|
41
|
+
self.ESSs: 'set[str]' = set()
|
|
42
|
+
self.result: IslandResult = IslandResult.Undefined
|
|
43
|
+
self.result_value:float = 0.0
|
|
44
|
+
for b in buses:
|
|
45
|
+
self.Buses.add(b)
|
|
46
|
+
self.Gens.update(gen.ID for gen in g.GensAtBus(b))
|
|
47
|
+
self.Lines.update(ln.ID for ln in g.LinesOfFBus(b))
|
|
48
|
+
self.Lines.update(ln.ID for ln in g.LinesOfTBus(b))
|
|
49
|
+
self.PVWs.update(p.ID for p in g._patb[b])
|
|
50
|
+
self.ESSs.update(e.ID for e in g._eatb[b])
|
|
51
|
+
|
|
52
|
+
def __repr__(self):
|
|
53
|
+
return f"Island: {self.Buses}\nGens: {self.Gens}\nLines: {self.Lines}\nPVWs: {self.PVWs}\nESSs: {self.ESSs}"
|
|
54
|
+
|
|
55
|
+
def __str__(self):
|
|
56
|
+
return self.__repr__()
|
|
57
|
+
|
|
58
|
+
def BusItems(self):
|
|
59
|
+
for b in self.Buses:
|
|
60
|
+
yield b, self.grid.Bus(b)
|
|
61
|
+
|
|
62
|
+
def GenItems(self):
|
|
63
|
+
for g in self.Gens:
|
|
64
|
+
yield g, self.grid.Gen(g)
|
|
65
|
+
|
|
66
|
+
def LineItems(self):
|
|
67
|
+
for l in self.Lines:
|
|
68
|
+
yield l, self.grid.Line(l)
|
|
69
|
+
|
|
70
|
+
def PVWItems(self):
|
|
71
|
+
for p in self.PVWs:
|
|
72
|
+
yield p, self.grid.PVWind(p)
|
|
73
|
+
|
|
74
|
+
def ESSItems(self):
|
|
75
|
+
for e in self.ESSs:
|
|
76
|
+
yield e, self.grid.ESS(e)
|
|
77
|
+
|
|
78
|
+
def YMat(self) -> 'tuple[dict[str,int], np.ndarray]':
|
|
79
|
+
'''Return the admittance matrix of the grid'''
|
|
80
|
+
n = len(self.Buses)
|
|
81
|
+
bus_dict = {b: i for i, b in enumerate(self.Buses)}
|
|
82
|
+
Y = np.zeros((n, n), dtype=complex)
|
|
83
|
+
for _, l in self.LineItems():
|
|
84
|
+
i = bus_dict[l.fBus]
|
|
85
|
+
j = bus_dict[l.tBus]
|
|
86
|
+
y = 1 / l.Z
|
|
87
|
+
Y[i, j] -= y
|
|
88
|
+
Y[j, i] -= y
|
|
89
|
+
Y[i, i] += y
|
|
90
|
+
Y[j, j] += y
|
|
91
|
+
return bus_dict, Y
|
|
@@ -10,7 +10,7 @@ class Line:
|
|
|
10
10
|
I: FloatVar # pu
|
|
11
11
|
|
|
12
12
|
def __init__(self, id: str, fbus: str, tbus: str, r_pu: float, x_pu: float, i_pu: NFloat = None,
|
|
13
|
-
p_pu: NFloat = None, q_pu: NFloat = None, max_I_kA: float = math.inf, length_km: NFloat = None):
|
|
13
|
+
p_pu: NFloat = None, q_pu: NFloat = None, max_I_kA: float = math.inf, length_km: NFloat = None, active: bool = True):
|
|
14
14
|
'''
|
|
15
15
|
Initialize
|
|
16
16
|
id: Line ID
|
|
@@ -29,6 +29,7 @@ class Line:
|
|
|
29
29
|
self.P = p_pu
|
|
30
30
|
self.Q = q_pu
|
|
31
31
|
self.L = length_km
|
|
32
|
+
self.active = active
|
|
32
33
|
|
|
33
34
|
@property
|
|
34
35
|
def ID(self) -> str:
|
|
@@ -81,8 +82,12 @@ class Line:
|
|
|
81
82
|
t = node.attrib["To"]
|
|
82
83
|
r, ru = ReadVal(node.attrib["R"])
|
|
83
84
|
if ru.lower() == "ohm": r = float(r) / Zb_Ohm
|
|
85
|
+
elif ru.lower() in ["pu",""]: r = float(r)
|
|
86
|
+
else: raise ValueError(f"Unknown unit '{ru}' for R, only 'ohm','pu', and non-unit (treated as pu) are supported")
|
|
84
87
|
x, xu = ReadVal(node.attrib["X"])
|
|
85
88
|
if xu.lower() == "ohm": x = float(x) / Zb_Ohm
|
|
89
|
+
elif xu.lower() in ["pu",""]: x = float(x)
|
|
90
|
+
else: raise ValueError(f"Unknown unit '{xu}' for X, only 'ohm','pu', and non-unit (treated as pu) are supported")
|
|
86
91
|
max_I = float(node.attrib.get("MaxIkA", math.inf))
|
|
87
92
|
l = float(node.attrib.get("Length_km", -1))
|
|
88
93
|
return Line(id, f, t, r, x, max_I_kA=max_I, length_km=l)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from enum import IntEnum
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from .island import *
|
|
5
|
+
from .grid import *
|
|
6
|
+
|
|
7
|
+
DEFAULT_SAVETO = "./fpowerkit_logs/"
|
|
8
|
+
|
|
9
|
+
class GridSolveResult(IntEnum):
|
|
10
|
+
'''Result of grid solving'''
|
|
11
|
+
Failed = 0
|
|
12
|
+
OK = 1
|
|
13
|
+
OKwithoutVICons = 2 # Deprecated
|
|
14
|
+
SubOKwithoutVICons = 3 # Deprecated
|
|
15
|
+
PartialOK = 4
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class SolverBase(ABC):
|
|
19
|
+
def __init__(self, grid:Grid, eps:float = 1e-6, max_iter:int = 1000, *, default_saveto:str = DEFAULT_SAVETO, **kwargs):
|
|
20
|
+
self.UpdateGrid(grid)
|
|
21
|
+
self.eps = eps
|
|
22
|
+
self.max_iter = max_iter
|
|
23
|
+
self.saveto = default_saveto
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def grid(self) -> Grid:
|
|
27
|
+
'''Get the grid'''
|
|
28
|
+
return self._g
|
|
29
|
+
@grid.setter
|
|
30
|
+
def grid(self, grid:Grid):
|
|
31
|
+
'''Set the grid'''
|
|
32
|
+
self.UpdateGrid(grid)
|
|
33
|
+
|
|
34
|
+
def UpdateGrid(self, grid:Grid):
|
|
35
|
+
'''Update the grid'''
|
|
36
|
+
self._g = grid
|
|
37
|
+
self._islands = Island.from_grid(grid)
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def Islands(self) -> 'list[Island]':
|
|
41
|
+
'''Get the islands of the grid'''
|
|
42
|
+
return self._islands
|
|
43
|
+
|
|
44
|
+
def SetErrorSaveTo(self, path:str = DEFAULT_SAVETO):
|
|
45
|
+
self.saveto = path
|
|
46
|
+
Path(path).mkdir(parents=True, exist_ok = True)
|
|
47
|
+
|
|
48
|
+
def _calc_line_params(self):
|
|
49
|
+
for l in self.grid.Lines:
|
|
50
|
+
if not l.active:
|
|
51
|
+
l.I = 0.0
|
|
52
|
+
l.P = 0.0
|
|
53
|
+
l.Q = 0.0
|
|
54
|
+
continue
|
|
55
|
+
vi = self.grid.Bus(l.fBus).V_cpx
|
|
56
|
+
vj = self.grid.Bus(l.tBus).V_cpx
|
|
57
|
+
assert vi is not None and vj is not None
|
|
58
|
+
i = (vi-vj)/l.Z
|
|
59
|
+
l.I = abs(i)
|
|
60
|
+
s = vi*i.conjugate()
|
|
61
|
+
l.P = s.real
|
|
62
|
+
l.Q = s.imag
|
|
63
|
+
|
|
64
|
+
def solve(self, _t:int, /, **kwargs):
|
|
65
|
+
ok_cnt = 0
|
|
66
|
+
res_val = 0.0
|
|
67
|
+
for i, il in enumerate(self._islands):
|
|
68
|
+
r, v = self.solve_island(i, il, _t, **kwargs)
|
|
69
|
+
il.result = r
|
|
70
|
+
il.result_value = v
|
|
71
|
+
if r == IslandResult.OK:
|
|
72
|
+
ok_cnt += 1
|
|
73
|
+
res_val += v
|
|
74
|
+
self._calc_line_params()
|
|
75
|
+
if ok_cnt == len(self._islands):
|
|
76
|
+
return GridSolveResult.OK, res_val
|
|
77
|
+
elif ok_cnt == 0:
|
|
78
|
+
return GridSolveResult.Failed, -1
|
|
79
|
+
else:
|
|
80
|
+
return GridSolveResult.PartialOK, res_val
|
|
81
|
+
|
|
82
|
+
@abstractmethod
|
|
83
|
+
def solve_island(self, i:int, island:Island, _t:int, /, **kwargs) -> 'tuple[IslandResult, float]':
|
|
84
|
+
'''Solve the island'''
|
|
85
|
+
raise NotImplementedError
|
|
@@ -1,21 +1,27 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
from typing import Union, Iterable
|
|
1
3
|
from .grid import Grid
|
|
2
4
|
from .solbase import DEFAULT_SAVETO, GridSolveResult, SolverBase
|
|
3
5
|
from warnings import warn
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
class Estimator(StrEnum):
|
|
8
|
+
DistFlow = 'DistFlow'
|
|
9
|
+
|
|
10
|
+
class Calculator(StrEnum):
|
|
11
|
+
OpenDSS = 'OpenDSS'
|
|
12
|
+
Newton = 'Newton'
|
|
13
|
+
NoneSolver = 'None'
|
|
14
|
+
|
|
15
|
+
CALCULATORS_NEED_SOURCE_BUS = [Calculator.OpenDSS]
|
|
8
16
|
|
|
9
17
|
class CombinedSolver(SolverBase):
|
|
10
18
|
"""
|
|
11
19
|
A class that use DistFlowSolver to estimate the power flow and then use OpenDSSSolver or NewtonSolver to solve the power flow problem.
|
|
12
20
|
"""
|
|
13
|
-
def __init__(self, grid:Grid, eps:float = 1e-6, max_iter:int = 1000, *,
|
|
14
|
-
default_saveto:str = DEFAULT_SAVETO, estimator:
|
|
15
|
-
calculator:
|
|
21
|
+
def __init__(self, grid:Grid, eps:float = 1e-6, max_iter:int = 1000, *, mlrp:float = 0.5,
|
|
22
|
+
default_saveto:str = DEFAULT_SAVETO, estimator:Estimator = Estimator.DistFlow,
|
|
23
|
+
calculator:Calculator = Calculator.Newton, source_bus:'Union[str,Iterable[str]]' = ""):
|
|
16
24
|
super().__init__(grid, eps, max_iter, default_saveto = default_saveto)
|
|
17
|
-
assert estimator in ESTIMATORS, f"Invalid estimator '{estimator}'. Choose from: " + ','.join(ESTIMATORS) + "."
|
|
18
|
-
assert calculator in CALCULATORS, f"Invalid solver type '{calculator}'. Choose from: " + ','.join(CALCULATORS) + "."
|
|
19
25
|
|
|
20
26
|
from .soldist import DistFlowSolver
|
|
21
27
|
self.est = DistFlowSolver(grid, mlrp = mlrp)
|
|
@@ -31,16 +37,19 @@ class CombinedSolver(SolverBase):
|
|
|
31
37
|
res, obj = self.est.solve(_t, timeout_s=timeout_s)
|
|
32
38
|
if res == GridSolveResult.Failed:
|
|
33
39
|
return res, obj
|
|
34
|
-
if self.cal_str ==
|
|
40
|
+
if self.cal_str == Calculator.NoneSolver:
|
|
35
41
|
return res, obj
|
|
36
|
-
elif self.cal_str ==
|
|
42
|
+
elif self.cal_str == Calculator.OpenDSS:
|
|
37
43
|
from .soldss import OpenDSSSolver
|
|
38
44
|
solver = OpenDSSSolver(self.est.grid, source_bus = self.source_bus)
|
|
39
|
-
elif self.cal_str ==
|
|
40
|
-
from .solgcal import GridCalSolver
|
|
41
|
-
solver = GridCalSolver(self.est.grid, slack_bus = self.source_bus)
|
|
42
|
-
else:
|
|
45
|
+
elif self.cal_str == Calculator.Newton:
|
|
43
46
|
from .solnt import NewtonSolver
|
|
44
47
|
solver = NewtonSolver(self.est.grid)
|
|
48
|
+
else:
|
|
49
|
+
raise ValueError(f"Invalid solver type '{self.cal_str}'.")
|
|
45
50
|
res, obj = solver.solve(_t, timeout_s = timeout_s)
|
|
46
|
-
return res, obj
|
|
51
|
+
return res, obj
|
|
52
|
+
|
|
53
|
+
def solve_island(self, i:int, island, _t:int):
|
|
54
|
+
'''Solve the island'''
|
|
55
|
+
raise NotImplementedError
|
|
@@ -2,9 +2,9 @@ from itertools import chain
|
|
|
2
2
|
from pathlib import Path
|
|
3
3
|
from typing import Optional, Union
|
|
4
4
|
from dataclasses import dataclass
|
|
5
|
-
from collections import deque
|
|
6
5
|
import math
|
|
7
6
|
from .solbase import *
|
|
7
|
+
from .island import *
|
|
8
8
|
|
|
9
9
|
VF = Union['Var', float] # type: ignore
|
|
10
10
|
|
|
@@ -15,58 +15,10 @@ class LoadReduceModule:
|
|
|
15
15
|
Limit: TimeFunc
|
|
16
16
|
Reduction: FloatVar = None
|
|
17
17
|
|
|
18
|
-
class _Island:
|
|
19
|
-
def __init__(self, g: Grid, buses: Iterable[BusID], cutlines: set[LineID]):
|
|
20
|
-
self.grid = g
|
|
21
|
-
self.Buses: set[str] = set()
|
|
22
|
-
self.Gens: set[str] = set()
|
|
23
|
-
self.Lines: set[str] = set()
|
|
24
|
-
self.PVWs: set[str] = set()
|
|
25
|
-
self.ESSs: set[str] = set()
|
|
26
|
-
for b in buses:
|
|
27
|
-
self.Buses.add(b)
|
|
28
|
-
self.Gens.update(gen.ID for gen in g._gatb[b])
|
|
29
|
-
self.Lines.update(ln.ID for ln in g._ladjfb[b] if ln.ID not in cutlines)
|
|
30
|
-
self.Lines.update(ln.ID for ln in g._ladjtb[b] if ln.ID not in cutlines)
|
|
31
|
-
self.PVWs.update(p.ID for p in g._patb[b])
|
|
32
|
-
self.ESSs.update(e.ID for e in g._eatb[b])
|
|
33
|
-
|
|
34
|
-
def __repr__(self):
|
|
35
|
-
return f"Island: {self.Buses}\nGens: {self.Gens}\nLines: {self.Lines}\nPVWs: {self.PVWs}\nESSs: {self.ESSs}"
|
|
36
|
-
|
|
37
|
-
def __str__(self):
|
|
38
|
-
return self.__repr__()
|
|
39
|
-
|
|
40
|
-
def BusItems(self):
|
|
41
|
-
for b in self.Buses:
|
|
42
|
-
yield b, self.grid.Bus(b)
|
|
43
|
-
|
|
44
|
-
def GenItems(self):
|
|
45
|
-
for g in self.Gens:
|
|
46
|
-
yield g, self.grid.Gen(g)
|
|
47
|
-
|
|
48
|
-
def LineItems(self):
|
|
49
|
-
for l in self.Lines:
|
|
50
|
-
yield l, self.grid.Line(l)
|
|
51
|
-
|
|
52
|
-
def PVWItems(self):
|
|
53
|
-
for p in self.PVWs:
|
|
54
|
-
yield p, self.grid.PVWind(p)
|
|
55
|
-
|
|
56
|
-
def ESSItems(self):
|
|
57
|
-
for e in self.ESSs:
|
|
58
|
-
yield e, self.grid.ESS(e)
|
|
59
|
-
|
|
60
|
-
class IslandResult(IntEnum):
|
|
61
|
-
Undefined = -1
|
|
62
|
-
OK = 0 # The island is solved without overflow
|
|
63
|
-
OverFlow = 1 # The island is solved but overflow
|
|
64
|
-
Failed = 2 # No island is solved
|
|
65
|
-
|
|
66
18
|
class DistFlowSolver(SolverBase):
|
|
67
19
|
'''DistFlow solver'''
|
|
68
20
|
def __init__(self, grid:Grid, eps:float = 1e-6, max_iter:int = 1000, *,
|
|
69
|
-
default_saveto:str = DEFAULT_SAVETO, mlrp: float = 0.5, secondary_cost: bool = True):
|
|
21
|
+
default_saveto:str = DEFAULT_SAVETO, mlrp: float = 0.5, secondary_cost: bool = True, bigM: float = 1e6):
|
|
70
22
|
'''
|
|
71
23
|
Initialize
|
|
72
24
|
grid: Grid object
|
|
@@ -74,17 +26,15 @@ class DistFlowSolver(SolverBase):
|
|
|
74
26
|
mlrp: Maximum proportion of load reduction
|
|
75
27
|
secondary_cost: Whether to include the secondary cost.
|
|
76
28
|
If False, the cost is calculated as CostB * Pg + CostC;
|
|
77
|
-
If True, the cost is calculated as CostA * Pg^2 + CostB * Pg + CostC
|
|
78
|
-
which is likely to induce numerical instability.
|
|
29
|
+
If True, the cost is calculated as CostA * Pg^2 + CostB * Pg + CostC.
|
|
79
30
|
'''
|
|
80
31
|
super().__init__(grid, eps, max_iter, default_saveto = default_saveto)
|
|
81
32
|
self._decb: dict[BusID, LoadReduceModule] = {}
|
|
82
|
-
self.C =
|
|
33
|
+
self.C = bigM
|
|
83
34
|
self._mlrp = mlrp
|
|
84
35
|
self._oflines: set[LineID] = set()
|
|
85
36
|
self._ofbuses: set[BusID] = set()
|
|
86
|
-
self._sec_cost = secondary_cost
|
|
87
|
-
self.UpdateGrid()
|
|
37
|
+
self._sec_cost = secondary_cost
|
|
88
38
|
|
|
89
39
|
@property
|
|
90
40
|
def OverflowLines(self):
|
|
@@ -94,46 +44,13 @@ class DistFlowSolver(SolverBase):
|
|
|
94
44
|
def OverflowBuses(self):
|
|
95
45
|
return self._ofbuses
|
|
96
46
|
|
|
97
|
-
def UpdateGrid(self, cut_overflow_lines: bool = False):
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
47
|
+
def UpdateGrid(self, grid:Grid, cut_overflow_lines: bool = False):
|
|
48
|
+
if cut_overflow_lines:
|
|
49
|
+
for lID in self._oflines:
|
|
50
|
+
self.grid.Line(lID).active = False
|
|
51
|
+
super().UpdateGrid(grid)
|
|
102
52
|
self.__il_relax = [False] * len(self._islands)
|
|
103
53
|
|
|
104
|
-
def __detect_islands(self, cutofl: bool):
|
|
105
|
-
'''Detect islands'''
|
|
106
|
-
q: deque[BusID] = deque()
|
|
107
|
-
visited: set[BusID] = set()
|
|
108
|
-
islands: list[set[BusID]] = []
|
|
109
|
-
for bus in self.grid.BusNames:
|
|
110
|
-
if bus in visited: continue
|
|
111
|
-
q.append(bus)
|
|
112
|
-
island: set[BusID] = set()
|
|
113
|
-
while len(q) > 0:
|
|
114
|
-
b = q.popleft()
|
|
115
|
-
if b in visited: continue
|
|
116
|
-
visited.add(b)
|
|
117
|
-
island.add(b)
|
|
118
|
-
for line in self.grid._ladjfb[b]:
|
|
119
|
-
if cutofl and line.ID in self._oflines: continue
|
|
120
|
-
if line._tBus in visited: continue
|
|
121
|
-
q.append(line._tBus)
|
|
122
|
-
for line in self.grid._ladjtb[b]:
|
|
123
|
-
if cutofl and line.ID in self._oflines: continue
|
|
124
|
-
if line._fBus in visited: continue
|
|
125
|
-
q.append(line._fBus)
|
|
126
|
-
islands.append(island)
|
|
127
|
-
return islands
|
|
128
|
-
|
|
129
|
-
@property
|
|
130
|
-
def Islands(self):
|
|
131
|
-
return self._islands
|
|
132
|
-
|
|
133
|
-
@property
|
|
134
|
-
def IslandResults(self):
|
|
135
|
-
return self._island_res
|
|
136
|
-
|
|
137
54
|
@property
|
|
138
55
|
def MLRP(self):
|
|
139
56
|
'''Get the maximum load reduction proportion'''
|
|
@@ -169,15 +86,19 @@ class DistFlowSolver(SolverBase):
|
|
|
169
86
|
val = 0
|
|
170
87
|
for i, il in enumerate(self._islands):
|
|
171
88
|
relax = self.__il_relax[i]
|
|
172
|
-
|
|
173
|
-
|
|
89
|
+
r, v = self.solve_island(i, il, _t, timeout_s, relax, relax)
|
|
90
|
+
self._islands[i].result = r
|
|
91
|
+
self._islands[i].result_value = v
|
|
92
|
+
if r == IslandResult.Failed:
|
|
174
93
|
self.__il_relax[i] = True
|
|
175
|
-
|
|
176
|
-
|
|
94
|
+
r, v = self.solve_island(i, il, _t, timeout_s, True, True)
|
|
95
|
+
self._islands[i].result = r
|
|
96
|
+
self._islands[i].result_value = v
|
|
97
|
+
if r != IslandResult.Failed:
|
|
177
98
|
allFail = False
|
|
178
|
-
val +=
|
|
179
|
-
if
|
|
180
|
-
if
|
|
99
|
+
val += v
|
|
100
|
+
if r != IslandResult.OK: allOK = False
|
|
101
|
+
if r == IslandResult.OverFlow: self.__il_relax[i] = False
|
|
181
102
|
if allOK:
|
|
182
103
|
return GridSolveResult.OK, val
|
|
183
104
|
elif allFail:
|
|
@@ -199,7 +120,7 @@ class DistFlowSolver(SolverBase):
|
|
|
199
120
|
assert ret != float('-inf'), "Value is -inf"
|
|
200
121
|
return ret
|
|
201
122
|
|
|
202
|
-
def
|
|
123
|
+
def solve_island(self, il_idx:int, island: Island, _t: int, timeout_s: float, relaxV: bool, relaxI: bool, clear_results: bool = False) -> 'tuple[IslandResult, float]':
|
|
203
124
|
|
|
204
125
|
''' ---------Variables----------
|
|
205
126
|
pg0[k]: Generator active power
|
|
@@ -217,7 +138,7 @@ class DistFlowSolver(SolverBase):
|
|
|
217
138
|
from cvxpy import Variable as Var
|
|
218
139
|
except ImportError:
|
|
219
140
|
raise ImportError("cvxpy or ecos is not installed. Please install them using 'pip install cvxpy ecos'")
|
|
220
|
-
cons:list[cp.Constraint] = []
|
|
141
|
+
cons: 'list[cp.Constraint]' = []
|
|
221
142
|
# Create GEN vars
|
|
222
143
|
pg0: 'dict[str, VF]' = {}
|
|
223
144
|
qg0: 'dict[str, VF]' = {}
|
|
@@ -330,7 +251,10 @@ class DistFlowSolver(SolverBase):
|
|
|
330
251
|
cons.append(
|
|
331
252
|
inflow + cp.sum(pg[j]) == outflow + pd[j] - dec # type: ignore
|
|
332
253
|
)
|
|
333
|
-
|
|
254
|
+
|
|
255
|
+
# flow_in and flow_out are Python generators, which cannot be reused, thus needed to be re-assigned
|
|
256
|
+
flow_in = island.grid.LinesOfTBus(j)
|
|
257
|
+
flow_out = island.grid.LinesOfFBus(j)
|
|
334
258
|
# Q constraint
|
|
335
259
|
q_inflow = cp.sum([Q[ln.ID] - ln.X * l[ln.ID] for ln in flow_in if ln.ID in island.Lines])
|
|
336
260
|
q_outflow = cp.sum([Q[ln.ID] for ln in flow_out if ln.ID in island.Lines])
|
|
@@ -367,7 +291,7 @@ class DistFlowSolver(SolverBase):
|
|
|
367
291
|
for gID, g in island.GenItems()])
|
|
368
292
|
|
|
369
293
|
problem = cp.Problem(cp.Minimize(decs + goal + crpe), cons)
|
|
370
|
-
|
|
294
|
+
|
|
371
295
|
try:
|
|
372
296
|
problem.solve(solver=cp.ECOS, verbose=False, max_iters=self.max_iter)
|
|
373
297
|
except cp.SolverError as e:
|