fpowerkit 0.1.0__tar.gz → 0.1.1__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.1.0
3
+ Version: 0.1.1
4
4
  Summary: A Distflow PDN solver based on Gurobi.
5
5
  Author: fmy_xfk
6
6
  Classifier: Programming Language :: Python :: 3
@@ -130,8 +130,8 @@ class Bus:
130
130
  def toXMLNode(self, Ub_kV:Optional[float] = None,
131
131
  Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
132
132
  e = Element("bus", {"ID": self.ID})
133
- if self.Lat is not None: e.attrib["Lat"] = str(self.Lat)
134
- if self.Lon is not None: e.attrib["Lon"] = str(self.Lon)
133
+ if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
134
+ if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
135
135
  if self.V is not None and self.FixedV:
136
136
  e.attrib["V"] = str(self.V * Ub_kV)+"kV" if Ub_kV else str(self.V)
137
137
  else:
@@ -199,6 +199,8 @@ class Generator:
199
199
  "ID": self.ID,
200
200
  "Bus": self.BusID,
201
201
  })
202
+ if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
203
+ if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
202
204
  if Sb_MVA is not None and Sb_kVA is not None:
203
205
  raise ValueError("Provide only ONE of Sb_MVA or Sb_kVA.")
204
206
  if Sb_MVA is not None:
@@ -210,8 +212,6 @@ class Generator:
210
212
  else:
211
213
  mul = 1
212
214
  unit = ["pu", "pu", "$/puh2", "$/puh", "$"]
213
- if self.Lat is not None: e.attrib["Lat"] = str(self.Lat)
214
- if self.Lon is not None: e.attrib["Lon"] = str(self.Lon)
215
215
  if self.Pmin: e.append(Func2Elem(self.Pmin, "Pmin", mul, unit[0]))
216
216
  if self.Pmax: e.append(Func2Elem(self.Pmax, "Pmax", mul, unit[0]))
217
217
  if self.Qmin: e.append(Func2Elem(self.Qmin, "Qmin", mul, unit[1]))
@@ -90,8 +90,62 @@ class Grid:
90
90
  self._patb[pvw.BusID].append(pvw)
91
91
 
92
92
  def AddGen(self, g: Generator):
93
+ '''Add a generator'''
93
94
  self._gens[g.ID] = g
95
+ assert g.BusID in self._bnames, f"Bus {g.BusID} undefined"
94
96
  self._gatb[g.BusID].append(g)
97
+
98
+ def DelGen(self, id: str):
99
+ '''Delete a generator'''
100
+ g = self._gens.pop(id)
101
+ self._gatb[g.BusID].remove(g)
102
+
103
+ def AddBus(self, b: Bus):
104
+ '''Add a bus'''
105
+ self._buses[b.ID] = b
106
+ self._bnames.append(b.ID)
107
+ self._ladjfb[b.ID] = []
108
+ self._ladjtb[b.ID] = []
109
+ self._gatb[b.ID] = []
110
+ self._patb[b.ID] = []
111
+
112
+ def DelBus(self, id: str):
113
+ '''Delete a bus and all related lines, generators and PVWinds'''
114
+ b = self._buses.pop(id)
115
+ self._bnames.remove(id)
116
+ for l in self._ladjfb[b.ID]: self._lines.pop(l.ID)
117
+ for l in self._ladjtb[b.ID]: self._lines.pop(l.ID)
118
+ for g in self._gatb[b.ID]: self._gens.pop(g.ID)
119
+ for p in self._patb[b.ID]: self._pvws.pop(p.ID)
120
+ self._ladjfb.pop(b.ID)
121
+ self._ladjtb.pop(b.ID)
122
+ self._gatb.pop(b.ID)
123
+ self._patb.pop(b.ID)
124
+
125
+ def AddLine(self, l: Line):
126
+ '''Add a line'''
127
+ self._lines[l.ID] = l
128
+ assert l.fBus in self._bnames, f"Bus {l.fBus} undefined"
129
+ assert l.tBus in self._bnames, f"Bus {l.tBus} undefined"
130
+ self._ladjfb[l.fBus].append(l)
131
+ self._ladjtb[l.tBus].append(l)
132
+
133
+ def DelLine(self, id: str):
134
+ '''Delete a line'''
135
+ l = self._lines.pop(id)
136
+ self._ladjfb[l.fBus].remove(l)
137
+ self._ladjtb[l.tBus].remove(l)
138
+
139
+ def AddPVWind(self, p: PVWind):
140
+ '''Add a PVWind'''
141
+ self._pvws[p.ID] = p
142
+ assert p.BusID in self._bnames, f"Bus {p.BusID} undefined"
143
+ self._patb[p.BusID].append(p)
144
+
145
+ def DelPVWind(self, id: str):
146
+ '''Delete a PVWind'''
147
+ p = self._pvws.pop(id)
148
+ self._patb[p.BusID].remove(p)
95
149
 
