Skip to content

Commit

Permalink
prepare for pypsa 0.31.0 (#1340)
Browse files Browse the repository at this point in the history
* prepare for pypsa `0.31.0`

* Update mock_snakemake (#1345)

* test: log env diff (#1338)

* Update mock_snakemake

---------

Co-authored-by: Lukas Trippe <lkstrp@pm.me>
Co-authored-by: Fabian Neumann <fabian.neumann@outlook.de>

* resolve other deprecations

---------

Co-authored-by: Davide Fioriti <67809479+davide-f@users.noreply.github.com>
Co-authored-by: Fabian Neumann <fabian.neumann@outlook.de>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 4435f02 commit 0e0191a
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 266 deletions.
2 changes: 1 addition & 1 deletion envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
- python>=3.8
- pip

- pypsa>=0.30.3
- pypsa>=0.31
- atlite>=0.2.9
- linopy

Expand Down
2 changes: 1 addition & 1 deletion scripts/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def mock_snakemake(
import os

import snakemake as sm
from pypsa.descriptors import Dict
from pypsa.definitions.structures import Dict
from snakemake.api import Workflow
from snakemake.common import SNAKEFILE_CHOICES
from snakemake.script import Snakemake
Expand Down
10 changes: 5 additions & 5 deletions scripts/add_brownfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def add_brownfield(n, n_p, year):

# first, remove generators, links and stores that track
# CO2 or global EU values since these are already in n
n_p.mremove(c.name, c.df.index[c.df.lifetime == np.inf])
n_p.remove(c.name, c.df.index[c.df.lifetime == np.inf])

# remove assets whose build_year + lifetime <= year
n_p.mremove(c.name, c.df.index[c.df.build_year + c.df.lifetime <= year])
n_p.remove(c.name, c.df.index[c.df.build_year + c.df.lifetime <= year])

# remove assets if their optimized nominal capacity is lower than a threshold
# since CHP heat Link is proportional to CHP electric Link, make sure threshold is compatible
Expand All @@ -60,12 +60,12 @@ def add_brownfield(n, n_p, year):
* c.df.p_nom_ratio[chp_heat.str.replace("heat", "electric")].values
/ c.df.efficiency[chp_heat].values
)
n_p.mremove(
n_p.remove(
c.name,
chp_heat[c.df.loc[chp_heat, f"{attr}_nom_opt"] < threshold_chp_heat],
)

n_p.mremove(
n_p.remove(
c.name,
c.df.index[
(c.df[f"{attr}_nom_extendable"] & ~c.df.index.isin(chp_heat))
Expand All @@ -77,7 +77,7 @@ def add_brownfield(n, n_p, year):
c.df[f"{attr}_nom"] = c.df[f"{attr}_nom_opt"]
c.df[f"{attr}_nom_extendable"] = False

n.import_components_from_dataframe(c.df, c.name)
n.add(c.name, c.df.index, **c.df)

# copy time-dependent
selection = n.component_attrs[c.name].type.str.contains(
Expand Down
38 changes: 19 additions & 19 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def add_missing_carriers(n, carriers):
"""
missing_carriers = set(carriers) - set(n.carriers.index)
if len(missing_carriers) > 0:
n.madd("Carrier", missing_carriers)
n.add("Carrier", missing_carriers)


def sanitize_carriers(n, config):
Expand Down Expand Up @@ -414,7 +414,7 @@ def attach_load(
logger.info(f"Load data scaled by factor {scaling}.")
load *= scaling

n.madd("Load", load.columns, bus=load.columns, p_set=load) # carrier="electricity"
n.add("Load", load.columns, bus=load.columns, p_set=load) # carrier="electricity"


def set_transmission_costs(
Expand Down Expand Up @@ -506,7 +506,7 @@ def attach_wind_and_solar(
else:
capital_cost = costs.at[car, "capital_cost"]

n.madd(
n.add(
"Generator",
ds.indexes["bus"],
" " + car,
Expand Down Expand Up @@ -568,7 +568,7 @@ def attach_conventional_generators(
caps = ppl.groupby("carrier").p_nom.sum().div(1e3).round(2)
logger.info(f"Adding {len(ppl)} generators with capacities [GW]pp \n{caps}")

n.madd(
n.add(
"Generator",
ppl.index,
carrier=ppl.carrier,
Expand Down Expand Up @@ -640,7 +640,7 @@ def attach_hydro(n, costs, ppl, profile_hydro, hydro_capacities, carriers, **par
)

if "ror" in carriers and not ror.empty:
n.madd(
n.add(
"Generator",
ror.index,
carrier="ror",
Expand All @@ -661,7 +661,7 @@ def attach_hydro(n, costs, ppl, profile_hydro, hydro_capacities, carriers, **par
# assume no natural inflow due to lack of data
max_hours = params.get("PHS_max_hours", 6)
phs = phs.replace({"max_hours": {0: max_hours, np.nan: max_hours}})
n.madd(
n.add(
"StorageUnit",
phs.index,
carrier="PHS",
Expand Down Expand Up @@ -725,7 +725,7 @@ def attach_hydro(n, costs, ppl, profile_hydro, hydro_capacities, carriers, **par
else:
p_max_pu = 1

n.madd(
n.add(
"StorageUnit",
hydro.index,
carrier="hydro",
Expand Down Expand Up @@ -844,7 +844,7 @@ def estimate_renewable_capacities(
def attach_storageunits(n, costs, extendable_carriers, max_hours):
carriers = extendable_carriers["StorageUnit"]

n.madd("Carrier", carriers)
n.add("Carrier", carriers)

buses_i = n.buses.index

Expand All @@ -854,7 +854,7 @@ def attach_storageunits(n, costs, extendable_carriers, max_hours):
for carrier in carriers:
roundtrip_correction = 0.5 if carrier == "battery" else 1

n.madd(
n.add(
"StorageUnit",
buses_i,
" " + carrier,
Expand All @@ -875,14 +875,14 @@ def attach_storageunits(n, costs, extendable_carriers, max_hours):
def attach_stores(n, costs, extendable_carriers):
carriers = extendable_carriers["Store"]

n.madd("Carrier", carriers)
n.add("Carrier", carriers)

buses_i = n.buses.index

if "H2" in carriers:
h2_buses_i = n.madd("Bus", buses_i + " H2", carrier="H2", location=buses_i)
h2_buses_i = n.add("Bus", buses_i + " H2", carrier="H2", location=buses_i)

n.madd(
n.add(
"Store",
h2_buses_i,
bus=h2_buses_i,
Expand All @@ -892,7 +892,7 @@ def attach_stores(n, costs, extendable_carriers):
capital_cost=costs.at["hydrogen storage underground", "capital_cost"],
)

n.madd(
n.add(
"Link",
h2_buses_i + " Electrolysis",
bus0=buses_i,
Expand All @@ -904,7 +904,7 @@ def attach_stores(n, costs, extendable_carriers):
marginal_cost=costs.at["electrolysis", "marginal_cost"],
)

n.madd(
n.add(
"Link",
h2_buses_i + " Fuel Cell",
bus0=h2_buses_i,
Expand All @@ -919,11 +919,11 @@ def attach_stores(n, costs, extendable_carriers):
)

if "battery" in carriers:
b_buses_i = n.madd(
b_buses_i = n.add(
"Bus", buses_i + " battery", carrier="battery", location=buses_i
)

n.madd(
n.add(
"Store",
b_buses_i,
bus=b_buses_i,
Expand All @@ -934,9 +934,9 @@ def attach_stores(n, costs, extendable_carriers):
marginal_cost=costs.at["battery", "marginal_cost"],
)

n.madd("Carrier", ["battery charger", "battery discharger"])
n.add("Carrier", ["battery charger", "battery discharger"])

n.madd(
n.add(
"Link",
b_buses_i + " charger",
bus0=buses_i,
Expand All @@ -949,7 +949,7 @@ def attach_stores(n, costs, extendable_carriers):
marginal_cost=costs.at["battery inverter", "marginal_cost"],
)

n.madd(
n.add(
"Link",
b_buses_i + " discharger",
bus0=b_buses_i,
Expand Down
21 changes: 10 additions & 11 deletions scripts/add_existing_baseyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def add_power_capacities_installed_before_baseyear(n, grouping_years, costs, bas
p_max_pu = n.generators_t.p_max_pu[capacity.index + name_suffix_by]

if not new_build.empty:
n.madd(
n.add(
"Generator",
new_capacity.index,
suffix=name_suffix,
Expand All @@ -295,7 +295,7 @@ def add_power_capacities_installed_before_baseyear(n, grouping_years, costs, bas
missing_bus = pd.Index(bus0).difference(n.buses.index)
if not missing_bus.empty:
logger.info(f"add buses {bus0}")
n.madd(
n.add(
"Bus",
bus0,
carrier=generator,
Expand All @@ -317,7 +317,7 @@ def add_power_capacities_installed_before_baseyear(n, grouping_years, costs, bas
new_capacity = capacity.loc[new_build.str.replace(name_suffix, "")]

if generator != "urban central solid biomass CHP":
n.madd(
n.add(
"Link",
new_capacity.index,
suffix=name_suffix,
Expand All @@ -344,7 +344,7 @@ def add_power_capacities_installed_before_baseyear(n, grouping_years, costs, bas
lambda i: i + " urban central heat" if i in central_heat else ""
)

n.madd(
n.add(
"Link",
new_capacity.index,
suffix=name_suffix,
Expand Down Expand Up @@ -504,7 +504,7 @@ def add_heating_capacities_installed_before_baseyear(
else costs.at[costs_name, "efficiency"]
)

n.madd(
n.add(
"Link",
nodes,
suffix=f" {heat_system} {heat_source} heat pump-{grouping_year}",
Expand All @@ -524,7 +524,7 @@ def add_heating_capacities_installed_before_baseyear(
)

# add resistive heater, gas boilers and oil boilers
n.madd(
n.add(
"Link",
nodes,
suffix=f" {heat_system} resistive heater-{grouping_year}",
Expand All @@ -551,7 +551,7 @@ def add_heating_capacities_installed_before_baseyear(
heat_system, "gas", nodes, heating_efficiencies, costs
)

n.madd(
n.add(
"Link",
nodes,
suffix=f" {heat_system} gas boiler-{grouping_year}",
Expand All @@ -578,7 +578,7 @@ def add_heating_capacities_installed_before_baseyear(
heat_system, "oil", nodes, heating_efficiencies, costs
)

n.madd(
n.add(
"Link",
nodes,
suffix=f" {heat_system} oil boiler-{grouping_year}",
Expand All @@ -602,7 +602,7 @@ def add_heating_capacities_installed_before_baseyear(
)

# delete links with p_nom=nan corresponding to extra nodes in country
n.mremove(
n.remove(
"Link",
[
index
Expand All @@ -613,7 +613,7 @@ def add_heating_capacities_installed_before_baseyear(

# delete links with capacities below threshold
threshold = snakemake.params.existing_capacities["threshold_capacity"]
n.mremove(
n.remove(
"Link",
[
index
Expand Down Expand Up @@ -683,7 +683,6 @@ def set_defaults(n):
)

if options["heating"]:

# one could use baseyear here instead (but dangerous if no data)
fn = snakemake.input.heating_efficiencies
year = int(snakemake.params["energy_totals_year"])
Expand Down
6 changes: 3 additions & 3 deletions scripts/add_transmission_projects_and_dlr.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def attach_transmission_projects(
if df.empty:
continue
if "new_buses" in path.name:
n.madd("Bus", df.index, **df)
n.add("Bus", df.index, **df)
elif "new_lines" in path.name:
n.madd("Line", df.index, **df)
n.add("Line", df.index, **df)
elif "new_links" in path.name:
n.madd("Link", df.index, **df)
n.add("Link", df.index, **df)
elif "adjust_lines" in path.name:
n.lines.update(df)
elif "adjust_links" in path.name:
Expand Down
Loading

0 comments on commit 0e0191a

Please sign in to comment.