fpowerkit 0.0.5__tar.gz → 0.0.7__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.0.5 → fpowerkit-0.0.7}/PKG-INFO +1 -1
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit/grid.py +53 -9
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit.egg-info/PKG-INFO +1 -1
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/setup.py +1 -1
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/LICENSE +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/README.md +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit/__init__.py +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit.egg-info/SOURCES.txt +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit.egg-info/dependency_links.txt +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit.egg-info/requires.txt +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/fpowerkit.egg-info/top_level.txt +0 -0
- {fpowerkit-0.0.5 → fpowerkit-0.0.7}/setup.cfg +0 -0
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from enum import IntEnum
|
|
2
|
+
from pathlib import Path
|
|
1
3
|
import zipfile
|
|
2
4
|
from io import BytesIO, TextIOWrapper
|
|
3
5
|
from typing import Any, TextIO, Iterable, Optional, Union
|
|
@@ -389,6 +391,13 @@ BusID = str
|
|
|
389
391
|
LineID = str
|
|
390
392
|
|
|
391
393
|
|
|
394
|
+
class GridSolveResult(IntEnum):
|
|
395
|
+
'''Result of grid solving'''
|
|
396
|
+
Failed = 0
|
|
397
|
+
OK = 1
|
|
398
|
+
OKwithoutVICons = 2
|
|
399
|
+
SubOKwithoutVICons = 3
|
|
400
|
+
|
|
392
401
|
class Grid:
|
|
393
402
|
Sb: float #MVA
|
|
394
403
|
Ub: float #kV
|
|
@@ -440,6 +449,10 @@ class Grid:
|
|
|
440
449
|
assert gen.BusID in self._bnames
|
|
441
450
|
self._gatb[gen.BusID].append(gen)
|
|
442
451
|
self.__holdShadowPrice = holdShadowPrice
|
|
452
|
+
self.__saveto:str = ""
|
|
453
|
+
|
|
454
|
+
def SetErrorSaveTo(self, path:str = ""):
|
|
455
|
+
self.__saveto = path
|
|
443
456
|
|
|
444
457
|
def AddGen(self, g: Generator):
|
|
445
458
|
self._gens[g.ID] = g
|
|
@@ -573,9 +586,41 @@ class Grid:
|
|
|
573
586
|
raise ValueError(f"Invalid power unit: {gens_unit}")
|
|
574
587
|
return Grid(Sb, Ub, buses, lines, gens, holdShadowPrice)
|
|
575
588
|
|
|
576
|
-
def
|
|
577
|
-
|
|
578
|
-
|
|
589
|
+
def savePQofBus(self, file_name: str, t:int):
|
|
590
|
+
with open(file_name, "w") as fp:
|
|
591
|
+
fp.write("Bus,Pd,Qd\n")
|
|
592
|
+
for bus in self.Buses:
|
|
593
|
+
fp.write(f"{bus.ID},{bus.Pd(t)},{bus.Qd(t)}\n")
|
|
594
|
+
|
|
595
|
+
def loadPQofBus(self, file_name: str):
|
|
596
|
+
with open(file_name) as fp:
|
|
597
|
+
for ln in fp.readlines()[1:]:
|
|
598
|
+
bn, p, q = ln.strip().split(',')
|
|
599
|
+
self.Bus(bn).Pd = ConstFunc(float(p) / self.Sb)
|
|
600
|
+
self.Bus(bn).Qd = ConstFunc(float(q) / self.Sb)
|
|
601
|
+
|
|
602
|
+
def solve(self, _t: int, /, *, max_v_pu: float = 1.1, min_v_pu: float = 0.9, max_I_kA: float = 0.866,
|
|
603
|
+
source_buses:'list[BusID]' = [], source_bus_V_pu:float = 1.0, timeout_s:float = 1) -> 'tuple[GridSolveResult, float]':
|
|
604
|
+
'''Get the best result at time _t, return a tuple: (result status, optimal objective value)'''
|
|
605
|
+
ok, val = self.__solve(_t, max_v_pu, min_v_pu, max_I_kA, source_buses, source_bus_V_pu, timeout_s)
|
|
606
|
+
if ok == GRB.Status.OPTIMAL:
|
|
607
|
+
return GridSolveResult.OK, val
|
|
608
|
+
else:
|
|
609
|
+
ok, val = self.__solve(_t, float("inf"), 0, float("inf"), source_buses, source_bus_V_pu, timeout_s)
|
|
610
|
+
if ok == GRB.Status.OPTIMAL:
|
|
611
|
+
return GridSolveResult.OKwithoutVICons, val
|
|
612
|
+
elif ok == GRB.Status.SUBOPTIMAL:
|
|
613
|
+
return GridSolveResult.SubOKwithoutVICons, val
|
|
614
|
+
else:
|
|
615
|
+
print(f"Failed to solve at time {_t}: {ok}")
|
|
616
|
+
if self.__saveto != "":
|
|
617
|
+
p = Path(self.__saveto)
|
|
618
|
+
p.mkdir(parents=True, exist_ok=True)
|
|
619
|
+
self.savePQofBus(str(p/f"{_t}_load.csv"), _t)
|
|
620
|
+
return GridSolveResult.Failed, val
|
|
621
|
+
|
|
622
|
+
def __solve(self, _t: int, max_v_pu:float, min_v_pu: float, max_I_kA: float,
|
|
623
|
+
source_buses:'list[BusID]', source_bus_V_pu:float, timeout_s:float) -> 'tuple[int, float]':
|
|
579
624
|
model = Model("model")
|
|
580
625
|
max_v = max_v_pu ** 2
|
|
581
626
|
min_v = min_v_pu ** 2
|
|
@@ -632,14 +677,13 @@ class Grid:
|
|
|
632
677
|
model.setObjective(
|
|
633
678
|
quicksum(g.CostA(_t) * pg0[g.ID] ** 2 + g.CostB(_t) * pg0[g.ID] + g.CostC(_t) for g in self.Gens),
|
|
634
679
|
GRB.MINIMIZE)
|
|
635
|
-
model.setParam(
|
|
636
|
-
model.setParam(
|
|
680
|
+
model.setParam(GRB.Param.OutputFlag, 0)
|
|
681
|
+
model.setParam(GRB.Param.QCPDual, 1)
|
|
682
|
+
model.setParam(GRB.Param.TimeLimit, timeout_s)
|
|
637
683
|
model.update()
|
|
638
684
|
model.optimize()
|
|
639
685
|
if model.Status != GRB.Status.OPTIMAL:
|
|
640
|
-
model.
|
|
641
|
-
if model.Status != GRB.Status.OPTIMAL:
|
|
642
|
-
return False, 0
|
|
686
|
+
return model.Status, -1
|
|
643
687
|
|
|
644
688
|
for bus in self.Buses:
|
|
645
689
|
j = bus.ID
|
|
@@ -661,4 +705,4 @@ class Grid:
|
|
|
661
705
|
gen.P = pg0[j].X
|
|
662
706
|
gen.Q = qg0[j].X
|
|
663
707
|
|
|
664
|
-
return
|
|
708
|
+
return model.Status, model.ObjVal
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|