-
Notifications
You must be signed in to change notification settings - Fork 30
/
import_kinetics.py
367 lines (222 loc) · 13.3 KB
/
import_kinetics.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
import torch
def set_nasa(self):
self.nasa_low = torch.zeros([self.n_species, 7]).to(self.device)
self.nasa_high = torch.zeros([self.n_species, 7]).to(self.device)
for i in range(self.n_species):
self.nasa_low[i, :] = torch.Tensor(self.model_yaml['species'][i]['thermo']['data'][0])
self.nasa_high[i, :] = torch.Tensor(self.model_yaml['species'][i]['thermo']['data'][1])
def set_transport(self,
species_viscosities_poly,
thermal_conductivity_poly,
binary_diff_coeffs_poly):
# Transport Properties
self.species_viscosities_poly = torch.from_numpy(species_viscosities_poly).to(self.device)
self.thermal_conductivity_poly = torch.from_numpy(thermal_conductivity_poly).to(self.device)
self.binary_diff_coeffs_poly = torch.from_numpy(binary_diff_coeffs_poly).to(self.device)
self.poly_order = self.species_viscosities_poly.shape[0]
def set_reactions(self):
self.reaction = [[None]] * self.n_reactions
self.n_rate_constants = [[None]] * self.n_reactions
self.reactant_stoich_coeffs = torch.Tensor(self.gas.reactant_stoich_coeffs()).to(self.device)
self.reactant_orders = torch.Tensor(self.gas.reactant_stoich_coeffs()).to(self.device)
self.product_stoich_coeffs = torch.Tensor(self.gas.product_stoich_coeffs()).to(self.device)
self.net_stoich_coeffs = self.product_stoich_coeffs - self.reactant_stoich_coeffs
self.efficiencies_coeffs = torch.ones([self.n_species, self.n_reactions]).to(self.device)
self.Arrhenius_coeffs = torch.zeros([self.n_reactions, 3]).to(self.device)
self.is_reversible = torch.ones([self.n_reactions]).to(self.device)
# reaction type 2 will be set as 1
self.is_three_body = torch.zeros([self.n_reactions]).to(self.device)
self.is_falloff = torch.zeros([self.n_reactions]).to(self.device)
self.is_Troe_falloff = torch.zeros([self.n_reactions]).to(self.device)
self.list_reaction_type1 = []
self.list_reaction_type2 = []
self.list_reaction_type4 = []
self.list_reaction_type4_Troe = []
# the id of Troe in type 4
self.list_id_Troe_in_type4 = []
self.list_reaction_type5 = []
# count the num in type 4
count_type4 = -1
for i in range(self.n_reactions):
# Type 1: regular reaction, 2: three-body, 4:fall-off, 5:pressure-dependent-Arrhenius
yaml_reaction = self.model_yaml['reactions'][i]
self.reaction[i] = {'equation': self.gas.reaction_equation(i)}
self.reaction[i]['reactants'] = self.gas.reactants(i)
self.reaction[i]['products'] = self.gas.products(i)
self.reaction[i]['reaction_type'] = self.gas.reaction_type(i)
if self.gas.reaction_type(i) in [1]:
self.list_reaction_type1.append(i)
if self.gas.reaction_type(i) in [2]:
self.list_reaction_type2.append(i)
if self.gas.reaction_type(i) in [4]:
# id of the current item in the list
count_type4 = count_type4 + 1
self.list_reaction_type4.append(i)
if self.gas.reaction_type(i) in [5]:
self.list_reaction_type5.append(i)
if self.gas.is_reversible(i) is False:
self.is_reversible[i].fill_(0)
if self.gas.reaction_type(i) in [1, 2]:
self.reaction[i]['A'] = torch.Tensor(
[yaml_reaction['rate-constant']['A']]).to(self.device)
self.reaction[i]['b'] = torch.Tensor(
[yaml_reaction['rate-constant']['b']]).to(self.device)
if type(yaml_reaction['rate-constant']['Ea']) is str:
Ea = list(map(eval, [yaml_reaction['rate-constant']['Ea'].split(' ')[0]]))
else:
Ea = [yaml_reaction['rate-constant']['Ea']]
self.reaction[i]['Ea'] = torch.Tensor(Ea).to(self.device)
if self.gas.reaction_type(i) in [2]:
self.is_three_body[i] = 1
if 'efficiencies' in yaml_reaction:
self.reaction[i]['efficiencies'] = yaml_reaction['efficiencies']
for key, value in self.reaction[i]['efficiencies'].items():
self.efficiencies_coeffs[self.gas.species_index(key), i] = value
if self.gas.reaction_type(i) in [4]:
self.is_falloff[i] = 1
if 'efficiencies' in yaml_reaction:
self.reaction[i]['efficiencies'] = yaml_reaction['efficiencies']
for key, value in self.reaction[i]['efficiencies'].items():
self.efficiencies_coeffs[self.gas.species_index(key), i] = value
high_p = yaml_reaction['high-P-rate-constant']
low_p = yaml_reaction['low-P-rate-constant']
self.reaction[i]['A'] = torch.Tensor([high_p['A']]).to(self.device)
self.reaction[i]['b'] = torch.Tensor([high_p['b']]).to(self.device)
if type(high_p['Ea']) is str:
Ea = list(map(eval, [high_p['Ea'].split(' ')[0]]))
else:
Ea = [high_p['Ea']]
self.reaction[i]['Ea'] = torch.Tensor(Ea).to(self.device)
self.reaction[i]['A_0'] = torch.Tensor([low_p['A']]).to(self.device)
self.reaction[i]['b_0'] = torch.Tensor([low_p['b']]).to(self.device)
if type(low_p['Ea']) is str:
Ea = list(map(eval, [low_p['Ea'].split(' ')[0]]))
else:
Ea = [low_p['Ea']]
self.reaction[i]['Ea_0'] = torch.Tensor(Ea).to(self.device)
if 'Troe' in yaml_reaction:
self.is_Troe_falloff[i] = 1
self.list_reaction_type4_Troe.append(i)
self.list_id_Troe_in_type4.append(count_type4)
Troe = yaml_reaction['Troe']
if 'T2' in Troe:
self.reaction[i]['Troe'] = {'A': torch.Tensor([Troe['A']]).to(self.device),
'T1': torch.Tensor([Troe['T1']]).to(self.device),
'T2': torch.Tensor([Troe['T2']]).to(self.device),
'T3': torch.Tensor([Troe['T3']]).to(self.device)
}
else:
self.reaction[i]['Troe'] = {'A': torch.Tensor([Troe['A']]).to(self.device),
'T1': torch.Tensor([Troe['T1']]).to(self.device),
'T2': torch.Tensor([1e30]).to(self.device),
'T3': torch.Tensor([Troe['T3']]).to(self.device)
}
if self.gas.reaction_type(i) in [5]:
self.n_rate_constants[i] = len(self.gas.reaction(i).rates)
self.reaction[i]['p_dep'] = {}
self.reaction[i]['p_dep']['A'] = [[None]] * self.n_rate_constants[i]
self.reaction[i]['P'] = [[None]] * self.n_rate_constants[i]
self.reaction[i]['b'] = [[None]] * self.n_rate_constants[i]
self.reaction[i]['Ea'] = [[None]] * self.n_rate_constants[i]
for j in range(self.n_rate_constants[i]):
pdep_arrhenius = yaml_reaction['rate-constants'][j]
if type(pdep_arrhenius['P']) is str:
P = list(map(eval, [pdep_arrhenius['P'].split(' ')[0]]))
self.reaction[i]['P'][j] = torch.Tensor(P).to(self.device)
if ([pdep_arrhenius['P'].split(' ')[1]] == ['atm'] or
[pdep_arrhenius['P'].split(' ')[1]] == ['ATM']):
self.reaction[i]['P'][j] = 101325 * self.reaction[i]['P'][j]
if [pdep_arrhenius['P'].split(' ')[1]] == ['MPa']:
self.reaction[i]['P'][j] = 1000000 * self.reaction[i]['P'][j]
else:
P = [pdep_arrhenius['P']]
self.reaction[i]['P'][j] = torch.Tensor(P).to(self.device)
if type(pdep_arrhenius['A']) is str:
A = list(map(eval, [pdep_arrhenius['A'].split(' ')[0]]))
else:
A = [pdep_arrhenius['A']]
self.reaction[i]['p_dep']['A'][j] = torch.Tensor(A).to(self.device)
if type(pdep_arrhenius['b']) is str:
b = list(map(eval, [pdep_arrhenius['b'].split(' ')[0]]))
else:
b = [pdep_arrhenius['b']]
self.reaction[i]['b'][j] = torch.Tensor(b).to(self.device)
if type(pdep_arrhenius['Ea']) is str:
Ea = list(map(eval, [pdep_arrhenius['Ea'].split(' ')[0]]))
else:
Ea = [pdep_arrhenius['Ea']]
self.reaction[i]['Ea'][j] = torch.Tensor(Ea).to(self.device)
if type(pdep_arrhenius['A']) is str:
A = list(map(eval, [pdep_arrhenius['A'].split(' ')[0]]))
else:
A = [pdep_arrhenius['A']]
self.reaction[i]['A'] = torch.Tensor(A).to(self.device)
if 'orders' in yaml_reaction:
for key, value in yaml_reaction['orders'].items():
self.reactant_orders[self.gas.species_index(key), i] = value
if 'units' in self.model_yaml:
if (self.model_yaml['units']['length'] == 'cm' and
self.model_yaml['units']['quantity'] == 'mol'):
if self.gas.reaction_type(i) in [1, 2, 4]:
self.reaction[i]['A'] *= 1e-3 ** (
self.reactant_stoich_coeffs[:, i].sum().item() - 1)
if self.gas.reaction_type(i) in [2]:
self.reaction[i]['A'] *= 1e-3
if self.gas.reaction_type(i) in [4]:
self.reaction[i]['A_0'] *= 1e-3
self.reaction[i]['A_0'] *= 1e-3 ** (
self.reactant_stoich_coeffs[:, i].sum().item() - 1)
if self.gas.reaction_type(i) in [5]:
for j in range(self.n_rate_constants[i]):
self.reaction[i]['p_dep']['A'][j] *= 1e-3 ** (
self.reactant_stoich_coeffs[:, i].sum().item() - 1)
if self.gas.reaction_type(i) in [1, 2, 4]:
self.Arrhenius_coeffs[i, 0] = self.reaction[i]['A']
self.Arrhenius_coeffs[i, 1] = self.reaction[i]['b']
self.Arrhenius_coeffs[i, 2] = self.reaction[i]['Ea']
if self.gas.reaction_type(i) in [5]:
self.Arrhenius_coeffs[i, 0] = self.reaction[i]['A']
self.Arrhenius_coeffs[i, 1] = self.reaction[i]['b'][0]
self.Arrhenius_coeffs[i, 2] = self.reaction[i]['Ea'][0]
self.Arrhenius_A = self.Arrhenius_coeffs[:, 0]
self.Arrhenius_b = self.Arrhenius_coeffs[:, 1]
self.Arrhenius_Ea = self.Arrhenius_coeffs[:, 2]
if self.vectorize is True:
# for falloff and Troe
self.length_type4 = len(self.list_reaction_type4)
self.Arrhenius_A0 = torch.zeros([self.length_type4]).to(self.device)
self.Arrhenius_b0 = torch.zeros([self.length_type4]).to(self.device)
self.Arrhenius_Ea0 = torch.zeros([self.length_type4]).to(self.device)
self.Arrhenius_Ainf = torch.zeros([self.length_type4]).to(self.device)
self.Arrhenius_binf = torch.zeros([self.length_type4]).to(self.device)
self.Arrhenius_Eainf = torch.zeros([self.length_type4]).to(self.device)
self.efficiencies_coeffs_type4 = torch.ones(
[self.n_species, self.length_type4]).to(self.device)
self.length_type4_Troe = len(self.list_reaction_type4_Troe)
self.Troe_A = torch.zeros([self.length_type4_Troe]).to(self.device)
self.Troe_T1 = torch.zeros([self.length_type4_Troe]).to(self.device)
self.Troe_T2 = torch.zeros([self.length_type4_Troe]).to(self.device)
self.Troe_T3 = torch.zeros([self.length_type4_Troe]).to(self.device)
# for matrix size transfer
self.mat_transfer_type4 = torch.zeros([self.length_type4, self.n_reactions]).to(self.device)
self.mat_transfer_type4_Troe = torch.zeros([self.length_type4_Troe, self.n_reactions]).to(self.device)
self.mat_transfer_type4_to_Troe = torch.zeros(self.length_type4, self.length_type4_Troe).to(self.device)
for i in range(self.length_type4):
index = self.list_reaction_type4[i]
self.Arrhenius_A0[i] = self.reaction[index]['A_0']
self.Arrhenius_b0[i] = self.reaction[index]['b_0']
self.Arrhenius_Ea0[i] = self.reaction[index]['Ea_0']
self.Arrhenius_Ainf[i] = self.reaction[index]['A']
self.Arrhenius_binf[i] = self.reaction[index]['b']
self.Arrhenius_Eainf[i] = self.reaction[index]['Ea']
self.efficiencies_coeffs_type4[:, i] = self.efficiencies_coeffs[:, index]
self.mat_transfer_type4[i, index] = 1
for i in range(self.length_type4_Troe):
index = self.list_reaction_type4_Troe[i]
self.Troe_A[i] = self.reaction[index]['Troe']['A']
self.Troe_T1[i] = self.reaction[index]['Troe']['T1']
self.Troe_T2[i] = self.reaction[index]['Troe']['T2']
self.Troe_T3[i] = self.reaction[index]['Troe']['T3']
self.mat_transfer_type4_Troe[i, index] = 1
index_in_type4 = self.list_id_Troe_in_type4[i]
self.mat_transfer_type4_to_Troe[index_in_type4, i] = 1