distgridforge 0.3.0__py3-none-any.whl

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 (38) hide show
  1. distgridforge-0.3.0.dist-info/METADATA +266 -0
  2. distgridforge-0.3.0.dist-info/RECORD +38 -0
  3. distgridforge-0.3.0.dist-info/WHEEL +5 -0
  4. distgridforge-0.3.0.dist-info/licenses/LICENCE.txt +21 -0
  5. distgridforge-0.3.0.dist-info/top_level.txt +1 -0
  6. gridforge/__init__.py +32 -0
  7. gridforge/case_study/config.py +158 -0
  8. gridforge/case_study/dickert_lv_network_script.py +109 -0
  9. gridforge/case_study/mv_lv_feeder.py +314 -0
  10. gridforge/case_study/simbench_network_test_case.py +168 -0
  11. gridforge/core/__init__.py +0 -0
  12. gridforge/core/constraints.py +421 -0
  13. gridforge/core/cost_model.py +363 -0
  14. gridforge/core/grid_enhancement.py +1932 -0
  15. gridforge/core/power_flow.py +1146 -0
  16. gridforge/core/strategy.py +297 -0
  17. gridforge/engines/__init__.py +0 -0
  18. gridforge/engines/gridlabd_engine.py +527 -0
  19. gridforge/engines/opendss_engine.py +885 -0
  20. gridforge/engines/pandapower_engine.py +3766 -0
  21. gridforge/engines/roseau_engine.py +1234 -0
  22. gridforge/examples/__init__.py +0 -0
  23. gridforge/examples/quick_start.py +85 -0
  24. gridforge/main.py +0 -0
  25. gridforge/strategies/__init__.py +0 -0
  26. gridforge/strategies/flexibility.py +1476 -0
  27. gridforge/strategies/line_upgrade.py +1846 -0
  28. gridforge/strategies/path_to_source.py +596 -0
  29. gridforge/strategies/sizing.py +210 -0
  30. gridforge/strategies/step_check.py +183 -0
  31. gridforge/strategies/tap_change.py +267 -0
  32. gridforge/strategies/transformer_upgrade.py +622 -0
  33. gridforge/utils/__init__.py +0 -0
  34. gridforge/utils/config.py +55 -0
  35. gridforge/utils/interactive_plot.py +700 -0
  36. gridforge/utils/logging.py +86 -0
  37. gridforge/utils/plot.py +1078 -0
  38. gridforge/utils/reporting.py +0 -0
