forked from open-energy-transition/pypsa-eur
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathadd_brownfield.py
More file actions
399 lines (334 loc) · 14.1 KB
/
Copy pathadd_brownfield.py
File metadata and controls
399 lines (334 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# SPDX-FileCopyrightText: Open Energy Transition gGmbH and contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
"""
Prepares brownfield data from previous planning horizon.
"""
import logging
import numpy as np
import pandas as pd
import pypsa
import xarray as xr
from scripts._helpers import (
configure_logging,
get_snapshots,
sanitize_custom_columns,
set_scenario_config,
update_config_from_wildcards,
)
from scripts.add_electricity import flatten, sanitize_carriers
from scripts.add_existing_baseyear import add_build_year_to_new_assets
logger = logging.getLogger(__name__)
idx = pd.IndexSlice
def add_brownfield(
n,
n_p,
year,
h2_retrofit=False,
h2_retrofit_capacity_per_ch4=None,
capacity_threshold=None,
):
"""
Add brownfield capacity from previous network.
Parameters
----------
n : pypsa.Network
Network to add brownfield to
n_p : pypsa.Network
Previous network to get brownfield from
year : int
Planning year
h2_retrofit : bool
Whether to allow hydrogen pipeline retrofitting
h2_retrofit_capacity_per_ch4 : float
Ratio of hydrogen to methane capacity for pipeline retrofitting
capacity_threshold : float
Threshold for removing assets with low capacity
"""
logger.info(f"Preparing brownfield for the year {year}")
# electric transmission grid set optimised capacities of previous as minimum
n.lines.s_nom_min = n_p.lines.s_nom_opt
dc_i = n.links[n.links.carrier == "DC"].index
n.links.loc[dc_i, "p_nom_min"] = n_p.links.loc[dc_i, "p_nom_opt"]
for c in n_p.iterate_components(["Link", "Generator", "Store"]):
attr = "e" if c.name == "Store" else "p"
# first, remove generators, links and stores that track
# CO2 or global EU values since these are already in n
n_p.remove(c.name, c.df.index[c.df.lifetime == np.inf])
# remove assets whose build_year + 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
chp_heat = c.df.index[
(c.df[f"{attr}_nom_extendable"] & c.df.index.str.contains("urban central"))
& c.df.index.str.contains("CHP")
& c.df.index.str.contains("heat")
]
if not chp_heat.empty:
threshold_chp_heat = (
capacity_threshold
* c.df.efficiency[chp_heat.str.replace("heat", "electric")].values
* c.df.p_nom_ratio[chp_heat.str.replace("heat", "electric")].values
/ c.df.efficiency[chp_heat].values
)
n_p.remove(
c.name,
chp_heat[c.df.loc[chp_heat, f"{attr}_nom_opt"] < threshold_chp_heat],
)
n_p.remove(
c.name,
c.df.index[
(c.df[f"{attr}_nom_extendable"] & ~c.df.index.isin(chp_heat))
& (c.df[f"{attr}_nom_opt"] < capacity_threshold)
],
)
# copy over assets but fix their capacity
c.df[f"{attr}_nom"] = c.df[f"{attr}_nom_opt"]
c.df[f"{attr}_nom_extendable"] = False
n.add(c.name, c.df.index, **c.df)
# copy time-dependent
selection = n.component_attrs[c.name].type.str.contains(
"series"
) & n.component_attrs[c.name].status.str.contains("Input")
for tattr in n.component_attrs[c.name].index[selection]:
n.import_series_from_dataframe(c.pnl[tattr], c.name, tattr)
# deal with gas network
if h2_retrofit:
# subtract the already retrofitted from the maximum capacity
h2_retrofitted_fixed_i = n.links[
(n.links.carrier == "H2 pipeline retrofitted")
& (n.links.build_year != year)
].index
h2_retrofitted = n.links[
(n.links.carrier == "H2 pipeline retrofitted")
& (n.links.build_year == year)
].index
# pipe capacity always set in prepare_sector_network to todays gas grid capacity * H2_per_CH4
# and is therefore constant up to this point
pipe_capacity = n.links.loc[h2_retrofitted, "p_nom_max"]
# already retrofitted capacity from gas -> H2
already_retrofitted = (
n.links.loc[h2_retrofitted_fixed_i, "p_nom"]
.rename(lambda x: x.split("-2")[0] + f"-{year}")
.groupby(level=0)
.sum()
)
remaining_capacity = pipe_capacity - already_retrofitted.reindex(
index=pipe_capacity.index
).fillna(0)
n.links.loc[h2_retrofitted, "p_nom_max"] = remaining_capacity
# reduce gas network capacity
gas_pipes_i = n.links[n.links.carrier == "gas pipeline"].index
if not gas_pipes_i.empty:
# subtract the already retrofitted from today's gas grid capacity
pipe_capacity = n.links.loc[gas_pipes_i, "p_nom"]
fr = "H2 pipeline retrofitted"
to = "gas pipeline"
CH4_per_H2 = 1 / h2_retrofit_capacity_per_ch4
already_retrofitted.index = already_retrofitted.index.str.replace(fr, to)
remaining_capacity = (
pipe_capacity
- CH4_per_H2
* already_retrofitted.reindex(index=pipe_capacity.index).fillna(0)
)
n.links.loc[gas_pipes_i, "p_nom"] = remaining_capacity
n.links.loc[gas_pipes_i, "p_nom_max"] = remaining_capacity
def disable_grid_expansion_if_limit_hit(n):
"""
Check if transmission expansion limit is already reached; then turn off.
In particular, this function checks if the total transmission
capital cost or volume implied by s_nom_min and p_nom_min are
numerically close to the respective global limit set in
n.global_constraints. If so, the nominal capacities are set to the
minimum and extendable is turned off; the corresponding global
constraint is then dropped.
"""
types = {"expansion_cost": "capital_cost", "volume_expansion": "length"}
for limit_type in types:
glcs = n.global_constraints.query(f"type == 'transmission_{limit_type}_limit'")
for name, glc in glcs.iterrows():
total_expansion = (
(
n.lines.query("s_nom_extendable")
.eval(f"s_nom_min * {types[limit_type]}")
.sum()
)
+ (
n.links.query("carrier == 'DC' and p_nom_extendable")
.eval(f"p_nom_min * {types[limit_type]}")
.sum()
)
).sum()
# Allow small numerical differences
if np.abs(glc.constant - total_expansion) / glc.constant < 1e-6:
logger.info(
f"Transmission expansion {limit_type} is already reached, disabling expansion and limit"
)
extendable_acs = n.lines.query("s_nom_extendable").index
n.lines.loc[extendable_acs, "s_nom_extendable"] = False
n.lines.loc[extendable_acs, "s_nom"] = n.lines.loc[
extendable_acs, "s_nom_min"
]
extendable_dcs = n.links.query(
"carrier == 'DC' and p_nom_extendable"
).index
n.links.loc[extendable_dcs, "p_nom_extendable"] = False
n.links.loc[extendable_dcs, "p_nom"] = n.links.loc[
extendable_dcs, "p_nom_min"
]
n.global_constraints.drop(name, inplace=True)
def adjust_renewable_profiles(
n, input_profiles, params, year, tyndp_renewable_carriers
):
"""
Adjusts renewable profiles according to the renewable technology specified,
using the latest year below or equal to the selected year.
"""
# temporal clustering
dr = get_snapshots(params["snapshots"], params["drop_leap_day"])
snapshotmaps = (
pd.Series(dr, index=dr).where(lambda x: x.isin(n.snapshots), pd.NA).ffill()
)
# TODO: hotfix remove filter for tyndp_renewable_carriers after tyndp generators are added
if len(tyndp_renewable_carriers) > 0:
logger.info(
f"Hotfix until TYNDP renewable carriers are added. Skipping renewable carriers '{', '.join(tyndp_renewable_carriers)}'."
)
for carrier in set(params["carriers"]) - set(tyndp_renewable_carriers):
if carrier == "hydro":
continue
with xr.open_dataset(getattr(input_profiles, "profile_" + carrier)) as ds:
if ds.indexes["bus"].empty or "year" not in ds.indexes:
continue
ds = ds.stack(bus_bin=["bus", "bin"])
closest_year = max(
(y for y in ds.year.values if y <= year), default=min(ds.year.values)
)
p_max_pu = ds["profile"].sel(year=closest_year).to_pandas()
p_max_pu.columns = p_max_pu.columns.map(flatten) + f" {carrier}"
# temporal_clustering
p_max_pu = p_max_pu.groupby(snapshotmaps).mean()
# replace renewable time series
n.generators_t.p_max_pu.loc[:, p_max_pu.columns] = p_max_pu
def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int):
"""
Update the efficiency of heat pumps from previous years to current year
(e.g. 2030 heat pumps receive 2040 heat pump COPs in 2030).
Parameters
----------
n : pypsa.Network
The original network.
n_p : pypsa.Network
The network with the updated parameters.
year : int
The year for which the efficiency is being updated.
Returns
-------
None
This function updates the efficiency in place and does not return a value.
"""
# get names of heat pumps in previous iteration that cannot be replaced by direct utilisation in this iteration
heat_pump_idx_previous_iteration = n_p.links.index[
n_p.links.index.str.contains("heat pump")
& n_p.links.index.str[:-4].isin(
n.links_t.efficiency.columns.str.rstrip( # sources that can be directly used are no longer represented by heat pumps in the dynamic efficiency dataframe
str(year)
)
)
]
# construct names of same-technology heat pumps in the current iteration
corresponding_idx_this_iteration = heat_pump_idx_previous_iteration.str[:-4] + str(
year
)
# update efficiency of heat pumps in previous iteration in-place to efficiency in this iteration
n_p.links_t["efficiency"].loc[:, heat_pump_idx_previous_iteration] = (
n.links_t["efficiency"].loc[:, corresponding_idx_this_iteration].values
)
# Change efficiency2 for heat pumps that use an explicitly modelled heat source
previous_iteration_columns = heat_pump_idx_previous_iteration.intersection(
n_p.links_t["efficiency2"].columns
)
current_iteration_columns = corresponding_idx_this_iteration.intersection(
n.links_t["efficiency2"].columns
)
n_p.links_t["efficiency2"].loc[:, previous_iteration_columns] = (
n.links_t["efficiency2"].loc[:, current_iteration_columns].values
)
def update_dynamic_ptes_capacity(
n: pypsa.Network, n_p: pypsa.Network, year: int
) -> None:
"""
Updates dynamic pit storage capacity based on district heating temperature changes.
Parameters
----------
n : pypsa.Network
Original network.
n_p : pypsa.Network
Network with updated parameters.
year : int
Target year for capacity update.
Returns
-------
None
Updates capacity in-place.
"""
# pit storages in previous iteration
dynamic_ptes_idx_previous_iteration = n_p.stores.index[
n_p.stores.index.str.contains("water pits")
]
# construct names of same-technology dynamic pit storage in the current iteration
corresponding_idx_this_iteration = dynamic_ptes_idx_previous_iteration.str[
:-4
] + str(year)
# update pit storage capacity in previous iteration in-place to capacity in this iteration
n_p.stores_t.e_max_pu[dynamic_ptes_idx_previous_iteration] = n.stores_t.e_max_pu[
corresponding_idx_this_iteration
].values
if __name__ == "__main__":
if "snakemake" not in globals():
from scripts._helpers import mock_snakemake
snakemake = mock_snakemake(
"add_brownfield",
clusters="39",
opts="",
sector_opts="",
planning_horizons=2050,
)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)
update_config_from_wildcards(snakemake.config, snakemake.wildcards)
logger.info(f"Preparing brownfield from the file {snakemake.input.network_p}")
year = int(snakemake.wildcards.planning_horizons)
n = pypsa.Network(snakemake.input.network)
tyndp_renewable_carriers = (
[
subcarrier
for carrier in snakemake.params.electricity["pecd_renewable_profiles"][
"technologies"
].values()
for subcarrier in carrier
]
if snakemake.params.electricity["pecd_renewable_profiles"]["enable"]
else []
)
adjust_renewable_profiles(
n, snakemake.input, snakemake.params, year, tyndp_renewable_carriers
)
add_build_year_to_new_assets(n, year)
n_p = pypsa.Network(snakemake.input.network_p)
update_heat_pump_efficiency(n, n_p, year)
if snakemake.params.tes and snakemake.params.dynamic_ptes_capacity:
update_dynamic_ptes_capacity(n, n_p, year)
add_brownfield(
n,
n_p,
year,
h2_retrofit=snakemake.params.H2_retrofit,
h2_retrofit_capacity_per_ch4=snakemake.params.H2_retrofit_capacity_per_CH4,
capacity_threshold=snakemake.params.threshold_capacity,
)
disable_grid_expansion_if_limit_hit(n)
n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
sanitize_custom_columns(n)
sanitize_carriers(n, snakemake.config)
n.export_to_netcdf(snakemake.output[0])