-
Notifications
You must be signed in to change notification settings - Fork 2
/
Run.py
275 lines (232 loc) · 10.9 KB
/
Run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# encoding: utf-8
#
# (c) Minh Ha-Duong 2017
# minh.haduong@gmail.com
# Creative Commons Attribution-ShareAlike 4.0 International
#
#"""Assess the scenarios."""
"""Define a model run and the tables comparing a pair of runs."""
from init import pd, PLANT_TYPE, SOURCES
from init import START_YEAR, END_YEAR, YEARS, present_value
from init import kW, MW, USD, MUSD, GUSD, GWh, MWh, TWh, kWh, Btu, MBtu, TBtu, g, t, kt, Mt, Gt
# %% Accounting functions
def residual_value(additions, plant_accounting_life, technology):
"""Return the residual value of the generation capacity at model end year.
A Series equal 0 for all years except at the end.
"""
lifetime = plant_accounting_life[technology]
idx = additions.index
n = len(idx)
remaining_fraction = pd.Series(0, index=idx)
for i in range(min(lifetime, n)):
# On average, plant opens middle of the year
remaining_fraction.iloc[n - i - 1] = 1 - (i + 0.5) / lifetime
result = pd.Series(0, index=YEARS, name=technology)
result[2050] = (remaining_fraction * additions[technology]).sum()
return result
# %%
class Run():
"""A run of the model.
Computes LCOE and CO2 emissions based on:
plan contains the policy control variable, a power generation plan
parameter describes the technical and economic environment
The model run is an immutable object, all the (linear) algebra is done in the initializer.
"""
def __init__(self, plan, parameter):
self.plan = plan
self.parameter = parameter
def pv(variable):
"""Present value of a variable summed across technologies."""
return present_value(variable, parameter.DISCOUNT_RATE).sum()
self.total_production = pv(plan.production[SOURCES])
self.investment = (plan.additions * MW
* parameter.construction_cost * USD / kW
/ MUSD)
self.total_investment = pv(self.investment)
self.salvage_value = pd.concat([residual_value(plan.additions,
parameter.plant_accounting_life,
fuel)
for fuel in SOURCES],
axis=1)
self.total_salvage_value = pv(self.salvage_value)
self.fixed_om_cost = (plan.capacities * MW *
parameter.fixed_operating_cost * USD / kW
/ MUSD)
self.total_fixed_om_cost = pv(self.fixed_om_cost)
self.variable_OM_cost = (plan.production * GWh
* parameter.variable_operating_cost * USD / MWh
/ MUSD)
self.total_variable_OM_cost = pv(self.variable_OM_cost)
self.heat_used = (plan.production * GWh
* parameter.heat_rate * Btu / kWh
/ TBtu)
self.fuel_cost = (self.heat_used * TBtu
* parameter.heat_price * USD / MBtu
/ MUSD)
self.total_fuel_cost = pv(self.fuel_cost)
self.total_cost = (self.total_investment - self.total_salvage_value
+ self.total_fixed_om_cost + self.total_variable_OM_cost
+ self.total_fuel_cost)
self.lcoe = self.total_cost / self.total_production
self.emissions = (plan.production * GWh
* parameter.EMISSION_FACTOR * g / kWh
/ kt)
self.emissions["Total"] = self.emissions.sum(axis=1)
self.total_emissions = self.emissions["Total"].sum() * kt / Gt
self.capture = (plan.production * GWh
* parameter.capture_factor * g / kWh
/ kt)
self.capture["Total"] = self.capture.sum(axis=1) * kt / Mt
self.total_capture = self.capture["Total"].sum() * Mt / Gt
self.external_cost = (self.emissions["Total"] * kt
* parameter.CARBON_PRICE * USD / t
/ MUSD)
self.total_external_cost = pv(self.external_cost)
self.signature = '#' + plan.digest + "-" + parameter.digest
def __str__(self):
return self.signature
def summarize(self):
print(self.summary())
def summary(self):
"""Key results."""
return (str(self) + " - Summary\n\n"
+ str(self.plan) + '\n'
+ str(self.parameter) + '\n'
+ "System LCOE: {:.2f} US cent / kWh\n".format(100 * self.lcoe)
+ "CO2 emissions {:.2f} Gt CO2eq\n".format(self.total_emissions)
+ "CO2 capture {:.2f} Gt CO2\n".format(self.total_capture))
def print_total(self):
"""Print the run economic results and the run GHG emissions by technology."""
print(self, " - Totals")
print()
print(self.total())
print()
print("GHG emissions over ", START_YEAR, "-", END_YEAR, "by source (Mt CO2eq)")
print()
print(self.emission_sum())
def total(self):
"""Dataframe tabulating the key results."""
def bnUSD(cost):
"""Format as integer number of billion USD."""
return [round(cost * MUSD / GUSD), "bn USD"]
d = pd.DataFrame()
d["Power produced"] = [(self.total_production * GWh / TWh).round(), "Twh"]
d["System LCOE"] = [round(self.lcoe * (MUSD / GWh) / (USD / MWh), 1), "USD/MWh"]
d["Total cost"] = bnUSD(self.total_cost)
d[" Construction"] = bnUSD(self.total_investment)
d[" Fuel cost"] = bnUSD(self.total_fuel_cost)
d[" O&M"] = bnUSD(self.total_fixed_om_cost + self.total_variable_OM_cost)
d[" Salvage value"] = bnUSD(-self.total_salvage_value)
d["CO2 emissions"] = [round(self.total_emissions, 1), "GtCO2eq"]
d["CO2 capture"] = [round(self.total_capture, 1), "GtCO2"]
d["CO2 cost"] = bnUSD(self.total_external_cost)
d["Cost with CO2"] = bnUSD(self.total_cost + self.total_external_cost)
d = d.transpose()
d.columns = [str(self), 'Unit']
return d
def carbon_intensity(self):
"""Dataframe tabulating the CO2 intensity of electricity (g/kWh)."""
key_years = [START_YEAR, 2030, 2050]
p = self.plan.production.loc[key_years, 'Total']
p.name = "GWh"
e = self.emissions.loc[key_years, 'Total']
e.name = "ktCO2eq"
intensity = (e * kt) / (p * GWh) / (g / kWh)
intensity.name = str(self)
return intensity.round()
def carbon_captured(self):
"""Dataframe tabulating the quantities of CO2 captured."""
key_years = [2025, 2030, 2035, 2040, 2050]
captured = self.capture.loc[key_years, "Total"]
captured.name = str(self)
return captured.round()
def emission_sum(self):
"""Dataframe tabulating the total emissions by technology during the model run."""
s = self.emissions[SOURCES].sum() * kt / Mt
s = s.round()
s.name = str(self)
return s
def string(self):
"""Detail results tables."""
return (str(self) + " - Detailed results tables"
+ "\n\n"
+ "Construction costs (M$)\n"
+ str(self.investment.loc[START_YEAR:, PLANT_TYPE].round())
+ "\n\n"
+ "Fixed operating costs (M$)\n"
+ str(self.fixed_om_cost.loc[START_YEAR:, PLANT_TYPE].round())
+ "\n\n"
+ "Variable operating costs (M$)\n"
+ str(self.variable_OM_cost.loc[START_YEAR:, SOURCES].round())
+ "\n\n"
+ "Heat used (TBtu)\n"
+ str(self.heat_used.loc[START_YEAR:, SOURCES].round())
+ "\n\n"
+ "Fuel costs (M$)\n"
+ str(self.fuel_cost.loc[START_YEAR:, SOURCES].round())
+ "\n\n"
+ "GHG emissions (ktCO2eq including CO2, CH4 and N20)\n"
+ str(self.emissions.loc[START_YEAR:, SOURCES + ["Total"]].round())
+ "\n\n"
+ "CO2 capture (kt CO2)\n"
+ str(self.capture.loc[START_YEAR:,
["CoalCCS", "GasCCS", "BioCCS", "Total"]].round())
)
class RunPair():
"""Compare two power development plans, for the same technico-economic parameters."""
def __init__(self, bau, alt, parameter):
self.BAU = Run(bau, parameter)
self.ALT = Run(alt, parameter)
def __str__(self):
name = str(self.BAU.parameter) + '\n\n'
name += "BAU = " + str(self.BAU.plan) + '\n'
name += "ALT = " + str(self.ALT.plan)
return name
def total(self, headers):
"""Dataframe comparing the key results of the two runs."""
units = self.BAU.total().iloc[:, 1]
total_BAU = self.BAU.total().iloc[:, 0] # Only the values
total_ALT = self.ALT.total().iloc[:, 0]
total_diff = total_ALT - total_BAU
d = pd.concat([total_BAU, total_ALT, total_diff, units], axis=1)
d.columns = headers + ['Units']
return d
def emission_sum(self, headers):
"""Dataframe comparing total intertemporal CO2 emissions of the two runs, by technology."""
es_BAU = self.BAU.emission_sum()
es_ALT = self.ALT.emission_sum()
es_diff = es_ALT - es_BAU
d = pd.concat([es_BAU, es_ALT, es_diff], axis=1)
d.columns = headers
return d
def carbon_intensity(self, headers):
"""Dataframe comparing the CO2 intensity of electricity in the two runs, for key years."""
ci_bau = self.BAU.carbon_intensity()
ci_alt = self.ALT.carbon_intensity()
ci_diff = ci_alt - ci_bau
table = pd.concat([ci_bau, ci_alt, ci_diff], axis=1)
table.columns = headers
return table
def carbon_captured(self, headers):
"""Dataframe comparing the carbon captured in the two runs, for key years."""
cc_bau = self.BAU.carbon_captured()
cc_alt = self.ALT.carbon_captured()
cc_diff = cc_alt - cc_bau
table = pd.concat([cc_bau, cc_alt, cc_diff], axis=1)
table.columns = headers
return table
def carbon_value(self, headers):
difference = self.total(headers)['difference']
return - difference['Total cost'] / difference['CO2 emissions']
def summary(self, headers):
return ("*******************\n\n"
+ str(self) + '\n\n'
+ 'Present value cost of avoided emissions: '
+ str(round(self.carbon_value(headers), 1)) + ' USD/tCO2eq\n\n'
+ str(self.total(headers)) + '\n\n'
+ 'Emissions by source (ktCO2eq)\n'
+ str(self.emission_sum(headers)) + '\n\n'
+ 'Average Carbon Intensity (g/kWh)\n'
+ str(self.carbon_intensity(headers)) + '\n\n'
+ 'Carbon Captured (Mt)\n'
+ str(self.carbon_captured(headers)) + '\n\n')