-
Notifications
You must be signed in to change notification settings - Fork 24
/
Reaction.inl
338 lines (322 loc) · 10.7 KB
/
Reaction.inl
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
// Copyright (c) "2019, by Stanford University
// Developer: Mario Di Renzo
// Affiliation: Center for Turbulence Research, Stanford University
// URL: https://ctr.stanford.edu
// Citation: Di Renzo, M., Lin, F., and Urzay, J. (2020).
// HTR solver: An open-source exascale-oriented task-based
// multi-GPU high-order code for hypersonic aerothermodynamics.
// Computer Physics Communications 255, 107262"
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//---------------------------------
// Arrhenius coefficients
//---------------------------------
__CUDA_HD__
inline double ArrheniusCoeff::CompRateCoeff(const double T) const {
double Kf = A;
if ( n != 0.0 )
Kf *= pow(T, n);
if ( fabs(EovR) > 1e-5 )
Kf *= exp(-EovR/T);
return Kf;
};
//---------------------------------
// Basic utilities
//---------------------------------
//-- Symbols that are going to be exported
//function Exports.Common(Reaction) local Exports = {}
// __demand(__inline)
// task Exports.AddEduct(r : Reaction, index : int, nu : double, ord : double)
// regentlib.assert(r.Neducts < MAX_NUM_REACTANTS, "Increase MAX_NUM_REACTANTS")
// r.educts[r.Neducts].ind = index
// r.educts[r.Neducts].nu = nu
// r.educts[r.Neducts].ord = ord
// r.Neducts += 1
// return r
// end
//
// __demand(__inline)
// task Exports.AddPduct(r : Reaction, index : int, nu : double, ord : double)
// regentlib.assert(r.Npducts < MAX_NUM_REACTANTS, "Increase MAX_NUM_REACTANTS")
// r.pducts[r.Npducts].ind = index
// r.pducts[r.Npducts].nu = nu
// r.pducts[r.Npducts].ord = ord
// r.Npducts += 1
// return r
// end
//
// __demand(__inline)
// task Exports.AddThirdb(r : Reaction, index : int, eff : double)
// regentlib.assert(r.Nthirdb < MAX_NUM_TB, "Increase MAX_NUM_TB")
// r.thirdb[r.Nthirdb].ind = index
// r.thirdb[r.Nthirdb].eff = eff
// r.Nthirdb += 1
// return r
// end
// return Exports
//end
// TODO: In order to avoid duplication of sources we should have a
// base reaction struct and derive each type of reaction from it
// Unfortunately we cannot do this kind of C++ stuff until we are based on Regent
template<typename Reac>
__CUDA_HD__
inline double CompBackwardRateCoeff(const Reac &r, const double Kf, const double P, const double T, const VecNSp &G) {
double sumNu = 0.0;
double sumNuG = 0.0;
__UNROLL__
for (int i = 0; i < r.Neducts; i++) {
sumNu -= r.educts[i].nu;
sumNuG -= r.educts[i].nu*G[r.educts[i].ind];
}
__UNROLL__
for (int i = 0; i < r.Npducts; i++) {
sumNu += r.pducts[i].nu;
sumNuG += r.pducts[i].nu*G[r.pducts[i].ind];
}
const double lnKc = - sumNuG - sumNu * ( log(T) + log(RGAS/P) );
return Kf * exp(-lnKc);
}
//---------------------------------
// Standard reactions
//---------------------------------
__CUDA_HD__
inline double Reaction::GetReactionRate(const double P, const double T, const VecNSp &C, const VecNSp &G) const {
// Forward reaction rate
const double Kf = ArrCoeff.CompRateCoeff(T);
double a = 1.0;
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
#ifdef FWD_ORDERS
if (educts[i].ord == 1)
#else
if (educts[i].nu == 1)
#endif
a *= C[ind];
else
#ifdef FWD_ORDERS
a *= pow(C[ind], educts[i].ord);
#else
a *= pow(C[ind], educts[i].nu);
#endif
}
// Backward reaction rate
double Kb = 0.0;
double b = 1.0;
if (has_backward) {
Kb = CompBackwardRateCoeff<Reaction>(*this, Kf, P, T, G);
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
if (pducts[i].nu == 1)
b *= C[ind];
else
b *= pow(C[ind], pducts[i].nu);
}
}
// Compute reaction rate
return (Kf*a - Kb*b);
}
__CUDA_HD__
inline void Reaction::AddProductionRates(VecNSp &w, const double P, const double T, const VecNSp &C, const VecNSp &G) const {
const double R = GetReactionRate(P, T, C, G);
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
w[ind] -= educts[i].nu*R;
}
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
w[ind] += pducts[i].nu*R;
}
}
//---------------------------------
// Thirdbody reactions
//---------------------------------
__CUDA_HD__
inline double ThirdbodyReaction::GetReactionRate(const double P, const double T, const VecNSp &C, const VecNSp &G) const {
// Forward reaction rate
const double Kf = ArrCoeff.CompRateCoeff(T);
double a = 1.0;
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
#ifdef FWD_ORDERS
if (educts[i].ord == 1)
#else
if (educts[i].nu == 1)
#endif
a *= C[ind];
else
#ifdef FWD_ORDERS
a *= pow(C[ind], educts[i].ord);
#else
a *= pow(C[ind], educts[i].nu);
#endif
}
// Backward reaction rate
double Kb = 0.0;
double b = 1.0;
if (has_backward) {
Kb = CompBackwardRateCoeff<ThirdbodyReaction>(*this, Kf, P, T, G);
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
if (pducts[i].nu == 1)
b *= C[ind];
else
b *= pow(C[ind], pducts[i].nu);
}
}
// Third body efficiency
double c = 1.0;
if (Nthirdb > 0) {
c = P/(RGAS*T);
__UNROLL__
for (int i = 0; i < Nthirdb; i++) {
const int ind = thirdb[i].ind;
c += C[ind]*(thirdb[i].eff - 1.0);
}
}
// Compute reaction rate
return c*(Kf*a - Kb*b);
}
__CUDA_HD__
inline void ThirdbodyReaction::AddProductionRates(VecNSp &w, const double P, const double T, const VecNSp &C, const VecNSp &G) const {
const double R = GetReactionRate(P, T, C, G);
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
w[ind] -= educts[i].nu*R;
}
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
w[ind] += pducts[i].nu*R;
}
}
//---------------------------------
// Fall-off reactions
//---------------------------------
__CUDA_HD__
inline double FalloffReaction::computeF(const double Pr, const double T) const {
if (Ftype == F_Lindemann)
return 1.0;
else if (Ftype == F_Troe2) {
const double Fc = (1 - FOdata.Troe2.alpha)*exp(-T/FOdata.Troe2.T3)
+ FOdata.Troe2.alpha *exp(-T/FOdata.Troe2.T1);
const double d = 0.14;
const double n = 0.75 - 1.27*log10(Fc);
const double c = -0.4 - 0.67*log10(Fc);
const double a = log10(Pr) + c;
const double f = a/(n - d*a);
return pow(Fc, 1.0/(1 + f*f));
}
else if (Ftype == F_Troe3) {
const double Fc = (1 - FOdata.Troe3.alpha)*exp(-T/FOdata.Troe3.T3)
+ FOdata.Troe3.alpha* exp(-T/FOdata.Troe3.T1)
+ exp(- FOdata.Troe3.T2/T);
const double d = 0.14;
const double n = 0.75 - 1.27*log10(Fc);
const double c = -0.4 - 0.67*log10(Fc);
const double a = log10(Pr) + c;
const double f = a/(n - d*a);
return pow(Fc, 1.0/(1 + f*f));
}
else if (Ftype == F_SRI) {
const double logPr = log10(Pr);
const double X = 1.0/(1 + logPr*logPr);
const double w = FOdata.SRI.A*exp(-FOdata.SRI.B/T)
+ exp(-T/FOdata.SRI.C);
return FOdata.SRI.D*pow(w, X)*pow(T, FOdata.SRI.E);
}
else
assert(false);
return 0.0;
}
__CUDA_HD__
inline double FalloffReaction::GetReactionRate(const double P, const double T, const VecNSp &C, const VecNSp &G) const {
// Forward rate coefficient
const double KfH = ArrCoeffH.CompRateCoeff(T);
const double KfL = ArrCoeffL.CompRateCoeff(T);
// Reduced pressure
double Pr = P/(RGAS * T);
if (Nthirdb > 0) {
__UNROLL__
for (int i = 0; i < Nthirdb; i++) {
const int ind = thirdb[i].ind;
Pr += C[ind]*(thirdb[i].eff - 1.0);
}
}
Pr *= (KfL/max(KfH, 1e-60));
// Use Lindemann formula
double F = computeF(Pr, T);
const double Kf = KfH*(Pr/(1 + Pr))*F;
// Forward reaction rate
double a = 1.0;
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
#ifdef FWD_ORDERS
if (educts[i].ord == 1)
#else
if (educts[i].nu == 1)
#endif
a *= C[ind];
else
#ifdef FWD_ORDERS
a *= pow(C[ind], educts[i].ord);
#else
a *= pow(C[ind], educts[i].nu);
#endif
}
// Backward reaction rate
double Kb = 0.0;
double b = 1.0;
if (has_backward) {
Kb = CompBackwardRateCoeff<FalloffReaction>(*this, Kf, P, T, G);
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
if (pducts[i].nu == 1)
b *= C[ind];
else
b *= pow(C[ind], pducts[i].nu);
}
}
// Compute reaction rate
return Kf*a - Kb*b;
}
__CUDA_HD__
inline void FalloffReaction::AddProductionRates(VecNSp &w, const double P, const double T, const VecNSp &C, const VecNSp &G) const {
const double R = GetReactionRate(P, T, C, G);
__UNROLL__
for (int i = 0; i < Neducts; i++) {
const int ind = educts[i].ind;
w[ind] -= educts[i].nu*R;
}
__UNROLL__
for (int i = 0; i < Npducts; i++) {
const int ind = pducts[i].ind;
w[ind] += pducts[i].nu*R;
}
}