96
150
  @property
97
151
  def BusNames(self) -> 'list[str]':
@@ -350,6 +404,11 @@ class Grid:
350
404
  Sb_MVA = self.Sb_MVA if PQmode == PQSaveMode.MVA else None,
351
405
  Sb_kVA = self.Sb_kVA if PQmode == PQSaveMode.KVA else None
352
406
  ))
407
+ for p in self.PVWinds:
408
+ e.append(p.toXMLNode(
409
+ Sb_MVA = self.Sb_MVA if PQmode == PQSaveMode.MVA else None,
410
+ Sb_kVA = self.Sb_kVA if PQmode == PQSaveMode.KVA else None
411
+ ))
353
412
  return e
354
413
 
355
414
  def saveFileXML(self, path:str,
@@ -9,23 +9,29 @@ class PVWind:
9
9
  Reactive power output is calculated from the power factor.
10
10
  Not all the power output is used, the maximum power abondon rate is given.
11
11
  '''
12
- def __init__(self, id: str, busid:str, p: FloatLike, pf: float, tag: str):
12
+ def __init__(self, id: str, busid:str, p: FloatLike, pf: float, cc:float, tag: str, Lat: NFloat=None, Lon: NFloat=None):
13
13
  '''
14
14
  Initialize
15
15
  id: PV Panel ID
16
16
  busid: ID of the bus where the PV panel is located
17
17
  p: Active power output, pu
18
18
  pf: Power factor, cos(phi). Reactive output Q = P * sqrt(1 - PF**2)
19
+ cc: curtail cost, $/puh
19
20
  tag: "PV" or "Wind", indicating the type of the generator
21
+ Lat: Latitude of the PV Panel
22
+ Lon: Longitude of the PV Panel
20
23
  '''
21
24
  self._id = id
22
25
  self._bus = busid
23
26
  self._p = Float2Func(p)
24
27
  self._pf = pf
28
+ self._cc = cc
25
29
  self._tag = tag
26
30
  self._cr:FloatVar = None
27
31
  self._pr:FloatVar = None
28
32
  self._qr:FloatVar = None
33
+ self.Lat = Lat
34
+ self.Lon = Lon
29
35
 
30
36
  @property
31
37
  def ID(self):
@@ -50,13 +56,21 @@ class PVWind:
50
56
  '''Power factor, cos(phi). Reactive output Q = P * sqrt(1 - PF**2)'''
51
57
  return self._pf
52
58
  @PF.setter
53
- def Phi(self, pf: float):
59
+ def PF(self, pf: float):
54
60
  self._pf = pf
55
61
 
56
62
  def Q(self, _t):
57
63
  '''Reactive power output, pu'''
58
64
  return self.P(_t) * math.sqrt(1 - self._pf**2)
59
65
 
66
+ def position(self):
67
+ '''Return the position of the PV Panel'''
68
+ return (self.Lat, self.Lon)
69
+
70
+ def LonLat(self):
71
+ '''Return the position of the PV Panel'''
72
+ return (self.Lon, self.Lat)
73
+
60
74
  def __repr__(self) -> str:
61
75
  return (f"PVPanel(id='{self.ID}', busid='{self.BusID}', P={self.P}, PF={self.PF})")
62
76
 
@@ -66,11 +80,29 @@ class PVWind:
66
80
  def str_t(self, _t: int) -> str:
67
81
  return (f"PVPanel(id='{self.ID}', busid='{self.BusID}', P={self.P(_t)}, PF={self.PF})")
68
82
 
83
+ @property
84
+ def Tag(self) -> str:
85
+ '''Return the type of the generator'''
86
+ return self._tag
87
+ @Tag.setter
88
+ def Tag(self, tag: str):
89
+ '''Set the type of the generator'''
90
+ self._tag = tag
91
+
69
92
  @property
70
93
  def CR(self):
71
94
  '''Curtailment rate'''
72
95
  return self._cr
73
96
 
97
+ @property
98
+ def CC(self):
99
+ '''Curtailment cost, $/puh'''
100
+ return self._cc
101
+ @CC.setter
102
+ def CC(self, cc: float):
103
+ '''Set the curtailment cost, $/puh'''
104
+ self._cc = cc
105
+
74
106
  @property
75
107
  def Pr(self):
76
108
  '''Real power output'''
@@ -91,16 +123,34 @@ class PVWind:
91
123
  busid = node.attrib["Bus"]
92
124
  p = ReadFloatLike(node.find("P"), Sb_MVA, Ub_kV)
93
125
  pf = float(node.attrib["pf"])
126
+ cc = ReadConst(node.attrib["cc"], Sb_MVA, Ub_kV) if "cc" in node.attrib else 0
94
127
  tag = node.tag
95
- return PVWind(id, busid, p, pf, tag)
128
+ lat = float(node.attrib["Lat"]) if "Lat" in node.attrib else None
129
+ lon = float(node.attrib["Lon"]) if "Lon" in node.attrib else None
130
+ return PVWind(id, busid, p, pf, cc, tag, lat, lon)
96
131
 
97
- def toXMLNode(self) -> 'Element':
132
+ def toXMLNode(self, Sb_MVA:Optional[float] = None, Sb_kVA:Optional[float] = None) -> 'Element':
98
133
  '''Convert to XML node'''
99
134
  e = Element(self._tag, {
100
- "id": self.ID,
101
- "busid": self.BusID,
135
+ "ID": self.ID,
136
+ "Bus": self.BusID,
102
137
  "pf": str(self.PF),
103
138
  })
104
- e.append(Func2Elem(self.P, "P"))
139
+ if self.Lat is not None: e.attrib["Lat"] = f"{self.Lat:.6f}"
140
+ if self.Lon is not None: e.attrib["Lon"] = f"{self.Lon:.6f}"
141
+ if Sb_MVA is not None and Sb_kVA is not None:
142
+ raise ValueError("Provide only ONE of Sb_MVA or Sb_kVA.")
143
+ if Sb_MVA is not None:
144
+ mul = Sb_MVA
145
+ unit = ["MW", "$/MWh"]
146
+ elif Sb_kVA is not None:
147
+ mul = Sb_kVA
148
+ unit = ["kW", "$/kWh"]
149
+ else:
150
+ mul = 1
151
+ unit = ["pu", "$/puh"]
152
+ e.append(Func2Elem(self.P, "P", mul, unit[0]))
153
+ e.attrib["cc"] = str(self.CC*mul) + unit[1]
154
+
105
155
  return e
106
156
 
@@ -199,7 +199,7 @@ class DistFlowSolver(SolverBase):
199
199
  '''DistFlow solver'''
200
200
  def __init__(self, grid:Grid, eps:float = 1e-6,
201
201
  default_saveto:str = DEFAULT_SAVETO, /, * ,
202
- mlrp:float = 0.5, crp:float = 0.0):
202
+ mlrp:float = 0.5):
203
203
  '''
204
204
  Initialize
205
205
  grid: Grid object
@@ -211,8 +211,6 @@ class DistFlowSolver(SolverBase):
211
211
  self.dec_buses:dict[BusID, LoadReduceModule] = {}
212
212
  self.C = 1e9
213
213
  self.max_load_reduction_proportion = mlrp
214
- assert crp >= 0, "Abandon PVW penalty should be non-negative"
215
- self.cr_penalty = crp
216
214
 
217
215
  def solve(self, _t: int, /, *, timeout_s:float = 1) -> 'tuple[GridSolveResult, float]':
218
216
  '''Get the best result at time _t, return a tuple: (result status, optimal objective value)'''
@@ -355,7 +353,7 @@ class DistFlowSolver(SolverBase):
355
353
  model.addConstr(pvwp[p.ID] * math.sqrt(1 - p.PF**2) == pvwq[p.ID])
356
354
 
357
355
  decs = self.C * (Qs(Pdec.values()) + Qs(dvmin.values()) + Qs(dvmax.values()) + Qs(dlmax.values()))
358
- crpe = self.cr_penalty * Qs(self.grid.PVWind(i).P(_t)-p for i, p in pvwp.items())
356
+ crpe = Qs(p.CC*(p.P(_t)-pvwp[p.ID]) for p in self.grid.PVWinds)
359
357
  goal = Qs(g.CostA(_t) * pg0[g.ID] ** 2 + g.CostB(_t) * pg0[g.ID] + g.CostC(_t) for g in self.grid.Gens)
360
358
 
361
359
  model.setObjective(decs + goal + crpe, GRB.MINIMIZE)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fpowerkit
3
- Version: 0.1.0
3
+ Version: 0.1.1
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("desc.md", "r", encoding="utf8") as f:
5
5
 
6
6
  setup(
7
7
  name="fpowerkit",
8
- version="0.1.0",
8
+ version="0.1.1",
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