From ea0e2f66ce41107addcc018bca05700c9ead542c Mon Sep 17 00:00:00 2001 From: Parisra <102019906+Parisra@users.noreply.github.com> Date: Mon, 29 Apr 2024 15:58:42 +0200 Subject: [PATCH] Fix empty array error In case there are no extendable solar generators in the network, the constraint is not implemented. Without this condition, an 'empty array' error will arise. --- scripts/solve_network.py | 101 +++++++++++++++------------------------ 1 file changed, 38 insertions(+), 63 deletions(-) diff --git a/scripts/solve_network.py b/scripts/solve_network.py index 08521273..de8c4935 100644 --- a/scripts/solve_network.py +++ b/scripts/solve_network.py @@ -228,76 +228,51 @@ def add_solar_potential_constraints(n, config): "solar-hsat": config["renewable"]["solar"]["capacity_per_sqkm"] / config["renewable"]["solar-hsat"]["capacity_per_sqkm"], } - gen_index = n.generators[n.generators.p_nom_extendable].index - - filters = [ - ("solar", True), - ("thermal", False), - ("rooftop", False), - ] ## filter all utility solar generation except solar thermal - solar = reduce( - lambda gen_index, f: gen_index[gen_index.str.contains(f[0]) == f[1]], - filters, - gen_index, - ) - solar_today = n.generators[ - (n.generators.carrier == "solar") & (n.generators.p_nom_extendable) - ].index - solar_hsat = n.generators[(n.generators.carrier == "solar-hsat")].index - land_use = pd.DataFrame(1, index=solar, columns=["land_use_factor"]) - for key in land_use_factors.keys(): - land_use = land_use.apply( - lambda x: (x * land_use_factors[key]) if key in x.name else x, axis=1 - ) - rename = {"Generator-ext": "Generator"} - if "m" in snakemake.wildcards.clusters: - location = pd.Series( - [" ".join(i.split(" ")[:2]) for i in n.generators.index], - index=n.generators.index, - ) - ggrouper = pd.Series( - n.generators.loc[solar].index.rename("bus").map(location), - index=n.generators.loc[solar].index, - ).to_xarray() - rhs = ( - n.generators.loc[solar_today, "p_nom_max"] - .groupby(n.generators.loc[solar_today].index.rename("bus").map(location)) - .sum() - - n.generators.loc[solar_hsat, "p_nom_opt"] - .groupby(n.generators.loc[solar_hsat].index.rename("bus").map(location)) - .sum() - * land_use_factors["solar-hsat"] - ).clip(lower=0) - else: - location = ( + gen_index = n.generators[n.generators.p_nom_extendable].index + filters = [("solar", True), ("thermal", False), ("rooftop", False)] ## filter all utility solar generation except solar thermal + solar = reduce(lambda gen_index, f: gen_index[gen_index.str.contains(f[0]) == f[1]], filters, gen_index) + solar_today = n.generators[(n.generators.carrier=='solar') & (n.generators.p_nom_extendable)].index + solar_hsat = n.generators[(n.generators.carrier=='solar-hsat') ].index + + if not solar.empty : + + land_use = pd.DataFrame(1, index=solar, columns=['land_use_factor']) + for key in land_use_factors.keys(): + land_use = land_use.apply(lambda x: (x*land_use_factors[key]) if key in x.name else x, axis=1) + + if "m" in snakemake.wildcards.clusters: + location = ( + pd.Series([' '.join(i.split(' ')[:2]) for i in n.generators.index], index=n.generators.index) + ) + ggrouper= pd.Series(n.generators.loc[solar].index.rename('bus').map(location), index=n.generators.loc[solar].index,).to_xarray() + rhs = (n.generators.loc[solar_today,"p_nom_max"] + .groupby(n.generators.loc[solar_today].index.rename('bus').map(location)).sum() - + n.generators.loc[solar_hsat,"p_nom_opt"] + .groupby(n.generators.loc[solar_hsat].index.rename('bus').map(location)).sum() * land_use_factors['solar-hsat'] ).clip(lower=0) + + else : + location = ( n.buses.location if "location" in n.buses.columns else pd.Series(n.buses.index, index=n.buses.index) - ) - ggrouper = n.generators.loc[solar].bus - rhs = ( - n.generators.loc[solar_today, "p_nom_max"] - .groupby(n.generators.loc[solar_today].bus.map(location)) - .sum() - - n.generators.loc[solar_hsat, "p_nom_opt"] - .groupby(n.generators.loc[solar_hsat].bus.map(location)) - .sum() - * land_use_factors["solar-hsat"] - ).clip(lower=0) + ) + ggrouper= (n.generators.loc[solar].bus) + rhs = (n.generators.loc[solar_today,"p_nom_max"] + .groupby(n.generators.loc[solar_today].bus.map(location)).sum() - + n.generators.loc[solar_hsat,"p_nom_opt"] + .groupby(n.generators.loc[solar_hsat].bus.map(location)).sum() * land_use_factors['solar-hsat'] ).clip(lower=0) - lhs = ( - ( - n.model["Generator-p_nom"].rename(rename).loc[solar] - * land_use.squeeze().values - ) - .groupby(ggrouper) - .sum() - ) + lhs = ( + (n.model["Generator-p_nom"].rename(rename).loc[solar] + *land_use.squeeze().values) + .groupby(ggrouper) + .sum() + ) - print("adding solar rooftop constraints...") - n.model.add_constraints(lhs <= rhs, name="solar_potential") + print('adding solar rooftop constraints...') + n.model.add_constraints(lhs <= rhs, name="solar_potential") def add_co2_sequestration_limit(n, limit=200):