From 7728aec358ecae657e138891176393b5dc62ef43 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 31 Oct 2023 11:32:11 +0100 Subject: [PATCH 1/8] Allow importing extnesion scenario, e.g. for lines from NEP --- etrago/tools/io.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/etrago/tools/io.py b/etrago/tools/io.py index 579331aa..7b7a3eff 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -112,12 +112,14 @@ def __init__( start_snapshot=1, end_snapshot=20, temp_id=1, + scenario_extension=False, **kwargs, ): self.scn_name = scn_name self.start_snapshot = start_snapshot self.end_snapshot = end_snapshot self.temp_id = temp_id + self.scenario_extension = scenario_extension super().__init__(engine, session, **kwargs) @@ -207,6 +209,14 @@ def fetch_by_relname(self, name): egon_etrago_transformer, ) + if self.scenario_extension: + from saio.grid import ( # noqa: F401 + egon_etrago_extension_bus as egon_etrago_bus, + egon_etrago_extension_line as egon_etrago_line, + egon_etrago_extension_link as egon_etrago_link, + egon_etrago_extension_transformer as egon_etrago_transformer, + ) + index = f"{name.lower()}_id" if name == "Transformer": @@ -796,22 +806,17 @@ def extension(self, **kwargs): """ if self.args["scn_extension"] is not None: - if self.args["gridversion"] is None: - ormcls_prefix = "EgoGridPfHvExtension" - else: - ormcls_prefix = "EgoPfHvExtension" - for i in range(len(self.args["scn_extension"])): scn_extension = self.args["scn_extension"][i] # Adding overlay-network to existing network scenario = NetworkScenario( + self.engine, self.session, version=self.args["gridversion"], - prefix=ormcls_prefix, - method=kwargs.get("method", "lopf"), start_snapshot=self.args["start_snapshot"], end_snapshot=self.args["end_snapshot"], - scn_name="extension_" + scn_extension, + scn_name=scn_extension, + scenario_extension=True, ) self.network = scenario.build_network(self.network) From 1b7b963aa243452e18cb0ae7d8d6be55d36c9e23 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Sat, 8 Jun 2024 12:13:04 +0200 Subject: [PATCH 2/8] Drop isolated buses when lines are decomissioned --- etrago/tools/io.py | 92 +++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 30 deletions(-) diff --git a/etrago/tools/io.py b/etrago/tools/io.py index 7b7a3eff..3791136c 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -853,40 +853,72 @@ def decommissioning(self, **kwargs): """ if self.args["scn_decommissioning"] is not None: - if self.args["gridversion"] is None: - ormclass = getattr( - import_module("egoio.db_tables.model_draft"), - "EgoGridPfHvExtensionLine", - ) - else: - ormclass = getattr( - import_module("egoio.db_tables.grid"), "EgoPfHvExtensionLine" + for i in range(len(self.args["scn_extension"])): + scn_decom = self.args["scn_extension"][i] + + df_decommisionning = pd.read_sql( + f""" + SELECT * FROM + grid.egon_etrago_extension_line + WHERE scn_name = 'decomissioining_{scn_decom}' + """, + self.session.bind ) - query = self.session.query(ormclass).filter( - ormclass.scn_name - == "decommissioning_" + self.args["scn_decommissioning"] - ) + self.network.mremove( + "Line", + df_decommisionning.line_id.astype(str).values, + + ) + + # buses only between removed lines + candidates = pd.concat([df_decommisionning.bus0, + df_decommisionning.bus1]) + candidates.drop_duplicates(inplace=True, keep="first") + candidates = candidates.astype(str) + + # Drop buses that are connecting other lines + candidates = candidates[~candidates.isin(self.network.lines.bus0)] + candidates = candidates[~candidates.isin(self.network.lines.bus1)] + + # Drop buses that are connection other DC-lines + candidates = candidates[~candidates.isin(self.dc_lines().bus0)] + candidates = candidates[~candidates.isin(self.dc_lines().bus1)] + + drop_buses = self.network.buses[ + (self.network.buses.index.isin(candidates.values)) & + (self.network.buses.country=="DE")] + + drop_links = self.network.links[ + (self.network.links.bus0.isin(drop_buses.index)) | + (self.network.links.bus1.isin(drop_buses.index))].index + + drop_trafos = self.network.transformers[ + (self.network.transformers.bus0.isin(drop_buses.index)) | + (self.network.transformers.bus1.isin(drop_buses.index))].index + + drop_su = self.network.storage_units[ + self.network.storage_units.bus.isin(candidates.values)].index + + self.network.mremove( + "StorageUnit", + drop_su + ) - df_decommisionning = pd.read_sql( - query.statement, self.session.bind, index_col="line_id" - ) - df_decommisionning.index = df_decommisionning.index.astype(str) - - for idx, row in self.network.lines.iterrows(): - if (row["s_nom_min"] != 0) & ( - row["scn_name"] - == "extension_" + self.args["scn_decommissioning"] - ): - self.network.lines.s_nom_min[ - self.network.lines.index == idx - ] = self.network.lines.s_nom_min - - # Drop decommissioning-lines from existing network - self.network.lines = self.network.lines[ - ~self.network.lines.index.isin(df_decommisionning.index) - ] + self.network.mremove( + "Transformer", + drop_trafos + ) + self.network.mremove( + "Link", + drop_links + ) + + self.network.mremove( + "Bus", + drop_buses.index + ) def distance(x0, x1, y0, y1): """ From c7ba56f894a2985497c13698358385d96b75352c Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Sat, 8 Jun 2024 12:13:43 +0200 Subject: [PATCH 3/8] Apply black --- etrago/tools/io.py | 55 ++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/etrago/tools/io.py b/etrago/tools/io.py index 3791136c..f3e035ed 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -855,70 +855,63 @@ def decommissioning(self, **kwargs): if self.args["scn_decommissioning"] is not None: for i in range(len(self.args["scn_extension"])): scn_decom = self.args["scn_extension"][i] - + df_decommisionning = pd.read_sql( f""" SELECT * FROM grid.egon_etrago_extension_line WHERE scn_name = 'decomissioining_{scn_decom}' """, - self.session.bind + self.session.bind, ) self.network.mremove( "Line", df_decommisionning.line_id.astype(str).values, - - ) - + ) + # buses only between removed lines - candidates = pd.concat([df_decommisionning.bus0, - df_decommisionning.bus1]) + candidates = pd.concat( + [df_decommisionning.bus0, df_decommisionning.bus1] + ) candidates.drop_duplicates(inplace=True, keep="first") candidates = candidates.astype(str) # Drop buses that are connecting other lines candidates = candidates[~candidates.isin(self.network.lines.bus0)] candidates = candidates[~candidates.isin(self.network.lines.bus1)] - + # Drop buses that are connection other DC-lines candidates = candidates[~candidates.isin(self.dc_lines().bus0)] candidates = candidates[~candidates.isin(self.dc_lines().bus1)] drop_buses = self.network.buses[ - (self.network.buses.index.isin(candidates.values)) & - (self.network.buses.country=="DE")] + (self.network.buses.index.isin(candidates.values)) + & (self.network.buses.country == "DE") + ] drop_links = self.network.links[ - (self.network.links.bus0.isin(drop_buses.index)) | - (self.network.links.bus1.isin(drop_buses.index))].index + (self.network.links.bus0.isin(drop_buses.index)) + | (self.network.links.bus1.isin(drop_buses.index)) + ].index drop_trafos = self.network.transformers[ - (self.network.transformers.bus0.isin(drop_buses.index)) | - (self.network.transformers.bus1.isin(drop_buses.index))].index + (self.network.transformers.bus0.isin(drop_buses.index)) + | (self.network.transformers.bus1.isin(drop_buses.index)) + ].index drop_su = self.network.storage_units[ - self.network.storage_units.bus.isin(candidates.values)].index + self.network.storage_units.bus.isin(candidates.values) + ].index - self.network.mremove( - "StorageUnit", - drop_su - ) + self.network.mremove("StorageUnit", drop_su) - self.network.mremove( - "Transformer", - drop_trafos - ) + self.network.mremove("Transformer", drop_trafos) - self.network.mremove( - "Link", - drop_links - ) + self.network.mremove("Link", drop_links) + + self.network.mremove("Bus", drop_buses.index) - self.network.mremove( - "Bus", - drop_buses.index - ) def distance(x0, x1, y0, y1): """ From 6327b4dc479603ed96e8040c9115ad023083d247 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Fri, 16 Aug 2024 10:44:01 +0200 Subject: [PATCH 4/8] Remove extra argument for scn_decomissioing In case components are replaced by an extension scenario these are always dropped. Thus, only the argument scn_extension is needed --- etrago/appl.py | 45 ++++++++++++--------------------------------- etrago/args.json | 1 - etrago/tools/io.py | 2 +- 3 files changed, 13 insertions(+), 35 deletions(-) diff --git a/etrago/appl.py b/etrago/appl.py index bc509e11..1cc9d2de 100644 --- a/etrago/appl.py +++ b/etrago/appl.py @@ -86,8 +86,7 @@ "model_formulation": "kirchhoff", # angles or kirchhoff "scn_name": "eGon100RE", # scenario: eGon2035, eGon100RE or status2019 # Scenario variations: - "scn_extension": None, # None or array of extension scenarios - "scn_decommissioning": None, # None or decommissioning scenario + "scn_extension": ["nep2021_c2035"], # None or array of extension scenarios # Export options: "lpfile": False, # save pyomo's lp file: False or /path/to/lpfile.lp "csv_export": "results", # save results as csv: False or /path/tofolder @@ -281,40 +280,20 @@ def run_etrago(args, json_path): scn_name : str Choose your scenario. Currently, there are two different scenarios: "eGon2035", "eGon100RE". Default: "eGon2035". - scn_extension : None or str - This option does currently not work! + scn_extension : None or list of str Choose extension-scenarios which will be added to the existing - network container. Data of the extension scenarios are located in - extension-tables (e.g. model_draft.ego_grid_pf_hv_extension_bus) - with the prefix 'extension\_'. - There are three overlay networks: - - * 'nep2035_confirmed' includes all planed new lines confirmed by the - Bundesnetzagentur - * 'nep2035_b2' includes all new lines planned by the - Netzentwicklungsplan 2025 in scenario 2035 B2 - * 'BE_NO_NEP 2035' includes planned lines to Belgium and Norway and - adds BE and NO as electrical neighbours - + network container. In case new lines replace existing ones, these are + dropped from the network. Data of the extension scenarios is located in + extension-tables (e.g. grid.egon_etrago_extension_line) + There are two overlay networks: + + * 'nep2021_confirmed' includes all planed new lines confirmed by the + Bundesnetzagentur included in the NEP version 2021 + * 'nep2021_c2035' includes all new lines planned by the + Netzentwicklungsplan 2021 in scenario 2035 C Default: None. - scn_decommissioning : NoneType or str - This option does currently not work! - - Choose an extra scenario which includes lines you want to decommission - from the existing network. Data of the decommissioning scenarios are - located in extension-tables - (e.g. model_draft.ego_grid_pf_hv_extension_bus) with the prefix - 'decommissioning\_'. - Currently, there are two decommissioning_scenarios which are linked to - extension-scenarios: - - * 'nep2035_confirmed' includes all lines that will be replaced in - confirmed projects - * 'nep2035_b2' includes all lines that will be replaced in - NEP-scenario 2035 B2 - Default: None. lpfile : bool or str State if and where you want to save pyomo's lp file. Options: False or '/path/tofile.lp'. Default: False. @@ -707,7 +686,7 @@ def run_etrago(args, json_path): # adjust network regarding eTraGo setting etrago.adjust_network() - + # ehv network clustering etrago.ehv_clustering() diff --git a/etrago/args.json b/etrago/args.json index 4198a390..213ab045 100644 --- a/etrago/args.json +++ b/etrago/args.json @@ -28,7 +28,6 @@ "model_formulation": "kirchhoff", "scn_name": "eGon2035", "scn_extension": null, - "scn_decommissioning": null, "lpfile": false, "csv_export": "results", "extendable": { diff --git a/etrago/tools/io.py b/etrago/tools/io.py index f3e035ed..25ef22f4 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -852,7 +852,7 @@ def decommissioning(self, **kwargs): Network container including decommissioning """ - if self.args["scn_decommissioning"] is not None: + if self.args["scn_extension"] is not None: for i in range(len(self.args["scn_extension"])): scn_decom = self.args["scn_extension"][i] From 471367b6928265c9cbd72a1ec92e9f52b6698a31 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Sun, 3 Aug 2025 11:52:38 +0200 Subject: [PATCH 5/8] Add cluster strategy for scn_name --- etrago/cluster/spatial.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/etrago/cluster/spatial.py b/etrago/cluster/spatial.py index efc91bd3..935278b6 100755 --- a/etrago/cluster/spatial.py +++ b/etrago/cluster/spatial.py @@ -109,11 +109,11 @@ def sum_with_inf(x): def strategies_buses(): - return {"geom": nan_links, "country": "first"} + return {"geom": nan_links, "country": "first", "scn_name": "first",} def strategies_lines(): - return {"geom": nan_links, "country": "first"} + return {"geom": nan_links, "country": "first", "scn_name": "first",} def strategies_one_ports(): @@ -139,6 +139,7 @@ def strategies_one_ports(): "e_initial": "sum", "e_min_pu": "mean", "e_max_pu": "mean", + "scn_name": "first", }, } @@ -154,6 +155,7 @@ def strategies_generators(): "capital_cost": "mean", "e_nom_max": sum_with_inf, "up_time_before": "mean", + "scn_name": "first", } From a5b17c51703cd93b40461c1aa92f5693801a3c4c Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 23 Sep 2025 15:32:09 +0200 Subject: [PATCH 6/8] Set default scenario name and cluster strategies --- etrago/appl.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/etrago/appl.py b/etrago/appl.py index 538ed77f..765f08bd 100644 --- a/etrago/appl.py +++ b/etrago/appl.py @@ -84,9 +84,9 @@ "BarHomogeneous": 1, }, "model_formulation": "kirchhoff", # angles or kirchhoff - "scn_name": "eGon100RE", # scenario: eGon2035, eGon100RE or status2019 + "scn_name": "eGon2035", # scenario: eGon2035, eGon100RE or status2019 # Scenario variations: - "scn_extension": ["nep2021_c2035"], # None or array of extension scenarios + "scn_extension": None, # None or array of extension scenarios # Export options: "lpfile": False, # save pyomo's lp file: False or /path/to/lpfile.lp "csv_export": "results", # save results as csv: False or /path/tofolder @@ -148,15 +148,15 @@ "strategy": "simultaneous", # select strategy to cluster other sectors }, "rural_heat": { - "base": ["CH4", "AC"], - "strategy": "simultaneous", # select strategy to cluster other sectors + "base": ["AC"], + "strategy": "consecutive", # select strategy to cluster other sectors }, - "H2": { + "H2_grid": { "base": ["CH4"], "strategy": "consecutive", # select strategy to cluster other sectors }, "H2_saltcavern": { - "base": ["H2_grid"], + "base": ["AC"], "strategy": "consecutive", # select strategy to cluster other sectors }, "Li_ion": { From fcac1489d734718c6ff25602206120dec501c44a Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 23 Sep 2025 15:35:51 +0200 Subject: [PATCH 7/8] Fix flake8 problems --- etrago/tools/io.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/etrago/tools/io.py b/etrago/tools/io.py index 25ef22f4..7bda2c27 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -48,7 +48,6 @@ __license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" __author__ = "ulfmueller, mariusves, pieterhexen, ClaraBuettner" -from importlib import import_module import os import numpy as np @@ -210,7 +209,7 @@ def fetch_by_relname(self, name): ) if self.scenario_extension: - from saio.grid import ( # noqa: F401 + from saio.grid import ( # noqa: F811 egon_etrago_extension_bus as egon_etrago_bus, egon_etrago_extension_line as egon_etrago_line, egon_etrago_extension_link as egon_etrago_link, @@ -858,7 +857,7 @@ def decommissioning(self, **kwargs): df_decommisionning = pd.read_sql( f""" - SELECT * FROM + SELECT * FROM grid.egon_etrago_extension_line WHERE scn_name = 'decomissioining_{scn_decom}' """, From 9b82639f0c00b730d02409555762ecca345b51c1 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 23 Sep 2025 15:38:53 +0200 Subject: [PATCH 8/8] Ignore flake8 warning --- etrago/tools/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etrago/tools/io.py b/etrago/tools/io.py index 7bda2c27..b3cc86eb 100644 --- a/etrago/tools/io.py +++ b/etrago/tools/io.py @@ -209,7 +209,7 @@ def fetch_by_relname(self, name): ) if self.scenario_extension: - from saio.grid import ( # noqa: F811 + from saio.grid import ( # noqa: F401,F811 egon_etrago_extension_bus as egon_etrago_bus, egon_etrago_extension_line as egon_etrago_line, egon_etrago_extension_link as egon_etrago_link,