fpowerkit 0.1.4__tar.gz → 0.1.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.
Files changed (23) hide show
  1. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/PKG-INFO +9 -2
  2. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/README.md +2 -0
  3. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/grid.py +2 -2
  4. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/solbase.py +2 -0
  5. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/soldist.py +40 -19
  6. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit.egg-info/PKG-INFO +9 -2
  7. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/setup.py +1 -1
  8. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/LICENSE +0 -0
  9. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/__init__.py +0 -0
  10. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/bus.py +0 -0
  11. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/cases.py +0 -0
  12. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/ess.py +0 -0
  13. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/gen.py +0 -0
  14. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/line.py +0 -0
  15. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/pvwind.py +0 -0
  16. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/soldss.py +0 -0
  17. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/solnt.py +0 -0
  18. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit/utils.py +0 -0
  19. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit.egg-info/SOURCES.txt +0 -0
  20. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit.egg-info/dependency_links.txt +0 -0
  21. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit.egg-info/requires.txt +0 -0
  22. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/fpowerkit.egg-info/top_level.txt +0 -0
  23. {fpowerkit-0.1.4 → fpowerkit-0.1.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: fpowerkit
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A Distflow PDN solver based on Gurobi.
5
5
  Author: fmy_xfk
6
6
  Classifier: Programming Language :: Python :: 3
@@ -12,6 +12,13 @@ License-File: LICENSE
12
12
  Requires-Dist: feasytools>=0.0.21
13
13
  Requires-Dist: gurobipy>=9.1.0
14
14
  Requires-Dist: numpy>=1.20.0
15
+ Dynamic: author
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: requires-dist
20
+ Dynamic: requires-python
21
+ Dynamic: summary
15
22
 
16
23
  # FPowerKit - A power distribution network calculation component
17
24
 
@@ -1,6 +1,8 @@
1
1
  # FPowerKit 配电网运算组件
2
2
  **本仓库已上线PyPI**,您可以通过`pip install fpowerkit`直接以Python包的形式安装使用本仓库的代码!
3
3
 
4
+ 这个项目是[V2Sim](https://gitee.com/fmy_xfk/v2sim)的附属项目。V2Sim的论文请见arXiv: https://arxiv.org/abs/2412.09808
5
+
4
6
  IEEE33和IEEE69节点的配网数据从公开渠道收集。除配网数据以外,本仓库代码均遵循LGPL3.0协议使用。
5
7
 
6
8
  ## 简介
@@ -234,9 +234,9 @@ class Grid:
234
234
  l = self._lines.get(line_id)
235
235
  if not l: raise ValueError(f"Invalid line name: {line_id}")
236
236
  if l.tBus == new_bus_id: return
237
- self._ladjfb[l.tBus].remove(l)
237
+ self._ladjtb[l.tBus].remove(l)
238
238
  l._tBus = new_bus_id
239
- self._ladjfb[l.tBus].append(l)
239
+ self._ladjtb[l.tBus].append(l)
240
240
 
241
241
  def ChangeLineID(self, old_id: str, new_id: str):
242
242
  if old_id == new_id: return
@@ -1,5 +1,6 @@
1
1
  from abc import ABC, abstractmethod
2
2
  from enum import IntEnum
3
+ from pathlib import Path
3
4
  from .grid import *
4
5
 
5
6
  DEFAULT_SAVETO = "./fpowerkit_logs/"
@@ -21,6 +22,7 @@ class SolverBase(ABC):
21
22
 
22
23
  def SetErrorSaveTo(self, path:str = DEFAULT_SAVETO):
23
24
  self.saveto = path
25
+ Path(path).mkdir(parents=True, exist_ok = True)
24
26
 
25
27
  @abstractmethod
26
28
  def solve(self, grid:Grid, _t:int, /, **kwargs):
@@ -67,12 +67,17 @@ class IslandResult(IntEnum):
67
67
 
68
68
  class DistFlowSolver(SolverBase):
69
69
  '''DistFlow solver'''
70
- def __init__(self, grid:Grid, eps:float = 1e-6, default_saveto:str = DEFAULT_SAVETO, /, *, mlrp:float = 0.5):
70
+ def __init__(self, grid:Grid, eps:float = 1e-6, default_saveto:str = DEFAULT_SAVETO, /,
71
+ *, mlrp:float = 0.5, secondary_cost:bool = True):
71
72
  '''
72
73
  Initialize
73
74
  grid: Grid object
74
75
  default_saveto: Default path to save the results
75
76
  mlrp: Maximum proportion of load reduction
77
+ secondary_cost: Whether to include the secondary cost.
78
+ If False, the cost is calculated as CostB * Pg + CostC;
79
+ If True, the cost is calculated as CostA * Pg^2 + CostB * Pg + CostC,
80
+ which is likely to induce numerical instability.
76
81
  '''
77
82
  super().__init__(grid, eps, default_saveto)
78
83
  self._decb:dict[BusID, LoadReduceModule] = {}
@@ -80,6 +85,7 @@ class DistFlowSolver(SolverBase):
80
85
  self._mlrp = mlrp
81
86
  self._oflines:set[LineID] = set()
82
87
  self._ofbuses:set[BusID] = set()
88
+ self._sec_cost = secondary_cost
83
89
  self.UpdateGrid()
84
90
 
85
91
  @property
@@ -95,6 +101,7 @@ class DistFlowSolver(SolverBase):
95
101
  _Island(self.grid, il, self._oflines) for il in self.__detect_islands(cut_overflow_lines)
96
102
  )
97
103
  self._island_res = [(IslandResult.Undefined, -1.0) for _ in range(len(self._islands))]
104
+ self.__il_relax = [False] * len(self._islands)
98
105
 
99
106
  def __detect_islands(self, cutofl:bool):
100
107
  '''Detect islands'''
@@ -163,11 +170,16 @@ class DistFlowSolver(SolverBase):
163
170
  allFail = True
164
171
  val = 0
165
172
  for i, il in enumerate(self._islands):
166
- self._island_res[i] = self.__solve(_t, il, timeout_s)
173
+ relax = self.__il_relax[i]
174
+ self._island_res[i] = self.__solve(_t, il, timeout_s, relax, relax)
175
+ if self._island_res[i][0] == IslandResult.Failed:
176
+ self.__il_relax[i] = True
177
+ self._island_res[i] = self.__solve(_t, il, timeout_s, True, True)
167
178
  if self._island_res[i][0] != IslandResult.Failed:
168
179
  allFail = False
169
180
  val += self._island_res[i][1]
170
181
  if self._island_res[i][0] != IslandResult.OK: allOK = False
182
+ if self._island_res[i][0] == IslandResult.OverFlow: self.__il_relax[i] = False
171
183
  if allOK:
172
184
  return GridSolveResult.OK, val
173
185
  elif allFail:
@@ -180,7 +192,7 @@ class DistFlowSolver(SolverBase):
180
192
  else:
181
193
  return GridSolveResult.PartialOK, val
182
194
 
183
- def __solve(self, _t: int, island:_Island, timeout_s:float) -> 'tuple[IslandResult, float]':
195
+ def __solve(self, _t: int, island:_Island, timeout_s:float, relaxV:bool, relaxI:bool, clear_results:bool=False) -> 'tuple[IslandResult, float]':
184
196
  model = Model("model")
185
197
 
186
198
  ''' ---------Variables----------
@@ -235,11 +247,11 @@ class DistFlowSolver(SolverBase):
235
247
  p, q = e.GetLoad(_t, island.grid.ChargePrice(_t), island.grid.DischargePrice(_t))
236
248
  e.P = p
237
249
  if p > 0:
238
- pg[e.BusID].append(p)
239
- qg[e.BusID].append(q)
250
+ pd[e.BusID] += p
251
+ qd[e.BusID] += q
240
252
  elif p < 0:
241
- pd[e.BusID] -= p
242
- qd[e.BusID] -= q
253
+ pg[e.BusID].append(-p)
254
+ qg[e.BusID].append(-q)
243
255
 
244
256
  # Create BUS vars
245
257
  v = {bID: model.addVar(name=f"v_{bID}", vtype='C') for bID in island.Buses}
@@ -249,18 +261,24 @@ class DistFlowSolver(SolverBase):
249
261
  if b.FixedV:
250
262
  assert b.V is not None, f"Bus {bid} has fixed voltage but not set"
251
263
  model.addConstr(v[bid] == b.V ** 2)
252
- else:
264
+ elif relaxV:
253
265
  dvmin[bid] = model.addVar(name=f"dvmin_{bid}", vtype='C', lb=0)
254
266
  dvmax[bid] = model.addVar(name=f"dvmax_{bid}", vtype='C', lb=0)
255
267
  model.addConstr(v[bid] >= b.MinV ** 2 - dvmin[bid])
256
268
  model.addConstr(v[bid] <= b.MaxV ** 2 + dvmax[bid])
269
+ else:
270
+ model.addConstr(v[bid] >= b.MinV ** 2)
271
+ model.addConstr(v[bid] <= b.MaxV ** 2)
257
272
 
258
273
  # Create Line vars
259
274
  dlmax:dict[str, Var] = {}
260
275
  l = {lID: model.addVar(name=f"l_{lID}", vtype='C', lb=0) for lID in island.Lines}
261
276
  for lID, ln in island.LineItems():
262
- dlmax[lID] = model.addVar(name=f"dlmax_{lID}", vtype='C', lb=0)
263
- model.addConstr(l[lID] <= (ln.max_I/island.grid.Ib) ** 2 + dlmax[lID])
277
+ if relaxI:
278
+ dlmax[lID] = model.addVar(name=f"dlmax_{lID}", vtype='C', lb=0)
279
+ model.addConstr(l[lID] <= (ln.max_I/island.grid.Ib) ** 2 + dlmax[lID])
280
+ else:
281
+ model.addConstr(l[lID] <= (ln.max_I/island.grid.Ib) ** 2)
264
282
 
265
283
  P = {lID: model.addVar(name=f"P_{lID}", vtype='C', lb=-GRB.INFINITY, ub=GRB.INFINITY) for lID in
266
284
  island.Lines}
@@ -298,7 +316,10 @@ class DistFlowSolver(SolverBase):
298
316
 
299
317
  decs = self.C * (Qs(Pdec.values()) + Qs(dvmin.values()) + Qs(dvmax.values()) + Qs(dlmax.values()))
300
318
  crpe = Qs(p.CC*(p.P(_t)-pvwp[pID]) for pID, p in island.PVWItems())
301
- goal = Qs(g.CostA(_t) * pg0[gID] ** 2 + g.CostB(_t) * pg0[gID] + g.CostC(_t) for gID, g in island.GenItems())
319
+ if self._sec_cost:
320
+ goal = Qs(g.CostA(_t) * pg0[gID] ** 2 + g.CostB(_t) * pg0[gID] + g.CostC(_t) for gID, g in island.GenItems())
321
+ else:
322
+ goal = Qs(g.CostB(_t) * pg0[gID] + g.CostC(_t) for gID, g in island.GenItems())
302
323
 
303
324
  model.setObjective(decs + goal + crpe, GRB.MINIMIZE)
304
325
  model.setParam(GRB.Param.OutputFlag, 0)
@@ -309,21 +330,21 @@ class DistFlowSolver(SolverBase):
309
330
  model.optimize()
310
331
 
311
332
  if model.Status != GRB.Status.OPTIMAL and model.Status != GRB.Status.SUBOPTIMAL:
312
- for bID, bus in island.BusItems():
313
- bus._v = bus.ShadowPrice = 0
314
- for pID, p in island.PVWItems():
333
+ for _, bus in island.BusItems():
334
+ if not bus.FixedV: bus._v = 0
335
+ bus.ShadowPrice = 0
336
+ for _, p in island.PVWItems():
315
337
  p._pr = p._qr = p._cr = 0
316
- for lid, line in island.LineItems():
338
+ for _, line in island.LineItems():
317
339
  line.I = line.P = line.Q = 0
318
340
  for j, gen in island.GenItems():
319
341
  if not gen.FixedP: gen._p = 0
320
342
  if not gen.FixedQ: gen._q = 0
321
- for eID, e in island.ESSItems():
343
+ for _, e in island.ESSItems():
322
344
  e.P = 0
323
345
  return IslandResult.Failed, -1
324
346
 
325
347
  for j, bus in island.BusItems():
326
- j = bus.ID
327
348
  bus._v = v[j].X ** 0.5
328
349
  try:
329
350
  sp = Pcons[j].Pi
@@ -351,13 +372,13 @@ class DistFlowSolver(SolverBase):
351
372
  overflow = False
352
373
  self._ofbuses.clear()
353
374
  for bID, bv in chain(dvmax.items(), dvmin.items()):
354
- if bv.X > 1e-6:
375
+ if bv.X > 1e-8:
355
376
  overflow = True
356
377
  self._ofbuses.add(bID)
357
378
 
358
379
  self._oflines.clear()
359
380
  for lID, lv in dlmax.items():
360
- if lv.X > 1e-6:
381
+ if lv.X > 1e-8:
361
382
  overflow = True
362
383
  self._oflines.add(lID)
363
384
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: fpowerkit
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A Distflow PDN solver based on Gurobi.
5
5
  Author: fmy_xfk
6
6
  Classifier: Programming Language :: Python :: 3
@@ -12,6 +12,13 @@ License-File: LICENSE
12
12
  Requires-Dist: feasytools>=0.0.21
13
13
  Requires-Dist: gurobipy>=9.1.0
14
14
  Requires-Dist: numpy>=1.20.0
15
+ Dynamic: author
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: requires-dist
20
+ Dynamic: requires-python
21
+ Dynamic: summary
15
22
 
16
23
  # FPowerKit - A power distribution network calculation component
17
24
 
@@ -5,7 +5,7 @@ with open("desc.md", "r", encoding="utf8") as f:
5
5
 
6
6
  setup(
7
7
  name="fpowerkit",
8
- version="0.1.4",
8
+ version="0.1.6",
9
9
  author="fmy_xfk",
10
10
  packages=find_packages(),
11
11
  description="A Distflow PDN solver based on Gurobi.",
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