@@ -0,0 +1,421 @@
1
+ import enum
2
+ from typing import Any
3
+
4
+ from gridforge.utils.logging import logger
5
+
6
+
7
+ class ConstraintType(enum.Enum):
8
+ """Enum defining types of constraint violations in power systems."""
9
+
10
+ OVER_VOLTAGE = "over_voltage"
11
+ UNDER_VOLTAGE = "under_voltage"
12
+ LINE_LOADING = "line_loading"
13
+ TRANSFORMER_LOADING = "transformer_loading"
14
+
15
+
16
+ class ConstraintViolation:
17
+ """
18
+ Represents a single constraint violation in the power grid.
19
+
20
+ Attributes:
21
+ element_id (Union[int, str]): Unique identifier for the element (e.g., bus, line, transformer).
22
+ element_type (str): Type of the element ('Bus', 'Line', 'Transformer').
23
+ violated_value (float): The threshold value that is violated by the constraint (e.g., voltage limit, loading limit).
24
+ absolute_value (float): The actual value measured at the element (e.g., voltage in p.u., current in A).
25
+ constraint_type (ConstraintType): The type of constraint violated (e.g., OVER_VOLTAGE).
26
+ violation_value (float): The magnitude of the violation (difference from the threshold).
27
+ """
28
+
29
+ def __init__(
30
+ self,
31
+ element_id,
32
+ element_type,
33
+ violated_value,
34
+ absolute_value,
35
+ constraint_type,
36
+ violation_value,
37
+ ):
38
+ """
39
+ Initialize a ConstraintViolation object.
40
+
41
+ Args:
42
+ element_id (Union[int, str]): Unique identifier for the element.
43
+ element_type (str): Type of the element.
44
+ violated_value (float): The threshold value that is violated.
45
+ absolute_value (float): The actual value measured.
46
+ constraint_type (ConstraintType): The type of constraint violated.
47
+ violation_value (float): The magnitude of the violation.
48
+ """
49
+ self.element_id = element_id
50
+ self.element_type = element_type
51
+ self.violated_value = violated_value
52
+ self.absolute_value = absolute_value
53
+ self.constraint_type = constraint_type
54
+ self.violation_value = violation_value
55
+
56
+ def __repr__(self):
57
+ return (
58
+ f"ConstraintViolation(element_id={self.element_id}, element_type={self.element_type}, "
59
+ f"violated_value={self.violated_value}, constraint_type={self.constraint_type}, "
60
+ f"violation_value={self.violation_value})"
61
+ )
62
+
63
+
64
+ _VOLTAGE_TYPES = (ConstraintType.OVER_VOLTAGE, ConstraintType.UNDER_VOLTAGE)
65
+
66
+
67
+ def _relative_excess(violation: ConstraintViolation) -> float:
68
+ """
69
+ Rate how far a violation is beyond its limit, on a scale common to all types: the
70
+ measure `GridEnhancementAlgorithm` rates its states by, which `FlexibilityStrategy`
71
+ shares to weigh what undoing steps would give back against what it would avoid.
72
+
73
+ A loading is rated against its limit: 110 % against 100 % gives 0.1. A voltage is
74
+ rated against the width of the allowed band, the distance of the violated limit
75
+ from 1 pu: 0.85 pu against a limit of 0.90 pu is half a band beyond it and gives
76
+ 0.5. Against the limit itself, of about 1 pu, it would give 0.056 and a deep
77
+ voltage excursion would weigh less than a slight overload. A voltage limit at
78
+ 1 pu leaves no band, and its excess is then rated against the limit.
79
+ """
80
+ if violation.violated_value is None:
81
+ # No limit to rate the excess against
82
+ return 0.0
83
+ limit = abs(violation.violated_value)
84
+ if violation.constraint_type in _VOLTAGE_TYPES:
85
+ band = abs(violation.violated_value - 1.0)
86
+ if band > 1e-9:
87
+ limit = band
88
+ if not limit:
89
+ return 0.0
90
+ return abs(violation.violation_value) / limit
91
+
92
+
93
+ class NetworkConstraints:
94
+ """
95
+ Defines network constraints and provides methods to check for violations.
96
+
97
+ This class encapsulates the logic for identifying voltage and thermal violations
98
+ based on configurable limits for different voltage levels and equipment types.
99
+
100
+ Attributes:
101
+ mv_max_voltage (float): Maximum allowable per unit voltage for medium voltage.
102
+ mv_min_voltage (float): Minimum allowable per unit voltage for medium voltage.
103
+ lv_max_voltage (float): Maximum allowable per unit voltage for low voltage.
104
+ lv_min_voltage (float): Minimum allowable per unit voltage for low voltage.
105
+ max_line_loading (float): Maximum allowable line loading in percentage.
106
+ max_transformer_loading (float): Maximum allowable transformer loading in percentage.
107
+ violations (List[ConstraintViolation]): List of identified violations.
108
+ logger (logging.Logger): Logger instance.
109
+ """
110
+
111
+ def __init__(
112
+ self,
113
+ mv_max_voltage=1.05,
114
+ mv_min_voltage=0.95,
115
+ lv_max_voltage=1.1,
116
+ lv_min_voltage=0.9,
117
+ max_line_loading=80.0,
118
+ max_transformer_loading=80.0,
119
+ ):
120
+ """
121
+ Initialize NetworkConstraints with configurable limits.
122
+
123
+ Args:
124
+ mv_max_voltage (float, optional): Medium voltage maximum allowable per unit voltage. Defaults to 1.05.
125
+ mv_min_voltage (float, optional): Medium voltage minimum allowable per unit voltage. Defaults to 0.95.
126
+ lv_max_voltage (float, optional): Low voltage maximum allowable per unit voltage. Defaults to 1.1.
127
+ lv_min_voltage (float, optional): Low voltage minimum allowable per unit voltage. Defaults to 0.9.
128
+ max_line_loading (float, optional): Maximum allowable line loading in percentage. Defaults to 80.0.
129
+ max_transformer_loading (float, optional): Maximum allowable transformer loading in percentage. Defaults to 80.0.
130
+ """
131
+ self.mv_max_voltage = mv_max_voltage
132
+ self.mv_min_voltage = mv_min_voltage
133
+ self.lv_max_voltage = lv_max_voltage
134
+ self.lv_min_voltage = lv_min_voltage
135
+ self.max_line_loading = max_line_loading
136
+ self.max_transformer_loading = max_transformer_loading
137
+ self.violations = []
138
+ self.logger = logger
139
+
140
+ def check_violations(
141
+ self,
142
+ bus_data,
143
+ line_data,
144
+ transformer_data,
145
+ ) -> list[ConstraintViolation]:
146
+ """
147
+ Check for violations of voltage, line loading, and transformer loading constraints.
148
+
149
+ Args:
150
+ bus_data (pandas.DataFrame): DataFrame with columns 'bus_id', 'vm_pu', and 'voltage_level'.
151
+ line_data (pandas.DataFrame): DataFrame with columns 'line_id' and 'loading_percent'.
152
+ transformer_data (pandas.DataFrame): DataFrame with columns 'transformer_id' and 'loading_percent'.
153
+
154
+ Returns:
155
+ List[ConstraintViolation]: A list of ConstraintViolation objects capturing all types of violations found.
156
+ """
157
+
158
+ self.violations = []
159
+ self.check_voltage_violations(bus_data)
160
+ self.check_line_loading_violations(line_data)
161
+ self.check_transformer_loading_violations(transformer_data)
162
+ self.logger.info(f"Found {len(self.violations)} constraint violations: ")
163
+ return self.violations
164
+
165
+ def check_voltage_violations(self, bus_data) -> list[ConstraintViolation]:
166
+ """
167
+ Check for voltage constraint violations in the network.
168
+
169
+ Args:
170
+ bus_data (pandas.DataFrame): DataFrame containing bus voltage data.
171
+ Expected format: DataFrame with columns 'bus_id', 'vm_pu', and 'voltage_level'
172
+ where 'voltage_level' indicates if it's 'mv' or 'lv'.
173
+
174
+ Returns:
175
+ List[ConstraintViolation]: List of ConstraintViolation objects for voltage violations.
176
+
177
+ Raises:
178
+ ValueError: If required columns are missing in bus_data.
179
+ """
180
+ if "bus_id" not in bus_data.columns or "vm_pu" not in bus_data.columns:
181
+ raise ValueError("bus_data must contain 'bus_id' and 'vm_pu' columns.")
182
+
183
+ if "voltage_level" not in bus_data.columns:
184
+ raise ValueError(
185
+ "bus_data must contain 'voltage_level' column to identify MV/LV nodes."
186
+ )
187
+
188
+ # Create masks for MV and LV buses
189
+ mv_buses = bus_data["voltage_level"] == "mv"
190
+ lv_buses = bus_data["voltage_level"] == "lv"
191
+
192
+ # Create masks for voltage violations based on bus type
193
+ over_voltage_mask = (mv_buses & (bus_data["vm_pu"] > self.mv_max_voltage)) | (
194
+ lv_buses & (bus_data["vm_pu"] > self.lv_max_voltage)
195
+ )
196
+
197
+ under_voltage_mask = (mv_buses & (bus_data["vm_pu"] < self.mv_min_voltage)) | (
198
+ lv_buses & (bus_data["vm_pu"] < self.lv_min_voltage)
199
+ )
200
+
201
+ voltage_violations = bus_data[over_voltage_mask | under_voltage_mask].copy()
202
+
203
+ if voltage_violations.empty:
204
+ return []
205
+
206
+ # Set default constraint type and violated values
207
+ voltage_violations["constraint_type"] = ConstraintType.UNDER_VOLTAGE
208
+ voltage_violations["violated_value"] = float(
209
+ "nan"
210
+ ) # Will be filled based on bus type
211
+ voltage_violations["violation_value"] = float(
212
+ "nan"
213
+ ) # Will be filled based on violation type and bus type
214
+
215
+ # For over-voltage violations
216
+ voltage_violations.loc[over_voltage_mask, "constraint_type"] = (
217
+ ConstraintType.OVER_VOLTAGE
218
+ )
219
+
220
+ # Set violated value and calculate violation value based on bus type and violation type
221
+ # For over-voltage violations - MV buses
222
+ mask = over_voltage_mask & mv_buses
223
+ voltage_violations.loc[mask, "violated_value"] = self.mv_max_voltage
224
+ voltage_violations.loc[mask, "violation_value"] = (
225
+ voltage_violations.loc[mask, "vm_pu"] - self.mv_max_voltage
226
+ )
227
+
228
+ # For over-voltage violations - LV buses
229
+ mask = over_voltage_mask & lv_buses
230
+ voltage_violations.loc[mask, "violated_value"] = self.lv_max_voltage
231
+ voltage_violations.loc[mask, "violation_value"] = (
232
+ voltage_violations.loc[mask, "vm_pu"] - self.lv_max_voltage
233
+ )
234
+
235
+ # For under-voltage violations - MV buses
236
+ mask = under_voltage_mask & mv_buses
237
+ voltage_violations.loc[mask, "violated_value"] = self.mv_min_voltage
238
+ voltage_violations.loc[mask, "violation_value"] = (
239
+ self.mv_min_voltage - voltage_violations.loc[mask, "vm_pu"]
240
+ )
241
+
242
+ # For under-voltage violations - LV buses
243
+ mask = under_voltage_mask & lv_buses
244
+ voltage_violations.loc[mask, "violated_value"] = self.lv_min_voltage
245
+ voltage_violations.loc[mask, "violation_value"] = (
246
+ self.lv_min_voltage - voltage_violations.loc[mask, "vm_pu"]
247
+ )
248
+
249
+ voltage_violations["element_type"] = "Bus"
250
+
251
+ voltage_violations = voltage_violations.rename(
252
+ columns={
253
+ "bus_id": "element_id",
254
+ "vm_pu": "absolute_value",
255
+ }
256
+ )
257
+
258
+ violations = [
259
+ ConstraintViolation(**violation_dict)
260
+ for violation_dict in voltage_violations[
261
+ [
262
+ "element_id",
263
+ "element_type",
264
+ "violated_value",
265
+ "absolute_value",
266
+ "constraint_type",
267
+ "violation_value",
268
+ ]
269
+ ].to_dict(orient="records")
270
+ ]
271
+
272
+ self.violations += violations
273
+
274
+ return violations
275
+
276
+ def check_line_loading_violations(self, branch_data) -> list[ConstraintViolation]:
277
+ """
278
+ Check for loading constraint violations in network branches.
279
+
280
+ Args:
281
+ branch_data (pandas.DataFrame): DataFrame containing branch loading data.
282
+ Expected format: DataFrame with columns 'line_id', 'loading_percent', and 'current'.
283
+
284
+ Returns:
285
+ List[ConstraintViolation]: List of ConstraintViolation objects for loading violations.
286
+ """
287
+ over_loading_mask = branch_data["loading_percent"] > self.max_line_loading
288
+
289
+ if over_loading_mask.sum() == 0:
290
+ return []
291
+
292
+ overloaded_lines = branch_data.loc[over_loading_mask].copy()
293
+ overloaded_lines["violation_value"] = (
294
+ overloaded_lines["loading_percent"] - self.max_line_loading
295
+ )
296
+ overloaded_lines["constraint_type"] = ConstraintType.LINE_LOADING
297
+ overloaded_lines["element_type"] = "Line"
298
+ overloaded_lines["violated_value"] = self.max_line_loading
299
+
300
+ overloaded_lines = overloaded_lines.rename(
301
+ columns={
302
+ "line_id": "element_id",
303
+ "current": "absolute_value",
304
+ }
305
+ )
306
+
307
+ violations = [
308
+ ConstraintViolation(**violation_dict)
309
+ for violation_dict in overloaded_lines[
310
+ [
311
+ "element_id",
312
+ "element_type",
313
+ "violated_value",
314
+ "absolute_value",
315
+ "constraint_type",
316
+ "violation_value",
317
+ ]
318
+ ].to_dict(orient="records")
319
+ ]
320
+
321
+ self.violations += violations
322
+
323
+ return violations
324
+
325
+ def check_transformer_loading_violations(
326
+ self, transformer_data
327
+ ) -> list[ConstraintViolation]:
328
+ """
329
+ Check for loading constraint violations in transformers.
330
+
331
+ Args:
332
+ transformer_data (pandas.DataFrame): DataFrame containing transformer loading data.
333
+ Expected format: DataFrame with columns 'transformer_id', 'loading_percent', and 'power'.
334
+
335
+ Returns:
336
+ List[ConstraintViolation]: List of ConstraintViolation objects for loading violations.
337
+ """
338
+ over_loading_mask = (
339
+ transformer_data["loading_percent"] > self.max_transformer_loading
340
+ )
341
+
342
+ if over_loading_mask.sum() == 0:
343
+ return []
344
+
345
+ violation_data = transformer_data.loc[over_loading_mask].copy()
346
+ violation_data["violation_value"] = (
347
+ violation_data["loading_percent"] - self.max_transformer_loading
348
+ )
349
+ violation_data["constraint_type"] = ConstraintType.TRANSFORMER_LOADING
350
+ violation_data["element_type"] = "Transformer"
351
+ violation_data["violated_value"] = self.max_transformer_loading
352
+
353
+ violation_data = violation_data.rename(
354
+ columns={
355
+ "transformer_id": "element_id",
356
+ "power": "absolute_value",
357
+ }
358
+ )
359
+
360
+ violations = [
361
+ ConstraintViolation(**violation_dict)
362
+ for violation_dict in violation_data[
363
+ [
364
+ "element_id",
365
+ "element_type",
366
+ "violated_value",
367
+ "absolute_value",
368
+ "constraint_type",
369
+ "violation_value",
370
+ ]
371
+ ].to_dict(orient="records")
372
+ ]
373
+
374
+ self.violations += violations
375
+
376
+ return violations
377
+
378
+ def violations_to_dataframe(self):
379
+ """
380
+ Convert the list of ConstraintViolation objects in self.violations to a pandas DataFrame.
381
+
382
+ Returns:
383
+ pandas.DataFrame: DataFrame containing the ConstraintViolation data.
384
+ """
385
+ import pandas as pd
386
+
387
+ return pd.DataFrame(
388
+ [
389
+ {
390
+ "element_id": v.element_id,
391
+ "element_type": v.element_type,
392
+ "violated_value": v.violated_value,
393
+ "constraint_type": v.constraint_type.value,
394
+ "violation_value": v.violation_value,
395
+ }
396
+ for v in self.violations
397
+ ]
398
+ )
399
+
400
+ def analyze_violations(self) -> dict[str, Any]:
401
+ """
402
+ Analyze a list of violations and return statistics.
403
+
404
+ Returns:
405
+ pandas.DataFrame: DataFrame containing summary statistics of violations grouped by constraint type.
406
+ """
407
+
408
+ violation_df = self.violations_to_dataframe()
409
+
410
+ # Group violations by constraint_type and calculate statistics
411
+ summary_df = (
412
+ violation_df.groupby("constraint_type")["violation_value"]
413
+ .agg(
414
+ max_violation="max",
415
+ average_violation="mean",
416
+ number_of_violations="count",
417
+ )
418
+ .reset_index()
419
+ )
420
+
421
+ return summary_df