From f5fd20f42eaef4d0333ee9f665e4337558e65183 Mon Sep 17 00:00:00 2001 From: mltja Date: Tue, 4 Apr 2023 09:52:49 +0200 Subject: [PATCH] Add mode to topology.assign_feeder and change tests --- edisgo/network/topology.py | 20 +++++--- tests/network/test_topology.py | 22 ++++++--- tests/tools/test_tools.py | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 12 deletions(-) diff --git a/edisgo/network/topology.py b/edisgo/network/topology.py index a987efd7..433e88dd 100755 --- a/edisgo/network/topology.py +++ b/edisgo/network/topology.py @@ -3012,13 +3012,21 @@ def check_integrity(self): f"optimisation." ) - def assign_feeders(self): - """Assign the feeder of the grid and the mv feeder to all grids.""" + def assign_feeders(self, mode: str = "grid_feeder"): + """Assign the feeder of the grid or the mv feeder to all grids. - for grid in self.grids: - grid.assign_grid_feeder(mode="grid_feeder") - - self.grids[0].assign_grid_feeder(mode="mv_feeder") + Parameters + ---------- + mode : str + Select 'grid_feeder' or 'mv_feeder' to assign the feeders. + """ + if mode == "grid_feeder": + for grid in self.grids: + grid.assign_grid_feeder(mode="grid_feeder") + elif mode == "mv_feeder": + self.grids[0].assign_grid_feeder(mode="mv_feeder") + else: + raise ValueError(f"Invalid mode '{mode}'!") def __repr__(self): return f"Network topology {self.id}" diff --git a/tests/network/test_topology.py b/tests/network/test_topology.py index 01439e6d..06d21fdc 100644 --- a/tests/network/test_topology.py +++ b/tests/network/test_topology.py @@ -876,25 +876,35 @@ def test_to_csv(self): shutil.rmtree(dir) def test_assign_feeders(self): - self.topology.assign_feeders() - assert self.topology.buses_df.iloc[0:2]["grid_feeder"].to_list() == [ + # Test mode 'grid_feeder' + self.topology.assign_feeders(mode="grid_feeder") + assert self.topology.buses_df.loc[ + ["Bus_MVStation_1", "Bus_Generator_1"], "grid_feeder" + ].to_list() == [ "station_node", "Bus_BranchTee_MVGrid_1_1", ] - assert self.topology.lines_df.iloc[0:2]["grid_feeder"].to_list() == [ + assert self.topology.lines_df.loc[ + ["Line_10003", "Line_10004"], "grid_feeder" + ].to_list() == [ "Bus_BranchTee_MVGrid_1_1", "Bus_BranchTee_MVGrid_1_4", ] - assert self.topology.buses_df.iloc[0:2]["mv_feeder"].to_list() == [ + # test mode 'mv_feeder' + self.topology.assign_feeders(mode="mv_feeder") + assert self.topology.buses_df.loc[ + ["Bus_MVStation_1", "Bus_Generator_1"], "mv_feeder" + ].to_list() == [ "station_node", "Bus_BranchTee_MVGrid_1_1", ] - assert self.topology.lines_df.iloc[0:2]["mv_feeder"].to_list() == [ + assert self.topology.lines_df.loc[ + ["Line_10003", "Line_10004"], "mv_feeder" + ].to_list() == [ "Bus_BranchTee_MVGrid_1_1", "Bus_BranchTee_MVGrid_1_4", ] - lv_grids_mv_bus = self.topology.grids[2].transformers_df["bus0"][0] feeder_of_lv_grids_mv_bus = self.topology.buses_df.loc[ lv_grids_mv_bus, "mv_feeder" diff --git a/tests/tools/test_tools.py b/tests/tools/test_tools.py index 7d8fd6c4..c53b5b80 100644 --- a/tests/tools/test_tools.py +++ b/tests/tools/test_tools.py @@ -143,6 +143,95 @@ def test_select_cable(self): assert cable_data.name == "NAYY 4x1x150" assert num_parallel_cables == 1 + def test_assign_feeder(self): + + # ######## test MV feeder mode ######## + topo = self.edisgo.topology + topo.assign_feeders(mode="mv_feeder") + + # check that all lines and all buses (except MV station bus and buses + # in aggregated load areas) have an MV feeder assigned + assert not topo.lines_df.mv_feeder.isna().any() + mv_station = topo.mv_grid.station.index[0] + buses_aggr_la = list( + topo.transformers_df[topo.transformers_df.bus0 == mv_station].bus1.unique() + ) + buses_aggr_la.append(mv_station) + assert ( + not topo.buses_df[~topo.buses_df.index.isin(buses_aggr_la)] + .mv_feeder.isna() + .any() + ) + + # check specific buses + # MV and LV bus in feeder 1 + assert ( + topo.buses_df.at["Bus_GeneratorFluctuating_7", "mv_feeder"] + == "Bus_BranchTee_MVGrid_1_6" + ) + assert ( + topo.buses_df.at["Bus_BranchTee_LVGrid_4_1", "mv_feeder"] + == "Bus_BranchTee_MVGrid_1_5" + ) + # MV bus in feeder 2 + assert ( + topo.buses_df.at["Bus_GeneratorFluctuating_3", "mv_feeder"] + == "Bus_BranchTee_MVGrid_1_5" + ) + + # check specific lines + assert topo.lines_df.at["Line_10003", "mv_feeder"] == "Bus_BranchTee_MVGrid_1_1" + + # ######## test LV feeder mode ######## + topo = self.edisgo.topology + topo.assign_feeders(mode="grid_feeder") + + # check that all buses and lines have a grid feeder assigned + assert not topo.lines_df.grid_feeder.isna().any() + assert not topo.buses_df.grid_feeder.isna().any() + + # check specific buses + assert ( + topo.buses_df.at["Bus_BranchTee_LVGrid_1_8", "grid_feeder"] + == "Bus_BranchTee_LVGrid_1_7" + ) + assert ( + topo.buses_df.at["Bus_BranchTee_LVGrid_2_4", "grid_feeder"] + == "Bus_BranchTee_LVGrid_2_1" + ) + + # check specific lines + assert ( + topo.lines_df.at["Line_30000005", "grid_feeder"] + == "Bus_BranchTee_LVGrid_3_3" + ) + assert ( + topo.lines_df.at["Line_40000001", "grid_feeder"] + == "Bus_GeneratorFluctuating_16" + ) + + # ######## test real ding0 network ######## + self.edisgo = EDisGo( + ding0_grid=pytest.ding0_test_network_2_path, + worst_case_analysis="worst-case", + ) + topo = self.edisgo.topology + topo.assign_feeders(mode="mv_feeder") + + # check that all lines and all buses have an MV feeder assigned + assert not topo.lines_df.mv_feeder.isna().any() + mv_station = topo.mv_grid.station.index[0] + buses_aggr_la = list( + topo.transformers_df[topo.transformers_df.bus0 == mv_station].bus1.unique() + ) + buses_aggr_la.append(mv_station) + assert not topo.buses_df.mv_feeder.isna().any() + + topo.assign_feeders(mode="grid_feeder") + # check that all buses and lines grid feeder assigned + assert not topo.lines_df.grid_feeder.isna().any() + assert not topo.buses_df.grid_feeder.isna().any() + def test_get_downstream_buses(self): # ######## test with LV bus ########