fpowerkit 0.0.6__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.6 → fpowerkit-0.0.7}/PKG-INFO +1 -1
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit/grid.py +39 -13
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit.egg-info/PKG-INFO +1 -1
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/setup.py +1 -1
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/LICENSE +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/README.md +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit/__init__.py +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit.egg-info/SOURCES.txt +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit.egg-info/dependency_links.txt +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit.egg-info/requires.txt +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/fpowerkit.egg-info/top_level.txt +0 -0
- {fpowerkit-0.0.6 → fpowerkit-0.0.7}/setup.cfg +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from enum import IntEnum
|
|
2
|
+
from pathlib import Path
|
|
2
3
|
import zipfile
|
|
3
4
|
from io import BytesIO, TextIOWrapper
|
|
4
5
|
from typing import Any, TextIO, Iterable, Optional, Union
|
|
@@ -395,6 +396,7 @@ class GridSolveResult(IntEnum):
|
|
|
395
396
|
Failed = 0
|
|
396
397
|
OK = 1
|
|
397
398
|
OKwithoutVICons = 2
|
|
399
|
+
SubOKwithoutVICons = 3
|
|
398
400
|
|
|
399
401
|
class Grid:
|
|
400
402
|
Sb: float #MVA
|
|
@@ -447,6 +449,10 @@ class Grid:
|
|
|
447
449
|
assert gen.BusID in self._bnames
|
|
448
450
|
self._gatb[gen.BusID].append(gen)
|
|
449
451
|
self.__holdShadowPrice = holdShadowPrice
|
|
452
|
+
self.__saveto:str = ""
|
|
453
|
+
|
|
454
|
+
def SetErrorSaveTo(self, path:str = ""):
|
|
455
|
+
self.__saveto = path
|
|
450
456
|
|
|
451
457
|
def AddGen(self, g: Generator):
|
|
452
458
|
self._gens[g.ID] = g
|
|
@@ -580,6 +586,12 @@ class Grid:
|
|
|
580
586
|
raise ValueError(f"Invalid power unit: {gens_unit}")
|
|
581
587
|
return Grid(Sb, Ub, buses, lines, gens, holdShadowPrice)
|
|
582
588
|
|
|
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
|
+
|
|
583
595
|
def loadPQofBus(self, file_name: str):
|
|
584
596
|
with open(file_name) as fp:
|
|
585
597
|
for ln in fp.readlines()[1:]:
|
|
@@ -587,9 +599,28 @@ class Grid:
|
|
|
587
599
|
self.Bus(bn).Pd = ConstFunc(float(p) / self.Sb)
|
|
588
600
|
self.Bus(bn).Qd = ConstFunc(float(q) / self.Sb)
|
|
589
601
|
|
|
590
|
-
def solve(self, _t: int, /, *, max_v_pu:float = 1.1, min_v_pu: float = 0.9, max_I_kA: float = 0.866,
|
|
591
|
-
source_buses:'list[BusID]' = [], source_bus_V_pu:float = 1.0) -> 'tuple[GridSolveResult, float]':
|
|
592
|
-
'''Get the best result at time _t, return a tuple: (
|
|
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]':
|
|
593
624
|
model = Model("model")
|
|
594
625
|
max_v = max_v_pu ** 2
|
|
595
626
|
min_v = min_v_pu ** 2
|
|
@@ -646,18 +677,13 @@ class Grid:
|
|
|
646
677
|
model.setObjective(
|
|
647
678
|
quicksum(g.CostA(_t) * pg0[g.ID] ** 2 + g.CostB(_t) * pg0[g.ID] + g.CostC(_t) for g in self.Gens),
|
|
648
679
|
GRB.MINIMIZE)
|
|
649
|
-
model.setParam(
|
|
650
|
-
model.setParam(
|
|
680
|
+
model.setParam(GRB.Param.OutputFlag, 0)
|
|
681
|
+
model.setParam(GRB.Param.QCPDual, 1)
|
|
682
|
+
model.setParam(GRB.Param.TimeLimit, timeout_s)
|
|
651
683
|
model.update()
|
|
652
684
|
model.optimize()
|
|
653
685
|
if model.Status != GRB.Status.OPTIMAL:
|
|
654
|
-
|
|
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
|
|
686
|
+
return model.Status, -1
|
|
661
687
|
|
|
662
688
|
for bus in self.Buses:
|
|
663
689
|
j = bus.ID
|
|
@@ -679,4 +705,4 @@ class Grid:
|
|
|
679
705
|
gen.P = pg0[j].X
|
|
680
706
|
gen.Q = qg0[j].X
|
|
681
707
|
|
|
682
|
-
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
|