Skip to content

Commit

Permalink
Merge pull request #331 from openego/features/remove_one_meter_lines
Browse files Browse the repository at this point in the history
Remove one meter lines
  • Loading branch information
birgits committed Nov 4, 2022
2 parents ebe4e78 + 8c11f9c commit 99af64b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 7 deletions.
3 changes: 3 additions & 0 deletions edisgo/edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,9 @@ def import_edisgo_from_files(
"""

if not os.path.exists(edisgo_path):
raise ValueError("Given edisgo_path does not exist.")

if not from_zip_archive and str(edisgo_path).endswith(".zip"):
from_zip_archive = True
logging.info("Given path is a zip archive. Setting 'from_zip_archive' to True.")
Expand Down
74 changes: 74 additions & 0 deletions edisgo/io/ding0_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pypsa import Network as PyPSANetwork

from edisgo.network.components import Switch
from edisgo.network.grids import MVGrid

if "READTHEDOCS" not in os.environ:
Expand Down Expand Up @@ -66,6 +67,75 @@ def sort_hvmv_transformer_buses(transformers_df):

return transformers_df

def remove_1m_lines_from_edisgo(edisgo):
"""
Method to remove 1m lines to reduce size of edisgo object.
"""

# close switches such that lines with connected switches are not removed
switches = [
Switch(id=_, topology=edisgo.topology)
for _ in edisgo.topology.switches_df.index
]
switch_status = {}
for switch in switches:
switch_status[switch] = switch.state
switch.close()

# get all lines with length of one meter and remove the ones that are end lines
number_of_lines_removed = 0
lines = edisgo.topology.lines_df.loc[edisgo.topology.lines_df.length == 0.001]
for name, line in lines.iterrows():
number_of_lines_removed += remove_1m_end_line(edisgo, line)
logger.debug(f"Removed {number_of_lines_removed} 1 m end lines.")

# set switches back to original state
for switch in switches:
if switch_status[switch] == "open":
switch.open()
return edisgo

def remove_1m_end_line(edisgo, line):
"""
Method that removes end lines and moves components of end bus to neighboring
bus. If the line is not an end line, the method will skip this line.
Returns
-------
int
Number of removed lines. Either 0, if no line was removed, or 1, if line
was removed.
"""
# check for end buses
if len(edisgo.topology.get_connected_lines_from_bus(line.bus1)) == 1:
end_bus = "bus1"
neighbor_bus = "bus0"
elif len(edisgo.topology.get_connected_lines_from_bus(line.bus0)) == 1:
end_bus = "bus0"
neighbor_bus = "bus1"
else:
return 0

# move connected elements of end bus to the other bus
connected_elements = edisgo.topology.get_connected_components_from_bus(
line[end_bus]
)
rename_dict = {line[end_bus]: line[neighbor_bus]}
for comp_type, components in connected_elements.items():
if not components.empty and comp_type != "lines":
setattr(
edisgo.topology,
comp_type.lower() + "_df",
getattr(edisgo.topology, comp_type.lower() + "_df").replace(
rename_dict
),
)

# remove line
edisgo.topology.remove_line(line.name)
return 1

grid = PyPSANetwork()
grid.import_from_csv_folder(path)

Expand Down Expand Up @@ -118,5 +188,9 @@ def sort_hvmv_transformer_buses(transformers_df):
mv_grid_id = list(set(grid.buses.mv_grid_id))[0]
edisgo_obj.topology.mv_grid = MVGrid(id=mv_grid_id, edisgo_obj=edisgo_obj)

# remove 1 m end lines
# ToDo Remove once fixed in ding0
remove_1m_lines_from_edisgo(edisgo_obj)

# Check data integrity
edisgo_obj.topology.check_integrity()
2 changes: 2 additions & 0 deletions tests/data/ding0_test_network_1/buses.csv
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Bus_BranchTee_LVGrid_5_3,0.4,,,1,5,FALSE
Bus_BranchTee_LVGrid_5_4,0.4,,,1,5,TRUE
Bus_BranchTee_LVGrid_5_5,0.4,,,1,5,FALSE
Bus_BranchTee_LVGrid_5_6,0.4,,,1,5,TRUE
Bus_GeneratorFluctuating_19,0.4,,,1,5,TRUE
Bus_Load_residential_LVGrid_5_3,0.4,,,1,5,TRUE
Bus_BranchTee_LVGrid_6_1,0.4,,,1,6,FALSE
Bus_BranchTee_LVGrid_6_2,0.4,,,1,6,TRUE
Bus_BranchTee_LVGrid_7_1,0.4,,,1,7,FALSE
Expand Down
2 changes: 1 addition & 1 deletion tests/data/ding0_test_network_1/generators.csv
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GeneratorFluctuating_15,Bus_BranchTee_LVGrid_2_4,PQ,0.006,solar,1122075,solar_so
GeneratorFluctuating_16,Bus_GeneratorFluctuating_16,PQ,0.03,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_17,Bus_BranchTee_LVGrid_4_2,PQ,0.055,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_18,Bus_BranchTee_LVGrid_4_2,PQ,0.01,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_19,Bus_BranchTee_LVGrid_5_6,PQ,0.023,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_19,Bus_GeneratorFluctuating_19,PQ,0.023,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_20,Bus_BranchTee_LVGrid_7_6,PQ,0.005,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_21,Bus_BranchTee_LVGrid_7_6,PQ,0.036,solar,1122075,solar_solar_roof_mounted
GeneratorFluctuating_22,Bus_BranchTee_LVGrid_7_4,PQ,0.008,solar,1122075,solar_solar_roof_mounted
Expand Down
8 changes: 5 additions & 3 deletions tests/data/ding0_test_network_1/lines.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Line_10016,Bus_BranchTee_MVGrid_1_3,virtual_BusBar_MVGrid_1_LVGrid_9_MV,0.661777
Line_10017,Bus_BranchTee_MVGrid_1_6,Bus_BranchTee_MVGrid_1_7,0.690714941884979,0.256053508483765,0.255564528497442,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10018,Bus_GeneratorFluctuating_7,Bus_BranchTee_MVGrid_1_7,0.468278333841994,0.173594493265127,0.173262983521538,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10019,Bus_BranchTee_MVGrid_1_8,Bus_BranchTee_MVGrid_1_7,0.403852029,0.14971115095836,0.14942525073,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10020,Bus_BranchTee_MVGrid_1_8,Bus_GeneratorFluctuating_5,0.001,0.000370707933124,0.00037,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10020,Bus_BranchTee_MVGrid_1_8,Bus_GeneratorFluctuating_5,0.0012,0.000370707933124,0.00037,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10021,Bus_BranchTee_MVGrid_1_8,BusBar_MVGrid_1_LVGrid_8_MV,1.378502271,0.511021727688593,0.51004584027,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10022,Bus_BranchTee_MVGrid_1_9,BusBar_MVGrid_1_LVGrid_8_MV,0.437506744572693,0.162187221008176,0.161877495491896,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10023,Bus_BranchTee_MVGrid_1_9,BusBar_MVGrid_1_LVGrid_3_MV,0.251147233966026,0.093102272013254,0.09292447656743,7.27461339178928,1,cable,48-AL1/8-ST1A
Expand All @@ -28,7 +28,7 @@ Line_10028,Bus_BranchTee_MVGrid_1_11,Bus_BranchTee_MVGrid_1_10,0.502639122266729
Line_10029,Bus_BranchTee_MVGrid_1_11,Bus_GeneratorFluctuating_3,0.203355255541373,0.075385406471563,0.075241444550308,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10030,Bus_BranchTee_MVGrid_1_11,BusBar_MVGrid_1_LVGrid_4_MV,0.262336104334286,0.097250075021459,0.097064358603686,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10031,Bus_BranchTee_MVGrid_1_9,virtual_BusBar_MVGrid_1_LVGrid_4_MV,0.543380191002877,0.201435347506981,0.201050670671064,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10032,BusBar_MVGrid_1_LVGrid_4_MV,Bus_GeneratorFluctuating_8,0.001,0.000370707933124,0.00037,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10032,BusBar_MVGrid_1_LVGrid_4_MV,Bus_GeneratorFluctuating_8,0.0012,0.000370707933124,0.00037,7.27461339178928,1,cable,48-AL1/8-ST1A
Line_10000007,Bus_BranchTee_LVGrid_1_9,Bus_BranchTee_LVGrid_1_10,0.033,0.002799159054349,0.014817,0.099766126515967,1,cable,NAYY 4x1x50
Line_10000009,Bus_BranchTee_LVGrid_1_11,Bus_BranchTee_LVGrid_1_12,0.018,0.001526814029645,0.008082,0.099766126515967,1,cable,NAYY 4x1x50
Line_10000010,Bus_BranchTee_LVGrid_1_11,Bus_BranchTee_LVGrid_1_13,0.078,0.006273132210688,0.016068,0.190525588832576,1,cable,NAYY 4x1x150
Expand All @@ -55,15 +55,17 @@ Line_30000007,Bus_BranchTee_LVGrid_3_5,Bus_BranchTee_LVGrid_3_6,0.018,0.00152681
Line_30000008,Bus_BranchTee_LVGrid_3_5,Bus_BranchTee_LVGrid_3_7,0.175,0.014074335088082,0.03605,0.190525588832576,1,cable,NAYY 4x1x150
Line_30000009,BusBar_MVGrid_1_LVGrid_3_LV,Bus_BranchTee_LVGrid_3_5,0.175,0.014074335088082,0.03605,0.190525588832576,1,cable,NAYY 4x1x150
Line_30000011,Bus_BranchTee_LVGrid_3_7,Bus_BranchTee_LVGrid_3_8,0.033,0.002799159054349,0.014817,0.099766126515967,1,cable,NAYY 4x1x50
Line_40000001,BusBar_MVGrid_1_LVGrid_4_LV,Bus_GeneratorFluctuating_16,0.001,8.51371609122834E-05,0.000868,0.085216899732389,1,cable,NAYY 4x1x35
Line_40000001,BusBar_MVGrid_1_LVGrid_4_LV,Bus_GeneratorFluctuating_16,0.0012,8.51371609122834E-05,0.000868,0.085216899732389,1,cable,NAYY 4x1x35
Line_40000004,Bus_BranchTee_LVGrid_4_1,Bus_BranchTee_LVGrid_4_2,0.018,0.001526814029645,0.008082,0.099766126515967,1,cable,NAYY 4x1x50
Line_40000005,BusBar_MVGrid_1_LVGrid_4_LV,Bus_BranchTee_LVGrid_4_1,0.135,0.010857344210806,0.02781,0.190525588832576,1,cable,NAYY 4x1x150
Line_50000001,Bus_GeneratorFluctuating_19,Bus_BranchTee_LVGrid_5_6,0.001,0.002554114827369,0.02604,0.085216899732389,1,cable,NAYY 4x1x35
Line_50000002,Bus_BranchTee_LVGrid_5_1,Bus_BranchTee_LVGrid_5_2,0.03,0.002554114827369,0.02604,0.085216899732389,1,cable,NAYY 4x1x35
Line_50000003,BusBar_MVGrid_1_LVGrid_5_LV,Bus_BranchTee_LVGrid_5_1,0.000561797752809,4.92418174354805E-05,5.61797752808989E-05,0.290291715348544,1,cable,NAYY 4x1x300
Line_50000005,Bus_BranchTee_LVGrid_5_3,Bus_BranchTee_LVGrid_5_4,0.03,0.002554114827369,0.02604,0.085216899732389,1,cable,NAYY 4x1x35
Line_50000006,BusBar_MVGrid_1_LVGrid_5_LV,Bus_BranchTee_LVGrid_5_3,0.2,0.015959290680236,0.025,0.252186597582029,1,cable,NAYY 4x1x240
Line_50000008,Bus_BranchTee_LVGrid_5_5,Bus_BranchTee_LVGrid_5_6,0.018,0.001526814029645,0.008082,0.099766126515967,1,cable,NAYY 4x1x50
Line_50000009,BusBar_MVGrid_1_LVGrid_5_LV,Bus_BranchTee_LVGrid_5_5,0.135,0.010857344210806,0.02781,0.190525588832576,1,cable,NAYY 4x1x150
Line_500000010,Bus_Load_residential_LVGrid_5_3,Bus_BranchTee_LVGrid_5_6,0.001,0.002554114827369,0.02604,0.085216899732389,1,cable,NAYY 4x1x35
Line_60000001,Bus_BranchTee_LVGrid_6_1,Bus_BranchTee_LVGrid_6_2,0.03,0.002554114827369,0.02604,0.085216899732389,1,cable,NAYY 4x1x35
Line_60000002,BusBar_MVGrid_1_LVGrid_6_LV,Bus_BranchTee_LVGrid_6_1,0.05,0.004382521751758,0.005,0.290291715348544,1,cable,NAYY 4x1x300
Line_70000002,Bus_BranchTee_LVGrid_7_1,Bus_BranchTee_LVGrid_7_2,0.03,0.002544690049408,0.01347,0.099766126515967,1,cable,NAYY 4x1x50
Expand Down
2 changes: 1 addition & 1 deletion tests/data/ding0_test_network_1/loads.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Load_residential_LVGrid_3_4,Bus_BranchTee_LVGrid_3_8,0.001209,residential,4.3
Load_residential_LVGrid_4_1,Bus_BranchTee_LVGrid_4_2,0.001354,residential,6.5
Load_industrial_LVGrid_5_1,Bus_BranchTee_LVGrid_5_2,0.07992,industrial,100
Load_agricultural_LVGrid_5_2,Bus_BranchTee_LVGrid_5_4,0.06196,agricultural,450
Load_residential_LVGrid_5_3,Bus_BranchTee_LVGrid_5_6,0.001354,residential,4.7
Load_residential_LVGrid_5_3,Bus_Load_residential_LVGrid_5_3,0.001354,residential,4.7
Load_industrial_LVGrid_6_1,Bus_BranchTee_LVGrid_6_2,0.07992,industrial,580
Load_agricultural_LVGrid_7_1,Bus_BranchTee_LVGrid_7_2,0.06196,agricultural,630
Load_residential_LVGrid_7_2,Bus_BranchTee_LVGrid_7_4,0.001164,residential,5.8
Expand Down
10 changes: 8 additions & 2 deletions tests/io/test_ding0_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import pytest
import shapely

Expand All @@ -10,11 +12,14 @@ class TestImportFromDing0:
@classmethod
def setup_class(self):
self.topology = Topology()
ding0_import.import_ding0_grid(pytest.ding0_test_network_path, self)

def test_import_ding0_grid(self):
def test_import_ding0_grid(self, caplog):
"""Test successful import of ding0 network."""

with caplog.at_level(logging.DEBUG):
ding0_import.import_ding0_grid(pytest.ding0_test_network_path, self)
assert "Removed 2 1 m end lines." in caplog.text

# buses, generators, loads, lines, transformers dataframes
# check number of imported components
assert self.topology.buses_df.shape[0] == 140
Expand Down Expand Up @@ -42,6 +47,7 @@ def test_path_error(self):
ding0_import.import_ding0_grid("wrong_directory", self.topology)

def test_transformer_buses(self):
ding0_import.import_ding0_grid(pytest.ding0_test_network_path, self)
assert (
self.topology.buses_df.loc[self.topology.transformers_df.bus1].v_nom.values
< self.topology.buses_df.loc[
Expand Down
6 changes: 6 additions & 0 deletions tests/test_edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,12 @@ def test_import_edisgo_from_files(self):
}
edisgo_obj.electromobility.flexibility_bands = flex_bands

# ################ test with non-existing path ######################

msg = "Given edisgo_path does not exist."
with pytest.raises(ValueError, match=msg):
import_edisgo_from_files("dummy_dir")

# ######################## test with default ########################
edisgo_obj.save(
save_dir, save_results=False, save_electromobility=True, save_heatpump=True
Expand Down

0 comments on commit 99af64b

Please sign in to comment.