forked from Fraunhofer-IEG/PyPSA-IEI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve_network.py
More file actions
1792 lines (1539 loc) · 85 KB
/
solve_network.py
File metadata and controls
1792 lines (1539 loc) · 85 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
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
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
"""
Solves optimal operation and capacity for a network with the option to
iteratively optimize while updating line reactances.
This script is used for optimizing the electrical network as well as the
sector coupled network.
Description
-----------
Total annual system costs are minimised with PyPSA. The full formulation of the
linear optimal power flow (plus investment planning
is provided in the
`documentation of PyPSA <https://pypsa.readthedocs.io/en/latest/optimal_power_flow.html#linear-optimal-power-flow>`_.
The optimization is based on the :func:`network.optimize` function.
Additionally, some extra constraints specified in :mod:`solve_network` are added.
.. note::
The rules ``solve_elec_networks`` and ``solve_sector_networks`` run
the workflow for all scenarios in the configuration file (``scenario:``)
based on the rule :mod:`solve_network`.
"""
import importlib
import logging
import os
import re
import sys
from pathlib import Path
import numpy as np
import pandas as pd
import pypsa
import xarray as xr
from linopy.oetc import OetcHandler, OetcSettings, OetcCredentials
from _benchmark import memory_logger
from _helpers import configure_logging, create_model, get_opt, update_config_with_sector_opts
from pypsa.descriptors import get_activity_mask
from pypsa.descriptors import get_switchable_as_dense as get_as_dense
logger = logging.getLogger(__name__)
pypsa.pf.logger.setLevel(logging.WARNING)
def add_land_use_constraint(n, planning_horizons, config):
if "m" in snakemake.wildcards.clusters:
_add_land_use_constraint_m(n, planning_horizons, config)
else:
_add_land_use_constraint(n)
def add_land_use_constraint_perfect(n):
"""
Add global constraints for tech capacity limit.
"""
logger.info("Add land-use constraint for perfect foresight")
def compress_series(s):
def process_group(group):
if group.nunique() == 1:
return pd.Series(group.iloc[0], index=[None])
else:
return group
return s.groupby(level=[0, 1]).apply(process_group)
def new_index_name(t):
# Convert all elements to string and filter out None values
parts = [str(x) for x in t if x is not None]
# Join with space, but use a dash for the last item if not None
return " ".join(parts[:2]) + (f"-{parts[-1]}" if len(parts) > 2 else "")
def check_p_min_p_max(p_nom_max):
p_nom_min = n.generators[ext_i].groupby(grouper).sum().p_nom_min
p_nom_min = p_nom_min.reindex(p_nom_max.index)
check = (
p_nom_min.groupby(level=[0, 1]).sum()
> p_nom_max.groupby(level=[0, 1]).min()
)
if check.sum():
logger.warning(
f"summed p_min_pu values at node larger than technical potential {check[check].index}"
)
grouper = [n.generators.carrier, n.generators.bus, n.generators.build_year]
ext_i = n.generators.p_nom_extendable
# get technical limit per node and investment period
p_nom_max = n.generators[ext_i].groupby(grouper).min().p_nom_max
# drop carriers without tech limit
p_nom_max = p_nom_max[~p_nom_max.isin([np.inf, np.nan])]
# carrier
carriers = p_nom_max.index.get_level_values(0).unique()
gen_i = n.generators[(n.generators.carrier.isin(carriers)) & (ext_i)].index
n.generators.loc[gen_i, "p_nom_min"] = 0
# check minimum capacities
check_p_min_p_max(p_nom_max)
# drop multi entries in case p_nom_max stays constant in different periods
# p_nom_max = compress_series(p_nom_max)
# adjust name to fit syntax of nominal constraint per bus
df = p_nom_max.reset_index()
df["name"] = df.apply(
lambda row: f"nom_max_{row['carrier']}"
+ (f"_{row['build_year']}" if row["build_year"] is not None else ""),
axis=1,
)
for name in df.name.unique():
df_carrier = df[df.name == name]
bus = df_carrier.bus
n.buses.loc[bus, name] = df_carrier.p_nom_max.values
return n
def _add_land_use_constraint(n):
# warning: this will miss existing offwind which is not classed AC-DC and has carrier 'offwind'
for carrier in ["solar", "onwind", "offwind-ac", "offwind-dc"]:
extendable_i = (n.generators.carrier == carrier) & n.generators.p_nom_extendable
n.generators.loc[extendable_i, "p_nom_min"] = 0
ext_i = (n.generators.carrier == carrier) & ~n.generators.p_nom_extendable
existing = (
n.generators.loc[ext_i, "p_nom"]
.groupby(n.generators.bus.map(n.buses.location))
.sum()
)
existing.index += " " + carrier + "-" + snakemake.wildcards.planning_horizons
n.generators.loc[existing.index, "p_nom_max"] -= existing
# 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"
]
n.generators.p_nom_max.clip(lower=0, inplace=True)
def _add_land_use_constraint_m(n, planning_horizons, config):
# if generators clustering is lower than network clustering, land_use accounting is at generators clusters
grouping_years = config["existing_capacities"]["grouping_years"]
current_horizon = snakemake.wildcards.planning_horizons
for carrier in ["solar", "onwind", "offwind-ac", "offwind-dc"]:
existing = n.generators.loc[n.generators.carrier == carrier, "p_nom"]
ind = list(
{i.split(sep=" ")[0] + " " + i.split(sep=" ")[1] for i in existing.index}
)
previous_years = [
str(y)
for y in planning_horizons + grouping_years
if y < int(snakemake.wildcards.planning_horizons)
]
for p_year in previous_years:
ind2 = [
i for i in ind if i + " " + carrier + "-" + p_year in existing.index
]
sel_current = [i + " " + carrier + "-" + current_horizon for i in ind2]
sel_p_year = [i + " " + carrier + "-" + p_year for i in ind2]
n.generators.loc[sel_current, "p_nom_max"] -= existing.loc[
sel_p_year
].rename(lambda x: x[:-4] + current_horizon)
n.generators.p_nom_max.clip(lower=0, inplace=True)
def add_co2_sequestration_limit(n, config, limit=200):
"""
Add a global constraint on the amount of Mt CO2 that can be sequestered.
"""
limit = limit * 1e6
for o in opts:
if "seq" not in o:
continue
limit = float(o[o.find("seq") + 3:]) * 1e6
break
if not n.investment_periods.empty:
periods = n.investment_periods
names = pd.Index([f"co2_sequestration_limit-{period}" for period in periods])
else:
periods = [np.nan]
names = pd.Index(["co2_sequestration_limit"])
n.madd(
"GlobalConstraint",
names,
sense=">=",
constant=-limit,
type="operational_limit",
carrier_attribute="co2 sequestered",
investment_period=periods,
)
def add_carbon_constraint(n, snapshots):
glcs = n.global_constraints.query('type == "co2_atmosphere"')
if glcs.empty:
return
for name, glc in glcs.iterrows():
carattr = glc.carrier_attribute
emissions = n.carriers.query(f"{carattr} != 0")[carattr]
if emissions.empty:
continue
# stores
n.stores["carrier"] = n.stores.bus.map(n.buses.carrier)
stores = n.stores.query("carrier in @emissions.index and not e_cyclic")
if not stores.empty:
last = n.snapshot_weightings.reset_index().groupby("period").last()
last_i = last.set_index([last.index, last.timestep]).index
final_e = n.model["Store-e"].loc[last_i, stores.index]
time_valid = int(glc.loc["investment_period"])
time_i = pd.IndexSlice[time_valid, :]
lhs = final_e.loc[time_i, :] - final_e.shift(snapshot=1).loc[time_i, :]
rhs = glc.constant
n.model.add_constraints(lhs <= rhs, name=f"GlobalConstraint-{name}")
def add_carbon_budget_constraint(n, snapshots):
glcs = n.global_constraints.query('type == "Co2Budget"')
if glcs.empty:
return
for name, glc in glcs.iterrows():
carattr = glc.carrier_attribute
emissions = n.carriers.query(f"{carattr} != 0")[carattr]
if emissions.empty:
continue
# stores
n.stores["carrier"] = n.stores.bus.map(n.buses.carrier)
stores = n.stores.query("carrier in @emissions.index and not e_cyclic")
if not stores.empty:
last = n.snapshot_weightings.reset_index().groupby("period").last()
last_i = last.set_index([last.index, last.timestep]).index
final_e = n.model["Store-e"].loc[last_i, stores.index]
time_valid = int(glc.loc["investment_period"])
time_i = pd.IndexSlice[time_valid, :]
weighting = n.investment_period_weightings.loc[time_valid, "years"]
lhs = final_e.loc[time_i, :] * weighting
rhs = glc.constant
n.model.add_constraints(lhs <= rhs, name=f"GlobalConstraint-{name}")
def add_max_growth(n, config):
"""
Add maximum growth rates for different carriers.
"""
opts = snakemake.params["sector"]["limit_max_growth"]
# take maximum yearly difference between investment periods since historic growth is per year
factor = n.investment_period_weightings.years.max() * opts["factor"]
for carrier in opts["max_growth"].keys():
max_per_period = opts["max_growth"][carrier] * factor
logger.info(
f"set maximum growth rate per investment period of {carrier} to {max_per_period} GW."
)
n.carriers.loc[carrier, "max_growth"] = max_per_period * 1e3
for carrier in opts["max_relative_growth"].keys():
max_r_per_period = opts["max_relative_growth"][carrier]
logger.info(
f"set maximum relative growth per investment period of {carrier} to {max_r_per_period}."
)
n.carriers.loc[carrier, "max_relative_growth"] = max_r_per_period
return n
def add_retrofit_gas_boiler_constraint(n, snapshots):
"""
Allow retrofitting of existing gas boilers to H2 boilers.
"""
c = "Link"
logger.info("Add constraint for retrofitting gas boilers to H2 boilers.")
# existing gas boilers
mask = n.links.carrier.str.contains("gas boiler") & ~n.links.p_nom_extendable
gas_i = n.links[mask].index
mask = n.links.carrier.str.contains("retrofitted H2 boiler")
h2_i = n.links[mask].index
n.links.loc[gas_i, "p_nom_extendable"] = True
p_nom = n.links.loc[gas_i, "p_nom"]
n.links.loc[gas_i, "p_nom"] = 0
# heat profile
cols = n.loads_t.p_set.columns[
n.loads_t.p_set.columns.str.contains("heat")
& ~n.loads_t.p_set.columns.str.contains("industry")
& ~n.loads_t.p_set.columns.str.contains("agriculture")
]
profile = n.loads_t.p_set[cols].div(
n.loads_t.p_set[cols].groupby(level=0).max(), level=0
)
# to deal if max value is zero
profile.fillna(0, inplace=True)
profile.rename(columns=n.loads.bus.to_dict(), inplace=True)
profile = profile.reindex(columns=n.links.loc[gas_i, "bus1"])
profile.columns = gas_i
rhs = profile.mul(p_nom)
dispatch = n.model["Link-p"]
active = get_activity_mask(n, c, snapshots, gas_i)
rhs = rhs[active]
p_gas = dispatch.sel(Link=gas_i)
p_h2 = dispatch.sel(Link=h2_i)
lhs = p_gas + p_h2
n.model.add_constraints(lhs == rhs, name="gas_retrofit")
def prepare_network(
n,
solve_opts=None,
config=None,
foresight=None,
planning_horizons=None,
co2_sequestration_potential=None,
):
if "clip_p_max_pu" in solve_opts:
for df in (
n.generators_t.p_max_pu,
n.generators_t.p_min_pu,
n.links_t.p_max_pu,
n.links_t.p_min_pu,
n.storage_units_t.inflow,
):
df.where(df > solve_opts["clip_p_max_pu"], other=0.0, inplace=True)
if load_shedding := solve_opts.get("load_shedding"):
# intersect between macroeconomic and surveybased willingness to pay
# http://journal.frontiersin.org/article/10.3389/fenrg.2015.00055/full
# TODO: retrieve color and nice name from config
n.add("Carrier", "load", color="#dd2e23", nice_name="Load shedding")
buses_i = n.buses.index
if not np.isscalar(load_shedding):
# TODO: do not scale via sign attribute (use Eur/MWh instead of Eur/kWh)
load_shedding = 1e2 # Eur/kWh
n.madd(
"Generator",
buses_i,
" load",
bus=buses_i,
carrier="load",
sign=1e-3, # Adjust sign to measure p and p_nom in kW instead of MW
marginal_cost=load_shedding, # Eur/kWh
p_nom=1e9, # kW
)
if solve_opts.get("curtailment_mode"):
n.add("Carrier", "curtailment", color="#fedfed", nice_name="Curtailment")
n.generators_t.p_min_pu = n.generators_t.p_max_pu
buses_i = n.buses.query("carrier == 'AC'").index
n.madd(
"Generator",
buses_i,
suffix=" curtailment",
bus=buses_i,
p_min_pu=-1,
p_max_pu=0,
marginal_cost=-0.1,
carrier="curtailment",
p_nom=1e6,
)
if solve_opts.get("noisy_costs"):
for t in n.iterate_components():
# if 'capital_cost' in t.df:
# t.df['capital_cost'] += 1e1 + 2.*(np.random.random(len(t.df)) - 0.5)
if "marginal_cost" in t.df:
t.df["marginal_cost"] += 1e-2 + 2e-3 * (
np.random.random(len(t.df)) - 0.5
)
for t in n.iterate_components(["Line", "Link"]):
t.df["capital_cost"] += (
1e-1 + 2e-2 * (np.random.random(len(t.df)) - 0.5)
) * t.df["length"]
if solve_opts.get("nhours"):
nhours = solve_opts["nhours"]
n.set_snapshots(n.snapshots[:nhours])
n.snapshot_weightings[:] = 8760.0 / nhours
if foresight == "myopic":
add_land_use_constraint(n, planning_horizons, config)
if foresight == "perfect":
n = add_land_use_constraint_perfect(n)
if snakemake.params["sector"]["limit_max_growth"]["enable"]:
n = add_max_growth(n, config)
if n.stores.carrier.eq("co2 sequestered").any():
limit = co2_sequestration_potential
add_co2_sequestration_limit(n, config, limit=limit)
return n
def add_country_carrier_limit_constraints(n, config):
"""
Add country & carrier limit constraint to the network.
Add minimum and maximum levels of generator nominal capacity per carrier
for individual countries. Opts and path for agg_p_nom_minmax.csv must be defined
in config.yaml. Default file is available at data/agg_p_nom_minmax.csv.
Parameters
----------
n : pypsa.Network
config : dict
"""
# load custom constraints
agg_p_nom_minmax = pd.read_csv(
config["policy_plans"]["agg_p_nom_limits"], index_col=[0, 1]
)
# filter out the current planning year
current_year = snakemake.wildcards.planning_horizons
agg_p_nom_minmax = agg_p_nom_minmax.query(f'year=={current_year}')
logger.info("Adding generation capacity constraints per carrier and country")
# all solar technologies should be grouped together, so the carrier needs to be changed. same for offwind.
carrier_map = {'solar rooftop': 'solar',
'offwind-ac': 'offwind',
'offwind-dc': 'offwind',
'residential rural micro gas CHP': 'gas for electricity',
'services rural micro gas CHP': 'gas for electricity',
'residential urban decentral micro gas CHP': 'gas for electricity',
'services urban decentral micro gas CHP': 'gas for electricity',
'urban central gas CHP': 'gas for electricity',
'urban central gas CHP CC': 'gas for electricity',
'OCGT': 'gas for electricity',
'CCGT': 'gas for electricity',
'allam': 'gas for electricity'
}
for cc, component in zip(n.iterate_components(['Store', 'Generator', 'Link']), ['Store', "Generator", 'Link']):
# add a column containing the country to the component dataframe
base_bus = pd.Series(cc.df.index.str[:5], index=cc.df.index, name='country')
component_bus = base_bus.map(n.buses.country)
cc = cc.df.drop('country', axis=1, errors='ignore').merge(component_bus, how='left', left_index=True,
right_index=True)
# rename the carrier for offind and solar technologies
cc.carrier = cc.carrier.replace(carrier_map)
if component == 'Store':
# extract all extendable carriers
e_nom = n.model[component + "-e_nom"]
cc_ext = cc.query("e_nom_extendable").rename_axis(index=component + "-ext")
# create a similar dataframe, where all components belong to the country 'Eur' to be able to group them
# together.
cc_ext_Eur = cc_ext.copy()
cc_ext_Eur['country'] = 'Eur'
# create left-hand side for the country-specific and European constraints
lhs = (e_nom.groupby(cc_ext.loc[:, ['country', 'carrier']]).sum()
+ e_nom.groupby(cc_ext_Eur.loc[:, ['country', 'carrier']]).sum())
# filter for all the non-extendable carriers
cc_old = cc.query("~e_nom_extendable").rename_axis(index=component + "-old")
cc_old_Eur = cc_old.copy()
cc_old_Eur['country'] = 'Eur'
# sum up all the capacities that exist before the current planning year
old_capas = pd.concat([cc_old.groupby(['country', 'carrier']).e_nom.sum(),
cc_old_Eur.groupby(['country', 'carrier']).e_nom.sum()])
else:
# extract all extendable carriers
p_nom = n.model[component + "-p_nom"]
if component == "Link":
# for links, multiply the efficiency to get the capacity of bus1, which is often the electricity bus
p_nom = p_nom * n.links.query('p_nom_extendable').efficiency.rename_axis("Link-ext")
cc_ext = cc.query("p_nom_extendable").rename_axis(index=component + "-ext")
# create a similar dataframe, where all components belong to the country 'Eur' to be able to group them
# together.
cc_ext_Eur = cc_ext.copy()
cc_ext_Eur['country'] = 'Eur'
# create left-hand side for the country-specific and European constraints
lhs = (p_nom.groupby(cc_ext.loc[:, ['country', 'carrier']]).sum()
+ p_nom.groupby(cc_ext_Eur.loc[:, ['country', 'carrier']]).sum())
# filter for all the non-extendable carriers
cc_old = cc.query("~p_nom_extendable").rename_axis(index=component + "-old")
cc_old_Eur = cc_old.copy()
cc_old_Eur['country'] = 'Eur'
# sum up all the capacities that exist before the current planning year
old_capas = pd.concat([cc_old.groupby(['country', 'carrier']).p_nom.sum(),
cc_old_Eur.groupby(['country', 'carrier']).p_nom.sum()])
# filter the custom constraints for the current component
agg_p_nom_minmax_for_component = agg_p_nom_minmax[agg_p_nom_minmax['component'] == component.lower()]
minimum = xr.DataArray(agg_p_nom_minmax_for_component["min"].dropna()).rename(dim_0="group")
# subtract old capacities from the custom constraint to get the minimum capacity expansion
old_idx = minimum.indexes['group'].intersection(old_capas.index)
minimum.loc[old_idx] = minimum.loc[old_idx] - old_capas.loc[old_idx]
minimum = minimum.where(minimum >= 0, 0)
# create the constraint for the countries and carriers that appear both in the input data and in the network
index = minimum.indexes["group"].intersection(lhs.indexes["group"])
if not index.empty:
name = "agg_e_nom_min_" if component == 'Store' else 'agg_p_nom_min_'
n.model.add_constraints(
lhs.sel(group=index) >= minimum.loc[index], name=name + component
)
# filter the custom constraints for the current component
maximum = xr.DataArray(agg_p_nom_minmax_for_component["max"].dropna()).rename(dim_0="group")
# subtract old capacities from the custom constraint to get the maximum capacity expansion
old_idx = maximum.indexes['group'].intersection(old_capas.index)
maximum.loc[old_idx] = maximum.loc[old_idx] - old_capas.loc[old_idx]
maximum = maximum.where(maximum >= 0, 0)
# create the constraint for the countries and carriers that appear both in the input data and in the network
index = maximum.indexes["group"].intersection(lhs.indexes["group"])
if not index.empty:
name = "agg_e_nom_max_" if component == 'Store' else 'agg_p_nom_max_'
n.model.add_constraints(
lhs.sel(group=index) <= maximum.loc[index], name=name + component
)
def add_national_grid_plan_constraints(n, config):
"""
Limit the expansion of the national electricity transmission lines to a factor compared to first year of the
planning_horizons (e.g., 2020) given by the Ember study (https://view.officeapps.live.com/op/view.aspx?src=https%3A%2F%2Fember-climate.org%2Fapp%2Fuploads%2F2024%2F03%2FEmber-Transmission-Grids-Data.xlsx&wdOrigin=BROWSELINK)
Parameters
----------
n : pypsa.Network
config : dict
"""
# filter the network for lines and links that connect clusters of the same country
national_links = n.links[(n.links.carrier == "DC") & (n.links.bus0.str[:2] == n.links.bus1.str[:2])]
national_lines = n.lines[(n.lines.bus0.str[:2] == n.lines.bus1.str[:2])]
# load expansion factors given by custom input file
national_grid_plans = pd.read_csv(config["policy_plans"]["include_national_grid_plans"]["national_grid_plan_data"], index_col=[0])
### extract most recent constraints for each country in the given year
# extract the current and previous planning year
current_year = int(snakemake.wildcards.planning_horizons)
all_years = snakemake.params.planning_horizons
previous_year = all_years[all_years.index(current_year) - 1]
countries = []
most_recent_factors = []
# check input for each country separately
for country, row in national_grid_plans.iterrows():
# find years, for which an input value exists
existing_years = row.dropna().index
# extract years with input that lie within the timespan between the previous and the current optimization year
years_in_timeframe = [int(x) for x in existing_years if (int(x) > previous_year) & (int(x) <= current_year)]
# if there is no input data fulfilling this criterion, continue to the next row
if len(years_in_timeframe) == 0:
continue
# find the most recent year with input data within the timespan of interest
most_recent_year = max(years_in_timeframe)
# add the factor from the input data to the list of factors and also add the corresponding country
most_recent_factors = most_recent_factors + [row[str(most_recent_year)]]
countries = countries + [country]
# In the following, we will distinguish between cases depending on the most_recent_factors.
# In case 1), most_recent_factors is empty, i.e.the file 'national_line_expansions.cvs' does not contain any
# relevant specifications for the model configuration. This is the case, for example, if no specifications have been
# made for the countries or for suitable years.
# In case 1, we distinguish between 3 sub-cases:
# 1A) The year is before the relevant data(e.g., 2023 if we have data only from 2026 and later),
# then all (except for tyndp relevant components) are set expandable = false.
# 1B) The year is the first of the planning_horizons (e.g., 2020): All is set to expandable = true.
# 1C) The year is after the relevant data in most_recent_factors(e.g., 2060 if we have data until 2050): All is set
# to expandable = open_for_optimization(true / false after certain year - as configured by user)
# In case 2) there is relevant data and this is then used accordingly to build the constraints.
# Case 1: handle all eventualities where there is no input data for the current optimization year
if len(most_recent_factors) == 0:
# extract the first year for which input data is given
first_data_year = int(min(national_grid_plans.columns))
# handle the case when this is not the first optimization year, but it is too early for any of the input data
# from the csv file to apply to this year
if (first_data_year > current_year) & (current_year != all_years[0]):
# Case 1A: find all the lines and links that should not be extended in this year. This includes all the
# national components that have not already been set by the TYNDP (those fulfill min=max)
idx_links_not_extendable = national_links.query('p_nom_min!=p_nom_max').index
idx_lines_not_extendable = national_lines.query('s_nom_min!=s_nom_max').index
# set all links except just-built TYNDP links to not extendable
n.links.loc[idx_links_not_extendable, 'p_nom_extendable'] = False
n.links.loc[idx_links_not_extendable, 'p_nom'] = n.links.loc[idx_links_not_extendable, 'p_nom_min']
# set all lines except just-built TYNDP lines to not extendable
n.lines.loc[idx_lines_not_extendable, 's_nom_extendable'] = False
n.lines.loc[idx_lines_not_extendable, 's_nom'] = n.lines.loc[idx_lines_not_extendable, 's_nom_min']
elif current_year == all_years[0]:
# Case 1B: handle the case where the current optimization year is the first one
# allow extension of national lines and links by setting them to extendable
n.links.loc[national_links.index, 'p_nom_extendable'] = True
n.lines.loc[national_lines.index, 's_nom_extendable'] = True
else:
# Case 1C: possibly one of the later optimization years
# open_for_optimization can be true or a year. In case of true, the following if clauses are skipped and all lines
# and links are set as extendable later in the code. In case of a year, optimization of the lines and links is
# allowed after this year.
open_for_optimization = config['policy_plans']['include_national_grid_plans']['optimize_after']
if (type(open_for_optimization) == int) & (current_year > open_for_optimization):
open_for_optimization = True
elif (type(open_for_optimization) == int) & (current_year <= open_for_optimization):
open_for_optimization = False
# find all components that were not already defined by TYNDP (those fulfill min=max)
idx_links_not_extendable = national_links.query('p_nom_min!=p_nom_max').index
idx_lines_not_extendable = national_lines.query('s_nom_min!=s_nom_max').index
# set these lines and links to the choice from the config
n.links.loc[idx_links_not_extendable, 'p_nom_extendable'] = open_for_optimization
n.lines.loc[idx_lines_not_extendable, 's_nom_extendable'] = open_for_optimization
if not open_for_optimization:
# when they are not extended, p_nom needs to be set to consider any previously built capacity
n.links.loc[idx_links_not_extendable, 'p_nom'] = n.links.loc[idx_links_not_extendable, 'p_nom_min']
n.lines.loc[idx_lines_not_extendable, 's_nom'] = n.lines.loc[idx_lines_not_extendable, 's_nom_min']
else:
# Case 2: handle the case when there is input data for the current time window
# put the factors for the current optimization year into pandas Series
most_recent_grid_plan = pd.Series(most_recent_factors, index=countries)
# load national transmission capacities from the first optimization year
resultdir = Path(snakemake.input.network.split('prenetworks-final')[0])
filename_base_year_capacities = (resultdir /
f"base_year_capacities/elec_s{snakemake.wildcards.simpl}_"
f"{snakemake.wildcards.clusters}_l{snakemake.wildcards.ll}_"
f"{snakemake.wildcards.opts}_{snakemake.wildcards.sector_opts}_"
f"{config['scenario']['planning_horizons'][0]}_base_year_capacities.csv")
existing_capacities = pd.read_csv(filename_base_year_capacities, index_col=[0])
# remove all reveresed links to avoid the links counting double
national_links = national_links[~(national_links.index.str.contains('reversed'))]
# devide the lines and links into extendable and non-extendable components
national_links_ext = national_links.query('p_nom_extendable')
national_links_fix = national_links.query('~p_nom_extendable')
national_lines_ext = national_lines.query('s_nom_extendable')
national_lines_fix = national_lines.query('~s_nom_extendable')
### find the expansion factor that is already given by the min values prior to this constraint
# calculate the p_nom_min per country
grouper_links = national_links.bus0.map(n.buses.country)
minimum_links = national_links.groupby(grouper_links).p_nom_min.sum()
# calculate the s_nom_min that is actually usable
s_nom_min_real = national_lines.s_nom_min * national_lines.s_max_pu
# calculate the s_nom_min per country
grouper_lines = national_lines.bus0.map(n.buses.country)
minimum_lines = s_nom_min_real.groupby(grouper_lines).sum()
# calculate the minimum capacity expansion compared to the base year
minimum_capacity = minimum_lines.add(minimum_links, fill_value=0)
minimum_factor = minimum_capacity / existing_capacities.loc[:, 'capacity']
# find countries, where the minimum expansion exceeds the exogenously given factor and replace it with the
# minimum factors
index = minimum_factor.index.intersection(most_recent_grid_plan.index)
to_change = most_recent_grid_plan.loc[index][minimum_factor.loc[index] > most_recent_grid_plan.loc[index]].index
most_recent_grid_plan[to_change] = minimum_factor.loc[to_change]
# find countries that are not defined in exogenous input and set them to minimum value
to_set = minimum_factor.index.difference(index)
most_recent_grid_plan = pd.concat([most_recent_grid_plan,minimum_factor[to_set]])
### define left-hand side of constraint
# extract variables from model
p_nom_per_cluster_ext = n.model['Link-p_nom'].sel({'Link-ext': national_links_ext.index})
s_nom_per_cluster_ext = n.model['Line-s_nom'].sel({'Line-ext': national_lines_ext.index})
# calculate s_nom that can be actually used
s_nom_real_per_cluster_ext = s_nom_per_cluster_ext * national_lines_ext.s_max_pu.rename_axis(index='Line-ext')
# sum up all p_nom for each country
grouper_links_ext = national_links_ext.bus0.map(n.buses.country).rename_axis(index='Link-ext')
p_nom_per_country_ext = p_nom_per_cluster_ext.groupby(grouper_links_ext).sum()
# sum up all s_nom for each country
grouper_lines_ext = national_lines_ext.bus0.map(n.buses.country).rename_axis(index='Line-ext')
s_nom_real_per_country_ext = s_nom_real_per_cluster_ext.groupby(grouper_lines_ext).sum()
# sum up capacities for lines and links
capacity_per_country_ext = (s_nom_real_per_country_ext + p_nom_per_country_ext).rename(bus0='country')
### define right-hand side
if national_lines_fix.empty & national_links_fix.empty:
# if all national lines and links are extendable, find countries that occur both in the input data and in
# the network
idx_countries = (most_recent_grid_plan.index.intersection(existing_capacities.index)
.intersection(capacity_per_country_ext.indexes['country']))
if not idx_countries.empty:
# define right-hand side as the capacity that should exist according to the input data
rhs = most_recent_grid_plan[idx_countries] * existing_capacities.loc[idx_countries, 'capacity']
else:
# if some lines and/or links not extendable, these need to be subtracted from the capacity given by the
# input data
# sum up the capacities that are not extendable
grouper_links_fix = national_links_fix.bus0.map(n.buses.country)
p_nom_per_country_fix = national_links_fix.groupby(grouper_links_fix).p_nom.sum()
grouper_lines_fix = national_lines_fix.bus0.map(n.buses.country)
s_nom_real_per_cluster_fix = national_lines_fix.s_nom * national_lines_fix.s_max_pu
s_nom_real_per_country_fix = s_nom_real_per_cluster_fix.groupby(grouper_lines_fix).sum()
capacity_per_country_fix = s_nom_real_per_country_fix.add(p_nom_per_country_fix, fill_value=0)
# find the countries that exist in all objects relevant for right-hand side
idx_countries = (most_recent_grid_plan.index.intersection(existing_capacities.index)
.intersection(capacity_per_country_fix.index)
.intersection(capacity_per_country_ext.indexes['country']))
if not idx_countries.empty:
# define right-hand side as the difference between the total capacity defined by the grid plans and the
# non-extendable capacities
rhs = (most_recent_grid_plan.loc[idx_countries] * existing_capacities.loc[idx_countries, 'capacity']
- capacity_per_country_fix[idx_countries])
### define constraint
if not idx_countries.empty:
# extract condition for constraint from config option
national_expansion_condition = config["policy_plans"]['include_national_grid_plans']['national_expansion_condition']
# set constraint according to config option
if national_expansion_condition == 'min':
n.model.add_constraints(capacity_per_country_ext.sel(country=idx_countries) >= rhs,
name='national-grid-plans-min')
if national_expansion_condition == 'max':
n.model.add_constraints(capacity_per_country_ext.sel(country=idx_countries) <= rhs,
name='national-grid-plans-max')
if national_expansion_condition == 'equal':
n.model.add_constraints(capacity_per_country_ext.sel(country=idx_countries) == rhs,
name='national-grid-plans-equal')
def add_self_sufficiency_constraints(n, config):
"""
Implements a degree of self-sufficiency for hydrogen and/or electricity. The self-sufficiency can be defined
for specific countries, per cluster and on the EU level. The degree of self-sufficiency is given by
local_generation/(local_generation+import).
Parameters
----------
n : pypsa.Network
config : dict
"""
def group(df, b="bus", eu=None):
"""
Group given dataframe df by bus location or country.
The optional argument `b` allows clustering by bus0 or bus1 for
lines and links. If a list of EU countries is given as 'eu', all
these countries will receive 'EU' as loaction.
"""
if per_country:
if eu is None:
return df[b].map(location).map(n.buses.country).to_xarray()
else:
group_country = df[b].map(location).map(n.buses.country).to_xarray()
group_eu_index = group_country.isin(eu)
group_country[group_eu_index] = 'EU'
return group_country
else:
return df[b].map(location).to_xarray()
eu_countries = ["FR",
"DE",
"GB",
"IT",
"ES",
"PL",
"SE",
"NL",
"BE",
"FI",
"DK",
"PT",
"RO",
"AT",
"BG",
"EE",
"GR",
"LV",
"CZ",
"HU",
"IE",
"SK",
"LT",
"HR",
"LU",
"SI"]
# assign the basic bus (e.g. AL0 1) to each bus as a location
location = (
n.buses.location
if "location" in n.buses.columns
else pd.Series(n.buses.index, index=n.buses.index)
)
# load custom constraints
self_sufficiency_limits_full = pd.read_csv(
config["policy_plans"]["self_sufficiency_limits"], index_col=[0]
)
# filter out the current planning year
current_year = snakemake.wildcards.planning_horizons
self_sufficiency_limits_full = self_sufficiency_limits_full.query(f'year=={current_year}')
logger.info("Adding self-sufficiency constraints per carrier and country")
# extract variables for each component type from model
p_links = n.model["Link-p"]
p_generators = n.model["Generator-p"]
p_storage = n.model["StorageUnit-p_dispatch"]
s_line = n.model["Line-s"]
for per_country in [True, False]:
if per_country:
self_sufficiency_limits = self_sufficiency_limits_full[self_sufficiency_limits_full.index.str.len() == 2]
per_country_name = 'country'
else:
self_sufficiency_limits = self_sufficiency_limits_full[self_sufficiency_limits_full.index.str.len() == 5]
per_country_name = 'cluster'
### create constraint for hydrogen
# calculate the energy of the hydrogen generation
h2_generation_carrier = ['H2 Electrolysis',
'SMR CC',
'SMR']
idx_gen_h2 = n.links[(n.links.carrier.isin(h2_generation_carrier))].index
efficiencies = n.links.loc[idx_gen_h2, 'efficiency']
generation_p_at_h2bus = p_links.loc[:, idx_gen_h2] * efficiencies
generation_p_per_country_h2 = generation_p_at_h2bus.groupby(group(n.links.loc[idx_gen_h2], b="bus1")).sum()
generation_e_h2 = ((generation_p_per_country_h2 * n.snapshot_weightings.generators)
.rename({"bus1": "bus"}).sum("snapshot"))
if per_country:
# add sum for EU
generation_p_eu_h2 = (generation_p_at_h2bus
.groupby(group(n.links.loc[idx_gen_h2], b="bus1", eu=eu_countries)).sum())
generation_e_eu_h2 = ((generation_p_eu_h2 * n.snapshot_weightings.generators)
.rename({"bus1": "bus"}).sum("snapshot"))
# combine with country-specific expressions for EU countries
generation_e_h2 = generation_e_h2.sel(bus=eu_countries) + generation_e_eu_h2
# calculate energy of cross-border hydrogen transport
h2_pipe_carrier = ['H2 pipeline retrofitted',
'H2 pipeline',
'H2 pipeline (Kernnetz)']
if per_country:
idx_cross_border_pipes_h2 = n.links[(n.links.carrier.isin(h2_pipe_carrier))
& (n.links.bus0.str[:2] != n.links.bus1.str[:2])].index
else:
idx_cross_border_pipes_h2 = n.links[(n.links.carrier.isin(h2_pipe_carrier))].index
p_pipes_out_per_country = (p_links.loc[:, idx_cross_border_pipes_h2]
.groupby(group(n.links.loc[idx_cross_border_pipes_h2], b="bus0")).sum())
e_pipes_out = ((p_pipes_out_per_country * n.snapshot_weightings.generators)
.rename({"bus0": "bus"}).sum("snapshot"))
p_pipes_in_per_country = (p_links.loc[:, idx_cross_border_pipes_h2]
.groupby(group(n.links.loc[idx_cross_border_pipes_h2], b="bus1")).sum())
e_pipes_in = ((p_pipes_in_per_country * n.snapshot_weightings.generators)
.rename({"bus1": "bus"}).sum("snapshot"))
if per_country:
# add pipes out of EU
idx_pipes_eu_h2 = n.links[(n.links.carrier.isin(h2_pipe_carrier))
& ~(n.links.bus0.str[:2].isin(eu_countries) & n.links.bus1.str[:2].isin(eu_countries))
& (n.links.bus0.str[:2]!=n.links.bus1.str[:2])].index
p_pipes_out_eu = (p_links.loc[:, idx_pipes_eu_h2]
.groupby(group(n.links.loc[idx_pipes_eu_h2], b="bus0", eu=eu_countries)).sum())
e_pipes_out_eu = ((p_pipes_out_eu * n.snapshot_weightings.generators)
.rename({"bus0": "bus"}).sum("snapshot"))
e_pipes_out = e_pipes_out.sel(bus=eu_countries) + e_pipes_out_eu
# add pipes into EU
p_pipes_in_eu = (p_links.loc[:, idx_pipes_eu_h2]
.groupby(group(n.links.loc[idx_pipes_eu_h2], b="bus1", eu=eu_countries)).sum())
e_pipes_in_eu = ((p_pipes_in_eu * n.snapshot_weightings.generators)
.rename({"bus1": "bus"}).sum("snapshot"))
e_pipes_in = e_pipes_in.sel(bus=eu_countries) + e_pipes_in_eu
# calculate the energy imported from outside of Europe
h2_import_gen_carrier = ['import pipeline-H2',
'import shipping-H2']
idx_h2_import = n.generators[(n.generators.carrier.isin(h2_import_gen_carrier))].index
if not idx_h2_import.empty: # some years have no import generators
p_h2_import_per_country = (p_generators.loc[:, idx_h2_import]
.groupby(group(n.generators.loc[idx_h2_import])).sum())
e_h2_import = (p_h2_import_per_country * n.snapshot_weightings.generators).sum("snapshot")
if per_country:
# add external imports to EU
p_h2_import_eu = (p_generators.loc[:, idx_h2_import]
.groupby(group(n.generators.loc[idx_h2_import], eu=eu_countries)).sum())
e_h2_import_eu = (p_h2_import_eu * n.snapshot_weightings.generators).sum("snapshot")
eu_import_countries = e_h2_import.indexes['bus'].intersection(eu_countries)
e_h2_import = e_h2_import.sel(bus=eu_import_countries) + e_h2_import_eu
# calculate import balances for countries/clusters
local_import_h2 = e_h2_import + e_pipes_in - e_pipes_out
else:
# calculate import balances for countries/clusters
local_import_h2 = e_pipes_in - e_pipes_out
# calculate the demand for H2 as energy carrier
demand_h2 = local_import_h2 + generation_e_h2
# reduce custom limits to those applying to hydrogen and minimum values
self_sufficiency_h2_min = (xr.DataArray(self_sufficiency_limits.query(f'carrier=="H2"')['min']
.dropna()).rename(country="bus"))
# find countries / clusters that appear both in the custom limit and in the network data
index = self_sufficiency_h2_min.indexes["bus"].intersection(generation_e_h2.indexes["bus"])
# define constraint: local generation should cover the local demand by the degree of self sufficiency. The local
# demand is given by the sum of imports and generation.
if not index.empty:
name = f"self-sufficiency-H2-min-{per_country_name}"
n.model.add_constraints(generation_e_h2.sel(bus=index)
- demand_h2.sel(bus=index) * (self_sufficiency_h2_min.loc[index]) >= 0, name=name)
# reduce custom limits to those applying to hydrogen and maximum values
self_sufficiency_h2_max = (xr.DataArray(self_sufficiency_limits.query(f'carrier=="H2"')['max']
.dropna()).rename(country="bus"))
# find countries / clusters that appear both in the custom limit and in the network data
index = self_sufficiency_h2_max.indexes["bus"].intersection(generation_e_h2.indexes["bus"])
# define constraint: local generation should cover the local demand by the degree of self sufficiency. The local
# demand is given by the sum of imports and generation.
if not index.empty:
name = f"self-sufficiency-H2-max-{per_country_name}"
n.model.add_constraints(generation_e_h2.sel(bus=index)
- demand_h2.sel(bus=index) * (self_sufficiency_h2_max.loc[index]) <= 0, name=name)
### create electricity constraint
# calculate electric energy generated in each country/cluster
gen_carrier_elec = ['offwind-ac',
'onwind',
'ror',
'solar',
'offwind-dc',
'solar rooftop']
idx_gens_elec = n.generators[(n.generators.carrier.isin(gen_carrier_elec))].index
p_generation_per_country_elec = (p_generators.loc[:, idx_gens_elec]
.groupby(group(n.generators.loc[idx_gens_elec])).sum())
e_generation_elec = (p_generation_per_country_elec * n.snapshot_weightings.generators).sum("snapshot")
if per_country:
# calculate electricity generation via generators inside the EU
p_generation_eu_elec = (p_generators.loc[:, idx_gens_elec]
.groupby(group(n.generators.loc[idx_gens_elec], eu=eu_countries)).sum())
e_generation_eu_elec = (p_generation_eu_elec * n.snapshot_weightings.generators).sum("snapshot")
e_generation_elec = e_generation_elec.sel(bus=eu_countries) + e_generation_eu_elec
# calculate electrical energy generated via storage units in each cluster/country
storage_unit_carrier = ["hydro"]
idx_storage_units = n.storage_units[(n.storage_units.carrier.isin(storage_unit_carrier))].index
p_storage_units_per_country = (p_storage.loc[:, idx_storage_units]
.groupby(group(n.storage_units.loc[idx_storage_units])).sum())
e_storage_units = (p_storage_units_per_country * n.snapshot_weightings.generators).sum("snapshot")
if per_country:
# sum up all storage units in the EU
p_storage_units_eu = (p_storage.loc[:, idx_storage_units]
.groupby(group(n.storage_units.loc[idx_storage_units], eu=eu_countries)).sum())
e_storage_units_eu = (p_storage_units_eu * n.snapshot_weightings.generators).sum("snapshot")
eu_storage_units = e_storage_units.indexes['bus'].intersection(eu_countries)
e_storage_units = e_storage_units.sel(bus=eu_storage_units) + e_storage_units_eu
# calculate electrical energy generated via links in each cluster/country
link_carrier_elec = [ # hydrogen to electricity
'H2 Fuel Cell',
'H2 turbine',
# combined heat and power
'residential rural micro gas CHP',
'services rural micro gas CHP',
'residential urban decentral micro gas CHP',
'services urban decentral micro gas CHP',
'urban central gas CHP',
'urban central gas CHP CC',
'urban central solid biomass CHP',
'urban central solid biomass CHP CC',
# conventional carriers
'coal',
'OCGT',
'CCGT',
'lignite',
'nuclear',
'oil',
'allam']