Skip to content

Commit

Permalink
move find_meshes to topology.py
Browse files Browse the repository at this point in the history
  • Loading branch information
joda9 committed Aug 15, 2024
1 parent 28fb71c commit bfe8d13
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
29 changes: 29 additions & 0 deletions edisgo/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,9 @@ def check_integrity(self):
f"optimisation."
)

# check for meshed grid
self.find_meshes()

def assign_feeders(self, mode: str = "grid_feeder"):
"""
Assigns MV or LV feeder to each bus and line, depending on the `mode`.
Expand Down Expand Up @@ -3160,3 +3163,29 @@ def aggregate_lv_grid_at_station(self, lv_grid_id: int | str) -> None:

def __repr__(self):
return f"Network topology {self.id}"

def find_meshes(edisgo_obj) -> list[list[int]] | None:
"""
Find all meshes in the grid.
Parameters
----------
edisgo_obj : EDisGo
EDisGo object.
Returns
-------
Optional[List[List[int]]]
List of all meshes in the grid.
Each mesh is represented as a list of node indices.
If no meshes are found, None is returned.
"""
meshes = nx.cycle_basis(edisgo_obj.to_graph())
if meshes:
logger.warning(
"Grid contains mesh(es). This might cause problems in "
"the power flow or optimisation."
)
return meshes
else:
return None
33 changes: 3 additions & 30 deletions edisgo/opf/powermodels_opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,17 @@
import subprocess
import sys

from typing import List, Optional
from typing import Optional

import networkx as nx
import numpy as np

from edisgo.flex_opt import exceptions
from edisgo.io.powermodels_io import from_powermodels
from edisgo.network.topology import Topology

logger = logging.getLogger(__name__)


def find_meshes(edisgo_obj) -> Optional[List[List[int]]]:
"""
Find all meshes in the grid.
Parameters
----------
edisgo_obj : EDisGo
EDisGo object.
Returns
-------
Optional[List[List[int]]]
List of all meshes in the grid.
Each mesh is represented as a list of node indices.
If no meshes are found, None is returned.
"""
meshes = nx.cycle_basis(edisgo_obj.to_graph())
if meshes:
logger.warning(
"Grid contains mesh(es). This might cause problems in "
"the power flow or optimisation."
)
return meshes
else:
return None


def pm_optimize(
edisgo_obj,
s_base: int = 1,
Expand Down Expand Up @@ -135,7 +108,7 @@ def pm_optimize(
Default: True.
"""
find_meshes(edisgo_obj)
Topology.find_meshes(edisgo_obj)
opf_dir = os.path.dirname(os.path.abspath(__file__))
solution_dir = os.path.join(opf_dir, "opf_solutions")
pm, hv_flex_dict = edisgo_obj.to_powermodels(
Expand Down
25 changes: 25 additions & 0 deletions tests/network/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,3 +1909,28 @@ def test_check_integrity(self, caplog):
assert "There are lines with very short line lengths" in caplog.text
assert "Very small values for impedance of lines" and line in caplog.text
caplog.clear()

def test_find_meshes(self, caplog: pytest.LogCaptureFixture):
meshes = Topology.find_meshes(self.edisgo)
assert not meshes
self.edisgo.topology.add_line(
"Bus_GeneratorFluctuating_2",
"Bus_GeneratorFluctuating_6",
0.1,
x=0.1,
r=0.1,
)
meshes = Topology.find_meshes(self.edisgo)
assert len(meshes) == 1
assert "Bus_GeneratorFluctuating_2" in meshes[0]
assert "Bus_GeneratorFluctuating_6" in meshes[0]
self.edisgo.topology.add_line(
"Bus_BranchTee_LVGrid_2_3", "Bus_BranchTee_LVGrid_3_3", 0.1, x=0.1, r=0.1
)
meshes = Topology.find_meshes(self.edisgo)
assert len(meshes) == 2
assert "Bus_BranchTee_LVGrid_2_3" in meshes[1]
assert (
"Grid contains mesh(es). This might cause problems"
" in the power flow or optimisation." in caplog.text
)
27 changes: 1 addition & 26 deletions tests/opf/test_powermodels_opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from edisgo import EDisGo
from edisgo.opf.powermodels_opf import find_meshes, pm_optimize
from edisgo.opf.powermodels_opf import pm_optimize
from edisgo.tools.tools import aggregate_district_heating_components


Expand Down Expand Up @@ -337,28 +337,3 @@ def test_pm_optimize(self):
)
)
)

def test_find_meshes(self, caplog: pytest.LogCaptureFixture):
meshes = find_meshes(self.edisgo)
assert not meshes
self.edisgo.topology.add_line(
"Bus_GeneratorFluctuating_2",
"Bus_GeneratorFluctuating_6",
0.1,
x=0.1,
r=0.1,
)
meshes = find_meshes(self.edisgo)
assert len(meshes) == 1
assert "Bus_GeneratorFluctuating_2" in meshes[0]
assert "Bus_GeneratorFluctuating_6" in meshes[0]
self.edisgo.topology.add_line(
"Bus_BranchTee_LVGrid_2_3", "Bus_BranchTee_LVGrid_3_3", 0.1, x=0.1, r=0.1
)
meshes = find_meshes(self.edisgo)
assert len(meshes) == 2
assert "Bus_BranchTee_LVGrid_2_3" in meshes[1]
assert (
"Grid contains mesh(es). This might cause problems"
" in the power flow or optimisation." in caplog.text
)

0 comments on commit bfe8d13

Please sign in to comment.