Reaction.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2017-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "Reaction.H"
30#include "DynamicList.H"
31
32// * * * * * * * * * * * * * * * * Static Data * * * * * * * * * * * * * * * //
33
34template<class ReactionThermo>
36
37
38// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
39
40template<class ReactionThermo>
42(
44 const speciesTable& species,
45 const List<specieCoeffs>& reactCoeffs
46)
47{
48 for (label i = 0; i < reactCoeffs.size(); ++i)
49 {
50 const specieCoeffs& coeff = reactCoeffs[i];
51
52 if (i)
53 {
54 reaction << " + ";
55 }
56 if (mag(coeff.stoichCoeff - 1) > SMALL)
57 {
58 reaction << coeff.stoichCoeff;
59 }
60 reaction << species[coeff.index];
61 if (mag(coeff.exponent - coeff.stoichCoeff) > SMALL)
62 {
63 reaction << "^" << coeff.exponent;
64 }
65 }
66}
67
68
69template<class ReactionThermo>
71(
73) const
74{
75 reactionStr(reaction, species_, lhs_);
76}
77
78
79template<class ReactionThermo>
81(
83) const
84{
85 reactionStr(reaction, species_, rhs_);
86}
87
88
89// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
90
91template<class ReactionThermo>
93{
94 return nUnNamedReactions++;
95}
96
97
98template<class ReactionThermo>
100(
102) const
103{
104 reactionStrLeft(reaction);
105 reaction << " = ";
106 reactionStrRight(reaction);
107 return reaction.str();
108}
109
110
111template<class ReactionThermo>
113(
114 const ReactionTable<ReactionThermo>& thermoDatabase
115)
116{
117
118 typename ReactionThermo::thermoType rhsThermo
119 (
120 rhs_[0].stoichCoeff
121 *(*thermoDatabase[species_[rhs_[0].index]]).W()
122 *(*thermoDatabase[species_[rhs_[0].index]])
123 );
124
125 for (label i=1; i<rhs_.size(); ++i)
126 {
127 rhsThermo +=
128 rhs_[i].stoichCoeff
129 *(*thermoDatabase[species_[rhs_[i].index]]).W()
130 *(*thermoDatabase[species_[rhs_[i].index]]);
131 }
132
133 typename ReactionThermo::thermoType lhsThermo
134 (
135 lhs_[0].stoichCoeff
136 *(*thermoDatabase[species_[lhs_[0].index]]).W()
137 *(*thermoDatabase[species_[lhs_[0].index]])
138 );
139
140 for (label i=1; i<lhs_.size(); ++i)
141 {
142 lhsThermo +=
143 lhs_[i].stoichCoeff
144 *(*thermoDatabase[species_[lhs_[i].index]]).W()
145 *(*thermoDatabase[species_[lhs_[i].index]]);
146 }
147
148 ReactionThermo::thermoType::operator=(lhsThermo == rhsThermo);
149}
150
151
152// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
153
154
155template<class ReactionThermo>
157(
158 const speciesTable& species,
159 const List<specieCoeffs>& lhs,
160 const List<specieCoeffs>& rhs,
161 const ReactionTable<ReactionThermo>& thermoDatabase,
162 bool initReactionThermo
164:
165 ReactionThermo::thermoType(*thermoDatabase[species[0]]),
166 name_("un-named-reaction-" + Foam::name(getNewReactionID())),
167 species_(species),
168 lhs_(lhs),
169 rhs_(rhs)
170{
171 if (initReactionThermo)
172 {
173 setThermo(thermoDatabase);
175}
176
177
178template<class ReactionThermo>
180(
182 const speciesTable& species
183)
184:
185 ReactionThermo::thermoType(r),
186 name_(r.name() + "Copy"),
187 species_(species),
188 lhs_(r.lhs_),
189 rhs_(r.rhs_)
190{}
191
192
193template<class ReactionThermo>
195(
196 const speciesTable& species,
197 Istream& is,
198 bool failUnknownSpecie
199)
200{
201 token t(is);
202 if (t.isNumber())
203 {
204 stoichCoeff = t.number();
205 is >> t;
206 }
207 else
208 {
209 stoichCoeff = 1.0;
210 }
211
212 exponent = stoichCoeff;
213
214 if (t.isWord())
215 {
216 word specieName = t.wordToken();
217
218 const size_t i = specieName.find('^');
219
220 if (i != word::npos)
221 {
222 exponent = atof(specieName.substr(i + 1).c_str());
223 specieName.resize(i);
224 }
225
226 // Lookup specie name: -1 if not found
227 index = species.find(specieName);
228
229 if (failUnknownSpecie && index < 0)
230 {
232 << "Unknown specie " << specieName << nl
233 << "Not in " << flatOutput(species) << endl
234 << exit(FatalError);
235 }
236 }
237 else
238 {
240 << "Expected a word but found " << t.info()
241 << exit(FatalIOError);
242 }
243}
244
245
246template<class ReactionThermo>
248(
249 Istream& is,
250 const speciesTable& species,
253 bool failUnknownSpecie
254)
255{
257
258 bool parsingRight = false;
259
260 while (is.good())
261 {
262 dlrhs.append(specieCoeffs(species, is, failUnknownSpecie));
263
264 if (dlrhs.last().index < 0)
265 {
266 dlrhs.remove();
267 }
268
269 if (is.good())
270 {
271 token t(is);
272 if (t.isPunctuation())
273 {
274 if (t == token::ADD)
275 {
276 }
277 else if (t == token::ASSIGN)
278 {
279 if (parsingRight)
280 {
282 << "Multiple '=' in reaction equation" << endl
284 }
285
286 lhs = dlrhs;
287 dlrhs.clear();
288 parsingRight = true;
290 else
291 {
293 << "Unknown punctuation token '" << t
294 << "' in reaction equation" << endl
295 << exit(FatalError);
296 }
297 }
298 else
299 {
300 rhs = dlrhs;
301 is.putBack(t);
302 return;
303 }
304 }
305 else if (parsingRight)
306 {
307 if (!dlrhs.empty())
308 {
309 rhs = dlrhs;
311 return;
312 }
313 }
314
316 << "Cannot continue reading reaction data from stream"
317 << exit(FatalIOError);
318}
319
320
321template<class ReactionThermo>
323(
324 const speciesTable& species,
325 const ReactionTable<ReactionThermo>& thermoDatabase,
326 const dictionary& dict,
327 bool initReactionThermo,
328 bool failUnknownSpecie
329)
331 ReactionThermo::thermoType(*thermoDatabase[species[0]]),
332 name_(dict.dictName()),
333 species_(species)
334{
335 setLRhs
336 (
337 IStringStream(dict.getString("reaction"))(),
338 species_,
339 lhs_,
340 rhs_,
341 failUnknownSpecie
342 );
343
344 if (initReactionThermo)
345 {
346 setThermo(thermoDatabase);
347 }
348}
349
350
351// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
352
353template<class ReactionThermo>
356(
357 const speciesTable& species,
358 const ReactionTable<ReactionThermo>& thermoDatabase,
359 const dictionary& dict
360)
361{
362 const word reactionTypeName(dict.get<word>("type"));
363
364 auto* ctorPtr = dictionaryConstructorTable(reactionTypeName);
365
366 if (!ctorPtr)
367 {
369 (
370 dict,
371 "reaction",
372 reactionTypeName,
373 *dictionaryConstructorTablePtr_
374 ) << exit(FatalIOError);
375 }
376
378 (
379 ctorPtr(species, thermoDatabase, dict)
380 );
381}
382
383
384// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
385
386template<class ReactionThermo>
388{
390 os.writeEntry("reaction", reactionStr(reaction));
391}
392
393
394template<class ReactionThermo>
396(
397 const scalar p,
398 const scalar T,
399 const scalarField& c
400) const
401{
402 return 0.0;
403}
404
405
406template<class ReactionThermo>
408(
409 const scalar kfwd,
410 const scalar p,
411 const scalar T,
412 const scalarField& c
413) const
414{
415 return 0.0;
416}
417
418
419template<class ReactionThermo>
421(
422 const scalar p,
423 const scalar T,
424 const scalarField& c
425) const
426{
427 return 0.0;
428}
429
430
431template<class ReactionThermo>
433{
435 return NullObjectRef<speciesTable>();
436}
437
438
439template<class ReactionThermo>
442{
444 return NullObjectRef<List<specieCoeffs>>();
445}
446
447
448template<class ReactionThermo>
451{
453 return NullObjectRef<List<specieCoeffs>>();
454}
455
456
457// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:655
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:112
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:231
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:239
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:73
virtual scalar kr(const scalar kfwd, const scalar p, const scalar T, const scalarField &c) const
Reverse rate constant from the given forward rate constant.
Definition: Reaction.C:408
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs, bool failUnknownSpecie=true)
Construct the left- and right-hand-side reaction coefficients.
Definition: Reaction.C:248
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:441
void reactionStrLeft(OStringStream &reaction) const
Add string representation of the left of the reaction.
Definition: Reaction.C:71
virtual const List< specieCoeffs > & grhs() const
Access to gas components of the reaction.
Definition: Reaction.C:450
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:396
void reactionStrRight(OStringStream &reaction) const
Add string representation of the right of the reaction.
Definition: Reaction.C:81
virtual const speciesTable & gasSpecies() const
Access to gas specie list.
Definition: Reaction.C:432
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
T & last()
Return the last element of the list.
Definition: UListI.H:216
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
string getString(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Same as get< string >(const word&, keyType::option)
Definition: dictionary.H:1548
virtual bool write()
Write the output fields.
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
label find(const word &val) const
Find index of the value (searches the hash).
A class for handling character strings derived from std::string.
Definition: string.H:79
A token holds an item read from Istream.
Definition: token.H:69
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:587
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
@ ASSIGN
Assignment/equals [isseparator].
Definition: token.H:137
@ ADD
Addition [isseparator].
Definition: token.H:152
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
scalar number() const
Return label, float or double value.
Definition: tokenI.H:593
static constexpr uint64_t npos
Out of range position or size.
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
const volScalarField & T
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
const word dictName("faMeshDefinition")
CombustionModel< rhoReactionThermo > & reaction
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
IOerror FatalIOError
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict