From c043100ada790d088ea190e9f8ba634e28a204e1 Mon Sep 17 00:00:00 2001 From: virio-andreyana <114650479+virio-andreyana@users.noreply.github.com> Date: Fri, 19 May 2023 15:42:07 +0200 Subject: [PATCH] add more params in simplify_network.py --- rules/build_electricity.smk | 8 +++- scripts/add_electricity.py | 2 +- scripts/cluster_network.py | 2 +- scripts/simplify_network.py | 76 +++++++++++++++++++------------------ 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/rules/build_electricity.smk b/rules/build_electricity.smk index 1ba1a1c5..f33d1686 100644 --- a/rules/build_electricity.smk +++ b/rules/build_electricity.smk @@ -318,6 +318,12 @@ rule simplify_network: clustering=config["clustering"], electricity=config["electricity"], costs=config["costs"], + renewable=config["renewable"], + length_factor=config["lines"]["length_factor"], + p_max_pu=config["links"].get("p_max_pu", 1.0), + exclude_carriers=config["clustering"]["simplify_network"].get("exclude_carriers", []), + focus_weights=config.get("focus_weights", None), + solver_name=config["solving"]["solver"]["name"], input: network=RESOURCES + "networks/elec.nc", tech_costs=COSTS, @@ -350,7 +356,7 @@ rule cluster_network: lines=config["lines"], renewable=config["renewable"], clustering=config["clustering"], - enable=config["enable"].get("custom_busmap", False), + custom_busmap=config["enable"].get("custom_busmap", False), input: network=RESOURCES + "networks/elec_s{simpl}.nc", regions_onshore=RESOURCES + "regions_onshore_elec_s{simpl}.geojson", diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index adb8bf31..3717f0e8 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -756,7 +756,7 @@ if __name__ == "__main__": ppl, conventional_carriers, extendable_carriers, - snakemake.params.get("conventional", {}), + snakemake.params["conventional"], conventional_inputs, ) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 78798804..d217e5ed 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -521,7 +521,7 @@ if __name__ == "__main__": for p in aggregation_strategies.keys() } - custom_busmap = snakemake.params["enable"].get("custom_busmap", False) + custom_busmap = snakemake.params["custom_busmap"] if custom_busmap: custom_busmap = pd.read_csv( snakemake.input.custom_busmap, index_col=0, squeeze=True diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index bb7f615c..91dde6b1 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -149,17 +149,17 @@ def simplify_network_to_380(n): return n, trafo_map -def _prepare_connection_costs_per_link(n, costs, config): +def _prepare_connection_costs_per_link(n, costs, renewable_param, length_factor_param): if n.links.empty: return {} connection_costs_per_link = {} - for tech in config["renewable"]: + for tech in renewable_param: if tech.startswith("offwind"): connection_costs_per_link[tech] = ( n.links.length - * config["lines"]["length_factor"] + * length_factor_param * ( n.links.underwater_fraction * costs.at[tech + "-connection-submarine", "capital_cost"] @@ -172,10 +172,10 @@ def _prepare_connection_costs_per_link(n, costs, config): def _compute_connection_costs_to_bus( - n, busmap, costs, config, connection_costs_per_link=None, buses=None + n, busmap, costs, renewable_param, length_factor_param, 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, config) + connection_costs_per_link = _prepare_connection_costs_per_link(n, costs, renewable_param, length_factor_param) if buses is None: buses = busmap.index[busmap.index != busmap.values] @@ -265,7 +265,7 @@ def _aggregate_and_move_components( n.mremove(c, df.index[df.bus0.isin(buses_to_del) | df.bus1.isin(buses_to_del)]) -def simplify_links(n, costs, config, output, aggregation_strategies=dict()): +def simplify_links(n, costs, renewable_param, length_factor_param, p_max_pu_param, exclude_carriers_param, output, aggregation_strategies=dict()): ## Complex multi-node links are folded into end-points logger.info("Simplifying connected link components") @@ -315,7 +315,7 @@ def simplify_links(n, costs, config, output, aggregation_strategies=dict()): busmap = n.buses.index.to_series() - connection_costs_per_link = _prepare_connection_costs_per_link(n, costs, config) + connection_costs_per_link = _prepare_connection_costs_per_link(n, costs, renewable_param, length_factor_param) connection_costs_to_bus = pd.DataFrame( 0.0, index=n.buses.index, columns=list(connection_costs_per_link) ) @@ -333,12 +333,11 @@ def simplify_links(n, costs, config, output, aggregation_strategies=dict()): ) busmap.loc[buses] = b[np.r_[0, m.argmin(axis=0), 1]] connection_costs_to_bus.loc[buses] += _compute_connection_costs_to_bus( - n, busmap, costs, config, connection_costs_per_link, buses + n, busmap, costs, renewable_param, length_factor_param, connection_costs_per_link, buses ) all_links = [i for _, i in sum(links, [])] - p_max_pu = config["links"].get("p_max_pu", 1.0) lengths = n.links.loc[all_links, "length"] name = lengths.idxmax() + "+{}".format(len(links) - 1) params = dict( @@ -354,8 +353,8 @@ def simplify_links(n, costs, config, output, aggregation_strategies=dict()): / lengths.sum() * n.links.loc[all_links, "underwater_fraction"] ), - p_max_pu=p_max_pu, - p_min_pu=-p_max_pu, + p_max_pu=p_max_pu_param, + p_min_pu=-p_max_pu_param, underground=False, under_construction=False, ) @@ -377,33 +376,29 @@ def simplify_links(n, costs, config, output, aggregation_strategies=dict()): logger.debug("Collecting all components using the busmap") - exclude_carriers = config["clustering"]["simplify_network"].get( - "exclude_carriers", [] - ) - _aggregate_and_move_components( n, busmap, connection_costs_to_bus, output, aggregation_strategies=aggregation_strategies, - exclude_carriers=exclude_carriers, + exclude_carriers=exclude_carriers_param, ) return n, busmap -def remove_stubs(n, costs, config, output, aggregation_strategies=dict()): +def remove_stubs(n, costs, renewable_param, length_factor_param, clustering_param, exclude_carriers_param, output, aggregation_strategies=dict()): logger.info("Removing stubs") - across_borders = config["clustering"]["simplify_network"].get( + across_borders = clustering_param["simplify_network"].get( "remove_stubs_across_borders", True ) matching_attrs = [] if across_borders else ["country"] busmap = busmap_by_stubs(n, matching_attrs) - connection_costs_to_bus = _compute_connection_costs_to_bus(n, busmap, costs, config) + connection_costs_to_bus = _compute_connection_costs_to_bus(n, busmap, costs, renewable_param, length_factor_param) - exclude_carriers = config["clustering"]["simplify_network"].get( + exclude_carriers = clustering_param["simplify_network"].get( "exclude_carriers", [] ) @@ -473,17 +468,15 @@ def aggregate_to_substations(n, aggregation_strategies=dict(), buses_i=None): def cluster( - n, n_clusters, config, algorithm="hac", feature=None, aggregation_strategies=dict() + n, n_clusters, focus_weights_param, renewable_param, solver_name_param, algorithm="hac", feature=None, aggregation_strategies=dict() ): logger.info(f"Clustering to {n_clusters} buses") - focus_weights = config.get("focus_weights", None) - renewable_carriers = pd.Index( [ tech for tech in n.generators.carrier.unique() - if tech.split("-", 2)[0] in config["renewable"] + if tech.split("-", 2)[0] in renewable_param ] ) @@ -492,10 +485,10 @@ def cluster( n_clusters, custom_busmap=False, aggregation_strategies=aggregation_strategies, - solver_name=config["solving"]["solver"]["name"], + solver_name=solver_name_param, algorithm=algorithm, feature=feature, - focus_weights=focus_weights, + focus_weights=focus_weights_param, ) return clustering.network, clustering.busmap @@ -531,23 +524,32 @@ if __name__ == "__main__": ) n, simplify_links_map = simplify_links( - n, technology_costs, snakemake.config, snakemake.output, aggregation_strategies + n, technology_costs, + snakemake.params['renewable'], + snakemake.params['length_factor'], + snakemake.params['p_max_pu'], + snakemake.params['exclude_carriers'], + snakemake.output, + aggregation_strategies ) busmaps = [trafo_map, simplify_links_map] - cluster_config = snakemake.params["clustering"]["simplify_network"] - if cluster_config.get("remove_stubs", True): + cluster_param = snakemake.params["clustering"]["simplify_network"] + if cluster_param.get("remove_stubs", True): n, stub_map = remove_stubs( n, technology_costs, - snakemake.config, + snakemake.params['renewable'], + snakemake.params['length_factor'], + snakemake.params["clustering"], + snakemake.params['exclude_carriers'], snakemake.output, aggregation_strategies=aggregation_strategies, ) busmaps.append(stub_map) - if cluster_config.get("to_substations", False): + if cluster_param.get("to_substations", False): n, substation_map = aggregate_to_substations(n, aggregation_strategies) busmaps.append(substation_map) @@ -558,10 +560,10 @@ if __name__ == "__main__": .get("cluster_network", {}) .get("algorithm", "hac") == "hac" - or cluster_config.get("algorithm", "hac") == "hac" + or cluster_param.get("algorithm", "hac") == "hac" ): carriers = ( - cluster_config.get("feature", "solar+onwind-time").split("-")[0].split("+") + cluster_param.get("feature", "solar+onwind-time").split("-")[0].split("+") ) for carrier in carriers: buses_i = list( @@ -577,9 +579,11 @@ if __name__ == "__main__": n, cluster_map = cluster( n, int(snakemake.wildcards.simpl), - snakemake.config, - cluster_config.get("algorithm", "hac"), - cluster_config.get("feature", None), + snakemake.params['focus_weights'], + snakemake.params['renewable'], + snakemake.params['solver_name'], + cluster_param.get("algorithm", "hac"), + cluster_param.get("feature", None), aggregation_strategies, ) busmaps.append(cluster_map)