-
Notifications
You must be signed in to change notification settings - Fork 1
/
pes_plot.py
559 lines (450 loc) · 18.8 KB
/
pes_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import cantera as ct
import os
import sys
sys.path.append(f'{os.getcwd()}/tools/PyEnergyDiagram')
print(os.getcwd())
from energydiagram import ED
import logging
############################################
#
# Plots a potential energy surface
# (enthalpy vs rxn coordinate) for a
# given cti file mechanism
#
# uses https://github.com/giacomomarchioro/PyEnergyDiagrams
#
############################################
class pes_reaction_combine():
"""
feed in a cantera reaction, get an object out of it that we can use for making a chart
arguments:
reaction - a ct reaction object
phase_gas - gas phase in mechanism (for looking up species)
phase_surf - solid phase in mechanism (for looking up species)
reverse - bool, True if we swap reactants and products, and change Ea to Ea from products
properties:
reactants - dict, reactant string as key (e.g. "CO2+H2O"), combined Hf as value
products - dict, reactant string as key (e.g. "CO2+H2O"), combined Hf as value
barrier - float, Ea for reaction as value
equation - string, cantera reaction equation
links - list of ids for connecting the diagram
[reactant id, Ea id, product id]
positions - int, list of positions for reactants, reactions, and products.
[reactant pos, Reaction Ea pos, product pos]
"""
def __init__(
self,
reaction,
phase_gas,
phase_surf,
reverse=False,
):
self.equation = reaction.equation
self.reactants = {}
self.products = {}
self.links = [-1,-1,-1]
self.positions = [-1, -1, -1]
if reverse:
reactants_obj = reaction.products
products_obj = reaction.reactants
# flip reaction equation, e.g. A <=> B is now B <=> A
str_orig = reaction.equation
split_list = str_orig.split("<=>")
str1 = split_list[1] + " <=> " + split_list[0]
str1 = str1.strip()
self.equation = str1
print("flipped equation: ", str_orig, self.equation)
else:
reactants_obj = reaction.reactants
products_obj = reaction.products
self.equation = reaction.equation
# lookup each reactant, put in dictionary as
# {species_name : enthalpy at phase temp (in Ev) * stoich coeff}
total_reac_enth = 0
reac_str = ""
for i in reactants_obj:
if i in phase_gas.species_names:
phase = phase_gas
elif i in phase_surf.species_names:
phase = phase_surf
else:
logging.error(f"species {i} cannot be found")
reac_str += f"{i} "
total_reac_enth += reactants_obj[i] * (phase.species(i).thermo.h(phase.T)/1000**2)/96
reac_str = reac_str.strip()
self.reactants[reac_str] = total_reac_enth
self.reactants[reac_str] = round(self.reactants[reac_str],3)
total_prod_enth = 0
prod_str = ""
for i in products_obj:
if i in phase_gas.species_names:
phase = phase_gas
elif i in phase_surf.species_names:
phase = phase_surf
else:
logging.error(f"species {i} cannot be found")
prod_str += f"{i} "
total_prod_enth += products_obj[i] * (phase.species(i).thermo.h(phase.T)/1000**2)/96
prod_str = prod_str.strip()
self.products[prod_str] = total_prod_enth
self.products[prod_str] = round(self.products[prod_str],3)
# reaction activation energy. need to add to
# reactant enthalpy to get barrier
# if reversed, need to get barrier from the products
if reverse:
self.barrier = (reaction.rate.activation_energy/1000**2)/96 + total_prod_enth
else:
self.barrier = (reaction.rate.activation_energy/1000**2)/96 + total_reac_enth
self.barrier = round(self.barrier, 3)
class pes_reaction():
"""
feed in a cantera reaction, get an object out of it that we can use for making a chart
arguments:
reaction - a ct reaction object
phase_gas - gas phase in mechanism (for looking up species)
phase_surf - solid phase in mechanism (for looking up species)
reverse - bool, True if we swap reactants and products, and change Ea to Ea from products
properties:
reactants - dict, species names as keys, Hf as value
products - dict, species names as keys, Hf as value
barrier - float, Ea for reaction as value
equation - string, cantera reaction equation
links - list of ids for connecting the diagram
[[reactant ids], Ea id, [product ids]]
"""
def __init__(
self,
reaction,
phase_gas,
phase_surf,
):
self.equation = reaction.equation
self.reactants = {}
self.products = {}
# lookup each reactant, put in dictionary as
# {species_name : enthalpy at phase temp (in Ev) * stoich coeff}
total_reac_enth = 0
for i in reaction.reactants:
if i in phase_gas.species_names:
phase = phase_gas
elif i in phase_surf.species_names:
phase = phase_surf
else:
logging.error(f"species {i} cannot be found")
self.reactants[i] = reaction.reactants[i] * (phase.species(i).thermo.h(phase.T)/1000**2)/96
total_reac_enth += self.reactants[i]
self.reactants[i] = round(self.reactants[i],3)
for i in reaction.products:
if i in phase_gas.species_names:
phase = phase_gas
elif i in phase_surf.species_names:
phase = phase_surf
else:
logging.error(f"species {i} cannot be found")
self.products[i] = reaction.products[i] * (phase.species(i).thermo.h(phase.T)/1000**2)/96
self.products[i] = round(self.products[i],3)
# reaction activation energy. need to add to
# reactant enthalpy to get barrier
self.barrier = (reaction.rate.activation_energy/1000**2)/96 + total_reac_enth
self.barrier = round(self.barrier, 3)
class pes_plot():
"""
Plots a potential energy surface
(enthalpy vs rxn coordinate) for a
given cti file mechanism
"""
def __init__(
self,
yaml_file,
temp=528,
press=75,
):
"""
initialize model
yaml_file = cti or yaml file for mechanism
temp = temperature (K)
press = pressure (atm)
"""
# set initial temps & pressures
self.temp = temp # kelvin
self.pressure = press * ct.one_atm # Pascals
# create thermo phases
self.yaml_file = yaml_file
self.gas = ct.Solution(yaml_file, "gas")
self.surf = ct.Interface(yaml_file,"surface1", [self.gas])
# initialize T and P for each phase
self.gas.TP = self.temp, self.pressure
self.surf.TP = self.temp, self.pressure
# create reaction diagram object
self.diagram = ED()
# initialize the reaction object dictionary
self.pes_rxn_dict = {}
def get_h_ev(self, species, temp):
"""
gets species enthalpy in eV.
species is a cantera Species object
"""
h_eV = (species.thermo.h(temp)/1000**2)/96
print(f'{species.name} enthalpy = {h_eV} eV')
return h_eV
def get_ea_ev(self, reaction):
"""
gets reaction Ea in eV.
reaction is a cantera Reaction object
"""
Ea_eV = (reaction.rate.activation_energy/1000**2)/96
print(f'{reaction.equation} enthalpy = {Ea_eV} eV')
return Ea_eV
def find_reactions(self, species, temp):
"""
find all reactions that involve a certain species or set of species.
species is a species object
pes_rxn_dict is a dictionary, reaction equation is the key, PES reaction object is the value
"""
pes_rxn_dict = {}
species_names = [i.name for i in species]
print(species_names)
# get combined H for species as the starting point for Ea
for i,j in enumerate(self.gas.reactions()):
if all(x in j.reactants.keys() for x in species_names):
pes_obj = pes_reaction_combine(j, self.gas, self.surf)
pes_rxn_dict[pes_obj.equation] = pes_obj
# if we want to show the reverse reaction, specify that
# in call to pes_reaction_combine
if all(x in j.products.keys() for x in species_names):
pes_obj = pes_reaction_combine(j, self.gas, self.surf, reverse=True)
pes_rxn_dict[pes_obj.equation] = pes_obj
for i,j in enumerate(self.surf.reactions()):
if all(x in j.reactants.keys() for x in species_names):
pes_obj = pes_reaction_combine(j, self.gas, self.surf)
pes_rxn_dict[pes_obj.equation] = pes_obj
# if we want to show the reverse reaction, specify that
# in call to pes_reaction_combine
if all(x in j.products.keys() for x in species_names):
pes_obj = pes_reaction_combine(j, self.gas, self.surf, reverse=True)
pes_rxn_dict[pes_obj.equation] = pes_obj
# if no reactions are found, throw an error
if len(pes_rxn_dict)==0:
raise Exception(f"no reactions found with reactants {species}")
return pes_rxn_dict
def plot_pes_diagram(
self,
species,
width=20,
height=40,
offset=None,
dimension=None,
space=None,
combined=True,
):
"""
plots a potential energy surface given an input for species.
the "species" are the starting point for the mechanism. each successive
run will take an input of the species and get all reactions for that pair.
inputs:
species - (str or [strs]) matching starting reactant species name in cantera mechanism.
width - (float) matplotlib plot width in inches
height - (float) matplotlib plot height in inches
offset - (float) vertical distance that energy level and upper/lower labels are spaced
dimension - (float) width of platform used for energy level
space - (float) distance between bars for energy levels
combined - (bool) if true combine all reactants to a single energy level. do the same for products.
"""
# get a list of all reactions containing the two species identified
species_obj = []
for i in species:
if i in self.gas.species_names:
species_obj.append(self.gas.species(i))
elif i in self.surf.species_names:
species_obj.append(self.surf.species(i))
else:
print(f'species {i} not found!')
rxns = self.find_reactions(species_obj, self.temp)
self.pes_rxn_dict.update(rxns)
link_num = 0
for i,j in self.pes_rxn_dict.items():
# generate a pes plot for each pes_reaction reactant
for k,l in j.reactants.items():
reac = k
H_r = l
# make a new energy level
self.diagram.add_level(H_r, k, 0)
j.positions[0] = 0
j.links[0] = link_num
link_num+=1
for i,j in self.pes_rxn_dict.items():
# plot rxn Ea. for it to show up between species, should be here
rxn_eq = j.equation
rxn_Ea = j.barrier
# make a new energy level
self.diagram.add_level(rxn_Ea, rxn_eq, 1)
j.positions[1] = 1
j.links[1] = link_num
link_num+=1
for i,j in self.pes_rxn_dict.items():
# generate a pes plot for each pes_reaction product
for k,l in j.products.items():
prod = k
H_p = l
# make a new energy level
self.diagram.add_level(H_p, prod,2)
j.positions[2] = 2
# add link id for line drawing
j.links[2] = link_num
link_num+=1
for i in self.pes_rxn_dict.values():
# get connections between each reac - Ea and each Ea - product
self.diagram.add_link(i.links[0],i.links[1])
self.diagram.add_link(i.links[1],i.links[2])
# optional arguements
if space:
self.diagram.space = space
if offset:
self.diagram.offset = offset
if dimension:
self.diagram.dimension = dimension
self.diagram.plot(show_IDs=True, ylabel="Energy / $eV$", width=width, height=height)
def add_next_reaction(
self,
species,
width,
height,
offset=None,
dimension=None,
space=None,
):
"""
adds the next reaction to the plot.
species is the product species selected for the next step
"""
# get a list of all reactions containing the species identified
species_obj = []
for i in species:
if i in self.gas.species_names:
species_obj.append(self.gas.species(i))
elif i in self.surf.species_names:
species_obj.append(self.surf.species(i))
rxns = self.find_reactions(species_obj, self.temp)
self.pes_rxn_dict.update(rxns)
# ED.position can be assigned to an integer (1,2,3,4, etc)
# so we do not need to use "l"
# get starting position (whatever the last energy level position was)
starting_pos = max(self.diagram.positions)
link_num = len(self.diagram.data)
for i,j in rxns.items():
# generate a pes plot for each pes_reaction
for k,l in self.pes_rxn_dict[i].reactants.items():
reac = k
H_r = l
# make a new energy level
self.diagram.add_level(H_r, k, starting_pos)
self.pes_rxn_dict[i].positions[0] = starting_pos
self.pes_rxn_dict[i].links[0] = link_num
link_num+=1
for i,j in rxns.items():
# plot rxn Ea. for it to show up between species, should be here
rxn_eq = self.pes_rxn_dict[i].equation
rxn_Ea = self.pes_rxn_dict[i].barrier
# make a new energy level
self.diagram.add_level(rxn_Ea, rxn_eq, starting_pos + 1)
self.pes_rxn_dict[i].positions[1] = starting_pos + 1
self.pes_rxn_dict[i].links[1] = link_num
link_num+=1
for i,j in rxns.items():
# generate a pes plot for each pes_reaction
for k,l in self.pes_rxn_dict[i].products.items():
prod = k
H_p = l
# make a new energy level
self.diagram.add_level(H_p, prod,starting_pos + 2)
self.pes_rxn_dict[i].positions[2] = starting_pos + 2
# add link id for line drawing
self.pes_rxn_dict[i].links[2] = link_num
link_num+=1
for i,j in rxns.items():
# get connections between each reac - Ea and each Ea-product
self.diagram.add_link(self.pes_rxn_dict[i].links[0],self.pes_rxn_dict[i].links[1])
self.diagram.add_link(self.pes_rxn_dict[i].links[1],self.pes_rxn_dict[i].links[2])
# optional arguements
if space:
self.diagram.space = space
if offset:
self.diagram.offset = offset
if dimension:
self.diagram.dimension = dimension
self.diagram.plot(show_IDs=True, ylabel="Energy / $eV$", width=width, height=height)
def _redraw(
self,
width=20,
height=40,
offset=None,
dimension=None,
space=None,
):
""" redraw after a trim"""
# create new reaction diagram object
# (is there a more efficient way to erase the old one?)
self.diagram = ED()
link_num = 0
for i,j in self.pes_rxn_dict.items():
# generate a pes plot for each pes_reaction reactant
for k,l in j.reactants.items():
reac = k
H_r = l
# make a new energy level
self.diagram.add_level(H_r, k, j.positions[0])
rxn_eq = j.equation
rxn_Ea = j.barrier
# make a new energy level
self.diagram.add_level(rxn_Ea, rxn_eq, j.positions[1])
# generate a pes plot for each pes_reaction product
for k,l in j.products.items():
prod = k
H_p = l
# make a new energy level
self.diagram.add_level(H_p, prod, j.positions[2])
self.diagram.create_data()
# go through reaction dictionary and match reactant and product entries
# match only adjacent ones
for i,j in self.pes_rxn_dict.items():
for m,n in enumerate(self.diagram.data):
# there has to be a better way to do this ".keys())[0]"" nonsense
if str(list(j.reactants.keys())[0]) == n[2] and j.positions[0] == n[1]:
j.links[0] = m
position = n[1]
if j.equation == n[2] and j.positions[1] == n[1]:
j.links[1] = m
if str(list(j.products.keys())[0]) == n[2] and j.positions[2] == n[1]:
j.links[2] = m
# need to figure out how to add links
for i in self.pes_rxn_dict.values():
# get connections between each reac - Ea and each Ea - product
self.diagram.add_link(i.links[0],i.links[1])
self.diagram.add_link(i.links[1],i.links[2])
# optional arguements
if space:
self.diagram.space = space
if offset:
self.diagram.offset = offset
if dimension:
self.diagram.dimension = dimension
self.diagram.plot(show_IDs=True, ylabel="Energy / $eV$", width=width, height=height)
def trim(
self,
reac,
width=20,
height=40,
offset=None,
dimension=None,
space=None,
):
"""
trims the specified reactions and their species from plot.
updates the pes_rxn_object to remove reactions we do not want.
runs through diagram.data and pes_rxn_object to update all of the links
reac is the reaction to be trimmed (will remove reactants + products)
"""
for key in list(self.pes_rxn_dict.keys()):
if reac == key:
del self.pes_rxn_dict[key]
self._redraw()