From f96c2d05899bb8d51087223f54a384410e4579df Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 22 Sep 2023 13:58:08 +0200 Subject: [PATCH] update solve smk files to properly consider scenanrios --- rules/common.smk | 7 +++++-- rules/solve_electricity.smk | 16 ++++++++-------- rules/solve_myopic.smk | 36 ++++++++++++++++++++---------------- rules/solve_overnight.smk | 14 +++++++------- scripts/solve_network.py | 8 ++++---- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/rules/common.smk b/rules/common.smk index dab15c0d..c4b8e6d2 100644 --- a/rules/common.smk +++ b/rules/common.smk @@ -10,7 +10,10 @@ def get_config(config, keys, default=None): """Retrieve a nested value from a dictionary using a tuple of keys.""" value = config for key in keys: - value = value.get(key, default) + if isinstance(value, list): + value = value[key] + else: + value = value.get(key, default) if value == default: return default return value @@ -40,7 +43,7 @@ def static_getter(wildcards, keys, default): def dynamic_getter(wildcards, keys, default): """Getter function for dynamic config values based on scenario.""" - if "run" not in wildcards: + if "run" not in wildcards.keys(): return get_config(config, keys, default) scenario_name = wildcards.run if scenario_name not in scenarios: diff --git a/rules/solve_electricity.smk b/rules/solve_electricity.smk index cfdb1da0..424748e2 100644 --- a/rules/solve_electricity.smk +++ b/rules/solve_electricity.smk @@ -5,11 +5,11 @@ rule solve_network: params: - solving=config["solving"], - foresight=config["foresight"], - planning_horizons=config["scenario"]["planning_horizons"], - co2_sequestration_potential=config["sector"].get( - "co2_sequestration_potential", 200 + solving=config_provider("solving"), + foresight=config_provider("foresight"), + planning_horizons=config_provider("scenario", "planning_horizons"), + co2_sequestration_potential=config_provider( + "sector", "co2_sequestration_potential", default=200 ), input: network=resources("networks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}.nc"), @@ -27,7 +27,7 @@ rule solve_network: threads: 4 resources: mem_mb=memory, - walltime=config["solving"].get("walltime", "12:00:00"), + walltime=config_provider("solving", "walltime", default="12:00:00"), shadow: "minimal" conda: @@ -38,7 +38,7 @@ rule solve_network: rule solve_operations_network: params: - options=config["solving"]["options"], + options=config_provider("solving", "options"), input: network=RESULTS + "networks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}.nc", output: @@ -58,7 +58,7 @@ rule solve_operations_network: threads: 4 resources: mem_mb=(lambda w: 10000 + 372 * int(w.clusters)), - walltime=config["solving"].get("walltime", "12:00:00"), + walltime=config_provider("solving", "walltime", default="12:00:00"), shadow: "minimal" conda: diff --git a/rules/solve_myopic.smk b/rules/solve_myopic.smk index 214733b7..7f851326 100644 --- a/rules/solve_myopic.smk +++ b/rules/solve_myopic.smk @@ -5,10 +5,10 @@ rule add_existing_baseyear: params: - baseyear=config["scenario"]["planning_horizons"][0], - sector=config["sector"], - existing_capacities=config["existing_capacities"], - costs=config["costs"], + baseyear=config_provider("scenario", "planning_horizons", 0), + sector=config_provider("sector"), + existing_capacities=config_provider("existing_capacities"), + costs=config_provider("costs"), input: network=RESULTS + "prenetworks/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc", @@ -16,7 +16,9 @@ rule add_existing_baseyear: busmap_s=resources("busmap_elec_s{simpl}.csv"), busmap=resources("busmap_elec_s{simpl}_{clusters}.csv"), clustered_pop_layout=resources("pop_layout_elec_s{simpl}_{clusters}.csv"), - costs="data/costs_{}.csv".format(config["scenario"]["planning_horizons"][0]), + costs=lambda w: "data/costs_{}.csv".format( + config_provider("scenario", "planning_horizons", 0)(w) + ), cop_soil_total=resources("cop_soil_total_elec_s{simpl}_{clusters}.nc"), cop_air_total=resources("cop_air_total_elec_s{simpl}_{clusters}.nc"), existing_heating="data/existing_infrastructure/existing_heating_raw.csv", @@ -27,7 +29,7 @@ rule add_existing_baseyear: RESULTS + "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc", wildcard_constraints: - planning_horizons=config["scenario"]["planning_horizons"][0], #only applies to baseyear + planning_horizons=config_provider("scenario", "planning_horizons", 0), #only applies to baseyear threads: 1 resources: mem_mb=2000, @@ -47,9 +49,11 @@ rule add_existing_baseyear: rule add_brownfield: params: - H2_retrofit=config["sector"]["H2_retrofit"], - H2_retrofit_capacity_per_CH4=config["sector"]["H2_retrofit_capacity_per_CH4"], - threshold_capacity=config["existing_capacities"]["threshold_capacity"], + H2_retrofit=config_provider("sector", "H2_retrofit"), + H2_retrofit_capacity_per_CH4=config_provider( + "sector", "H2_retrofit_capacity_per_CH4" + ), + threshold_capacity=config_provider("existing_capacities", " threshold_capacity"), input: network=RESULTS + "prenetworks/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc", @@ -82,11 +86,11 @@ ruleorder: add_existing_baseyear > add_brownfield rule solve_sector_network_myopic: params: - solving=config["solving"], - foresight=config["foresight"], - planning_horizons=config["scenario"]["planning_horizons"], - co2_sequestration_potential=config["sector"].get( - "co2_sequestration_potential", 200 + solving=config_provider("solving"), + foresight=config_provider("foresight"), + planning_horizons=config_provider("scenario", "planning_horizons"), + co2_sequestration_potential=config_provider( + "sector", "co2_sequestration_potential", default=200 ), input: network=RESULTS @@ -105,8 +109,8 @@ rule solve_sector_network_myopic: + "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_python.log", threads: 4 resources: - mem_mb=config["solving"]["mem"], - walltime=config["solving"].get("walltime", "12:00:00"), + mem_mb=config_provider("solving", "mem"), + walltime=config_provider("solving", "walltime", default="12:00:00"), benchmark: ( BENCHMARKS diff --git a/rules/solve_overnight.smk b/rules/solve_overnight.smk index d8476868..8f2ff139 100644 --- a/rules/solve_overnight.smk +++ b/rules/solve_overnight.smk @@ -5,11 +5,11 @@ rule solve_sector_network: params: - solving=config["solving"], - foresight=config["foresight"], - planning_horizons=config["scenario"]["planning_horizons"], - co2_sequestration_potential=config["sector"].get( - "co2_sequestration_potential", 200 + solving=config_provider("solving"), + foresight=config_provider("foresight"), + planning_horizons=config_provider("scenario", "planning_horizons"), + co2_sequestration_potential=config_provider( + "sector", "co2_sequestration_potential", default=200 ), input: network=RESULTS @@ -27,8 +27,8 @@ rule solve_sector_network: + "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_python.log", threads: config["solving"]["solver"].get("threads", 4) resources: - mem_mb=config["solving"]["mem"], - walltime=config["solving"].get("walltime", "12:00:00"), + mem_mb=config_provider("solving", "mem"), + walltime=config_provider("solving", "walltime", default="12:00:00"), benchmark: ( BENCHMARKS diff --git a/scripts/solve_network.py b/scripts/solve_network.py index 1e1e738b..bf860054 100644 --- a/scripts/solve_network.py +++ b/scripts/solve_network.py @@ -137,10 +137,10 @@ def add_co2_sequestration_limit(n, limit=200): n.add( "GlobalConstraint", "co2_sequestration_limit", - sense="<=", - constant=limit, - type="primary_energy", - carrier_attribute="co2_absorptions", + sense=">=", + constant=-limit, + type="operational_limit", + carrier_attribute="co2 sequestered", )