fpowerkit 0.3.5.post1__tar.gz → 0.3.6__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.5.post1 → fpowerkit-0.3.6}/PKG-INFO +1 -1
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/lpwrapper.py +80 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solbase.py +2 -1
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solcmb.py +22 -7
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/soldist.py +0 -6
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solldf2.py +6 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solnt.py +110 -6
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/pyproject.toml +1 -1
- fpowerkit-0.3.6/test_cmb.py +31 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/.gitignore +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/LICENSE +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/README.md +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/desc.md +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/__init__.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/bus.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/cases.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/ess.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/gen.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/grid.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/island.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/line.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/pvwind.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solcvx.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/soldss.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/solldf.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/fpowerkit/utils.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/main.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/test.py +0 -0
- {fpowerkit-0.3.5.post1 → fpowerkit-0.3.6}/test314t.py +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from copy import deepcopy
|
|
2
2
|
from dataclasses import dataclass
|
|
3
|
+
from itertools import chain
|
|
3
4
|
from typing import Dict, Iterable, List, Optional, Union
|
|
4
5
|
|
|
5
6
|
|
|
@@ -57,17 +58,51 @@ class _LPVar:
|
|
|
57
58
|
expr.add_term(self.idx, 1.0 / scalar)
|
|
58
59
|
return expr
|
|
59
60
|
|
|
61
|
+
def __le__(self, other):
|
|
62
|
+
expr = _LPExpr()
|
|
63
|
+
expr.add_term(self.idx, 1.0)
|
|
64
|
+
if isinstance(other, _LPExpr):
|
|
65
|
+
return expr <= other
|
|
66
|
+
elif isinstance(other, _LPVar):
|
|
67
|
+
expr.add_term(other.idx, -1.0)
|
|
68
|
+
return expr <= 0
|
|
69
|
+
elif isinstance(other, (int, float)):
|
|
70
|
+
expr.add_const(-other)
|
|
71
|
+
return expr <= 0
|
|
72
|
+
else:
|
|
73
|
+
raise TypeError("Unsupported type for comparison")
|
|
74
|
+
|
|
75
|
+
def __ge__(self, other):
|
|
76
|
+
expr = _LPExpr()
|
|
77
|
+
expr.add_term(self.idx, 1.0)
|
|
78
|
+
if isinstance(other, _LPExpr):
|
|
79
|
+
return expr >= other
|
|
80
|
+
elif isinstance(other, _LPVar):
|
|
81
|
+
expr.add_term(other.idx, -1.0)
|
|
82
|
+
return expr >= 0
|
|
83
|
+
elif isinstance(other, (int, float)):
|
|
84
|
+
expr.add_const(-other)
|
|
85
|
+
return expr >= 0
|
|
86
|
+
else:
|
|
87
|
+
raise TypeError("Unsupported type for comparison")
|
|
88
|
+
|
|
60
89
|
class _LPEqCons: # expr == 0
|
|
61
90
|
def __init__(self, expr:'_LPExpr'):
|
|
62
91
|
self.expr = expr
|
|
63
92
|
self.rhs = -self.expr.const
|
|
64
93
|
self.expr.const = 0
|
|
94
|
+
|
|
95
|
+
def __str__(self):
|
|
96
|
+
return f"{str(self.expr)} == {self.rhs}"
|
|
65
97
|
|
|
66
98
|
class _LPIneqCons: # expr <= 0
|
|
67
99
|
def __init__(self, expr:'_LPExpr'):
|
|
68
100
|
self.expr = expr
|
|
69
101
|
self.rhs = -self.expr.const
|
|
70
102
|
self.expr.const = 0
|
|
103
|
+
|
|
104
|
+
def __str__ (self):
|
|
105
|
+
return f"{str(self.expr)} <= {self.rhs}"
|
|
71
106
|
|
|
72
107
|
class _LPExpr:
|
|
73
108
|
def __init__(self):
|
|
@@ -162,6 +197,27 @@ class _LPExpr:
|
|
|
162
197
|
|
|
163
198
|
def __ge__(self, other):
|
|
164
199
|
return _LPIneqCons(other - self)
|
|
200
|
+
|
|
201
|
+
def __str__(self):
|
|
202
|
+
terms_str = [f"{coeff}*x{var_idx}" for var_idx, coeff in self.terms.items()]
|
|
203
|
+
if self.const != 0:
|
|
204
|
+
terms_str.append(f"{self.const}")
|
|
205
|
+
return " + ".join(terms_str) if terms_str else "0"
|
|
206
|
+
|
|
207
|
+
def to_string(self, prob:'LinProgProblem') -> str:
|
|
208
|
+
terms_str = [f"{coeff}*{prob.get_var_name(var_idx)}" for var_idx, coeff in self.terms.items()]
|
|
209
|
+
if self.const != 0:
|
|
210
|
+
terms_str.append(f"{self.const}")
|
|
211
|
+
return " + ".join(terms_str) if terms_str else "0"
|
|
212
|
+
|
|
213
|
+
def value(self, prob:'LinProgProblem') -> float:
|
|
214
|
+
val = self.const
|
|
215
|
+
for var_idx, coeff in self.terms.items():
|
|
216
|
+
var = prob.variables[var_idx]
|
|
217
|
+
if var.x is None:
|
|
218
|
+
raise ValueError(f"Variable {var.name} has no assigned value")
|
|
219
|
+
val += coeff * var.x
|
|
220
|
+
return val
|
|
165
221
|
|
|
166
222
|
def quicksum(items:Iterable[Union[_LPVar, _LPExpr]]) -> _LPExpr:
|
|
167
223
|
result = _LPExpr()
|
|
@@ -185,6 +241,30 @@ class LinProgProblem:
|
|
|
185
241
|
self.__ineqs:List[_LPIneqCons] = []
|
|
186
242
|
self.objective:_LPExpr = _LPExpr()
|
|
187
243
|
|
|
244
|
+
@property
|
|
245
|
+
def variables(self) -> List[_LPVar]:
|
|
246
|
+
return self.__vars
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
def equations(self) -> List[_LPEqCons]:
|
|
250
|
+
return self.__eqs
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def inequalities(self) -> List[_LPIneqCons]:
|
|
254
|
+
return self.__ineqs
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def constraints(self) -> chain[Union[_LPEqCons, _LPIneqCons]]:
|
|
258
|
+
return chain(self.__eqs, self.__ineqs)
|
|
259
|
+
|
|
260
|
+
def get_var_name(self, idx:int) -> str:
|
|
261
|
+
"""
|
|
262
|
+
Get the name of a variable by its index.
|
|
263
|
+
idx: Index of the variable
|
|
264
|
+
Returns: Name of the variable
|
|
265
|
+
"""
|
|
266
|
+
return self.__vars[idx].name
|
|
267
|
+
|
|
188
268
|
def add_var(self, name:str, lb:Optional[float] = 0, ub: Optional[float] = None):
|
|
189
269
|
"""
|
|
190
270
|
Add a variable to the linear programming problem.
|
|
@@ -21,6 +21,7 @@ class SolverBase(ABC):
|
|
|
21
21
|
self.eps = eps
|
|
22
22
|
self.max_iter = max_iter
|
|
23
23
|
self.saveto = default_saveto
|
|
24
|
+
Path(self.saveto).mkdir(parents=True, exist_ok=True)
|
|
24
25
|
|
|
25
26
|
@property
|
|
26
27
|
def grid(self) -> Grid:
|
|
@@ -43,7 +44,7 @@ class SolverBase(ABC):
|
|
|
43
44
|
|
|
44
45
|
def SetErrorSaveTo(self, path:str = DEFAULT_SAVETO):
|
|
45
46
|
self.saveto = path
|
|
46
|
-
Path(path).mkdir(parents=True, exist_ok
|
|
47
|
+
Path(path).mkdir(parents=True, exist_ok=True)
|
|
47
48
|
|
|
48
49
|
def _calc_line_params(self):
|
|
49
50
|
for l in self.grid.Lines:
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
|
-
from typing import Union, Iterable
|
|
2
|
+
from typing import Tuple, Union, Iterable
|
|
3
3
|
from warnings import warn
|
|
4
4
|
from .grid import Grid
|
|
5
5
|
from .solbase import *
|
|
6
6
|
|
|
7
7
|
class Estimator(Enum):
|
|
8
8
|
DistFlow = 'DistFlow'
|
|
9
|
+
LinDistFlow = 'LinDistFlow'
|
|
10
|
+
LinDistFlow2 = 'LinDistFlow2'
|
|
9
11
|
|
|
10
12
|
class Calculator(Enum):
|
|
11
13
|
OpenDSS = 'OpenDSS'
|
|
@@ -23,10 +25,19 @@ class CombinedSolver(SolverBase):
|
|
|
23
25
|
calculator:Calculator = Calculator.Newton, source_bus:'Union[str,Iterable[str]]' = ""):
|
|
24
26
|
super().__init__(grid, eps, max_iter, default_saveto = default_saveto)
|
|
25
27
|
|
|
26
|
-
from .soldist import DistFlowSolver
|
|
27
28
|
if isinstance(estimator, Estimator):
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
if estimator == Estimator.DistFlow:
|
|
30
|
+
from .soldist import DistFlowSolver
|
|
31
|
+
estimator = DistFlowSolver(grid, mlrp = mlrp, default_saveto=default_saveto)
|
|
32
|
+
elif estimator == Estimator.LinDistFlow:
|
|
33
|
+
from .solldf import LinDistFlowSolver
|
|
34
|
+
estimator = LinDistFlowSolver(grid, default_saveto=default_saveto)
|
|
35
|
+
elif estimator == Estimator.LinDistFlow2:
|
|
36
|
+
from .solldf2 import LinDistFlow2Solver
|
|
37
|
+
estimator = LinDistFlow2Solver(grid, default_saveto=default_saveto)
|
|
38
|
+
else:
|
|
39
|
+
raise ValueError(f"Invalid estimator type '{estimator}'. Candidates are: {list(Estimator)}")
|
|
40
|
+
|
|
30
41
|
self.est = estimator
|
|
31
42
|
self.cal_str = calculator
|
|
32
43
|
if calculator in CALCULATORS_NEED_SOURCE_BUS:
|
|
@@ -36,7 +47,7 @@ class CombinedSolver(SolverBase):
|
|
|
36
47
|
if source_bus != "":
|
|
37
48
|
warn(Warning("source_bus is ignored when not using OpenDSSSolver."))
|
|
38
49
|
|
|
39
|
-
def solve(self, _t:int, /, *, timeout_s: float = 1):
|
|
50
|
+
def solve(self, _t:int, /, *, timeout_s: float = 1) -> Tuple[GridSolveResult, float]:
|
|
40
51
|
res, obj = self.est.solve(_t, timeout_s=timeout_s)
|
|
41
52
|
if res == GridSolveResult.Failed:
|
|
42
53
|
return res, obj
|
|
@@ -44,10 +55,10 @@ class CombinedSolver(SolverBase):
|
|
|
44
55
|
return res, obj
|
|
45
56
|
elif self.cal_str == Calculator.OpenDSS:
|
|
46
57
|
from .soldss import OpenDSSSolver
|
|
47
|
-
solver = OpenDSSSolver(self.est.grid, source_bus = self.source_bus)
|
|
58
|
+
solver = OpenDSSSolver(self.est.grid, source_bus = self.source_bus, default_saveto=self.saveto)
|
|
48
59
|
elif self.cal_str == Calculator.Newton:
|
|
49
60
|
from .solnt import NewtonSolver
|
|
50
|
-
solver = NewtonSolver(self.est.grid)
|
|
61
|
+
solver = NewtonSolver(self.est.grid, default_saveto=self.saveto, is_cmb_calculator=True)
|
|
51
62
|
else:
|
|
52
63
|
raise ValueError(f"Invalid solver type '{self.cal_str}'.")
|
|
53
64
|
res, obj = solver.solve(_t, timeout_s = timeout_s)
|
|
@@ -56,5 +67,9 @@ class CombinedSolver(SolverBase):
|
|
|
56
67
|
def solve_island(self, i:int, island, _t:int):
|
|
57
68
|
'''Solve the island'''
|
|
58
69
|
raise NotImplementedError
|
|
70
|
+
|
|
71
|
+
def SetErrorSaveTo(self, path:str = DEFAULT_SAVETO):
|
|
72
|
+
self.est.SetErrorSaveTo(path)
|
|
73
|
+
self.saveto = path
|
|
59
74
|
|
|
60
75
|
__all__ = ["CombinedSolver", "Estimator", "Calculator", "CALCULATORS_NEED_SOURCE_BUS"]
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import math
|
|
2
2
|
import warnings
|
|
3
3
|
from itertools import chain
|
|
4
|
-
from pathlib import Path
|
|
5
4
|
from typing import Optional, Union
|
|
6
5
|
from dataclasses import dataclass
|
|
7
6
|
from feasytools import TimeFunc
|
|
@@ -105,11 +104,6 @@ class DistFlowSolver(SolverBase):
|
|
|
105
104
|
if allOK:
|
|
106
105
|
return GridSolveResult.OK, val
|
|
107
106
|
elif allFail:
|
|
108
|
-
print(f"Failed to solve at time {_t}")
|
|
109
|
-
if self.saveto != "":
|
|
110
|
-
p = Path(self.saveto)
|
|
111
|
-
p.mkdir(parents=True, exist_ok=True)
|
|
112
|
-
self.grid.savePQofBus(str(p/f"{_t}_load.csv"), _t)
|
|
113
107
|
return GridSolveResult.Failed, val
|
|
114
108
|
else:
|
|
115
109
|
return GridSolveResult.PartialOK, val
|
|
@@ -25,6 +25,7 @@ class LinDistFlow2Solver(SolverBase):
|
|
|
25
25
|
qg0: 'dict[str, _LPVar]' = {}
|
|
26
26
|
pg: 'dict[str, list]' = {b: [] for b in island.Buses}
|
|
27
27
|
qg: 'dict[str, list]' = {b: [] for b in island.Buses}
|
|
28
|
+
qg_abs: 'dict[str, list]' = {b: [] for b in island.Buses}
|
|
28
29
|
pd: 'dict[str, float]' = {bID: b.Pd(_t) for bID, b in island.BusItems()}
|
|
29
30
|
qd: 'dict[str, float]' = {bID: b.Qd(_t) for bID, b in island.BusItems()}
|
|
30
31
|
|
|
@@ -41,7 +42,12 @@ class LinDistFlow2Solver(SolverBase):
|
|
|
41
42
|
qd[g.BusID] -= _parse(g.Q, _t)
|
|
42
43
|
else:
|
|
43
44
|
q = prob.add_var(f"Qg_{gID}", _parse(g.Qmin, _t), _parse(g.Qmax, _t))
|
|
45
|
+
q_abs = prob.add_var(f"Qg_abs_{gID}", 0, None)
|
|
44
46
|
qg[g.BusID].append(q)
|
|
47
|
+
qg_abs[g.BusID].append(q_abs)
|
|
48
|
+
prob.add_cons(q_abs >= q)
|
|
49
|
+
prob.add_cons(q_abs >= -q)
|
|
50
|
+
obj = q_abs * _parse(g.CostB, _t) * 0.1 + obj # Assuming reactive power cost is 10% of active power cost
|
|
45
51
|
qg0[gID] = q
|
|
46
52
|
# Assuming no cost for reactive power generation
|
|
47
53
|
|
|
@@ -23,6 +23,7 @@ class _NRProblem:
|
|
|
23
23
|
B: Any # np.ndarray 2D
|
|
24
24
|
V: 'list[float]'
|
|
25
25
|
theta: 'list[float]'
|
|
26
|
+
slack: int
|
|
26
27
|
|
|
27
28
|
def _presolve(il:Island, _t:int):
|
|
28
29
|
'''Check the bus type'''
|
|
@@ -34,6 +35,7 @@ def _presolve(il:Island, _t:int):
|
|
|
34
35
|
theta: 'list[float]' = []
|
|
35
36
|
Ps: 'list[float]' = []
|
|
36
37
|
Qs: 'list[float]' = []
|
|
38
|
+
slack:int = -1
|
|
37
39
|
bus_dict, Y = il.YMat()
|
|
38
40
|
for bid, i in bus_dict.items():
|
|
39
41
|
bus = il.grid.Bus(bid)
|
|
@@ -63,6 +65,7 @@ def _presolve(il:Island, _t:int):
|
|
|
63
65
|
raise ValueError(f"Bus {bus.ID}: Invalid bus type VQ")
|
|
64
66
|
else:
|
|
65
67
|
busType[bus.ID] = BusType.Slack
|
|
68
|
+
slack = i
|
|
66
69
|
slack_cnt += 1
|
|
67
70
|
if slack_cnt > 1:
|
|
68
71
|
raise ValueError('Only one slack bus is allowed')
|
|
@@ -82,7 +85,59 @@ def _presolve(il:Island, _t:int):
|
|
|
82
85
|
Qs.append(q)
|
|
83
86
|
if slack_cnt == 0:
|
|
84
87
|
raise ValueError('No slack bus is found')
|
|
85
|
-
return _NRProblem(eq_P + eq_Q, len(eq_P), Ps, Qs, bus_dict, Y.real, Y.imag, V, theta)
|
|
88
|
+
return _NRProblem(eq_P + eq_Q, len(eq_P), Ps, Qs, bus_dict, Y.real, Y.imag, V, theta, slack)
|
|
89
|
+
|
|
90
|
+
def _presolve2(il:Island, _t:int):
|
|
91
|
+
'''Check the bus type after using distflow-like algorithm'''
|
|
92
|
+
busType: 'dict[BusID, BusType]' = {}
|
|
93
|
+
slack_cnt = 0
|
|
94
|
+
eq_P: 'list[int]' = []
|
|
95
|
+
eq_Q: 'list[int]' = []
|
|
96
|
+
V: 'list[float]' = []
|
|
97
|
+
theta: 'list[float]' = []
|
|
98
|
+
Ps: 'list[float]' = []
|
|
99
|
+
Qs: 'list[float]' = []
|
|
100
|
+
bus_dict, Y = il.YMat()
|
|
101
|
+
slack = -1
|
|
102
|
+
|
|
103
|
+
for bid, i in bus_dict.items():
|
|
104
|
+
bus = il.grid.Bus(bid)
|
|
105
|
+
V.append(bus.V if bus.V is not None else 1.0)
|
|
106
|
+
theta.append(bus.theta if bus.theta is not None else 0.0)
|
|
107
|
+
|
|
108
|
+
p = -bus.Pd(_t)
|
|
109
|
+
q = -bus.Qd(_t)
|
|
110
|
+
for g in il.grid.GensAtBus(bus.ID):
|
|
111
|
+
assert g.P is not None
|
|
112
|
+
if isinstance(g.P, TimeFunc): p += g.P(_t)
|
|
113
|
+
else: p += g.P
|
|
114
|
+
|
|
115
|
+
assert g.Q is not None
|
|
116
|
+
if isinstance(g.Q, TimeFunc): q += g.Q(_t)
|
|
117
|
+
else: q += g.Q
|
|
118
|
+
|
|
119
|
+
if bus.FixedV:
|
|
120
|
+
# After Distflow presolve, all the output of generator are fixed, except for the source bus whose V is fixed.
|
|
121
|
+
assert len(il.grid.GensAtBus(bus.ID)) == 1, "Only one generator is allowed at the source bus"
|
|
122
|
+
busType[bus.ID] = BusType.Slack
|
|
123
|
+
slack_cnt += 1
|
|
124
|
+
slack = i
|
|
125
|
+
if slack_cnt > 1:
|
|
126
|
+
raise ValueError('Only one slack bus is allowed')
|
|
127
|
+
else:
|
|
128
|
+
# All other nodes are treated as PQ
|
|
129
|
+
|
|
130
|
+
busType[bus.ID] = BusType.PQ
|
|
131
|
+
bus.V = 1.0
|
|
132
|
+
eq_P.append(i)
|
|
133
|
+
eq_Q.append(i)
|
|
134
|
+
|
|
135
|
+
Ps.append(p)
|
|
136
|
+
Qs.append(q)
|
|
137
|
+
|
|
138
|
+
if slack_cnt == 0:
|
|
139
|
+
raise ValueError('No slack bus is found')
|
|
140
|
+
return _NRProblem(eq_P + eq_Q, len(eq_P), Ps, Qs, bus_dict, Y.real, Y.imag, V, theta, slack)
|
|
86
141
|
|
|
87
142
|
def _solve(prb: _NRProblem, max_iter:int = 100, eps:float = 1e-6):
|
|
88
143
|
'''Solve the power flow problem'''
|
|
@@ -154,23 +209,72 @@ def _solve(prb: _NRProblem, max_iter:int = 100, eps:float = 1e-6):
|
|
|
154
209
|
V[b] += V[b] * x0
|
|
155
210
|
if cnt >= max_iter:
|
|
156
211
|
raise ValueError("Bad solution")
|
|
157
|
-
|
|
212
|
+
P_inject = [P(i) for i in range(m)]
|
|
213
|
+
Q_inject = [Q(i) for i in range(m)]
|
|
214
|
+
return cnt, V, T, P_inject, Q_inject, prb.bus_dict
|
|
158
215
|
|
|
159
216
|
class NewtonSolver(SolverBase):
|
|
160
|
-
def __init__(self, grid:Grid, eps:float = 1e-6, default_saveto:str = DEFAULT_SAVETO, max_iter:int = 100):
|
|
217
|
+
def __init__(self, grid:Grid, eps:float = 1e-6, default_saveto:str = DEFAULT_SAVETO, max_iter:int = 100, is_cmb_calculator:bool = False):
|
|
161
218
|
super().__init__(grid, eps, max_iter, default_saveto = default_saveto)
|
|
219
|
+
self.__is_cmb_calc = is_cmb_calculator
|
|
162
220
|
|
|
163
221
|
def solve_island(self, i:int, island:Island, _t:int, **kwargs) -> 'tuple[IslandResult, float]':
|
|
164
222
|
try:
|
|
165
|
-
|
|
166
|
-
|
|
223
|
+
if self.__is_cmb_calc:
|
|
224
|
+
prb = _presolve2(island, _t)
|
|
225
|
+
else:
|
|
226
|
+
prb = _presolve(island, _t)
|
|
227
|
+
cnt, V, theta, P_inject, Q_inject, bus_dict = _solve(prb, self.max_iter, self.eps)
|
|
167
228
|
except ValueError as e:
|
|
168
229
|
return IslandResult.Failed, 0.0
|
|
169
230
|
|
|
170
|
-
for b in bus_dict.
|
|
231
|
+
for b, i in bus_dict.items():
|
|
171
232
|
bus = island.grid.Bus(b)
|
|
172
233
|
bus.V = V[bus_dict[b]]
|
|
173
234
|
bus.theta = theta[bus_dict[b]]
|
|
235
|
+
gens = self.grid.GensAtBus(b)
|
|
236
|
+
if len(gens) == 0:
|
|
237
|
+
continue
|
|
238
|
+
pg = P_inject[i] + bus.Pd(_t)
|
|
239
|
+
qg = Q_inject[i] + bus.Qd(_t)
|
|
240
|
+
|
|
241
|
+
# Allocate the generation to minimize generation cost
|
|
242
|
+
from .lpwrapper import LinProgProblem, quicksum
|
|
243
|
+
prob = LinProgProblem()
|
|
244
|
+
p_dict = {}; q_dict = {}; obj = 0
|
|
245
|
+
for g in gens:
|
|
246
|
+
if g.FixedP:
|
|
247
|
+
if isinstance(g.P, TimeFunc):
|
|
248
|
+
pg -= g.P(_t)
|
|
249
|
+
else:
|
|
250
|
+
assert g.P is not None
|
|
251
|
+
pg -= g.P
|
|
252
|
+
else:
|
|
253
|
+
assert g.Pmin is not None and g.Pmax is not None
|
|
254
|
+
p_dict[g.ID] = prob.add_var(f'pg_{g.ID}', lb=g.Pmin(_t), ub=g.Pmax(_t))
|
|
255
|
+
obj += g.CostB(_t) * p_dict[g.ID] + g.CostC(_t)
|
|
256
|
+
if g.FixedQ:
|
|
257
|
+
if isinstance(g.Q, TimeFunc):
|
|
258
|
+
qg -= g.Q(_t)
|
|
259
|
+
else:
|
|
260
|
+
assert g.Q is not None
|
|
261
|
+
qg -= g.Q
|
|
262
|
+
else:
|
|
263
|
+
assert g.Qmin is not None and g.Qmax is not None
|
|
264
|
+
q_dict[g.ID] = prob.add_var(f'qg_{g.ID}', lb=g.Qmin(_t), ub=g.Qmax(_t))
|
|
265
|
+
q_abs = prob.add_var(f'qg_abs_{g.ID}', lb=0.0)
|
|
266
|
+
prob.add_cons(q_abs >= q_dict[g.ID])
|
|
267
|
+
prob.add_cons(q_abs >= -q_dict[g.ID])
|
|
268
|
+
obj += g.CostB(_t) * q_abs * 0.1
|
|
269
|
+
prob.add_cons(quicksum(p_dict.values()) == pg)
|
|
270
|
+
prob.add_cons(quicksum(q_dict.values()) == qg)
|
|
271
|
+
prob.set_objective(obj)
|
|
272
|
+
prob.solve()
|
|
273
|
+
for gid, var in p_dict.items():
|
|
274
|
+
island.grid.Gen(gid)._p = var.x
|
|
275
|
+
for gid, var in q_dict.items():
|
|
276
|
+
island.grid.Gen(gid)._q = var.x
|
|
277
|
+
|
|
174
278
|
return IslandResult.OK, 0.0
|
|
175
279
|
|
|
176
280
|
__all__ = ['NewtonSolver', 'BusType']
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from fpowerkit import Grid, CombinedSolver, Estimator, Calculator, LinDistFlow2Solver, NewtonSolver
|
|
2
|
+
|
|
3
|
+
def test_cmb():
|
|
4
|
+
grid = Grid.fromFile("cases/3nodes.xml")
|
|
5
|
+
solver = CombinedSolver(grid=grid, estimator=Estimator.LinDistFlow2, calculator=Calculator.Newton)
|
|
6
|
+
res, obj = solver.solve(0)
|
|
7
|
+
print(res, obj)
|
|
8
|
+
|
|
9
|
+
def test_spl():
|
|
10
|
+
grid = Grid.fromFile("cases/3nodes.xml")
|
|
11
|
+
solver0 = LinDistFlow2Solver(grid=grid)
|
|
12
|
+
res, obj = solver0.solve(0)
|
|
13
|
+
print(res, obj)
|
|
14
|
+
for b in grid.Buses:
|
|
15
|
+
print(b.ID, b.V, b.theta, b.Pd, b.Qd)
|
|
16
|
+
for l in grid.Lines:
|
|
17
|
+
print(l.ID, l.P, l.Q, l.I)
|
|
18
|
+
for g in grid.Gens:
|
|
19
|
+
print(g.ID, g.P, g.Q)
|
|
20
|
+
solver1 = NewtonSolver(grid=grid, is_cmb_calculator=True)
|
|
21
|
+
res, obj = solver1.solve(0)
|
|
22
|
+
print(res, obj)
|
|
23
|
+
for b in grid.Buses:
|
|
24
|
+
print(b.ID, b.V, b.theta, b.Pd, b.Qd)
|
|
25
|
+
for l in grid.Lines:
|
|
26
|
+
print(l.ID, l.P, l.Q, l.I)
|
|
27
|
+
for g in grid.Gens:
|
|
28
|
+
print(g.ID, g.P, g.Q)
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
test_spl()
|
|
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
|
|
File without changes
|