fpowerkit 0.0.4__tar.gz → 0.0.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fpowerkit
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A Distflow PDN solver based on Gurobi.
5
5
  Author: fmy_xfk
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,3 +1,4 @@
1
+ from enum import IntEnum
1
2
  import zipfile
2
3
  from io import BytesIO, TextIOWrapper
3
4
  from typing import Any, TextIO, Iterable, Optional, Union
@@ -7,6 +8,7 @@ from gurobipy import Model, GRB, quicksum, Var, Constr
7
8
 
8
9
  FloatVar = Optional[float]
9
10
  FloatLike = Union[int, float, TimeFunc, 'list[tuple[int, float]]']
11
+ FloatVals = Union[int, float]
10
12
  Qs = quicksum
11
13
 
12
14
 
@@ -50,8 +52,9 @@ def _readVal(s: str) -> 'tuple[float, str]':
50
52
  return float(s), ""
51
53
 
52
54
 
53
- def _readFloatLike(e: Element, sb_mva:float, ub_kv:float) -> FloatLike:
54
- def _valconv(v:FloatLike, u:str) -> FloatLike:
55
+ def _readFloatLike(e: Optional[Element], sb_mva:float, ub_kv:float) -> FloatLike:
56
+ assert e is not None
57
+ def _valconv(v:FloatVals, u:str) -> FloatVals:
55
58
  if u == "pu":
56
59
  return v
57
60
  elif u == "kVA":
@@ -130,7 +133,7 @@ class Bus:
130
133
  return f"Bus(id='{self.ID}', v_pu={_tfv(self.V)}, pd_pu={self.Pd(_t)}, qd_pu={self.Qd(_t)}, lat={self.Lat}, lon={self.Lon})"
131
134
 
132
135
  @property
133
- def position(self) -> 'tuple[float, float]':
136
+ def position(self) -> 'tuple[Optional[float], Optional[float]]':
134
137
  '''Return the position of the bus, (lat, lon)'''
135
138
  return self.Lat, self.Lon
136
139
 
@@ -140,7 +143,9 @@ class Bus:
140
143
  p = _readFloatLike(node.find("Pd"), Sb_MVA, Ub_kV)
141
144
  q = _readFloatLike(node.find("Qd"), Sb_MVA, Ub_kV)
142
145
  lat = node.attrib.get("Lat", None)
146
+ if lat is not None: lat = float(lat)
143
147
  lon = node.attrib.get("Lon", None)
148
+ if lon is not None: lon = float(lon)
144
149
  return Bus(id, p, q, lat, lon)
145
150
 
146
151
  @staticmethod
@@ -174,12 +179,12 @@ class Bus:
174
179
  q = q * loop_times
175
180
  bn = ret[0].strip().split('|')
176
181
  if len(bn) == 1:
177
- return bn[0], makeFunc(tl, p), makeFunc(tl, q)
182
+ return bn[0], makeFunc(tl, p), makeFunc(tl, q), None, None
178
183
  elif len(bn) == 3:
179
184
  return bn[0], makeFunc(tl, p), makeFunc(tl, q), float(bn[1]), float(bn[2])
180
185
  else:
181
186
  raise ValueError(f"Bad bus name format at line {i} in bus CSV file, should be 'name' or 'name|lat|lon'")
182
-
187
+
183
188
  return [Bus(*parseline(ln,i)) for i,ln in enumerate(data[1:],start=1)]
184
189
 
185
190
 
@@ -385,6 +390,12 @@ BusID = str
385
390
  LineID = str
386
391
 
387
392
 
393
+ class GridSolveResult(IntEnum):
394
+ '''Result of grid solving'''
395
+ Failed = 0
396
+ OK = 1
397
+ OKwithoutVICons = 2
398
+
388
399
  class Grid:
389
400
  Sb: float #MVA
390
401
  Ub: float #kV
@@ -569,8 +580,15 @@ class Grid:
569
580
  raise ValueError(f"Invalid power unit: {gens_unit}")
570
581
  return Grid(Sb, Ub, buses, lines, gens, holdShadowPrice)
571
582
 
583
+ def loadPQofBus(self, file_name: str):
584
+ with open(file_name) as fp:
585
+ for ln in fp.readlines()[1:]:
586
+ bn, p, q = ln.strip().split(',')
587
+ self.Bus(bn).Pd = ConstFunc(float(p) / self.Sb)
588
+ self.Bus(bn).Qd = ConstFunc(float(q) / self.Sb)
589
+
572
590
  def solve(self, _t: int, /, *, max_v_pu:float = 1.1, min_v_pu: float = 0.9, max_I_kA: float = 0.866,
573
- source_buses:'list[BusID]' = [], source_bus_V_pu:float = 1.0) -> 'tuple[bool, float]':
591
+ source_buses:'list[BusID]' = [], source_bus_V_pu:float = 1.0) -> 'tuple[GridSolveResult, float]':
574
592
  '''Get the best result at time _t, return a tuple: (success?, optimal objective value)'''
575
593
  model = Model("model")
576
594
  max_v = max_v_pu ** 2
@@ -623,19 +641,23 @@ class Grid:
623
641
 
624
642
  assert len(source_buses) > 0, "No source bus specified"
625
643
  for b in source_buses:
626
- model.addConstr(v[b] == source_bus_V_pu, f"src_{b}")
644
+ model.addConstr(v[b] == source_bus_V_pu ** 2, f"src_{b}")
627
645
 
628
646
  model.setObjective(
629
647
  quicksum(g.CostA(_t) * pg0[g.ID] ** 2 + g.CostB(_t) * pg0[g.ID] + g.CostC(_t) for g in self.Gens),
630
648
  GRB.MINIMIZE)
631
649
  model.setParam('OutputFlag', 0)
632
- model.setParam('QCPDual', 1) # 启用二次约束规划对偶
650
+ model.setParam('QCPDual', 1)
633
651
  model.update()
634
652
  model.optimize()
635
653
  if model.Status != GRB.Status.OPTIMAL:
636
- model.optimize()
637
- if model.Status != GRB.Status.OPTIMAL:
638
- return False, 0
654
+ # Remove VI constraints and try again
655
+ state, val = self.solve(_t, max_v_pu=float("inf"), min_v_pu=0, max_I_kA=float("inf"),
656
+ source_buses=source_buses,source_bus_V_pu=source_bus_V_pu)
657
+ if state == GridSolveResult.OK:
658
+ return GridSolveResult.OKwithoutVICons, val
659
+ else:
660
+ return GridSolveResult.Failed, val
639
661
 
640
662
  for bus in self.Buses:
641
663
  j = bus.ID
@@ -657,4 +679,4 @@ class Grid:
657
679
  gen.P = pg0[j].X
658
680
  gen.Q = qg0[j].X
659
681
 
660
- return True, model.ObjVal
682
+ return GridSolveResult.OK, model.ObjVal
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fpowerkit
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A Distflow PDN solver based on Gurobi.
5
5
  Author: fmy_xfk
6
6
  Classifier: Programming Language :: Python :: 3
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf8") as f:
5
5
 
6
6
  setup(
7
7
  name="fpowerkit",
8
- version="0.0.4",
8
+ version="0.0.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