From 33b299ba8636f7f2d7e5c2ab85404c32bdb59e3a Mon Sep 17 00:00:00 2001 From: Davide Fioriti Date: Sat, 30 Sep 2023 23:31:19 +0200 Subject: [PATCH 1/2] Fix simplify_link bug with links --- Snakefile | 4 ++-- scripts/simplify_network.py | 45 +++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Snakefile b/Snakefile index ea3b690f0..cb0896838 100644 --- a/Snakefile +++ b/Snakefile @@ -559,8 +559,8 @@ rule simplify_network: build_shape_options=config["build_shape_options"], electricity=config["electricity"], costs=config["costs"], - lines_types=config["lines"]["types"], - lines_length_factor=config["lines"]["length_factor"], + config_lines=config["lines"], + config_links=config["links"], focus_weights=config.get("focus_weights", None), input: network="networks/" + RDIR + "elec.nc", diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index 7e3e57354..4e2d1c2c0 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -155,14 +155,16 @@ def simplify_network_to_base_voltage(n, linetype, base_voltage): return n, trafo_map -def _prepare_connection_costs_per_link(n, costs, renewable_config, lines_length_factor): +def _prepare_connection_costs_per_link( + n, costs, renewable_config, hvdc_as_lines, lines_length_factor +): if n.links.empty: return {} connection_costs_per_link = {} # initialize dc_lengths and underwater_fractions by the hvdc_as_lines option - if config["electricity"]["hvdc_as_lines"]: + if hvdc_as_lines: dc_lengths = n.lines.length unterwater_fractions = n.lines.underwater_fraction else: @@ -190,13 +192,14 @@ def _compute_connection_costs_to_bus( busmap, costs, renewable_config, + hvdc_as_lines, lines_length_factor, connection_costs_per_link=None, buses=None, ): if connection_costs_per_link is None: connection_costs_per_link = _prepare_connection_costs_per_link( - n, costs, renewable_config, lines_length_factor + n, costs, renewable_config, hvdc_as_lines, lines_length_factor ) if buses is None: @@ -304,8 +307,11 @@ def simplify_links( n, costs, renewable_config, - lines_length_factor, + hvdc_as_lines, + config_lines, + config_links, output, + exclude_carriers=[], aggregation_strategies=dict(), ): ## Complex multi-node links are folded into end-points @@ -378,7 +384,7 @@ def split_links(nodes): busmap = n.buses.index.to_series() connection_costs_per_link = _prepare_connection_costs_per_link( - n, costs, renewable_config, lines_length_factor + n, costs, renewable_config, hvdc_as_lines, config_lines["length_factor"] ) connection_costs_to_bus = pd.DataFrame( 0.0, index=n.buses.index, columns=list(connection_costs_per_link) @@ -401,7 +407,8 @@ def split_links(nodes): busmap, costs, renewable_config, - lines_length_factor, + hvdc_as_lines, + config_lines, connection_costs_per_link, buses, ) @@ -412,7 +419,6 @@ def split_links(nodes): all_links = list(set(n.links.index).intersection(all_dc_branches)) all_dc_lines = list(set(n.lines.index).intersection(all_dc_branches)) - # p_max_pu = config["links"].get("p_max_pu", 1.0) all_dc_lengths = pd.concat( [n.links.loc[all_links, "length"], n.lines.loc[all_dc_lines, "length"]] ) @@ -420,7 +426,7 @@ def split_links(nodes): # HVDC part is represented as "Link" component if dc_as_links: - p_max_pu = config["links"].get("p_max_pu", 1.0) + p_max_pu = config_links.get("p_max_pu", 1.0) lengths = n.links.loc[all_links, "length"] i_links = [i for _, i in sum(dc_edges, []) if _ == "Link"] length = sum(n.links.loc[i_links, "length"].mean() for l in dc_edges) @@ -430,7 +436,7 @@ def split_links(nodes): ).sum() / lengths.sum() # HVDC part is represented as "Line" component else: - p_max_pu = config["lines"].get("p_max_pu", 1.0) + p_max_pu = config_lines.get("p_max_pu", 1.0) lengths = n.lines.loc[all_dc_lines, "length"] length = lengths.sum() / len(lengths) if len(lengths) > 0 else 0 p_nom = n.lines.loc[all_dc_lines, "s_nom"].min() @@ -470,10 +476,6 @@ def split_links(nodes): logger.debug("Collecting all components using the busmap") - exclude_carriers = config["cluster_options"]["simplify_network"].get( - "exclude_carriers", [] - ) - _aggregate_and_move_components( n, busmap, @@ -490,6 +492,7 @@ def remove_stubs( costs, cluster_config, renewable_config, + hvdc_as_lines, lines_length_factor, output, aggregation_strategies=dict(), @@ -506,6 +509,7 @@ def remove_stubs( busmap, costs, renewable_config, + hvdc_as_lines, lines_length_factor, ) @@ -829,8 +833,11 @@ def merge_isolated_nodes(n, threshold, aggregation_strategies=dict()): n = pypsa.Network(snakemake.input.network) base_voltage = snakemake.params.electricity["base_voltage"] - linetype = snakemake.params.lines_types[base_voltage] - + linetype = snakemake.params.config_lines["types"][base_voltage] + exclude_carriers = snakemake.params.cluster_options["simplify_network"].get( + "exclude_carriers", [] + ) + hvdc_as_lines = snakemake.params.electricity["hvdc_as_lines"] aggregation_strategies = snakemake.params.cluster_options.get( "aggregation_strategies", {} ) @@ -854,8 +861,11 @@ def merge_isolated_nodes(n, threshold, aggregation_strategies=dict()): n, technology_costs, snakemake.params.renewable, - snakemake.params.lines_length_factor, + hvdc_as_lines, + snakemake.params.config_lines, + snakemake.params.config_links, snakemake.output, + exclude_carriers, aggregation_strategies, ) @@ -863,13 +873,14 @@ def merge_isolated_nodes(n, threshold, aggregation_strategies=dict()): cluster_config = snakemake.params.cluster_options["simplify_network"] renewable_config = snakemake.params.renewable - lines_length_factor = snakemake.params.lines_length_factor + lines_length_factor = snakemake.params.config_lines["length_factor"] if cluster_config.get("remove_stubs", True): n, stub_map = remove_stubs( n, technology_costs, cluster_config, renewable_config, + hvdc_as_lines, lines_length_factor, snakemake.output, aggregation_strategies=aggregation_strategies, From 1232b3b569c079f4256c460dce04c809e8eabfd6 Mon Sep 17 00:00:00 2001 From: Davide Fioriti Date: Sat, 30 Sep 2023 23:34:24 +0200 Subject: [PATCH 2/2] Revise release note --- doc/release_notes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 8410a9667..302a4c4a4 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -14,7 +14,7 @@ E.g. if a new rule becomes available describe how to use it `snakemake -j1 run_t **New Features and major Changes** -* Add params: section in rule definition to keep track of changed settings in config.yaml. `PR #823 `__ +* Add params: section in rule definition to keep track of changed settings in config.yaml. `PR #823 `__ and `PR #880 `__ * Fix Natural Gas implementation in "add_electricity" to avoid "Natural Gas" to be filtered out `PR #797 `__