From 9f00fa701b76c1d59ba0bd3b0fbef620b4674528 Mon Sep 17 00:00:00 2001 From: joda9 Date: Mon, 22 Jul 2024 14:26:17 +0200 Subject: [PATCH] Add warning if meshes are in the grid --- edisgo/opf/powermodels_opf.py | 28 ++++++++++++++++++++++++++++ tests/opf/test_powermodels_opf.py | 27 ++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/edisgo/opf/powermodels_opf.py b/edisgo/opf/powermodels_opf.py index db4925d3b..2d082f0cf 100644 --- a/edisgo/opf/powermodels_opf.py +++ b/edisgo/opf/powermodels_opf.py @@ -4,6 +4,7 @@ import subprocess import sys +import networkx as nx import numpy as np from edisgo.flex_opt import exceptions @@ -12,6 +13,32 @@ logger = logging.getLogger(__name__) +def find_meshes(edisgo_obj) -> list: + """ + Find all meshes in the grid. + + Parameters + ---------- + edisgo_obj : :class:`~edisgo.EDisGo` + EDisGo object. + + Returns + ------- + meshes : list + List of all meshes in the grid. + + """ + 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=1, @@ -105,6 +132,7 @@ def pm_optimize( Default: True. """ + 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( diff --git a/tests/opf/test_powermodels_opf.py b/tests/opf/test_powermodels_opf.py index 4f6482f97..931a7cb6b 100644 --- a/tests/opf/test_powermodels_opf.py +++ b/tests/opf/test_powermodels_opf.py @@ -3,7 +3,7 @@ import pytest from edisgo import EDisGo -from edisgo.opf.powermodels_opf import pm_optimize +from edisgo.opf.powermodels_opf import find_meshes, pm_optimize from edisgo.tools.tools import aggregate_district_heating_components @@ -337,3 +337,28 @@ def test_pm_optimize(self): ) ) ) + + def test_find_meshes(self, caplog): + 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 + )