Skip to content

Commit

Permalink
Merge pull request #403 from openego/fix/#402_quickfix_multiple_subgrids
Browse files Browse the repository at this point in the history
Fix/#402 quickfix multiple subgrids
  • Loading branch information
nesnoj authored Jul 24, 2024
2 parents 656f85b + cb3dc00 commit 97eb137
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pytest-parallel
sphinx
sphinx_rtd_theme
Cython
pymetis
pymetis
pyvis
47 changes: 47 additions & 0 deletions ding0/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from geoalchemy2.shape import from_shape
import subprocess
import json
from pyvis.network import Network as pynet

if not 'READTHEDOCS' in os.environ:
from shapely.wkt import loads as wkt_loads
Expand Down Expand Up @@ -389,6 +390,52 @@ def run_ding0(
logger.info("STEP 9: Open all switch disconnectors in MV grid")
self.control_circuit_breakers(mode='open')

# ==============================================
# Quickfix for https://github.com/openego/ding0/issues/402
# Removes all nodes belonging to subgraphs
for grid_district in self.mv_grid_districts():
subgraphs = [s for s in nx.connected_components(grid_district.mv_grid.graph) if len(s) > 1]
subgraph_max_len = 0
for s in subgraphs:
if len(s) > subgraph_max_len:
subgraph_max_len = len(s)

# Do we have subgraphs?
if len(subgraphs) > 1:
# Log
errstr = ", ".join([f"Grid {_ + 1} ({len(s)} nodes)" for _, s in enumerate(subgraphs)])
logger.error(
f"Grid in {str(grid_district)} has {len(subgraphs)} subgrids (cf. issue #402): {errstr}. "
f"Only the largest graph will be retained."
)

# Plot with pyvis for debugging
net = pynet(
directed=False,
select_menu=True,
filter_menu=True,
)
net.show_buttons()
graph_relabeled = grid_district.mv_grid.graph.copy()
graph_relabeled = nx.relabel_nodes(
graph_relabeled,
dict(zip(
graph_relabeled.nodes(),
[str(n) for n in graph_relabeled.nodes()]
))
)
for n1, n2, d in graph_relabeled.edges(data=True):
d.pop("branch", None)
net.from_nx(graph_relabeled)
net.write_html(f"{str(grid_district)}_graph_debug.html", notebook=False)

# Remove all nodes belonging to isolated subgraphs
for component in subgraphs:
if len(component) < subgraph_max_len:
for node in component:
grid_district.mv_grid.graph.remove_node(node)
# ==============================================

logger.info("STEP 10: Do power flow analysis of MV grid")
self.run_powerflow(session, method='onthefly', export_pypsa=False, debug=debug)
if export_mv_figures:
Expand Down
14 changes: 14 additions & 0 deletions ding0/grid/mv_grid/urban_mv_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ def mv_urban_connect(mv_grid, osm_graph_red, core_graph, stub_graph, stub_dict,
routed_graph_node_set = routed_graph_node_set - forbidden_object_set
root_nodes_to_remove = []

# ==============================================
# Detect problem with multiple subgraphs, see https://github.com/openego/ding0/issues/402
from collections import Counter
stub_root_nodes = [s["root"] for s in stub_dict.values()]
if len(stub_root_nodes) > len(set(stub_root_nodes)):
duplicated_root_nodes = {r: c for r, c in Counter(stub_root_nodes).items() if c > 1}
for r, c in duplicated_root_nodes.items():
stubs = [s["comp"] for s in stub_dict.values() if s["root"] == r]
logger.error(
f"Multiple stubs share the same root node, this is likely to result in multiple graphs "
f"(cf. issue #402): Root: {r}, Count: {c}, Stubs: {stubs}"
)
# ==============================================

for key, stub_data in stub_dict.items():

# get root_node, loads (mv_station or mv_load) and cable distributor nodes in osm stub graph
Expand Down

0 comments on commit 97eb137

Please sign in to comment.