2020-07-07 16:20:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-02-19 15:21:48 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2020-2024 The PyPSA-Eur Authors
|
2023-03-06 17:49:23 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-09 11:48:00 +00:00
|
|
|
"""
|
|
|
|
Adds existing power and heat generation capacities for initial planning
|
|
|
|
horizon.
|
|
|
|
"""
|
2023-03-09 11:45:43 +00:00
|
|
|
|
2020-07-07 16:20:51 +00:00
|
|
|
import logging
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
2023-03-09 07:36:41 +00:00
|
|
|
import country_converter as coco
|
2020-07-07 16:20:51 +00:00
|
|
|
import numpy as np
|
2024-01-19 09:47:58 +00:00
|
|
|
import pandas as pd
|
2024-04-11 06:41:59 +00:00
|
|
|
import powerplantmatching as pm
|
2020-07-07 16:20:51 +00:00
|
|
|
import pypsa
|
|
|
|
import xarray as xr
|
2024-02-12 10:54:13 +00:00
|
|
|
from _helpers import (
|
|
|
|
configure_logging,
|
|
|
|
set_scenario_config,
|
2024-02-17 16:16:28 +00:00
|
|
|
update_config_from_wildcards,
|
2024-02-12 10:54:13 +00:00
|
|
|
)
|
2023-05-03 11:24:57 +00:00
|
|
|
from add_electricity import sanitize_carriers
|
2022-08-01 16:15:35 +00:00
|
|
|
from prepare_sector_network import cluster_heat_buses, define_spatial, prepare_costs
|
2020-07-18 09:22:30 +00:00
|
|
|
|
2024-01-19 09:47:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-03-09 07:36:18 +00:00
|
|
|
cc = coco.CountryConverter()
|
2024-01-19 09:47:58 +00:00
|
|
|
idx = pd.IndexSlice
|
2022-03-18 09:18:24 +00:00
|
|
|
spatial = SimpleNamespace()
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2020-08-12 16:08:01 +00:00
|
|
|
def add_build_year_to_new_assets(n, baseyear):
|
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-07-01 18:09:04 +00:00
|
|
|
n : pypsa.Network
|
|
|
|
baseyear : int
|
|
|
|
year in which optimized assets are built
|
2020-08-12 16:08:01 +00:00
|
|
|
"""
|
2021-07-01 18:09:04 +00:00
|
|
|
# Give assets with lifetimes and no build year the build year baseyear
|
2020-08-12 16:08:01 +00:00
|
|
|
for c in n.iterate_components(["Link", "Generator", "Store"]):
|
2022-01-07 10:38:25 +00:00
|
|
|
assets = c.df.index[(c.df.lifetime != np.inf) & (c.df.build_year == 0)]
|
2020-08-12 16:08:01 +00:00
|
|
|
c.df.loc[assets, "build_year"] = baseyear
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
# add -baseyear to name
|
2020-08-12 16:08:01 +00:00
|
|
|
rename = pd.Series(c.df.index, c.df.index)
|
2023-10-08 09:20:36 +00:00
|
|
|
rename[assets] += f"-{str(baseyear)}"
|
2020-08-12 16:08:01 +00:00
|
|
|
c.df.rename(index=rename, inplace=True)
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
# rename time-dependent
|
|
|
|
selection = n.component_attrs[c.name].type.str.contains(
|
|
|
|
"series"
|
|
|
|
) & n.component_attrs[c.name].status.str.contains("Input")
|
|
|
|
for attr in n.component_attrs[c.name].index[selection]:
|
2024-01-31 16:10:08 +00:00
|
|
|
c.pnl[attr] = c.pnl[attr].rename(columns=rename)
|
2020-08-12 16:08:01 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2024-03-06 09:26:23 +00:00
|
|
|
def add_existing_renewables(df_agg, costs):
|
2020-08-11 09:09:39 +00:00
|
|
|
"""
|
|
|
|
Append existing renewables to the df_agg pd.DataFrame with the conventional
|
|
|
|
power plants.
|
|
|
|
"""
|
2024-06-04 15:02:56 +00:00
|
|
|
tech_map = {"solar": "PV", "onwind": "Onshore", "offwind-ac": "Offshore"}
|
2021-09-27 09:16:12 +00:00
|
|
|
|
2024-05-21 13:28:24 +00:00
|
|
|
countries = snakemake.config["countries"] # noqa: F841
|
2024-04-11 06:41:59 +00:00
|
|
|
irena = pm.data.IRENASTAT().powerplant.convert_country_to_alpha2()
|
|
|
|
irena = irena.query("Country in @countries")
|
|
|
|
irena = irena.groupby(["Technology", "Country", "Year"]).Capacity.sum()
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2024-04-11 06:41:59 +00:00
|
|
|
irena = irena.unstack().reset_index()
|
|
|
|
|
|
|
|
for carrier, tech in tech_map.items():
|
|
|
|
df = (
|
|
|
|
irena[irena.Technology.str.contains(tech)]
|
|
|
|
.drop(columns=["Technology"])
|
|
|
|
.set_index("Country")
|
|
|
|
)
|
2020-08-10 18:30:29 +00:00
|
|
|
df.columns = df.columns.astype(int)
|
|
|
|
|
|
|
|
# calculate yearly differences
|
|
|
|
df.insert(loc=0, value=0.0, column="1999")
|
2021-07-01 18:09:04 +00:00
|
|
|
df = df.diff(axis=1).drop("1999", axis=1).clip(lower=0)
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
# distribute capacities among nodes according to capacity factor
|
|
|
|
# weighting with nodal_fraction
|
2020-08-17 10:08:49 +00:00
|
|
|
elec_buses = n.buses.index[n.buses.carrier == "AC"].union(
|
|
|
|
n.buses.index[n.buses.carrier == "DC"]
|
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
nodal_fraction = pd.Series(0.0, elec_buses)
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
for country in n.buses.loc[elec_buses, "country"].unique():
|
2020-08-12 16:08:01 +00:00
|
|
|
gens = n.generators.index[
|
|
|
|
(n.generators.index.str[:2] == country)
|
|
|
|
& (n.generators.carrier == carrier)
|
|
|
|
]
|
2020-08-10 18:30:29 +00:00
|
|
|
cfs = n.generators_t.p_max_pu[gens].mean()
|
2021-07-01 18:09:04 +00:00
|
|
|
cfs_key = cfs / cfs.sum()
|
2023-12-08 09:35:13 +00:00
|
|
|
nodal_fraction.loc[n.generators.loc[gens, "bus"]] = cfs_key.groupby(
|
|
|
|
n.generators.loc[gens, "bus"]
|
|
|
|
).sum()
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
nodal_df = df.loc[n.buses.loc[elec_buses, "country"]]
|
2020-08-10 18:30:29 +00:00
|
|
|
nodal_df.index = elec_buses
|
2021-07-01 18:09:04 +00:00
|
|
|
nodal_df = nodal_df.multiply(nodal_fraction, axis=0)
|
2020-08-10 18:30:29 +00:00
|
|
|
|
|
|
|
for year in nodal_df.columns:
|
|
|
|
for node in nodal_df.index:
|
2024-04-11 06:41:59 +00:00
|
|
|
name = f"{node}-{carrier}-{year}"
|
2021-07-01 18:09:04 +00:00
|
|
|
capacity = nodal_df.loc[node, year]
|
2020-08-10 18:30:29 +00:00
|
|
|
if capacity > 0.0:
|
2024-06-04 15:02:56 +00:00
|
|
|
cost_key = carrier.split("-")[0]
|
2024-04-11 06:41:59 +00:00
|
|
|
df_agg.at[name, "Fueltype"] = carrier
|
2021-07-01 18:09:04 +00:00
|
|
|
df_agg.at[name, "Capacity"] = capacity
|
|
|
|
df_agg.at[name, "DateIn"] = year
|
2024-06-04 15:02:56 +00:00
|
|
|
df_agg.at[name, "lifetime"] = costs.at[cost_key, "lifetime"]
|
2024-04-11 06:41:59 +00:00
|
|
|
df_agg.at[name, "DateOut"] = (
|
2024-06-04 15:02:56 +00:00
|
|
|
year + costs.at[cost_key, "lifetime"] - 1
|
2024-04-11 06:41:59 +00:00
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
df_agg.at[name, "cluster_bus"] = node
|
|
|
|
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2020-08-12 16:08:01 +00:00
|
|
|
def add_power_capacities_installed_before_baseyear(n, grouping_years, costs, baseyear):
|
2020-07-07 16:20:51 +00:00
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-07-01 18:09:04 +00:00
|
|
|
n : pypsa.Network
|
2021-09-27 09:16:12 +00:00
|
|
|
grouping_years :
|
2021-07-01 18:09:04 +00:00
|
|
|
intervals to group existing capacities
|
2021-09-27 09:16:12 +00:00
|
|
|
costs :
|
2021-07-01 18:09:04 +00:00
|
|
|
to read lifetime to estimate YearDecomissioning
|
|
|
|
baseyear : int
|
2020-07-07 16:20:51 +00:00
|
|
|
"""
|
2023-02-24 13:42:51 +00:00
|
|
|
logger.debug(
|
|
|
|
f"Adding power capacities installed before {baseyear} from powerplants.csv"
|
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2020-08-10 18:30:29 +00:00
|
|
|
df_agg = pd.read_csv(snakemake.input.powerplants, index_col=0)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
rename_fuel = {
|
|
|
|
"Hard Coal": "coal",
|
|
|
|
"Lignite": "lignite",
|
|
|
|
"Nuclear": "nuclear",
|
|
|
|
"Oil": "oil",
|
|
|
|
"OCGT": "OCGT",
|
|
|
|
"CCGT": "CCGT",
|
2022-08-08 11:52:41 +00:00
|
|
|
"Bioenergy": "urban central solid biomass CHP",
|
2021-07-01 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 05:24:12 +00:00
|
|
|
# Replace Fueltype "Natural Gas" with the respective technology (OCGT or CCGT)
|
|
|
|
df_agg.loc[df_agg["Fueltype"] == "Natural Gas", "Fueltype"] = df_agg.loc[
|
|
|
|
df_agg["Fueltype"] == "Natural Gas", "Technology"
|
|
|
|
]
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
fueltype_to_drop = [
|
|
|
|
"Hydro",
|
|
|
|
"Wind",
|
|
|
|
"Solar",
|
|
|
|
"Geothermal",
|
|
|
|
"Waste",
|
|
|
|
"Other",
|
|
|
|
"CCGT, Thermal",
|
|
|
|
]
|
|
|
|
|
|
|
|
technology_to_drop = ["Pv", "Storage Technologies"]
|
|
|
|
|
2024-04-11 08:29:30 +00:00
|
|
|
# drop unused fueltypes and technologies
|
2021-07-01 18:09:04 +00:00
|
|
|
df_agg.drop(df_agg.index[df_agg.Fueltype.isin(fueltype_to_drop)], inplace=True)
|
|
|
|
df_agg.drop(df_agg.index[df_agg.Technology.isin(technology_to_drop)], inplace=True)
|
2020-07-07 16:20:51 +00:00
|
|
|
df_agg.Fueltype = df_agg.Fueltype.map(rename_fuel)
|
|
|
|
|
2022-08-09 13:30:58 +00:00
|
|
|
# Intermediate fix for DateIn & DateOut
|
|
|
|
# Fill missing DateIn
|
2022-08-09 19:30:45 +00:00
|
|
|
biomass_i = df_agg.loc[df_agg.Fueltype == "urban central solid biomass CHP"].index
|
|
|
|
mean = df_agg.loc[biomass_i, "DateIn"].mean()
|
|
|
|
df_agg.loc[biomass_i, "DateIn"] = df_agg.loc[biomass_i, "DateIn"].fillna(int(mean))
|
2022-08-09 13:30:58 +00:00
|
|
|
# Fill missing DateOut
|
2023-03-06 16:19:23 +00:00
|
|
|
dateout = (
|
|
|
|
df_agg.loc[biomass_i, "DateIn"]
|
2023-06-15 16:52:25 +00:00
|
|
|
+ snakemake.params.costs["fill_values"]["lifetime"]
|
2023-03-06 16:19:23 +00:00
|
|
|
)
|
2022-08-09 19:30:45 +00:00
|
|
|
df_agg.loc[biomass_i, "DateOut"] = df_agg.loc[biomass_i, "DateOut"].fillna(dateout)
|
2022-08-09 13:30:58 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
# assign clustered bus
|
2022-04-12 12:37:05 +00:00
|
|
|
busmap_s = pd.read_csv(snakemake.input.busmap_s, index_col=0).squeeze()
|
|
|
|
busmap = pd.read_csv(snakemake.input.busmap, index_col=0).squeeze()
|
2021-07-06 07:55:41 +00:00
|
|
|
|
|
|
|
inv_busmap = {}
|
2023-02-16 19:13:26 +00:00
|
|
|
for k, v in busmap.items():
|
2021-07-06 07:55:41 +00:00
|
|
|
inv_busmap[v] = inv_busmap.get(v, []) + [k]
|
2022-01-07 10:32:31 +00:00
|
|
|
|
2020-08-10 18:30:29 +00:00
|
|
|
clustermaps = busmap_s.map(busmap)
|
|
|
|
clustermaps.index = clustermaps.index.astype(int)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2020-08-10 18:30:29 +00:00
|
|
|
df_agg["cluster_bus"] = df_agg.bus.map(clustermaps)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
# include renewables in df_agg
|
2024-03-06 09:26:23 +00:00
|
|
|
add_existing_renewables(df_agg, costs)
|
|
|
|
|
|
|
|
# drop assets which are already phased out / decommissioned
|
|
|
|
phased_out = df_agg[df_agg["DateOut"] < baseyear].index
|
|
|
|
df_agg.drop(phased_out, inplace=True)
|
2020-08-10 18:30:29 +00:00
|
|
|
|
2024-04-11 12:21:43 +00:00
|
|
|
older_assets = (df_agg.DateIn < min(grouping_years)).sum()
|
2024-04-11 06:54:15 +00:00
|
|
|
if older_assets:
|
2024-04-11 12:21:43 +00:00
|
|
|
logger.warning(
|
|
|
|
f"There are {older_assets} assets with build year "
|
|
|
|
f"before first power grouping year {min(grouping_years)}. "
|
|
|
|
"These assets are dropped and not considered."
|
|
|
|
"Consider to redefine the grouping years to keep them."
|
|
|
|
)
|
|
|
|
to_drop = df_agg[df_agg.DateIn < min(grouping_years)].index
|
2024-04-11 06:54:15 +00:00
|
|
|
df_agg.drop(to_drop, inplace=True)
|
2024-04-11 12:21:43 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
df_agg["grouping_year"] = np.take(
|
2024-04-11 12:21:43 +00:00
|
|
|
grouping_years[::-1], np.digitize(df_agg.DateIn, grouping_years[::-1])
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
|
|
|
|
2024-03-05 11:00:50 +00:00
|
|
|
# calculate (adjusted) remaining lifetime before phase-out (+1 because assuming
|
|
|
|
# phase out date at the end of the year)
|
2024-03-05 12:49:33 +00:00
|
|
|
df_agg["lifetime"] = df_agg.DateOut - df_agg["grouping_year"] + 1
|
2024-03-05 11:00:50 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
df = df_agg.pivot_table(
|
|
|
|
index=["grouping_year", "Fueltype"],
|
|
|
|
columns="cluster_bus",
|
|
|
|
values="Capacity",
|
|
|
|
aggfunc="sum",
|
|
|
|
)
|
|
|
|
|
2022-08-08 13:28:04 +00:00
|
|
|
lifetime = df_agg.pivot_table(
|
|
|
|
index=["grouping_year", "Fueltype"],
|
|
|
|
columns="cluster_bus",
|
|
|
|
values="lifetime",
|
|
|
|
aggfunc="mean", # currently taken mean for clustering lifetimes
|
|
|
|
)
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
carrier = {
|
|
|
|
"OCGT": "gas",
|
|
|
|
"CCGT": "gas",
|
|
|
|
"coal": "coal",
|
|
|
|
"oil": "oil",
|
|
|
|
"lignite": "lignite",
|
2022-08-08 13:28:04 +00:00
|
|
|
"nuclear": "uranium",
|
|
|
|
"urban central solid biomass CHP": "biomass",
|
2021-07-01 18:09:04 +00:00
|
|
|
}
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2020-08-10 18:30:29 +00:00
|
|
|
for grouping_year, generator in df.index:
|
2021-07-01 18:09:04 +00:00
|
|
|
# capacity is the capacity in MW at each node for this
|
2020-08-10 18:30:29 +00:00
|
|
|
capacity = df.loc[grouping_year, generator]
|
|
|
|
capacity = capacity[~capacity.isna()]
|
|
|
|
capacity = capacity[
|
2023-06-15 16:52:25 +00:00
|
|
|
capacity > snakemake.params.existing_capacities["threshold_capacity"]
|
2023-03-06 08:27:45 +00:00
|
|
|
]
|
2022-08-01 13:24:07 +00:00
|
|
|
suffix = "-ac" if generator == "offwind" else ""
|
2022-08-08 13:28:04 +00:00
|
|
|
name_suffix = f" {generator}{suffix}-{grouping_year}"
|
2024-04-11 08:29:30 +00:00
|
|
|
name_suffix_by = f" {generator}{suffix}-{baseyear}"
|
2022-08-01 13:24:07 +00:00
|
|
|
asset_i = capacity.index + name_suffix
|
2024-06-04 15:02:56 +00:00
|
|
|
if generator in ["solar", "onwind", "offwind-ac"]:
|
|
|
|
cost_key = generator.split("-")[0]
|
2022-01-07 11:09:02 +00:00
|
|
|
# to consider electricity grid connection costs or a split between
|
|
|
|
# solar utility and rooftop as well, rather take cost assumptions
|
|
|
|
# from existing network than from the cost database
|
2022-03-17 17:15:14 +00:00
|
|
|
capital_cost = n.generators.loc[
|
|
|
|
n.generators.carrier == generator + suffix, "capital_cost"
|
|
|
|
].mean()
|
2022-11-28 15:56:42 +00:00
|
|
|
marginal_cost = n.generators.loc[
|
|
|
|
n.generators.carrier == generator + suffix, "marginal_cost"
|
|
|
|
].mean()
|
2022-08-01 13:24:07 +00:00
|
|
|
# check if assets are already in network (e.g. for 2020)
|
|
|
|
already_build = n.generators.index.intersection(asset_i)
|
|
|
|
new_build = asset_i.difference(n.generators.index)
|
|
|
|
|
|
|
|
# this is for the year 2020
|
|
|
|
if not already_build.empty:
|
2024-06-07 16:01:59 +00:00
|
|
|
n.generators.loc[already_build, "p_nom"] = n.generators.loc[
|
|
|
|
already_build, "p_nom_min"
|
|
|
|
] = capacity.loc[already_build.str.replace(name_suffix, "")].values
|
2022-08-01 13:24:07 +00:00
|
|
|
new_capacity = capacity.loc[new_build.str.replace(name_suffix, "")]
|
2022-01-07 11:09:02 +00:00
|
|
|
|
2021-07-05 10:19:43 +00:00
|
|
|
if "m" in snakemake.wildcards.clusters:
|
2022-08-01 13:24:07 +00:00
|
|
|
for ind in new_capacity.index:
|
2021-07-05 10:19:43 +00:00
|
|
|
# existing capacities are split evenly among regions in every country
|
2023-10-08 09:20:36 +00:00
|
|
|
inv_ind = list(inv_busmap[ind])
|
2021-07-06 07:55:41 +00:00
|
|
|
|
2022-11-18 08:08:07 +00:00
|
|
|
# for offshore the splitting only includes coastal regions
|
2021-07-06 07:55:41 +00:00
|
|
|
inv_ind = [
|
2024-04-11 08:29:30 +00:00
|
|
|
i for i in inv_ind if (i + name_suffix_by) in n.generators.index
|
2021-07-06 07:55:41 +00:00
|
|
|
]
|
|
|
|
|
2024-05-13 08:51:19 +00:00
|
|
|
p_max_pu = n.generators_t.p_max_pu[
|
|
|
|
[i + name_suffix_by for i in inv_ind]
|
|
|
|
]
|
2024-04-11 08:29:30 +00:00
|
|
|
p_max_pu.columns = [i + name_suffix for i in inv_ind]
|
2022-01-07 10:32:31 +00:00
|
|
|
|
2021-07-05 10:19:43 +00:00
|
|
|
n.madd(
|
|
|
|
"Generator",
|
2021-07-06 07:55:41 +00:00
|
|
|
[i + name_suffix for i in inv_ind],
|
|
|
|
bus=ind,
|
|
|
|
carrier=generator,
|
2022-08-01 13:24:07 +00:00
|
|
|
p_nom=new_capacity[ind]
|
|
|
|
/ len(inv_ind), # split among regions in a country
|
2022-11-28 15:56:42 +00:00
|
|
|
marginal_cost=marginal_cost,
|
2022-01-07 11:09:02 +00:00
|
|
|
capital_cost=capital_cost,
|
2024-06-04 15:02:56 +00:00
|
|
|
efficiency=costs.at[cost_key, "efficiency"],
|
2021-07-06 07:55:41 +00:00
|
|
|
p_max_pu=p_max_pu,
|
|
|
|
build_year=grouping_year,
|
2024-06-04 15:02:56 +00:00
|
|
|
lifetime=costs.at[cost_key, "lifetime"],
|
2021-07-06 07:55:41 +00:00
|
|
|
)
|
|
|
|
|
2021-07-05 10:19:43 +00:00
|
|
|
else:
|
2024-04-11 08:29:30 +00:00
|
|
|
p_max_pu = n.generators_t.p_max_pu[capacity.index + name_suffix_by]
|
2021-07-06 07:55:41 +00:00
|
|
|
|
2022-08-01 13:24:07 +00:00
|
|
|
if not new_build.empty:
|
|
|
|
n.madd(
|
|
|
|
"Generator",
|
|
|
|
new_capacity.index,
|
2024-04-25 16:05:41 +00:00
|
|
|
suffix=name_suffix,
|
2022-08-01 13:24:07 +00:00
|
|
|
bus=new_capacity.index,
|
|
|
|
carrier=generator,
|
|
|
|
p_nom=new_capacity,
|
2022-11-28 15:56:42 +00:00
|
|
|
marginal_cost=marginal_cost,
|
2022-08-01 13:24:07 +00:00
|
|
|
capital_cost=capital_cost,
|
2024-06-04 15:02:56 +00:00
|
|
|
efficiency=costs.at[cost_key, "efficiency"],
|
2022-08-01 13:24:07 +00:00
|
|
|
p_max_pu=p_max_pu.rename(columns=n.generators.bus),
|
|
|
|
build_year=grouping_year,
|
2024-06-04 15:02:56 +00:00
|
|
|
lifetime=costs.at[cost_key, "lifetime"],
|
2022-08-01 13:24:07 +00:00
|
|
|
)
|
2021-07-06 07:55:41 +00:00
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
else:
|
2022-03-18 12:46:40 +00:00
|
|
|
bus0 = vars(spatial)[carrier[generator]].nodes
|
|
|
|
if "EU" not in vars(spatial)[carrier[generator]].locations:
|
2023-10-24 14:46:58 +00:00
|
|
|
bus0 = bus0.intersection(capacity.index + " " + carrier[generator])
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2023-08-23 11:27:40 +00:00
|
|
|
# check for missing bus
|
|
|
|
missing_bus = pd.Index(bus0).difference(n.buses.index)
|
|
|
|
if not missing_bus.empty:
|
|
|
|
logger.info(f"add buses {bus0}")
|
2023-08-30 10:05:16 +00:00
|
|
|
n.madd(
|
|
|
|
"Bus",
|
|
|
|
bus0,
|
|
|
|
carrier=generator,
|
|
|
|
location=vars(spatial)[carrier[generator]].locations,
|
|
|
|
unit="MWh_el",
|
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2022-08-01 13:24:07 +00:00
|
|
|
already_build = n.links.index.intersection(asset_i)
|
|
|
|
new_build = asset_i.difference(n.links.index)
|
2022-08-08 13:28:04 +00:00
|
|
|
lifetime_assets = lifetime.loc[grouping_year, generator].dropna()
|
2022-08-01 13:24:07 +00:00
|
|
|
|
|
|
|
# this is for the year 2020
|
|
|
|
if not already_build.empty:
|
|
|
|
n.links.loc[already_build, "p_nom_min"] = capacity.loc[
|
|
|
|
already_build.str.replace(name_suffix, "")
|
|
|
|
].values
|
|
|
|
|
|
|
|
if not new_build.empty:
|
|
|
|
new_capacity = capacity.loc[new_build.str.replace(name_suffix, "")]
|
|
|
|
|
2022-08-08 13:28:04 +00:00
|
|
|
if generator != "urban central solid biomass CHP":
|
|
|
|
n.madd(
|
|
|
|
"Link",
|
|
|
|
new_capacity.index,
|
|
|
|
suffix=name_suffix,
|
|
|
|
bus0=bus0,
|
|
|
|
bus1=new_capacity.index,
|
|
|
|
bus2="co2 atmosphere",
|
|
|
|
carrier=generator,
|
|
|
|
marginal_cost=costs.at[generator, "efficiency"]
|
|
|
|
* costs.at[generator, "VOM"], # NB: VOM is per MWel
|
|
|
|
capital_cost=costs.at[generator, "efficiency"]
|
|
|
|
* costs.at[generator, "fixed"], # NB: fixed cost is per MWel
|
|
|
|
p_nom=new_capacity / costs.at[generator, "efficiency"],
|
|
|
|
efficiency=costs.at[generator, "efficiency"],
|
|
|
|
efficiency2=costs.at[carrier[generator], "CO2 intensity"],
|
|
|
|
build_year=grouping_year,
|
|
|
|
lifetime=lifetime_assets.loc[new_capacity.index],
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
key = "central solid biomass CHP"
|
2024-04-11 17:14:45 +00:00
|
|
|
central_heat = n.buses.query(
|
|
|
|
"carrier == 'urban central heat'"
|
|
|
|
).location.unique()
|
|
|
|
heat_buses = new_capacity.index.map(
|
|
|
|
lambda i: i + " urban central heat" if i in central_heat else ""
|
|
|
|
)
|
|
|
|
|
2022-08-08 13:28:04 +00:00
|
|
|
n.madd(
|
|
|
|
"Link",
|
|
|
|
new_capacity.index,
|
|
|
|
suffix=name_suffix,
|
|
|
|
bus0=spatial.biomass.df.loc[new_capacity.index]["nodes"].values,
|
|
|
|
bus1=new_capacity.index,
|
2024-04-11 17:14:45 +00:00
|
|
|
bus2=heat_buses,
|
2022-08-08 13:28:04 +00:00
|
|
|
carrier=generator,
|
|
|
|
p_nom=new_capacity / costs.at[key, "efficiency"],
|
|
|
|
capital_cost=costs.at[key, "fixed"]
|
|
|
|
* costs.at[key, "efficiency"],
|
|
|
|
marginal_cost=costs.at[key, "VOM"],
|
|
|
|
efficiency=costs.at[key, "efficiency"],
|
|
|
|
build_year=grouping_year,
|
|
|
|
efficiency2=costs.at[key, "efficiency-heat"],
|
|
|
|
lifetime=lifetime_assets.loc[new_capacity.index],
|
|
|
|
)
|
2023-02-22 16:44:05 +00:00
|
|
|
# check if existing capacities are larger than technical potential
|
|
|
|
existing_large = n.generators[
|
|
|
|
n.generators["p_nom_min"] > n.generators["p_nom_max"]
|
|
|
|
].index
|
|
|
|
if len(existing_large):
|
|
|
|
logger.warning(
|
|
|
|
f"Existing capacities larger than technical potential for {existing_large},\
|
|
|
|
adjust technical potential to existing capacities"
|
|
|
|
)
|
|
|
|
n.generators.loc[existing_large, "p_nom_max"] = n.generators.loc[
|
|
|
|
existing_large, "p_nom_min"
|
|
|
|
]
|
2023-03-06 08:27:45 +00:00
|
|
|
|
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
def add_heating_capacities_installed_before_baseyear(
|
|
|
|
n,
|
|
|
|
baseyear,
|
|
|
|
grouping_years,
|
|
|
|
ashp_cop,
|
|
|
|
gshp_cop,
|
|
|
|
time_dep_hp_cop,
|
|
|
|
costs,
|
|
|
|
default_lifetime,
|
|
|
|
):
|
2020-07-07 16:20:51 +00:00
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-07-01 18:09:04 +00:00
|
|
|
n : pypsa.Network
|
|
|
|
baseyear : last year covered in the existing capacities database
|
2020-07-18 09:22:30 +00:00
|
|
|
grouping_years : intervals to group existing capacities
|
2021-07-01 18:09:04 +00:00
|
|
|
linear decommissioning of heating capacities from 2020 to 2045 is
|
|
|
|
currently assumed heating capacities split between residential and
|
|
|
|
services proportional to heating load in both 50% capacities
|
2024-04-11 08:29:30 +00:00
|
|
|
in rural buses 50% in urban buses
|
2020-07-30 06:22:27 +00:00
|
|
|
"""
|
2023-02-24 13:42:51 +00:00
|
|
|
logger.debug(f"Adding heating capacities installed before {baseyear}")
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2024-02-07 16:15:08 +00:00
|
|
|
existing_heating = pd.read_csv(
|
|
|
|
snakemake.input.existing_heating_distribution, header=[0, 1], index_col=0
|
2023-03-06 08:27:45 +00:00
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2024-01-19 17:42:49 +00:00
|
|
|
for name in existing_heating.columns.get_level_values(0).unique():
|
2020-07-30 06:22:27 +00:00
|
|
|
name_type = "central" if name == "urban central" else "decentral"
|
|
|
|
|
2024-01-19 17:42:49 +00:00
|
|
|
nodes = pd.Index(n.buses.location[n.buses.index.str.contains(f"{name} heat")])
|
2024-02-23 14:27:17 +00:00
|
|
|
|
2024-02-23 14:23:55 +00:00
|
|
|
if (name_type != "central") and options["electricity_distribution_grid"]:
|
|
|
|
nodes_elec = nodes + " low voltage"
|
2020-07-07 16:20:51 +00:00
|
|
|
else:
|
2024-02-23 14:23:55 +00:00
|
|
|
nodes_elec = nodes
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2020-07-07 16:20:51 +00:00
|
|
|
heat_pump_type = "air" if "urban" in name else "ground"
|
2020-07-30 06:22:27 +00:00
|
|
|
|
|
|
|
# Add heat pumps
|
2021-07-01 18:09:04 +00:00
|
|
|
costs_name = f"decentral {heat_pump_type}-sourced heat pump"
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
cop = {"air": ashp_cop, "ground": gshp_cop}
|
|
|
|
|
|
|
|
if time_dep_hp_cop:
|
2024-01-19 17:42:49 +00:00
|
|
|
efficiency = cop[heat_pump_type][nodes]
|
2021-07-01 18:09:04 +00:00
|
|
|
else:
|
|
|
|
efficiency = costs.at[costs_name, "efficiency"]
|
2021-09-27 09:16:12 +00:00
|
|
|
|
2024-03-14 12:44:15 +00:00
|
|
|
valid_grouping_years = pd.Series(
|
2024-04-11 07:26:30 +00:00
|
|
|
[
|
|
|
|
int(grouping_year)
|
|
|
|
for grouping_year in grouping_years
|
|
|
|
if int(grouping_year) + default_lifetime > int(baseyear)
|
|
|
|
and int(grouping_year) < int(baseyear)
|
|
|
|
]
|
|
|
|
)
|
2024-04-11 12:21:43 +00:00
|
|
|
|
2024-04-11 07:52:17 +00:00
|
|
|
# get number of years of each interval
|
2024-04-11 12:21:43 +00:00
|
|
|
_years = (
|
|
|
|
valid_grouping_years.diff()
|
|
|
|
.shift(-1)
|
|
|
|
.fillna(baseyear - valid_grouping_years.iloc[-1])
|
|
|
|
)
|
2024-03-14 12:44:15 +00:00
|
|
|
# Installation is assumed to be linear for the past
|
2024-04-11 07:52:17 +00:00
|
|
|
ratios = _years / _years.sum()
|
2022-08-09 19:30:45 +00:00
|
|
|
|
2024-03-14 12:44:15 +00:00
|
|
|
for ratio, grouping_year in zip(ratios, valid_grouping_years):
|
2020-08-11 09:09:39 +00:00
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
n.madd(
|
|
|
|
"Link",
|
2024-01-19 17:42:49 +00:00
|
|
|
nodes,
|
2021-07-01 18:09:04 +00:00
|
|
|
suffix=f" {name} {heat_pump_type} heat pump-{grouping_year}",
|
2024-02-23 14:23:55 +00:00
|
|
|
bus0=nodes_elec,
|
2024-01-19 17:42:49 +00:00
|
|
|
bus1=nodes + " " + name + " heat",
|
2021-07-01 18:09:04 +00:00
|
|
|
carrier=f"{name} {heat_pump_type} heat pump",
|
|
|
|
efficiency=efficiency,
|
|
|
|
capital_cost=costs.at[costs_name, "efficiency"]
|
|
|
|
* costs.at[costs_name, "fixed"],
|
2024-01-22 08:18:26 +00:00
|
|
|
p_nom=existing_heating.loc[nodes, (name, f"{heat_pump_type} heat pump")]
|
|
|
|
* ratio
|
|
|
|
/ costs.at[costs_name, "efficiency"],
|
2021-07-01 18:09:04 +00:00
|
|
|
build_year=int(grouping_year),
|
|
|
|
lifetime=costs.at[costs_name, "lifetime"],
|
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
|
|
|
# add resistive heater, gas boilers and oil boilers
|
2020-07-18 09:22:30 +00:00
|
|
|
n.madd(
|
|
|
|
"Link",
|
2024-01-19 17:42:49 +00:00
|
|
|
nodes,
|
2021-07-01 18:09:04 +00:00
|
|
|
suffix=f" {name} resistive heater-{grouping_year}",
|
2024-02-23 14:23:55 +00:00
|
|
|
bus0=nodes_elec,
|
2024-01-19 17:42:49 +00:00
|
|
|
bus1=nodes + " " + name + " heat",
|
2021-07-01 18:09:04 +00:00
|
|
|
carrier=name + " resistive heater",
|
2023-10-08 09:20:57 +00:00
|
|
|
efficiency=costs.at[f"{name_type} resistive heater", "efficiency"],
|
2023-10-08 09:20:36 +00:00
|
|
|
capital_cost=(
|
|
|
|
costs.at[f"{name_type} resistive heater", "efficiency"]
|
|
|
|
* costs.at[f"{name_type} resistive heater", "fixed"]
|
|
|
|
),
|
|
|
|
p_nom=(
|
2024-01-22 08:18:26 +00:00
|
|
|
existing_heating.loc[nodes, (name, "resistive heater")]
|
2023-10-08 09:20:36 +00:00
|
|
|
* ratio
|
|
|
|
/ costs.at[f"{name_type} resistive heater", "efficiency"]
|
|
|
|
),
|
2021-07-01 18:09:04 +00:00
|
|
|
build_year=int(grouping_year),
|
2024-01-19 17:42:49 +00:00
|
|
|
lifetime=costs.at[f"{name_type} resistive heater", "lifetime"],
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
n.madd(
|
|
|
|
"Link",
|
2024-01-19 17:42:49 +00:00
|
|
|
nodes,
|
2021-07-01 18:09:04 +00:00
|
|
|
suffix=f" {name} gas boiler-{grouping_year}",
|
2024-02-02 12:19:08 +00:00
|
|
|
bus0="EU gas" if "EU gas" in spatial.gas.nodes else nodes + " gas",
|
2024-01-19 17:42:49 +00:00
|
|
|
bus1=nodes + " " + name + " heat",
|
2021-07-01 18:09:04 +00:00
|
|
|
bus2="co2 atmosphere",
|
|
|
|
carrier=name + " gas boiler",
|
2023-10-08 09:20:36 +00:00
|
|
|
efficiency=costs.at[f"{name_type} gas boiler", "efficiency"],
|
2021-07-01 18:09:04 +00:00
|
|
|
efficiency2=costs.at["gas", "CO2 intensity"],
|
2023-10-08 09:20:36 +00:00
|
|
|
capital_cost=(
|
|
|
|
costs.at[f"{name_type} gas boiler", "efficiency"]
|
|
|
|
* costs.at[f"{name_type} gas boiler", "fixed"]
|
|
|
|
),
|
|
|
|
p_nom=(
|
2024-01-22 08:18:26 +00:00
|
|
|
existing_heating.loc[nodes, (name, "gas boiler")]
|
2023-10-08 09:20:36 +00:00
|
|
|
* ratio
|
|
|
|
/ costs.at[f"{name_type} gas boiler", "efficiency"]
|
|
|
|
),
|
2021-07-01 18:09:04 +00:00
|
|
|
build_year=int(grouping_year),
|
2023-10-08 09:20:36 +00:00
|
|
|
lifetime=costs.at[f"{name_type} gas boiler", "lifetime"],
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
2021-09-27 09:16:12 +00:00
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
n.madd(
|
|
|
|
"Link",
|
2024-01-19 17:42:49 +00:00
|
|
|
nodes,
|
2021-07-01 18:09:04 +00:00
|
|
|
suffix=f" {name} oil boiler-{grouping_year}",
|
2022-03-21 08:14:15 +00:00
|
|
|
bus0=spatial.oil.nodes,
|
2024-01-19 17:42:49 +00:00
|
|
|
bus1=nodes + " " + name + " heat",
|
2021-07-01 18:09:04 +00:00
|
|
|
bus2="co2 atmosphere",
|
|
|
|
carrier=name + " oil boiler",
|
|
|
|
efficiency=costs.at["decentral oil boiler", "efficiency"],
|
|
|
|
efficiency2=costs.at["oil", "CO2 intensity"],
|
|
|
|
capital_cost=costs.at["decentral oil boiler", "efficiency"]
|
|
|
|
* costs.at["decentral oil boiler", "fixed"],
|
2024-01-19 17:42:49 +00:00
|
|
|
p_nom=(
|
2024-01-22 08:18:26 +00:00
|
|
|
existing_heating.loc[nodes, (name, "oil boiler")]
|
2024-01-19 17:42:49 +00:00
|
|
|
* ratio
|
|
|
|
/ costs.at["decentral oil boiler", "efficiency"]
|
|
|
|
),
|
2021-07-01 18:09:04 +00:00
|
|
|
build_year=int(grouping_year),
|
2023-10-08 09:20:36 +00:00
|
|
|
lifetime=costs.at[f"{name_type} gas boiler", "lifetime"],
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2020-07-18 09:22:30 +00:00
|
|
|
# delete links with p_nom=nan corresponding to extra nodes in country
|
2020-08-10 18:30:29 +00:00
|
|
|
n.mremove(
|
|
|
|
"Link",
|
|
|
|
[
|
|
|
|
index
|
|
|
|
for index in n.links.index.to_list()
|
|
|
|
if str(grouping_year) in index and np.isnan(n.links.p_nom[index])
|
2023-03-06 08:27:45 +00:00
|
|
|
],
|
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2022-08-09 19:30:45 +00:00
|
|
|
# delete links with capacities below threshold
|
2023-06-15 16:52:25 +00:00
|
|
|
threshold = snakemake.params.existing_capacities["threshold_capacity"]
|
2021-07-01 18:09:04 +00:00
|
|
|
n.mremove(
|
|
|
|
"Link",
|
|
|
|
[
|
|
|
|
index
|
|
|
|
for index in n.links.index.to_list()
|
|
|
|
if str(grouping_year) in index and n.links.p_nom[index] < threshold
|
2023-03-06 08:27:45 +00:00
|
|
|
],
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2024-02-23 14:27:17 +00:00
|
|
|
# %%
|
2020-07-07 16:20:51 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if "snakemake" not in globals():
|
2023-03-06 18:09:45 +00:00
|
|
|
from _helpers import mock_snakemake
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
snakemake = mock_snakemake(
|
|
|
|
"add_existing_baseyear",
|
2024-06-04 15:02:56 +00:00
|
|
|
configfiles="config/config.yaml",
|
2021-07-01 18:09:04 +00:00
|
|
|
simpl="",
|
2024-06-04 15:02:56 +00:00
|
|
|
clusters="20",
|
|
|
|
ll="v1.5",
|
2021-09-27 09:16:12 +00:00
|
|
|
opts="",
|
2024-06-04 15:02:56 +00:00
|
|
|
sector_opts="none",
|
|
|
|
planning_horizons=2030,
|
2020-07-07 16:20:51 +00:00
|
|
|
)
|
|
|
|
|
2024-02-12 10:53:20 +00:00
|
|
|
configure_logging(snakemake)
|
|
|
|
set_scenario_config(snakemake)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2024-02-17 10:57:16 +00:00
|
|
|
update_config_from_wildcards(snakemake.config, snakemake.wildcards)
|
2022-07-20 09:35:12 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
options = snakemake.params.sector
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
baseyear = snakemake.params.baseyear
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2023-07-13 20:31:55 +00:00
|
|
|
n = pypsa.Network(snakemake.input.network)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2022-03-18 09:18:24 +00:00
|
|
|
# define spatial resolution of carriers
|
2022-03-18 12:46:40 +00:00
|
|
|
spatial = define_spatial(n.buses[n.buses.carrier == "AC"].index, options)
|
2020-08-12 16:08:01 +00:00
|
|
|
add_build_year_to_new_assets(n, baseyear)
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
Nyears = n.snapshot_weightings.generators.sum() / 8760.0
|
|
|
|
costs = prepare_costs(
|
|
|
|
snakemake.input.costs,
|
2023-06-15 16:52:25 +00:00
|
|
|
snakemake.params.costs,
|
2021-07-01 18:09:04 +00:00
|
|
|
Nyears,
|
|
|
|
)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
grouping_years_power = snakemake.params.existing_capacities["grouping_years_power"]
|
|
|
|
grouping_years_heat = snakemake.params.existing_capacities["grouping_years_heat"]
|
2022-08-02 07:18:06 +00:00
|
|
|
add_power_capacities_installed_before_baseyear(
|
|
|
|
n, grouping_years_power, costs, baseyear
|
|
|
|
)
|
2020-07-30 06:22:27 +00:00
|
|
|
|
2024-02-17 10:57:16 +00:00
|
|
|
if options["heating"]:
|
2020-07-30 06:22:27 +00:00
|
|
|
time_dep_hp_cop = options["time_dep_hp_cop"]
|
2021-07-01 18:09:04 +00:00
|
|
|
ashp_cop = (
|
|
|
|
xr.open_dataarray(snakemake.input.cop_air_total)
|
|
|
|
.to_pandas()
|
|
|
|
.reindex(index=n.snapshots)
|
2023-03-06 08:27:45 +00:00
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
gshp_cop = (
|
|
|
|
xr.open_dataarray(snakemake.input.cop_soil_total)
|
|
|
|
.to_pandas()
|
|
|
|
.reindex(index=n.snapshots)
|
2023-03-06 08:27:45 +00:00
|
|
|
)
|
2024-01-29 11:22:54 +00:00
|
|
|
default_lifetime = snakemake.params.existing_capacities[
|
|
|
|
"default_heating_lifetime"
|
|
|
|
]
|
2022-08-02 07:18:06 +00:00
|
|
|
add_heating_capacities_installed_before_baseyear(
|
|
|
|
n,
|
|
|
|
baseyear,
|
|
|
|
grouping_years_heat,
|
|
|
|
ashp_cop,
|
|
|
|
gshp_cop,
|
|
|
|
time_dep_hp_cop,
|
|
|
|
costs,
|
|
|
|
default_lifetime,
|
|
|
|
)
|
2020-07-07 16:20:51 +00:00
|
|
|
|
2023-01-30 10:55:40 +00:00
|
|
|
if options.get("cluster_heat_buses", False):
|
|
|
|
cluster_heat_buses(n)
|
|
|
|
|
2022-06-30 06:42:18 +00:00
|
|
|
n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
|
2022-08-01 16:15:35 +00:00
|
|
|
|
2023-05-03 11:24:57 +00:00
|
|
|
sanitize_carriers(n, snakemake.config)
|
2022-08-01 16:15:35 +00:00
|
|
|
|
2020-07-30 06:22:27 +00:00
|
|
|
n.export_to_netcdf(snakemake.output[0])
|