-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
1645 lines (1213 loc) · 96.2 KB
/
utils.py
File metadata and controls
1645 lines (1213 loc) · 96.2 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
import math
from pathlib import Path
import pandas as pd
import numpy as np
import json
from pyomo.environ import value
import datetime
import matplotlib.pyplot as plt
import matplotlib as mpl
'''[[[ Functions used to load data ]]]'''
def get_config(path) -> dict:
with open(Path(__file__).parent / path) as file:
return json.load(file)
def read_constant(path, year_costs=None) :
'''Reads model constants. This includes the parameters of the different technologies (efficiencies, costs, potentials, ...) as well as constant constraints (potentials, carbon budget, ...).
There are two possible formats : either the file provided has a single column which contains the inputs, or it regroups several years in which case only one will be used. In the latter case, years should be provided as different columns with the first row as the index.
path: str
The path to the file. The file's first column should contain the index. If several years are provided, the year index should be the first row.
year: int (optional)
Required if several years are provided in the same file, in which case this is the one selected.
Returns : pd.Series'''
#This function could be optimized by not reading data files twice, but since files read should be very small and to improve readability, it was kept this way
df = pd.read_csv(Path(__file__).parent / path, index_col=0, header=None)
if df.shape[1] > 1 : # number of columns is greater than 1, which means we have several years
if year_costs is None :
raise ValueError(f"Error reading {path} : Year of interest needs to be provided when data includes several columns")
df = pd.read_csv(path, index_col=0) #re-read the df with the first row (which contains years) as the column names
df = df.loc[:, str(year_costs)].squeeze() #Select the column of interest
else :
df = df.squeeze("columns") # Squeez columns of the dataframe to get a pandas series
return df
def read_profile(path, time_scale, nb_years=1, years_of_interest=None) :
'''Reads model inputs that are variable in time, like demand profiles or VRE production profiles.
There are two possible formats : if the file contains one year's worth of data, then it is duplicated to match the number of years the simulation will run for ; otherwise, the file has to contain several years' worth of data, corresponding to the number of years. We consider that a year contains 365 days.
path: str
The path to the file. The file should contain a single profile, with date and hour in the first two columns and data in the third.
For hourly timeseries the date format should either be %Y-%m-%d or %m-%d. For others it does not matter as the index is never refered to
(neither for referencing nor for operation on series like adding series).
time_scale: str
Can be "hourly", "daily" or "monthly"
nb_years: int. Defaults to 1.
years_of_interest: str or int list
Used to generate the index when the timeseries only contains one year and is expanded to several years, or when a 1-year long series with no year is provided
Returns: pd.Series containing data for the correct number of years'''
if time_scale == "hourly":
yearly_nb_lines = 8760
index_col = [0, 1]
elif time_scale == "daily":
yearly_nb_lines = 365
index_col = 0
elif time_scale == "monthly":
yearly_nb_lines = 12
index_col = 0
else:
raise ValueError(f"time scale '{time_scale}' not recognized")
df = pd.read_csv(Path(__file__).parent / path, index_col=index_col, header=0).squeeze("columns")
if nb_years == 1:
if df.shape[0] == yearly_nb_lines :
# For now no daily or monthly series are compared, added or accessed by index, so it is only necessary to have a rigorous datetime format for hourly series.
# This section should be changed if this changes
if time_scale == "hourly" :
if len(df.index.get_level_values(0)[0]) > 5 : # This is a really messy way to check wether the date format includes a year
df.index = pd.MultiIndex.from_arrays([pd.to_datetime(df.index.get_level_values(0), format="%Y-%m-%d").to_series().dt.date,
df.index.get_level_values(1)])
else : # In this case the date format only constains day and month
if years_of_interest is None :
raise ValueError(f"Error reading {path} : Year of interest needs to be provided when the timeseries contains one year and has no specified year")
else :
df.index = pd.MultiIndex.from_arrays([pd.to_datetime(df.index.get_level_values(0), format="%m-%d").to_series().apply(lambda dt: dt.replace(year=years_of_interest[0])).dt.date,
df.index.get_level_values(1)])
else :
raise ValueError(f"Error reading {path} : Expected file with {yearly_nb_lines} lines, got {df.shape[0]}")
else:
if df.shape[0] == yearly_nb_lines*nb_years : # Timeseries provided already contains the correct number of years
# For now no daily or monthly series are compared, added or accessed by index, so it is only necessary to have a rigorous datetime format for hourly series
if time_scale == "hourly" :
df.index = pd.MultiIndex.from_arrays([pd.to_datetime(df.index.get_level_values(0), format="%Y-%m-%d").to_series().dt.date,
df.index.get_level_values(1)])
elif df.shape[0] == yearly_nb_lines : # Timeseries provided contains one year. The study is on several years, so the timeseries will be repeated
df_1y = df.copy()
df = pd.concat([df_1y]*nb_years, ignore_index=True)
# For now no daily or monthly series are compared, added or accessed by index, so it is only necessary to have a rigorous datetime format for hourly series.
# This section should be changed if this changes
# years_of_interest allows the construction of the date index
if time_scale == "hourly" :
if years_of_interest is None :
raise ValueError(f"Error reading {path} : Years of interest need to be provided when the timeseries only contains one year at hourly timescale and nb_years is greater than one")
elif len(years_of_interest) != nb_years :
raise ValueError(f"Error reading {path} : nb_years={nb_years} but years_of_interest contains {len(years_of_interest)} years.")
else :
date_range = pd.concat([pd.date_range(f"{year}/01/01", f"{year}/12/31", freq="1d").to_series() for year in years_of_interest], ignore_index=True)
select_29_02 = (date_range.dt.day == 29) & (date_range.dt.month == 2) # create boolean series indicating 29/02 days
date_range = date_range.loc[~select_29_02].dt.date # remove those days
df.index = pd.MultiIndex.from_product([date_range, pd.date_range("00:00", "23:00", freq="1h").to_series().dt.hour],
names = ["Date", "Hour"])
else :
raise ValueError(f"Error reading {path} : Expected file with {yearly_nb_lines} or {yearly_nb_lines*nb_years} lines, got {df.shape[0]}")
return df.squeeze()
'''[[[ Functions used when defining the model ]]]'''
def calculate_annuities_capex(discount_rate, capex, construction_time, lifetime):
"""Calculate annuities for energy technologies and renovation technologies based on capex data.
Assumes that all provided series have the same index and that it contains all relevant technologies"""
annuities = construction_time.copy()
for i in annuities.index:
annuities.at[i] = discount_rate.at[i] * capex.at[i] * (
discount_rate.at[i] * construction_time.at[i] + 1) / (
1 - (1 + discount_rate.at[i]) ** (-lifetime.at[i]))
return annuities
def calculate_annuities_storage_capex(discount_rate, storage_capex, construction_time, lifetime):
"""Calculate annuities for storage technologies based on capex data.
Assumes that all provided series have the same index and that it contains all relevant technologies"""
storage_annuities = storage_capex.copy()
for i in storage_annuities.index:
storage_annuities.at[i] = discount_rate.at[i] * storage_capex.at[i] * (
discount_rate.at[i] * construction_time.at[i] + 1) / (
1 - (1 + discount_rate.at[i]) ** (-lifetime.at[i]))
return storage_annuities
def update_vom_costs_scc(vOM_init, scc, emission_rate):
"""Add emission cost related to social cost of carbon to fossil vectors vOM costs.
:param vOM_init: float
Initial vOM in M€/GW = €/kW
:param scc: int
€/tCO2
:param emission_rate: float
tCO2/MWh.
Returns
vOM in M€/GW(h) = €/kW(h)
"""
return vOM_init + scc * emission_rate / 1000
def define_month_hours(first_month, nb_years, months_hours, hours_by_months):
"""
Calculates range of hours for each month
:param first_month: int
:param nb_years: int
:param months_hours: dict
:param hours_by_months: dict
:return:
Dict containing the range of hours for each month considered in the model
"""
j = first_month + 1
for i in range(2, 12 * nb_years + 1):
hour = months_hours[i - 1][-1] + 1 # get the first hour for a given month
months_hours[i] = range(hour, hour + hours_by_months[j])
j += 1
if j == 13:
j = 1
return months_hours
def shift_range(h, L, last_hour):
return [(h - L + i)%(last_hour + 1) for i in range(2*L + 1)]
'''[[[ Functions used to process outputs ]]]'''
def get_technical_cost(model, objective, scc, nb_years, carbon_content):
"""Returns technical cost (social cost without CO2 emissions-related cost)"""
gene_ngas = sum(value(model.gene["natural_gas", hour]) for hour in model.h) # GWh
gene_coal = sum(value(model.gene['coal', hour]) for hour in model.h) # GWh
total_emissions_gas = gene_ngas * carbon_content.at['natural_gas'] / 1000
total_emissions_coal = gene_coal * carbon_content.at['coal'] / 1000 # MtCO2
emissions = pd.Series({"natural_gas": total_emissions_gas / nb_years,
'coal': total_emissions_coal / nb_years}) # MtCO2/yr
total_emissions = total_emissions_gas + total_emissions_coal
technical_cost = objective - total_emissions * scc / 1000 # 1e9€/yr
return technical_cost, emissions
def extract_carbon_footprint(model, gene_per_tech, carbon_footprint, nb_years): #MtCO2eq/yr
"""Calculates the yearly and per energy carbon footprint of each technology"""
footprint = pd.Series(dtype=float)
for tech in model.all_tech:
if not (tech in model.CH4_balance_biogas) :
footprint.at[tech] = gene_per_tech.at[tech]*carbon_footprint.at[tech]/nb_years
footprint.at["biogas"] = sum(gene_per_tech.at[tech] for tech in model.CH4_balance_biogas)*carbon_footprint.at["biogas"]/nb_years
footprint.at["elec_network"] = sum(gene_per_tech.at[tech] for tech in model.elec_balance)*carbon_footprint.at["network"]/nb_years
footprint.at["elec_network_primarygene"] = sum(gene_per_tech.at[tech] for tech in model.elec_primary_prod)*carbon_footprint.at["network"]/nb_years
footprint.at["TOTAL"] = footprint.sum() - footprint.at["elec_network_primarygene"]
return footprint
def extract_hourly_balance(model, elec_demand, H2_demand, CH4_demand, conversion_efficiency, eta_in, eta_out, load_shift_period, last_hour):
"""Extracts hourly defined data, including demand, generation and storage
Returns a dataframe with hourly generation for each hour.
Using this function, you limit the number of times model output values are extracted, which is costly in computational power.
You can then manipulate pandas DataFrames which are must faster."""
hourly_balance = pd.DataFrame(index=elec_demand.index)
hourly_balance.loc[:, "elec_demand"] = elec_demand
hourly_balance.loc[:, "H2_demand"] = H2_demand
hourly_balance.loc[:, "CH4_demand"] = CH4_demand
hourly_balance.loc[:, "load_shift_up"] = value(model.dsm_up[:]) # GW
hourly_balance.loc[:, "load_shift_down"] = [sum(value(model.dsm_down[hh, h]) for hh in shift_range(h, load_shift_period, last_hour)) for h in model.h] # GW
hourly_balance.loc[:, "elec_demand_w/_shift"] = hourly_balance.loc[:, "elec_demand"] + hourly_balance.loc[:, "load_shift_up"] - hourly_balance.loc[:, "load_shift_down"]
hourly_balance.loc[:, "demand_on_hold"] = pd.Series(index=elec_demand.index, dtype=float)
hourly_balance.iloc[0, hourly_balance.columns.get_loc("demand_on_hold")] = hourly_balance.iloc[0, hourly_balance.columns.get_loc("load_shift_down")] - hourly_balance.iloc[0, hourly_balance.columns.get_loc("load_shift_up")]
for h in range(1, last_hour+1):
hourly_balance.iloc[h, hourly_balance.columns.get_loc("demand_on_hold")] = hourly_balance.iloc[h-1, hourly_balance.columns.get_loc("demand_on_hold")] + hourly_balance.iloc[h, hourly_balance.columns.get_loc("load_shift_down")] - hourly_balance.iloc[h, hourly_balance.columns.get_loc("load_shift_up")]
for tech in model.prod_tech:
hourly_balance.loc[:, tech] = value(model.gene[tech, :]) # GW
for tech in model.conversion_tech:
hourly_balance.loc[:, tech] = value(model.conv_output[tech, :]) # GW
for tech in model.str:
hourly_balance.loc[:, tech] = value(model.str_output[tech, :]) # GW
for tech in model.reserve:
hourly_balance.loc[:, tech+"_frr"] = value(model.frr[tech, :]) # GW
hourly_balance.loc[:, tech+"_fcr"] = value(model.fcr[tech, :]) # GW
# We add technologies which include a conversion parameter, to express their hourly generation in GWh of the input vector
for tech in model.conversion_tech:
hourly_balance.loc[:, tech + "_input"] = value(model.conv_output[tech, :]) / conversion_efficiency.at[tech]
for tech in model.str:
hourly_balance.loc[:, tech + "_input"] = value(model.str_input[tech, :]) # GW
hourly_balance.loc[:, tech + "_state_charge"] = value(model.state_of_charge[tech, :]) # GW
hourly_balance.loc[:, "lake_state_charge"] = value(model.lake_stored[:]) # GW
hourly_balance.loc[:, "storage_input_losses"] = sum(hourly_balance.loc[:, tech + "_input"]*(1 - eta_in.at[tech]) for tech in model.str)
hourly_balance.loc[:, "storage_output_losses"] = sum(hourly_balance.loc[:, tech]*(1/eta_out.at[tech] - 1) for tech in model.str)
return hourly_balance # GW
def extract_hourly_demand(vector, model, conversion_efficiency, hourly_balance):
"""Only useful when annual demand is provided instead of a demand profile.
Extracts how much of the demand is satisfied at each hour.
:param vector: string
Should be one of : 'methane', 'CH4', 'hydrogen', 'H2'
:param model: pyomo model
:param conversion_efficiency: pd.Series
:return: pd.Series"""
if vector == "methane" or vector == "CH4":
supply_list = model.CH4_balance
usage_list = model.use_CH4
elif vector == "hydrogen" or vector == "H2":
supply_list = model.H2_balance
usage_list = model.use_H2
supply = pd.Series(index=hourly_balance.index, dtype=float)
for h in model.h:
supply.iat[h] = sum(hourly_balance.iloc[h, hourly_balance.columns.get_loc(tech)] for tech in supply_list) # GW
usage = pd.Series(index=model.hourly_balance.index, dtype=float)
for h in model.h:
usage.iat[h] = sum(hourly_balance.iloc[h, hourly_balance.columns.get_loc(tech+"_input")] for tech in usage_list)
return supply - usage
def extract_curtailment(model, conversion_efficiency, hourly_balance):
"""Calculates and returns hourly curtailed electricity.
Also adds it to hourly_balance"""
curtailment = pd.Series(index=hourly_balance.index, dtype=float)
for h in model.h:
elec_supply_h = sum(hourly_balance.iloc[h, hourly_balance.columns.get_loc(tech)] for tech in model.elec_balance)
elec_usage_h = sum(hourly_balance.iloc[h, hourly_balance.columns.get_loc(tech+"_input")] for tech in model.use_elec)
curtailment.iat[h] = elec_supply_h - elec_usage_h - hourly_balance.iloc[h, hourly_balance.columns.get_loc("elec_demand_w/_shift")]
if math.isclose(curtailment.iat[h], 0, abs_tol=1e-09):
curtailment.iat[h] = 0
hourly_balance.loc[:, "curtailment"] = curtailment
return curtailment
def get_carbon_content(model, hourly_balance, carbon_content, conversion_efficiency):
"""Estimates the carbon content of gas, based on methodology by ADEME and RTE (méthode moyenne horaire).
Returns the result in gCO2/kWh = tCO2/GWh
Also adds a column in hourly_balance that indicates the carbon content of electricity in gCO2/kWh = tCO2/GWh"""
# Estimate carbon content of gas
CH4_carbon_content = (hourly_balance.loc[:, 'natural_gas'].sum() * carbon_content.at['natural_gas']) / (hourly_balance.loc[:, 'natural_gas'] + hourly_balance.loc[:, 'methanization'] + hourly_balance.loc[:, 'pyrogazification']).sum()
hourly_balance.loc[:, 'CH4_carbon_content'] = hourly_balance.apply(
lambda row: 1000*(row.at["ch4_ocgt"] / conversion_efficiency.at["ch4_ocgt"] * CH4_carbon_content +
row.at["ch4_ccgt"] / conversion_efficiency.at["ch4_ccgt"] * CH4_carbon_content +
row.at['coal'] * carbon_content.at['coal']) / sum(row[tech] for tech in model.elec_balance),
axis=1)
return CH4_carbon_content * 1e3
def extract_peak_load(hourly_balance:pd.DataFrame, conversion_efficiency):
"""Deprecated
Returns the value of peak load for electricity in GW. Includes electricity demand, as well as demand for electrolysis and methanation."""
peak_load = hourly_balance.copy()[["elec_demand", "electrolysis", "methanation"]]
peak_load["peak_electricity_load"] = peak_load["elec_demand"] + peak_load["electrolysis"] / conversion_efficiency[
"electrolysis"] + peak_load["methanation"] / conversion_efficiency["methanation"]
ind = peak_load["peak_electricity_load"].idxmax()
peak_load_info = peak_load.loc[ind]
peak_load_info.name = "Peak Load"
peak_load_info["hour"] = str(ind)
peak_load_info["year"] = str(ind//8760)
return peak_load_info # GW
def extract_spot_price(model, nb_hours):
"""Extracts spot prices in 1e3€/GW(h) = €/MW(h). Dual is in 1e9€/GW(h) = €/W(h) because the objective function is in 1e9€ and constraints are in GW(h)"""
spot_price = pd.DataFrame({"elec": [- 1e6 * model.dual[model.electricity_adequacy_constraint[h]] for h in model.h],
"CH4": [- 1e6 * model.dual[model.methane_balance_constraint[h]] for h in model.h],
"H2": [- 1e6 * model.dual[model.hydrogen_balance_constraint[h]] for h in model.h],
"fcr": [1e6 * model.dual[model.fcr_provision_constraint[h]] for h in model.h],
"frr": [1e6 * model.dual[model.frr_provision_constraint[h]] for h in model.h]
})
return spot_price
def extract_carbon_value(model, carbon_constraint, scc):
"""Extracts the social value of carbon in the considered model. Corresponds to the given SCC or to the shadow price of the carbon constraint if the latter is used. Carbon constraint is in ktCO2 and the objective function is in 1e9€, so the dual is in 1e6€/tCO2"""
if carbon_constraint:
# TODO: here we only consider the carbon value for one of the given years !! to modify in future
carbon_value = -1e6 * model.dual[model.carbon_budget_constraint[0]] # €/tCO2
else:
carbon_value = scc
return carbon_value
def extract_primary_gene(model, nb_years, hourly_balance):
"""Extracts yearly primary energy generation per source of energy in TWh"""
primary_generation = pd.Series(dtype=float)
for tech in model.prod_tech:
primary_generation.at[tech] = hourly_balance.loc[:, tech].sum() / 1000 / nb_years # TWh
return primary_generation
def extract_CH4_to_power(model, conversion_efficiency, nb_years, hourly_balance):
"""Extracts CH4 used to produce electricity in TWh"""
gas_to_power_input = pd.Series(dtype=float)
for tech in model.from_CH4_to_elec:
gas_to_power_input.at[tech] = hourly_balance.loc[:, tech+"_input"].sum() / 1000 / nb_years # TWh
return gas_to_power_input
def extract_H2_to_power(model, conversion_efficiency, nb_years, hourly_balance):
"""Extracts H2 used to produce electricity in TWh"""
H2_to_power_input = pd.Series(dtype=float)
for tech in model.from_H2_to_elec:
H2_to_power_input.at[tech] = hourly_balance.loc[:, tech+"_input"].sum() / 1000 / nb_years # TWh
return H2_to_power_input
def extract_power_to_CH4(model, conversion_efficiency, nb_years, hourly_balance):
"""Extracts electricity generation necessary to produce CH4 in TWh"""
power_to_CH4_input = pd.Series(dtype=float)
for tech in model.from_elec_to_CH4:
power_to_CH4_input[tech] = hourly_balance.loc[:, tech+"_input"].sum() / 1000 / nb_years # TWh
return power_to_CH4_input
def extract_power_to_H2(model, conversion_efficiency, nb_years, hourly_balance):
"""Extracts electricity used to produce H2 in TWh"""
power_to_H2_input = pd.Series(dtype=float)
for tech in model.from_elec_to_H2:
power_to_H2_input.at[tech] = hourly_balance.loc[:, tech+"_input"].sum() / 1000 / nb_years # TWh
return power_to_H2_input
def extract_balance(vector, model, demand, conversion_efficiency, hourly_balance, demand_is_profile=True):
"""Extracts total supply and usage (including demand) of the given vector (elec, CH4 or H2) in TWh
:param vector: string
Should be one of : 'elec', 'electricity', 'methane', 'CH4', 'hydrogen', 'H2'
:param model: pyomo model
:param demand: pd.Series or int
Type should be consistent with 'demand_is_profile'
:param conversion_efficiency: pd.Series
:param demand_is_profile: string
:return: pd.Series tuple
"""
if vector == "elec" or vector == "electricity":
supply_list = model.elec_balance
usage_list = model.use_elec
elif vector == "methane" or vector == "CH4":
supply_list = model.CH4_balance
usage_list = model.use_CH4
elif vector == "hydrogen" or vector == "H2":
supply_list = model.H2_balance
usage_list = model.use_H2
else:
raise ValueError(f"{vector} is not a recognized vector")
supply = pd.Series(dtype=float)
for tech in supply_list:
supply.at[tech] = hourly_balance.loc[:, tech].sum() / 1000 # TWh
usage = pd.Series(dtype=float)
for tech in usage_list:
usage.at[tech] = hourly_balance.loc[:, tech+"_input"].sum() / 1000 # TWh
if demand_is_profile:
usage.at["demand"] = demand.sum() / 1000 # TWh
else:
usage.at["demand"] = demand / 1000 # TWh
if vector == "elec" or vector == "electricity":
usage.at["curtailment"] = hourly_balance.loc[:, "curtailment"].sum() / 1000 # TWh
return supply, usage
def extract_storage_losses(elec_str_losses, CH4_str_losses, H2_str_losses):
str_losses = pd.concat([elec_str_losses, CH4_str_losses, H2_str_losses])
str_losses.loc[np.isclose(str_losses, 0)] = 0
str_losses.at["TOTAL"] = str_losses.sum()
return str_losses
def extract_annualized_costs_investment_new_capa(capacities, energy_capacities, existing_capacities, existing_energy_capacities,
annuities, storage_annuities, fOM):
"""
Returns the annualized costs coming from newly invested capacities and energy capacities. This includes annualized CAPEX + fOM.
Unit: 1e6€/yr
:param model: pyomo model
:param existing_capacities: pd.Series
:return:
"""
new_capacity = capacities - existing_capacities # pd.Series
costs_new_capacity = pd.concat([new_capacity, annuities, fOM], axis=1, ignore_index=True).rename(columns={0: "new_capacity", 1: "annuities", 2: "fOM"})
costs_new_capacity.loc[:, "annualized_costs"] = costs_new_capacity.loc[:, "new_capacity"] * (costs_new_capacity.loc[:, "annuities"] + costs_new_capacity.loc[:, "fOM"]) # includes both annuity and fOM ! not to be counted twice in the LCOE
new_storage_capacity = energy_capacities - existing_energy_capacities
costs_new_energy_capacity = pd.concat([new_storage_capacity, storage_annuities], axis=1, ignore_index=True).rename(columns={0: "new_capacity", 1: "storage_annuities"})
costs_new_energy_capacity.loc[:, "annualized_costs"] = costs_new_energy_capacity.loc[:, "new_capacity"] * costs_new_energy_capacity.loc[:, "storage_annuities"]
return costs_new_capacity.loc[:, "annualized_costs"], costs_new_energy_capacity.loc[:, "annualized_costs"]
def extract_annualized_costs_investment_new_capa_nofOM(capacities, energy_capacities, existing_capacities, existing_energy_capacities,
annuities, storage_annuities):
"""
Returns the annualized investment coming from newly invested capacities and energy capacities, without fOM. Unit: 1e6€/yr
:param model: pyomo model
:param existing_capacities: pd.Series
:return:
"""
new_capacity = capacities - existing_capacities # pd.Series
costs_new_capacity = pd.concat([new_capacity, annuities], axis=1, ignore_index=True).rename(columns={0: "new_capacity", 1: "annuities"})
costs_new_capacity = costs_new_capacity.dropna()
costs_new_capacity.loc[:, "annualized_costs"] = costs_new_capacity.loc[:, "new_capacity"] * costs_new_capacity.loc[:, "annuities"]
new_storage_capacity = energy_capacities - existing_energy_capacities
costs_new_energy_capacity = pd.concat([new_storage_capacity, storage_annuities], axis=1, ignore_index=True).rename(columns={0: "new_capacity", 1: "storage_annuities"})
costs_new_energy_capacity = costs_new_energy_capacity.dropna()
costs_new_energy_capacity.loc[:, "annualized_costs"] = costs_new_energy_capacity.loc[:, "new_capacity"] * costs_new_energy_capacity.loc[:, "storage_annuities"]
return costs_new_capacity.loc[:, "annualized_costs"], costs_new_energy_capacity.loc[:, "annualized_costs"]
def extract_OM_cost(model, capacities, fOM, vOM, generation, scc, carbon_content, carbon_constraint=True, nb_years=1):
"""Returns operation and maintenance costs, which corresponds to fOM and vOM, including SCC when it is used. vOM for gas include the SCC. Unit: 1e6€/yr
IMPORTANT REMARK: we divide generation by number of total years to get the average yearly generation
:param scc: int
Social cost of carbon used to estimate optimal power mix.
"""
if not carbon_constraint: # ie optimization with a given social cost of carbon
# we remove the SCC in this vOM
vOM_no_scc = vOM.copy()
vOM_no_scc.at["natural_gas"] = update_vom_costs_scc(vOM_no_scc.at["natural_gas"], scc=(-scc), emission_rate=carbon_content.at['natural_gas']) # €/kWh
vOM_no_scc.at["coal"] = update_vom_costs_scc(vOM_no_scc.at["coal"], scc=(-scc), emission_rate=carbon_content.at['coal']) # €/kWh
# variable cost only due to actual scc, not anticipated scc
vOM_SCC_only = vOM - vOM_no_scc
system_fOM_vOM = pd.concat([capacities, fOM, vOM_no_scc, vOM_SCC_only, generation/nb_years], axis=1, ignore_index=True).rename(
columns={0: "capacity", 1: "fOM", 2: "vOM_no_scc", 3: "vOM_SCC_only", 4: "generation"})
system_fOM_vOM = system_fOM_vOM.dropna()
system_fOM_vOM.loc[:, "OM_cost_noSCC"] = system_fOM_vOM.loc[:, "capacity"] * system_fOM_vOM.loc[:, "fOM"] + system_fOM_vOM.loc[:, "generation"] * system_fOM_vOM.loc[:, "vOM_no_scc"]
system_fOM_vOM.loc[:, "OM_cost_SCC_only"] = system_fOM_vOM.loc[:, "generation"] * system_fOM_vOM.loc[:, "vOM_SCC_only"]
carbon_cost = system_fOM_vOM.loc[:, "OM_cost_SCC_only"].sum()
system_fOM_vOM = system_fOM_vOM.loc[:, "OM_cost_noSCC"]
system_fOM_vOM.rename(columns={'OM_cost_noSCC': 'OM_cost'}, inplace=True)
system_vOM_fOM.loc["carbon_cost", "OM_cost"] = carbon_cost
else:
system_fOM_vOM = pd.concat([capacities, fOM, vOM, generation/nb_years], axis=1, ignore_index=True).rename(columns={0: "capacity", 1: "fOM", 2: "vOM", 3: "generation"})
system_fOM_vOM = system_fOM_vOM.dropna()
system_fOM_vOM.loc[:, "OM_cost"] = system_fOM_vOM.loc[:, "capacity"] * system_fOM_vOM.loc[:, "fOM"] + system_fOM_vOM.loc[:, "generation"] * system_fOM_vOM.loc[:, "vOM"]
system_fOM_vOM = system_fOM_vOM.loc[:, "OM_cost"]
return system_fOM_vOM
def calculate_lcoe_per_tech(model, hourly_balance, annuities, storage_annuities, fOM, vOM, spot_price, nb_years, gene_per_tech, capacity, energy_capacity, existing_capacity, existing_energy_capacity):
"""This function calculates the levelized cost of energy produced by the newly installed plants (ie not present in 2020 and not coming from an earlier
instance of the model) for each technology. Generation is attributed proportionnately to capacity to newly constructed plants and old ones, as in the
model all plants are aggregated."""
lcoe = pd.DataFrame(columns=["lcoe [€/MWh]", "lcoe without input [€/MWh]"], dtype=object)
for tech in (model.prod_tech | model.conversion_tech | model.str):
if capacity.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
# /!\ gene is in TWh
cost_without_input = (capacity.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech] # 1e6€
if tech in model.str:
cost_without_input += (energy_capacity.at[tech] - existing_energy_capacity.at[tech])*storage_annuities.at[tech]*nb_years
if tech in model.conversion_tech or tech in model.str:
# /!\ spot_prices are in 1e3€/GWh
if tech in model.use_elec:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]).sum()/1e3 # 1e6€
if tech in model.use_CH4:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()/1e3 # 1e6€
if tech in model.use_H2:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]).sum()/1e3 # 1e6€
if gene_new_installation != 0:
lcoe.at[tech, "lcoe [€/MWh]"] = cost / gene_new_installation
lcoe.at[tech, "lcoe without input [€/MWh]"] = cost_without_input / gene_new_installation
else:
lcoe.at[tech, "lcoe [€/MWh]"] = "No new plants"
else : # primary production
if gene_new_installation != 0:
lcoe.at[tech, "lcoe [€/MWh]"] = cost_without_input / gene_new_installation
else:
lcoe.at[tech, "lcoe [€/MWh]"] = "No new plants"
"""
for tech in model.prod_tech:
if nominal_power.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/nominal_power.at[tech]
# /!\ gene is in TWh
cost_without_input = (nominal_power.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech] # 1e6€
if gene_new_installation != 0:
lcoe.at[tech, "lcoe [€/MWh]"] = cost_without_input / gene_new_installation
else:
lcoe.at[tech, "lcoe [€/MWh]"] = "No new plants"
for tech in model.conversion_tech:
if output_power.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/output_power.at[tech]
# /!\ gene is in TWh
cost_without_input = (output_power.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech] # 1e6€
if tech in model.use_elec:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]).sum()/1e3 # 1e6€
if tech in model.use_CH4:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()/1e3 # 1e6€
if tech in model.use_H2:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]).sum()/1e3 # 1e6€
if gene_new_installation != 0:
lcoe.at[tech, "lcoe [€/MWh]"] = cost / gene_new_installation
lcoe.at[tech, "lcoe without input [€/MWh]"] = cost_without_input / gene_new_installation
else:
lcoe.at[tech, "lcoe [€/MWh]"] = "No new plants"
for tech in model.str:
if output_power.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/output_power.at[tech]
# /!\ gene is in TWh
cost_without_input = (output_power.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech] # 1e6€
cost_without_input += (energy_capacity.at[tech] - existing_energy_capacity.at[tech])*storage_annuities.at[tech]*nb_years
if tech in model.use_elec:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]).sum()/1e3 # 1e6€
if tech in model.use_CH4:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()/1e3 # 1e6€
if tech in model.use_H2:
cost = cost_without_input + (hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]).sum()/1e3 # 1e6€
if gene_new_installation != 0:
lcoe.at[tech, "lcoe [€/MWh]"] = cost / gene_new_installation
lcoe.at[tech, "lcoe without input [€/MWh]"] = cost_without_input / gene_new_installation
else:
lcoe.at[tech, "lcoe [€/MWh]"] = "No new plants"
"""
return lcoe
def compute_costs(model, annuities, fOM, vOM, storage_annuities, gene_per_tech, capacity, existing_capacity,
energy_capacity, existing_energy_capacity, nb_years):
costs_elec = 0 # 1e6€
for tech in model.elec_balance:
if capacity.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
# /!\ gene is in TWh
costs_elec += (capacity.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech]
if tech in model.str:
costs_elec += (energy_capacity.at[tech] - existing_energy_capacity.at[tech])*storage_annuities.at[tech]*nb_years
costs_CH4 = 0 # 1e6€
for tech in model.CH4_balance:
if capacity.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
# /!\ gene is in TWh
costs_CH4 += (capacity.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech]
if tech in model.str:
costs_CH4 += (energy_capacity.at[tech] - existing_energy_capacity.at[tech])*storage_annuities.at[tech]*nb_years
costs_H2 = 0 # 1e6€
for tech in model.H2_balance:
if capacity.at[tech] != 0:
gene_new_installation = gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
# /!\ gene is in TWh
costs_H2 += (capacity.at[tech] - existing_capacity.at[tech])*(annuities.at[tech] + fOM.at[tech])*nb_years + gene_new_installation*1000*vOM.at[tech]
if tech in model.str:
costs_H2 += (energy_capacity.at[tech] - existing_energy_capacity.at[tech])*storage_annuities.at[tech]*nb_years
return costs_elec, costs_CH4, costs_H2
def compute_lcoe(costs_elec, costs_CH4, costs_H2, G2P_bought, P2G_CH4_bought, P2G_H2_bought, sumgene_elec, sumgene_CH4, sumgene_H2):
"""Compute LCOE by using the costs of buying electricity / CH4 / H2. Parameters sumgene_elec, sumgene_CH4 and
sumgene_H2 refer to the total production from each system (which can be used either to satisfy final demand, or for
vector coupling)."""
if sumgene_elec != 0 :
lcoe_elec = (costs_elec + G2P_bought) / sumgene_elec # €/MWh
else :
lcoe_elec = "No electricity production"
if sumgene_CH4 != 0 :
lcoe_CH4 = (costs_CH4 + P2G_CH4_bought) / sumgene_CH4 # €/MWh
else :
lcoe_CH4 = "No CH4 production"
if sumgene_H2 != 0 :
lcoe_H2 = (costs_H2 + P2G_H2_bought) / sumgene_H2 # €/MWh
else :
lcoe_H2 = "No H2 production"
return lcoe_elec, lcoe_CH4, lcoe_H2
def compute_lcoe_volumetric(model, gene_per_tech, conversion_efficiency, costs_elec, costs_CH4, costs_H2, elec_demand_tot, CH4_demand_tot, H2_demand_tot):
"""Computes a volumetric LCOE, where costs of each system (respectively, electricity, methane and hydrogen) are distributed across the different systems based on volumes (eg, volume of demand versus volume of gas used for the electricity system)."""
gene_from_CH4_to_elec = sum(gene_per_tech.at[tech]/conversion_efficiency.at[tech] for tech in model.from_CH4_to_elec) # TWh
gene_from_H2_to_elec = sum(gene_per_tech.at[tech]/conversion_efficiency.at[tech] for tech in model.from_H2_to_elec) # TWh
gene_from_elec_to_CH4 = sum(gene_per_tech.at[tech]/conversion_efficiency.at[tech] for tech in model.from_elec_to_CH4) # TWh
gene_from_elec_to_H2 = sum(gene_per_tech.at[tech]/conversion_efficiency.at[tech] for tech in model.from_elec_to_H2) # TWh
costs_CH4_to_demand = costs_CH4 * CH4_demand_tot / (CH4_demand_tot + gene_from_CH4_to_elec) # 1e6 €
costs_CH4_to_elec = costs_CH4 * gene_from_CH4_to_elec / (CH4_demand_tot + gene_from_CH4_to_elec)
costs_H2_to_demand = costs_H2 * H2_demand_tot / (H2_demand_tot + gene_from_H2_to_elec)
costs_H2_to_elec = costs_H2 * gene_from_H2_to_elec / (H2_demand_tot + gene_from_H2_to_elec)
costs_elec_to_demand = costs_elec * elec_demand_tot / (
elec_demand_tot + gene_from_elec_to_H2 + gene_from_elec_to_CH4)
costs_elec_to_CH4 = costs_elec * gene_from_elec_to_CH4 / (
elec_demand_tot + gene_from_elec_to_H2 + gene_from_elec_to_CH4)
costs_elec_to_H2 = costs_elec * gene_from_elec_to_H2 / (
elec_demand_tot + gene_from_elec_to_H2 + gene_from_elec_to_CH4)
if elec_demand_tot != 0 :
lcoe_elec_volume = (costs_CH4_to_elec + costs_H2_to_elec + costs_elec_to_demand) / elec_demand_tot # € / MWh
else :
lcoe_elec_volume = "No exogenous demand for electricity"
if CH4_demand_tot != 0 :
lcoe_CH4_volume = (costs_elec_to_CH4 + costs_CH4_to_demand) / CH4_demand_tot # € / MWh
else :
lcoe_CH4_volume = "No exogenous demand for CH4"
if H2_demand_tot != 0 :
lcoe_H2_volume = (costs_elec_to_H2 + costs_H2_to_demand) / H2_demand_tot # € / MWh
else :
lcoe_H2_volume = "No exogenous demand for H2"
return lcoe_elec_volume, lcoe_CH4_volume, lcoe_H2_volume
def compute_lcoe_value(model, hourly_balance, costs_elec, costs_CH4, costs_H2, elec_demand_tot, CH4_demand_tot, H2_demand_tot,
elec_demand, CH4_demand, H2_demand, spot_price):
gene_from_CH4_to_elec_value = sum(
(hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()
for tech in model.from_CH4_to_elec)
gene_from_H2_to_elec_value = sum(
(hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]).sum()
for tech in model.from_H2_to_elec)
gene_from_elec_to_CH4_value = sum(
(hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]).sum()
for tech in model.from_elec_to_CH4)
gene_from_elec_to_H2_value = sum(
(hourly_balance.loc[:, tech+"_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()
for tech in model.from_elec_to_H2)
elec_demand_tot_value = (elec_demand.reset_index(drop=True)*spot_price.loc[:, "elec"]).sum()
CH4_demand_tot_value = (CH4_demand.reset_index(drop=True)*spot_price.loc[:, "CH4"]).sum()
H2_demand_tot_value = (H2_demand.reset_index(drop=True)*spot_price.loc[:, "H2"]).sum()
# 1e6 €
costs_CH4_to_demand_value = costs_CH4 * CH4_demand_tot_value / (
CH4_demand_tot_value + gene_from_CH4_to_elec_value)
costs_CH4_to_elec_value = costs_CH4 * gene_from_CH4_to_elec_value / (
CH4_demand_tot_value + gene_from_CH4_to_elec_value)
costs_H2_to_demand_value = costs_H2 * H2_demand_tot_value / (
H2_demand_tot_value + gene_from_H2_to_elec_value)
costs_H2_to_elec_value = costs_H2 * gene_from_H2_to_elec_value / (
H2_demand_tot_value + gene_from_H2_to_elec_value)
costs_elec_to_demand_value = costs_elec * elec_demand_tot_value / (
elec_demand_tot_value + gene_from_elec_to_H2_value + gene_from_elec_to_CH4_value)
costs_elec_to_CH4_value = costs_elec * gene_from_elec_to_CH4_value / (
elec_demand_tot_value + gene_from_elec_to_H2_value + gene_from_elec_to_CH4_value)
costs_elec_to_H2_value = costs_elec * gene_from_elec_to_H2_value / (
elec_demand_tot_value + gene_from_elec_to_H2_value + gene_from_elec_to_CH4_value)
if elec_demand_tot != 0 :
lcoe_elec_value = (costs_CH4_to_elec_value + costs_H2_to_elec_value + costs_elec_to_demand_value) / elec_demand_tot # € / MWh
else :
lcoe_elec_value = "No exogenous demand for electricity"
if CH4_demand_tot != 0 :
lcoe_CH4_value = (costs_elec_to_CH4_value + costs_CH4_to_demand_value) / CH4_demand_tot # € / MWh
else :
lcoe_CH4_value = "No exogenous demand for CH4"
if H2_demand_tot != 0 :
lcoe_H2_value = (costs_elec_to_H2_value + costs_H2_to_demand_value) / H2_demand_tot # € / MWh
else :
lcoe_H2_demand = "No exogenous demand for H2"
return lcoe_elec_value, lcoe_CH4_value, lcoe_H2_value
def transportation_distribution_cost(model, prediction_transport_and_distrib_annuity, capacity):
"""Estimation of annualized transport and distribution cost, based on solar and onshore wind capacities."""
solar_capacity = sum(capacity.at[tech] for tech in model.solar)
onshore_capacity = capacity.at["onshore"]
transport_and_distrib_annuity = prediction_transport_and_distrib_annuity.at["intercept"] + \
prediction_transport_and_distrib_annuity.at["solar"] * solar_capacity + \
prediction_transport_and_distrib_annuity.at["onshore"] * onshore_capacity # 1e9 €/yr
return transport_and_distrib_annuity
def extract_profit(model, hourly_balance, spot_price, vOM, new_annuities, new_str_annuities, frr_requirements, fcr_requirement, reserve_activation_rate, conversion_efficiency, capacity):
"""Extracts profit collected by each tech. This profit should be null except for rare goods (ie for techs with a limiting potential), so this output
is used to identify issues"""
profits = pd.Series(dtype=float)
for tech in model.elec_balance:
profits.at[tech] = (hourly_balance.loc[:, tech].reset_index(drop=True)*(spot_price.loc[:, "elec"]/1000 - vOM.at[tech])).sum() - new_annuities.at[tech] # 1e6€/yr
if tech in model.str:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]/1000).sum() - new_str_annuities.at[tech] # 1e6€/yr
if tech in model.from_CH4_to_elec:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]/1000).sum() # 1e6€/yr
if tech in model.from_H2_to_elec:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]/1000).sum() # 1e6€/yr
if tech in model.reserve:
profits.at[tech] += (hourly_balance.loc[:, tech+"_frr"].reset_index(drop=True)*spot_price.loc[:, "frr"]/1000).sum() # 1e6€/yr
profits.at[tech] += - (hourly_balance.loc[:, tech+"_frr"].reset_index(drop=True)*reserve_activation_rate.at["frr"]*vOM.at[tech]).sum() # 1e6€/yr
profits.at[tech] += (hourly_balance.loc[:, tech+"_fcr"].reset_index(drop=True)*spot_price.loc[:, "fcr"]/1000).sum() # 1e6€/yr
profits.at[tech] += - (hourly_balance.loc[:, tech+"_fcr"]*reserve_activation_rate.at["fcr"]*vOM.at[tech]).sum() # 1e6€/yr
if tech in model.from_CH4_to_elec:
profits.at[tech] += -(hourly_balance.loc[:, tech+"_frr"].reset_index(drop=True)/conversion_efficiency.at[tech]*reserve_activation_rate.at["frr"]*spot_price.loc[:, "CH4"]/1000).sum() # 1e6€/yr
profits.at[tech] += -(hourly_balance.loc[:, tech+"_fcr"].reset_index(drop=True)/conversion_efficiency.at[tech]*reserve_activation_rate.at["fcr"]*spot_price.loc[:, "CH4"]/1000).sum() # 1e6€/yr
if tech in model.from_H2_to_elec:
profits.at[tech] += -(hourly_balance.loc[:, tech+"_frr"].reset_index(drop=True)/conversion_efficiency.at[tech]*reserve_activation_rate.at["frr"]*spot_price.loc[:, "H2"]/1000).sum() # 1e6€/yr
profits.at[tech] += -(hourly_balance.loc[:, tech+"_fcr"].reset_index(drop=True)/conversion_efficiency.at[tech]*reserve_activation_rate.at["fcr"]*spot_price.loc[:, "H2"]/1000).sum() # 1e6€/yr
if tech in model.vre:
profits.at[tech] += - frr_requirements.at[tech]*capacity.at[tech]*spot_price.loc[:, "frr"].sum()/1000 # 1e6€/yr
for tech in model.CH4_balance:
profits.at[tech] = (hourly_balance.loc[:, tech].reset_index(drop=True)*(spot_price.loc[:, "CH4"]/1000 - vOM.at[tech])).sum() - new_annuities.at[tech] # 1e6€/yr
if tech in model.str:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "CH4"]/1000).sum() - new_str_annuities.at[tech] # 1e6€/yr
if tech in model.from_elec_to_CH4:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]/1000).sum() # 1e6€/yr
for tech in model.H2_balance:
profits.at[tech] = (hourly_balance.loc[:, tech].reset_index(drop=True)*(spot_price.loc[:, "H2"]/1000 - vOM[tech])).sum() - new_annuities.at[tech] # 1e6€/yr
if tech in model.str:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "H2"]/1000).sum() - new_str_annuities.at[tech] # 1e6€/yr
if tech in model.from_elec_to_H2:
profits.at[tech] += -(hourly_balance.loc[:, tech + "_input"].reset_index(drop=True)*spot_price.loc[:, "elec"]/1000).sum() # 1e6€/yr
profits.loc[np.isclose(profits, 0)] = 0
return profits
def extract_summary(objective, model, elec_demand, H2_demand, H2_demand_is_profile, CH4_demand, CH4_demand_is_profile, capacity, existing_capacity,
energy_capacity, existing_energy_capacity, annuities,
storage_annuities, fOM, vOM, conversion_efficiency, transportation_distribution_cost,
scc, nb_years, carbon_constraint, carbon_content, hourly_balance, spot_price):
"""This function compiles different general statistics of the electricity mix, including in particular LCOE."""
summary = pd.Series(dtype=float) # final dictionary for output
summary.at["total system cost [1e9€]"] = objective
# Total demands
elec_demand_tot = elec_demand.sum() / 1000 # electricity demand in TWh
summary.at["elec_demand_tot [TWh]"] = elec_demand_tot
if H2_demand_is_profile:
H2_demand_tot = H2_demand.sum() / 1000 # H2 demand in TWh
else:
H2_demand_tot = H2_demand / 1000 # H2 demand in TWh
H2_demand = extract_hourly_demand("H2", model, conversion_efficiency, hourly_balance)
summary.at["H2_demand_tot [TWh]"] = H2_demand_tot
if CH4_demand_is_profile:
CH4_demand_tot = CH4_demand.sum() / 1000 # CH4 demand in TWh
else:
CH4_demand_tot = CH4_demand / 1000 # CH4 demand in TWh
CH4_demand = extract_hourly_demand("CH4", model, conversion_efficiency, hourly_balance)
summary.at["CH4_demand_tot [TWh]"] = CH4_demand_tot
# Prices weighted by hourly demand (ie average price paid by consumers)
if elec_demand_tot != 0:
weighted_elec_price_demand = (spot_price.loc[:, "elec"]*elec_demand.reset_index(drop=True)).sum() / (elec_demand_tot * 1e3) # €/MWh
else :
weighted_elec_price_demand = "No exogenous demand for electricity"
summary.at["elec_price_weighted_by_demand [€/MWh]"] = weighted_elec_price_demand
if CH4_demand_tot != 0:
weighted_CH4_price_demand = (spot_price.loc[:, "CH4"]*CH4_demand.reset_index(drop=True)).sum() / (CH4_demand_tot * 1e3) # €/MWh
else :
weighted_CH4_price_demand = "No exogenous demand for CH4"
summary.at["CH4_price_weighted_by_demand [€/MWh]"] = weighted_CH4_price_demand
if H2_demand_tot != 0:
weighted_H2_price_demand = (spot_price.loc[:, "H2"]*H2_demand.reset_index(drop=True)).sum() / (H2_demand_tot * 1e3) # €/MWh
else :
weighted_H2_price_demand = "No exogenous demand for H2"
summary.at["H2_price_weighted_by_demand [€/MWh]"] = weighted_H2_price_demand
summary.at["load_shifted [TWh]"] = hourly_balance.loc[:, "load_shift_up"].sum()/1000
summary.at["load_shifted [%]"] = summary.at["load_shifted [TWh]"]/elec_demand_tot*100
# Overall energy generated by the technology in TWh over total considered years
gene_per_tech = pd.Series(dtype=float)
for tech in model.all_tech:
gene_per_tech.at[tech] = hourly_balance.loc[:, tech].sum() / 1000 # TWh
if math.isclose(gene_per_tech.at[tech], 0, abs_tol=5e-04):
gene_per_tech.at[tech] = 0
primary_gene_elec = sum(gene_per_tech.at[tech] for tech in model.elec_primary_prod)
summary.at["primary_gene_elec [TWh]"] = primary_gene_elec
gene_elec = sum(gene_per_tech.at[tech] for tech in model.elec_prod)
summary.at["gene_elec [TWh]"] = gene_elec
gene_elec_new_installation = 0
for tech in model.elec_balance:
if capacity.at[tech] != 0:
gene_elec_new_installation += gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
summary.at["gene_curtailed [TWh]"] = hourly_balance.loc[:, "curtailment"].sum()/1000
summary.at["gene_curtailed [%]"] = summary.at["gene_curtailed [TWh]"]/summary.at["primary_gene_elec [TWh]"]*100
primary_gene_CH4 = sum(gene_per_tech.at[tech] for tech in model.CH4_primary_prod)
summary.at["primary_gene_CH4 [TWh]"] = primary_gene_CH4
gene_CH4 = sum(gene_per_tech.at[tech] for tech in model.CH4_prod)
summary.at["gene_CH4 [TWh]"] = gene_CH4
gene_CH4_new_installation = 0
for tech in model.CH4_balance:
if capacity.at[tech] != 0:
gene_CH4_new_installation += gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
gene_H2 = sum(gene_per_tech.at[tech] for tech in model.H2_balance)
summary.at["gene_H2 [TWh]"] = gene_H2
gene_H2_new_installation = 0
for tech in model.H2_balance:
if capacity.at[tech] != 0:
gene_H2_new_installation += gene_per_tech.at[tech]*(capacity.at[tech] - existing_capacity.at[tech])/capacity.at[tech]
# Monetary values of the energy converted between gas and electricity in 1e6€
G2P_CH4_bought = (spot_price.loc[:, "CH4"] * sum(hourly_balance.loc[:, tech+"_input"] for tech in model.from_CH4_to_elec).reset_index(drop=True)).sum() / 1e3
G2P_H2_bought = (spot_price.loc[:, "H2"] * sum(hourly_balance.loc[:, tech+"_input"] for tech in model.from_H2_to_elec).reset_index(drop=True)).sum() / 1e3
G2P_bought = G2P_CH4_bought + G2P_H2_bought
P2G_CH4_bought = (spot_price.loc[:, "elec"] * sum(hourly_balance.loc[:, tech+"_input"] for tech in model.from_elec_to_CH4).reset_index(drop=True)).sum() / 1e3
P2G_H2_bought = (spot_price.loc[:, "elec"] * sum(hourly_balance.loc[:, tech+"_input"] for tech in model.from_elec_to_H2).reset_index(drop=True)).sum() / 1e3
# We calculate the costs associated to functioning of each system (elec, CH4, gas)
costs_elec, costs_CH4, costs_H2 = compute_costs(model, annuities, fOM, vOM, storage_annuities, gene_per_tech, capacity, existing_capacity, energy_capacity, existing_energy_capacity, nb_years) # 1e6 €
# We first calculate LCOE by using total costs.
lcoe_elec, lcoe_CH4, lcoe_H2 = \