-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathadd_electricity.py
executable file
·1225 lines (1036 loc) · 40.9 KB
/
add_electricity.py
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
"""
Adds existing electrical generators, hydro-electric plants as well as
greenfield and battery and hydrogen storage to the clustered network.
Description
-----------
The rule :mod:`add_electricity` ties all the different data inputs from the
preceding rules together into a detailed PyPSA network that is stored in
``networks/base_s_{clusters}_elec.nc``. It includes:
- today's transmission topology and transfer capacities (optionally including
lines which are under construction according to the config settings ``lines:
under_construction`` and ``links: under_construction``),
- today's thermal and hydro power generation capacities (for the technologies
listed in the config setting ``electricity: conventional_carriers``), and
- today's load time-series (upsampled in a top-down approach according to
population and gross domestic product)
It further adds extendable ``generators`` with **zero** capacity for
- photovoltaic, onshore and AC- as well as DC-connected offshore wind
installations with today's locational, hourly wind and solar capacity factors
(but **no** current capacities),
- additional open- and combined-cycle gas turbines (if ``OCGT`` and/or ``CCGT``
is listed in the config setting ``electricity: extendable_carriers``)
Furthermore, it attaches additional extendable components to the clustered
network with **zero** initial capacity:
- ``StorageUnits`` of carrier 'H2' and/or 'battery'. If this option is chosen,
every bus is given an extendable ``StorageUnit`` of the corresponding carrier.
The energy and power capacities are linked through a parameter that specifies
the energy capacity as maximum hours at full dispatch power and is configured
in ``electricity: max_hours:``. This linkage leads to one investment variable
per storage unit. The default ``max_hours`` lead to long-term hydrogen and
short-term battery storage units.
- ``Stores`` of carrier 'H2' and/or 'battery' in combination with ``Links``. If
this option is chosen, the script adds extra buses with corresponding carrier
where energy ``Stores`` are attached and which are connected to the
corresponding power buses via two links, one each for charging and
discharging. This leads to three investment variables for the energy capacity,
charging and discharging capacity of the storage unit.
"""
import logging
import numpy as np
import pandas as pd
import powerplantmatching as pm
import pypsa
import xarray as xr
from _helpers import (
configure_logging,
get_snapshots,
rename_techs,
set_scenario_config,
update_p_nom_max,
)
from powerplantmatching.export import map_country_bus
from pypsa.clustering.spatial import DEFAULT_ONE_PORT_STRATEGIES, normed_or_uniform
idx = pd.IndexSlice
logger = logging.getLogger(__name__)
def normed(s: pd.Series) -> pd.Series:
"""
Normalize a pandas Series by dividing each element by the sum of all elements.
Parameters
----------
s : pd.Series
Input series to normalize
Returns
-------
pd.Series
Normalized series where all elements sum to 1
"""
return s / s.sum()
def calculate_annuity(n: float, r: float | pd.Series) -> float | pd.Series:
"""
Calculate the annuity factor for an asset with lifetime n years and discount rate r.
The annuity factor is used to calculate the annual payment required to pay off a loan
over n years at interest rate r. For example, annuity(20, 0.05) * 20 = 1.6.
Parameters
----------
n : float
Lifetime of the asset in years
r : float | pd.Series
Discount rate (interest rate). Can be a single float or a pandas Series of rates.
Returns
-------
float | pd.Series
Annuity factor. Returns a float if r is float, or pd.Series if r is pd.Series.
Examples
--------
>>> calculate_annuity(20, 0.05)
0.08024258718774728
"""
if isinstance(r, pd.Series):
return pd.Series(1 / n, index=r.index).where(
r == 0, r / (1.0 - 1.0 / (1.0 + r) ** n)
)
elif r > 0:
return r / (1.0 - 1.0 / (1.0 + r) ** n)
else:
return 1 / n
def add_missing_carriers(n, carriers):
"""
Function to add missing carriers to the network without raising errors.
"""
missing_carriers = set(carriers) - set(n.carriers.index)
if len(missing_carriers) > 0:
n.add("Carrier", missing_carriers)
def sanitize_carriers(n, config):
"""
Sanitize the carrier information in a PyPSA Network object.
The function ensures that all unique carrier names are present in the network's
carriers attribute, and adds nice names and colors for each carrier according
to the provided configuration dictionary.
Parameters
----------
n : pypsa.Network
A PyPSA Network object that represents an electrical power system.
config : dict
A dictionary containing configuration information, specifically the
"plotting" key with "nice_names" and "tech_colors" keys for carriers.
Returns
-------
None
The function modifies the 'n' PyPSA Network object in-place, updating the
carriers attribute with nice names and colors.
Warnings
--------
Raises a warning if any carrier's "tech_colors" are not defined in the config dictionary.
"""
for c in n.iterate_components():
if "carrier" in c.df:
add_missing_carriers(n, c.df.carrier)
carrier_i = n.carriers.index
nice_names = (
pd.Series(config["plotting"]["nice_names"])
.reindex(carrier_i)
.fillna(carrier_i.to_series())
)
n.carriers["nice_name"] = n.carriers.nice_name.where(
n.carriers.nice_name != "", nice_names
)
tech_colors = config["plotting"]["tech_colors"]
colors = pd.Series(tech_colors).reindex(carrier_i)
# try to fill missing colors with tech_colors after renaming
missing_colors_i = colors[colors.isna()].index
colors[missing_colors_i] = missing_colors_i.map(rename_techs).map(tech_colors)
if colors.isna().any():
missing_i = list(colors.index[colors.isna()])
logger.warning(f"tech_colors for carriers {missing_i} not defined in config.")
n.carriers["color"] = n.carriers.color.where(n.carriers.color != "", colors)
def sanitize_locations(n):
if "location" in n.buses.columns:
n.buses["x"] = n.buses.x.where(n.buses.x != 0, n.buses.location.map(n.buses.x))
n.buses["y"] = n.buses.y.where(n.buses.y != 0, n.buses.location.map(n.buses.y))
n.buses["country"] = n.buses.country.where(
n.buses.country.ne("") & n.buses.country.notnull(),
n.buses.location.map(n.buses.country),
)
def add_co2_emissions(n, costs, carriers):
"""
Add CO2 emissions to the network's carriers attribute.
"""
suptechs = n.carriers.loc[carriers].index.str.split("-").str[0]
n.carriers.loc[carriers, "co2_emissions"] = costs.co2_emissions[suptechs].values
def load_costs(tech_costs, config, max_hours, Nyears=1.0):
for key in ("marginal_cost", "capital_cost"):
if key in config:
config["overwrites"][key] = config[key]
# set all asset costs and other parameters
costs = pd.read_csv(tech_costs, index_col=[0, 1]).sort_index()
# correct units from kW to MW
costs.loc[costs.unit.str.contains("/kW"), "value"] *= 1e3
costs.unit = costs.unit.str.replace("/kW", "/MW")
# correct units from GW to MW
costs.loc[costs.unit.str.contains("/GW"), "value"] /= 1e3
costs.unit = costs.unit.str.replace("/GW", "/MW")
fill_values = config["fill_values"]
costs = costs.value.unstack().fillna(fill_values)
for attr in ("investment", "lifetime", "FOM", "VOM", "efficiency", "fuel"):
overwrites = config["overwrites"].get(attr)
if overwrites is not None:
overwrites = pd.Series(overwrites)
costs.loc[overwrites.index, attr] = overwrites
logger.info(
f"Overwriting {attr} of {overwrites.index} to {overwrites.values}"
)
costs["capital_cost"] = (
(
calculate_annuity(costs["lifetime"], costs["discount rate"])
+ costs["FOM"] / 100.0
)
* costs["investment"]
* Nyears
)
costs.at["OCGT", "fuel"] = costs.at["gas", "fuel"]
costs.at["CCGT", "fuel"] = costs.at["gas", "fuel"]
costs["marginal_cost"] = costs["VOM"] + costs["fuel"] / costs["efficiency"]
costs = costs.rename(columns={"CO2 intensity": "co2_emissions"})
costs.at["OCGT", "co2_emissions"] = costs.at["gas", "co2_emissions"]
costs.at["CCGT", "co2_emissions"] = costs.at["gas", "co2_emissions"]
costs.at["solar", "capital_cost"] = costs.at["solar-utility", "capital_cost"]
costs = costs.rename({"solar-utility single-axis tracking": "solar-hsat"})
def costs_for_storage(store, link1, link2=None, max_hours=1.0):
capital_cost = link1["capital_cost"] + max_hours * store["capital_cost"]
if link2 is not None:
capital_cost += link2["capital_cost"]
return pd.Series(
dict(capital_cost=capital_cost, marginal_cost=0.0, co2_emissions=0.0)
)
costs.loc["battery"] = costs_for_storage(
costs.loc["battery storage"],
costs.loc["battery inverter"],
max_hours=max_hours["battery"],
)
costs.loc["H2"] = costs_for_storage(
costs.loc["hydrogen storage underground"],
costs.loc["fuel cell"],
costs.loc["electrolysis"],
max_hours=max_hours["H2"],
)
for attr in ("marginal_cost", "capital_cost"):
overwrites = config["overwrites"].get(attr)
if overwrites is not None:
overwrites = pd.Series(overwrites)
costs.loc[overwrites.index, attr] = overwrites
logger.info(
f"Overwriting {attr} of {overwrites.index} to {overwrites.values}"
)
return costs
def load_and_aggregate_powerplants(
ppl_fn: str,
costs: pd.DataFrame,
consider_efficiency_classes: bool = False,
aggregation_strategies: dict = None,
exclude_carriers: list = None,
) -> pd.DataFrame:
if not aggregation_strategies:
aggregation_strategies = {}
if not exclude_carriers:
exclude_carriers = []
carrier_dict = {
"ocgt": "OCGT",
"ccgt": "CCGT",
"bioenergy": "biomass",
"ccgt, thermal": "CCGT",
"hard coal": "coal",
}
tech_dict = {
"Run-Of-River": "ror",
"Reservoir": "hydro",
"Pumped Storage": "PHS",
}
ppl = (
pd.read_csv(ppl_fn, index_col=0, dtype={"bus": "str"})
.powerplant.to_pypsa_names()
.rename(columns=str.lower)
.replace({"carrier": carrier_dict, "technology": tech_dict})
)
# Replace carriers "natural gas" and "hydro" with the respective technology;
# OCGT or CCGT and hydro, PHS, or ror)
ppl["carrier"] = ppl.carrier.where(
~ppl.carrier.isin(["hydro", "natural gas"]), ppl.technology
)
cost_columns = [
"VOM",
"FOM",
"efficiency",
"capital_cost",
"marginal_cost",
"fuel",
"lifetime",
]
ppl = ppl.join(costs[cost_columns], on="carrier", rsuffix="_r")
ppl["efficiency"] = ppl.efficiency.combine_first(ppl.efficiency_r)
ppl["lifetime"] = (ppl.dateout - ppl.datein).fillna(np.inf)
ppl["build_year"] = ppl.datein.fillna(0).astype(int)
ppl["marginal_cost"] = (
ppl.carrier.map(costs.VOM) + ppl.carrier.map(costs.fuel) / ppl.efficiency
)
strategies = {
**DEFAULT_ONE_PORT_STRATEGIES,
**{"country": "first"},
**aggregation_strategies.get("generators", {}),
}
strategies = {k: v for k, v in strategies.items() if k in ppl.columns}
to_aggregate = ~ppl.carrier.isin(exclude_carriers)
df = ppl[to_aggregate].copy()
if consider_efficiency_classes:
for c in df.carrier.unique():
df_c = df.query("carrier == @c")
low = df_c.efficiency.quantile(0.10)
high = df_c.efficiency.quantile(0.90)
if low < high:
labels = ["low", "medium", "high"]
suffix = pd.cut(
df_c.efficiency, bins=[0, low, high, 1], labels=labels
).astype(str)
df.update({"carrier": df_c.carrier + " " + suffix + " efficiency"})
grouper = ["bus", "carrier"]
weights = df.groupby(grouper).p_nom.transform(normed_or_uniform)
for k, v in strategies.items():
if v == "capacity_weighted_average":
df[k] = df[k] * weights
strategies[k] = pd.Series.sum
aggregated = df.groupby(grouper, as_index=False).agg(strategies)
aggregated.index = aggregated.bus + " " + aggregated.carrier
aggregated.build_year = aggregated.build_year.astype(int)
disaggregated = ppl[~to_aggregate][aggregated.columns].copy()
disaggregated.index = (
disaggregated.bus
+ " "
+ disaggregated.carrier
+ " "
+ disaggregated.index.astype(str)
)
return pd.concat([aggregated, disaggregated])
def attach_load(
n: pypsa.Network,
load_fn: str,
busmap_fn: str,
scaling: float = 1.0,
) -> None:
"""
Attach load data to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the load data to.
load_fn : str
Path to the load data file.
busmap_fn : str
Path to the busmap file.
scaling : float, optional
Scaling factor for the load data, by default 1.0.
"""
load = (
xr.open_dataarray(load_fn).to_dataframe().squeeze(axis=1).unstack(level="time")
)
# apply clustering busmap
busmap = pd.read_csv(busmap_fn, dtype=str).set_index("Bus").squeeze()
load = load.groupby(busmap).sum().T
logger.info(f"Load data scaled by factor {scaling}.")
load *= scaling
n.add("Load", load.columns, bus=load.columns, p_set=load) # carrier="electricity"
def set_transmission_costs(
n: pypsa.Network,
costs: pd.DataFrame,
line_length_factor: float = 1.0,
link_length_factor: float = 1.0,
) -> None:
"""
Set the transmission costs for lines and links in the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to set the transmission costs for.
costs : pd.DataFrame
DataFrame containing the cost data.
line_length_factor : float, optional
Factor to scale the line length, by default 1.0.
link_length_factor : float, optional
Factor to scale the link length, by default 1.0.
"""
n.lines["capital_cost"] = (
n.lines["length"]
* line_length_factor
* costs.at["HVAC overhead", "capital_cost"]
)
if n.links.empty:
return
dc_b = n.links.carrier == "DC"
# If there are no dc links, then the 'underwater_fraction' column
# may be missing. Therefore we have to return here.
if n.links.loc[dc_b].empty:
return
costs = (
n.links.loc[dc_b, "length"]
* link_length_factor
* (
(1.0 - n.links.loc[dc_b, "underwater_fraction"])
* costs.at["HVDC overhead", "capital_cost"]
+ n.links.loc[dc_b, "underwater_fraction"]
* costs.at["HVDC submarine", "capital_cost"]
)
+ costs.at["HVDC inverter pair", "capital_cost"]
)
n.links.loc[dc_b, "capital_cost"] = costs
def attach_wind_and_solar(
n: pypsa.Network,
costs: pd.DataFrame,
profile_filenames: dict,
carriers: list | set,
extendable_carriers: list | set,
line_length_factor: float = 1.0,
landfall_lengths: dict = None,
) -> None:
"""
Attach wind and solar generators to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the generators to.
costs : pd.DataFrame
DataFrame containing the cost data.
profile_filenames : dict
Dictionary containing the paths to the wind and solar profiles.
carriers : list | set
List of renewable energy carriers to attach.
extendable_carriers : list | set
List of extendable renewable energy carriers.
line_length_factor : float, optional
Factor to scale the line length, by default 1.0.
landfall_lengths : dict, optional
Dictionary containing the landfall lengths for offshore wind, by default None.
"""
add_missing_carriers(n, carriers)
if landfall_lengths is None:
landfall_lengths = {}
for car in carriers:
if car == "hydro":
continue
landfall_length = landfall_lengths.get(car, 0.0)
with xr.open_dataset(profile_filenames["profile_" + car]) as ds:
if ds.indexes["bus"].empty:
continue
# if-statement for compatibility with old profiles
if "year" in ds.indexes:
ds = ds.sel(year=ds.year.min(), drop=True)
supcar = car.split("-", 2)[0]
if supcar == "offwind":
distance = ds["average_distance"].to_pandas()
submarine_cost = costs.at[car + "-connection-submarine", "capital_cost"]
underground_cost = costs.at[
car + "-connection-underground", "capital_cost"
]
connection_cost = line_length_factor * (
distance * submarine_cost + landfall_length * underground_cost
)
capital_cost = (
costs.at["offwind", "capital_cost"]
+ costs.at[car + "-station", "capital_cost"]
+ connection_cost
)
logger.info(
f"Added connection cost of {connection_cost.min():0.0f}-{connection_cost.max():0.0f} Eur/MW/a to {car}"
)
else:
capital_cost = costs.at[car, "capital_cost"]
n.add(
"Generator",
ds.indexes["bus"],
" " + car,
bus=ds.indexes["bus"],
carrier=car,
p_nom_extendable=car in extendable_carriers["Generator"],
p_nom_max=ds["p_nom_max"].to_pandas(),
marginal_cost=costs.at[supcar, "marginal_cost"],
capital_cost=capital_cost,
efficiency=costs.at[supcar, "efficiency"],
p_max_pu=ds["profile"].transpose("time", "bus").to_pandas(),
lifetime=costs.at[supcar, "lifetime"],
)
def attach_conventional_generators(
n: pypsa.Network,
costs: pd.DataFrame,
ppl: pd.DataFrame,
conventional_carriers: list,
extendable_carriers: dict,
conventional_params: dict,
conventional_inputs: dict,
unit_commitment: pd.DataFrame = None,
fuel_price: pd.DataFrame = None,
):
"""
Attach conventional generators to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the generators to.
costs : pd.DataFrame
DataFrame containing the cost data.
ppl : pd.DataFrame
DataFrame containing the power plant data.
conventional_carriers : list
List of conventional energy carriers.
extendable_carriers : dict
Dictionary of extendable energy carriers.
conventional_params : dict
Dictionary of conventional generator parameters.
conventional_inputs : dict
Dictionary of conventional generator inputs.
unit_commitment : pd.DataFrame, optional
DataFrame containing unit commitment data, by default None.
fuel_price : pd.DataFrame, optional
DataFrame containing fuel price data, by default None.
"""
carriers = list(set(conventional_carriers) | set(extendable_carriers["Generator"]))
ppl = ppl.query("carrier in @carriers")
# reduce carriers to those in power plant dataset
carriers = list(set(carriers) & set(ppl.carrier.unique()))
add_missing_carriers(n, carriers)
add_co2_emissions(n, costs, carriers)
if unit_commitment is not None:
committable_attrs = ppl.carrier.isin(unit_commitment).to_frame("committable")
for attr in unit_commitment.index:
default = pypsa.components.component_attrs["Generator"].default[attr]
committable_attrs[attr] = ppl.carrier.map(unit_commitment.loc[attr]).fillna(
default
)
else:
committable_attrs = {}
if fuel_price is not None:
fuel_price = fuel_price.assign(
OCGT=fuel_price["gas"], CCGT=fuel_price["gas"]
).drop("gas", axis=1)
missing_carriers = list(set(carriers) - set(fuel_price))
fuel_price = fuel_price.assign(**costs.fuel[missing_carriers])
fuel_price = fuel_price.reindex(ppl.carrier, axis=1)
fuel_price.columns = ppl.index
marginal_cost = fuel_price.div(ppl.efficiency).add(ppl.carrier.map(costs.VOM))
else:
marginal_cost = ppl.marginal_cost
# Define generators using modified ppl DataFrame
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.add(
"Generator",
ppl.index,
carrier=ppl.carrier,
bus=ppl.bus,
p_nom_min=ppl.p_nom.where(ppl.carrier.isin(conventional_carriers), 0),
p_nom=ppl.p_nom.where(ppl.carrier.isin(conventional_carriers), 0),
p_nom_extendable=ppl.carrier.isin(extendable_carriers["Generator"]),
efficiency=ppl.efficiency,
marginal_cost=marginal_cost,
capital_cost=ppl.capital_cost,
build_year=ppl.build_year,
lifetime=ppl.lifetime,
**committable_attrs,
)
for carrier in set(conventional_params) & set(carriers):
# Generators with technology affected
idx = n.generators.query("carrier == @carrier").index
for attr in list(set(conventional_params[carrier]) & set(n.generators)):
values = conventional_params[carrier][attr]
if f"conventional_{carrier}_{attr}" in conventional_inputs:
# Values affecting generators of technology k country-specific
# First map generator buses to countries; then map countries to p_max_pu
values = pd.read_csv(
conventional_inputs[f"conventional_{carrier}_{attr}"], index_col=0
).iloc[:, 0]
bus_values = n.buses.country.map(values)
n.generators.update(
{attr: n.generators.loc[idx].bus.map(bus_values).dropna()}
)
else:
# Single value affecting all generators of technology k indiscriminantely of country
n.generators.loc[idx, attr] = values
def attach_hydro(
n: pypsa.Network,
costs: pd.DataFrame,
ppl: pd.DataFrame,
profile_hydro: str,
hydro_capacities: str,
carriers: list,
**params,
):
"""
Attach hydro generators and storage units to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the hydro units to.
costs : pd.DataFrame
DataFrame containing the cost data.
ppl : pd.DataFrame
DataFrame containing the power plant data.
profile_hydro : str
Path to the hydro profile data.
hydro_capacities : str
Path to the hydro capacities data.
carriers : list
List of hydro energy carriers.
**params :
Additional parameters for hydro units.
"""
add_missing_carriers(n, carriers)
add_co2_emissions(n, costs, carriers)
ror = ppl.query('carrier == "ror"')
phs = ppl.query('carrier == "PHS"')
hydro = ppl.query('carrier == "hydro"')
country = ppl["bus"].map(n.buses.country).rename("country")
inflow_idx = ror.index.union(hydro.index)
if not inflow_idx.empty:
dist_key = ppl.loc[inflow_idx, "p_nom"].groupby(country).transform(normed)
with xr.open_dataarray(profile_hydro) as inflow:
inflow_countries = pd.Index(country[inflow_idx])
missing_c = inflow_countries.unique().difference(
inflow.indexes["countries"]
)
assert missing_c.empty, (
f"'{profile_hydro}' is missing "
f"inflow time-series for at least one country: {', '.join(missing_c)}"
)
inflow_t = (
inflow.sel(countries=inflow_countries)
.rename({"countries": "name"})
.assign_coords(name=inflow_idx)
.transpose("time", "name")
.to_pandas()
.multiply(dist_key, axis=1)
)
if "ror" in carriers and not ror.empty:
n.add(
"Generator",
ror.index,
carrier="ror",
bus=ror["bus"],
p_nom=ror["p_nom"],
efficiency=costs.at["ror", "efficiency"],
capital_cost=costs.at["ror", "capital_cost"],
weight=ror["p_nom"],
p_max_pu=(
inflow_t[ror.index] # pylint: disable=E0606
.divide(ror["p_nom"], axis=1)
.where(lambda df: df <= 1.0, other=1.0)
),
)
if "PHS" in carriers and not phs.empty:
# fill missing max hours to params value and
# 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.add(
"StorageUnit",
phs.index,
carrier="PHS",
bus=phs["bus"],
p_nom=phs["p_nom"],
capital_cost=costs.at["PHS", "capital_cost"],
max_hours=phs["max_hours"],
efficiency_store=np.sqrt(costs.at["PHS", "efficiency"]),
efficiency_dispatch=np.sqrt(costs.at["PHS", "efficiency"]),
cyclic_state_of_charge=True,
)
if "hydro" in carriers and not hydro.empty:
hydro_max_hours = params.get("hydro_max_hours")
assert hydro_capacities is not None, "No path for hydro capacities given."
hydro_stats = pd.read_csv(
hydro_capacities, comment="#", na_values="-", index_col=0
)
e_target = hydro_stats["E_store[TWh]"].clip(lower=0.2) * 1e6
e_installed = hydro.eval("p_nom * max_hours").groupby(hydro.country).sum()
e_missing = e_target - e_installed
missing_mh_i = hydro.query("max_hours.isnull() or max_hours == 0").index
# some countries may have missing storage capacity but only one plant
# which needs to be scaled to the target storage capacity
missing_mh_single_i = hydro.index[
~hydro.country.duplicated() & hydro.country.isin(e_missing.dropna().index)
]
missing_mh_i = missing_mh_i.union(missing_mh_single_i)
if hydro_max_hours == "energy_capacity_totals_by_country":
# watch out some p_nom values like IE's are totally underrepresented
max_hours_country = (
e_missing / hydro.loc[missing_mh_i].groupby("country").p_nom.sum()
)
elif hydro_max_hours == "estimate_by_large_installations":
max_hours_country = (
hydro_stats["E_store[TWh]"] * 1e3 / hydro_stats["p_nom_discharge[GW]"]
)
else:
raise ValueError(f"Unknown hydro_max_hours method: {hydro_max_hours}")
max_hours_country.clip(0, inplace=True)
missing_countries = pd.Index(hydro["country"].unique()).difference(
max_hours_country.dropna().index
)
if not missing_countries.empty:
logger.warning(
f"Assuming max_hours=6 for hydro reservoirs in the countries: {', '.join(missing_countries)}"
)
hydro_max_hours = hydro.max_hours.where(
(hydro.max_hours > 0) & ~hydro.index.isin(missing_mh_single_i),
hydro.country.map(max_hours_country),
).fillna(6)
if params.get("flatten_dispatch", False):
buffer = params.get("flatten_dispatch_buffer", 0.2)
average_capacity_factor = inflow_t[hydro.index].mean() / hydro["p_nom"]
p_max_pu = (average_capacity_factor + buffer).clip(upper=1)
else:
p_max_pu = 1
n.add(
"StorageUnit",
hydro.index,
carrier="hydro",
bus=hydro["bus"],
p_nom=hydro["p_nom"],
max_hours=hydro_max_hours,
capital_cost=costs.at["hydro", "capital_cost"],
marginal_cost=costs.at["hydro", "marginal_cost"],
p_max_pu=p_max_pu, # dispatch
p_min_pu=0.0, # store
efficiency_dispatch=costs.at["hydro", "efficiency"],
efficiency_store=0.0,
cyclic_state_of_charge=True,
inflow=inflow_t.loc[:, hydro.index],
)
def attach_OPSD_renewables(n: pypsa.Network, tech_map: dict[str, list[str]]) -> None:
"""
Attach renewable capacities from the OPSD dataset to the network.
Args:
- n: The PyPSA network to attach the capacities to.
- tech_map: A dictionary mapping fuel types to carrier names.
Returns:
- None
"""
tech_string = ", ".join(sum(tech_map.values(), []))
logger.info(f"Using OPSD renewable capacities for carriers {tech_string}.")
df = pm.data.OPSD_VRE().powerplant.convert_country_to_alpha2()
technology_b = ~df.Technology.isin(["Onshore", "Offshore"])
df["Fueltype"] = df.Fueltype.where(technology_b, df.Technology).replace(
{"Solar": "PV"}
)
df = df.query("Fueltype in @tech_map").powerplant.convert_country_to_alpha2()
df = df.dropna(subset=["lat", "lon"])
for fueltype, carriers in tech_map.items():
gens = n.generators[lambda df: df.carrier.isin(carriers)]
buses = n.buses.loc[gens.bus.unique()]
gens_per_bus = gens.groupby("bus").p_nom.count()
caps = map_country_bus(df.query("Fueltype == @fueltype"), buses)
caps = caps.groupby(["bus"]).Capacity.sum()
caps = caps / gens_per_bus.reindex(caps.index, fill_value=1)
n.generators.update({"p_nom": gens.bus.map(caps).dropna()})
n.generators.update({"p_nom_min": gens.bus.map(caps).dropna()})
def estimate_renewable_capacities(
n: pypsa.Network,
year: int,
tech_map: dict,
expansion_limit: bool,
countries: list,
):
"""
Estimate a different between renewable capacities in the network and
reported country totals from IRENASTAT dataset. Distribute the difference
with a heuristic.
Heuristic: n.generators_t.p_max_pu.mean() * n.generators.p_nom_max
Args:
- n: The PyPSA network.
- year: The year of optimisation.
- tech_map: A dictionary mapping fuel types to carrier names.
- expansion_limit: Boolean value from config file
- countries: A list of country codes to estimate capacities for.
Returns:
- None
"""
if not len(countries) or not len(tech_map):
return
capacities = pm.data.IRENASTAT().powerplant.convert_country_to_alpha2()
capacities = capacities.query(
"Year == @year and Technology in @tech_map and Country in @countries"
)
capacities = capacities.groupby(["Technology", "Country"]).Capacity.sum()
logger.info(
f"Heuristics applied to distribute renewable capacities [GW]: "
f"\n{capacities.groupby('Technology').sum().div(1e3).round(2)}"
)
for ppm_technology, techs in tech_map.items():
tech_i = n.generators.query("carrier in @techs").index
if ppm_technology in capacities.index.get_level_values("Technology"):
stats = capacities.loc[ppm_technology].reindex(countries, fill_value=0.0)
else:
stats = pd.Series(0.0, index=countries)
country = n.generators.bus[tech_i].map(n.buses.country)
existent = n.generators.p_nom[tech_i].groupby(country).sum()
missing = stats - existent
dist = n.generators_t.p_max_pu.mean() * n.generators.p_nom_max
n.generators.loc[tech_i, "p_nom"] += (
dist[tech_i]
.groupby(country)
.transform(lambda s: normed(s) * missing[s.name])
.where(lambda s: s > 0.1, 0.0) # only capacities above 100kW
)
n.generators.loc[tech_i, "p_nom_min"] = n.generators.loc[tech_i, "p_nom"]
if expansion_limit:
assert np.isscalar(expansion_limit)
logger.info(
f"Reducing capacity expansion limit to {expansion_limit * 100:.2f}% of installed capacity."
)
n.generators.loc[tech_i, "p_nom_max"] = (
expansion_limit * n.generators.loc[tech_i, "p_nom_min"]
)
def attach_storageunits(
n: pypsa.Network,
costs: pd.DataFrame,
extendable_carriers: dict,
max_hours: dict,
):
"""
Attach storage units to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the storage units to.
costs : pd.DataFrame
DataFrame containing the cost data.
extendable_carriers : dict
Dictionary of extendable energy carriers.
max_hours : dict
Dictionary of maximum hours for storage units.
"""
carriers = extendable_carriers["StorageUnit"]
n.add("Carrier", carriers)
buses_i = n.buses.index
lookup_store = {"H2": "electrolysis", "battery": "battery inverter"}
lookup_dispatch = {"H2": "fuel cell", "battery": "battery inverter"}
for carrier in carriers:
roundtrip_correction = 0.5 if carrier == "battery" else 1
n.add(
"StorageUnit",
buses_i,
" " + carrier,
bus=buses_i,
carrier=carrier,
p_nom_extendable=True,
capital_cost=costs.at[carrier, "capital_cost"],
marginal_cost=costs.at[carrier, "marginal_cost"],
efficiency_store=costs.at[lookup_store[carrier], "efficiency"]
** roundtrip_correction,
efficiency_dispatch=costs.at[lookup_dispatch[carrier], "efficiency"]
** roundtrip_correction,
max_hours=max_hours[carrier],
cyclic_state_of_charge=True,
)
def attach_stores(
n: pypsa.Network,
costs: pd.DataFrame,
extendable_carriers: dict,
):
"""
Attach stores to the network.
Parameters
----------
n : pypsa.Network
The PyPSA network to attach the stores to.
costs : pd.DataFrame
DataFrame containing the cost data.
extendable_carriers : dict
Dictionary of extendable energy carriers.
"""