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-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
34 template<class ReactionThermo>
36 
37 
38 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
39 
40 template<class ReactionThermo>
42 (
44 ) const
45 {
46  for (label i = 0; i < lhs_.size(); ++i)
47  {
48  if (i > 0)
49  {
50  reaction << " + ";
51  }
52  if (mag(lhs_[i].stoichCoeff - 1) > SMALL)
53  {
54  reaction << lhs_[i].stoichCoeff;
55  }
56  reaction << species_[lhs_[i].index];
57  if (mag(lhs_[i].exponent - lhs_[i].stoichCoeff) > SMALL)
58  {
59  reaction << "^" << lhs_[i].exponent;
60  }
61  }
62 }
63 
64 
65 template<class ReactionThermo>
67 (
69 ) const
70 {
71  for (label i = 0; i < rhs_.size(); ++i)
72  {
73  if (i > 0)
74  {
75  reaction << " + ";
76  }
77  if (mag(rhs_[i].stoichCoeff - 1) > SMALL)
78  {
79  reaction << rhs_[i].stoichCoeff;
80  }
81  reaction << species_[rhs_[i].index];
82  if (mag(rhs_[i].exponent - rhs_[i].stoichCoeff) > SMALL)
83  {
84  reaction << "^" << rhs_[i].exponent;
85  }
86  }
87 }
88 
89 
90 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
91 
92 template<class ReactionThermo>
94 {
95  return nUnNamedReactions++;
96 }
97 
98 
99 template<class ReactionThermo>
101 (
102  OStringStream& reaction
103 ) const
104 {
105  reactionStrLeft(reaction);
106  reaction << " = ";
107  reactionStrRight(reaction);
108  return reaction.str();
109 }
110 
111 
112 template<class ReactionThermo>
114 (
115  const ReactionTable<ReactionThermo>& thermoDatabase
116 )
117 {
118 
119  typename ReactionThermo::thermoType rhsThermo
120  (
121  rhs_[0].stoichCoeff
122  *(*thermoDatabase[species_[rhs_[0].index]]).W()
123  *(*thermoDatabase[species_[rhs_[0].index]])
124  );
125 
126  for (label i=1; i<rhs_.size(); ++i)
127  {
128  rhsThermo +=
129  rhs_[i].stoichCoeff
130  *(*thermoDatabase[species_[rhs_[i].index]]).W()
131  *(*thermoDatabase[species_[rhs_[i].index]]);
132  }
133 
134  typename ReactionThermo::thermoType lhsThermo
135  (
136  lhs_[0].stoichCoeff
137  *(*thermoDatabase[species_[lhs_[0].index]]).W()
138  *(*thermoDatabase[species_[lhs_[0].index]])
139  );
140 
141  for (label i=1; i<lhs_.size(); ++i)
142  {
143  lhsThermo +=
144  lhs_[i].stoichCoeff
145  *(*thermoDatabase[species_[lhs_[i].index]]).W()
146  *(*thermoDatabase[species_[lhs_[i].index]]);
147  }
148 
149  ReactionThermo::thermoType::operator=(lhsThermo == rhsThermo);
150 }
151 
152 
153 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
154 
155 
156 template<class ReactionThermo>
158 (
159  const speciesTable& species,
160  const List<specieCoeffs>& lhs,
161  const List<specieCoeffs>& rhs,
162  const ReactionTable<ReactionThermo>& thermoDatabase,
163  bool initReactionThermo
164 )
165 :
166  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
167  name_("un-named-reaction-" + Foam::name(getNewReactionID())),
168  species_(species),
169  lhs_(lhs),
170  rhs_(rhs)
171 {
172  if (initReactionThermo)
173  {
174  setThermo(thermoDatabase);
175  }
176 }
177 
178 
179 template<class ReactionThermo>
181 (
182  const Reaction<ReactionThermo>& r,
183  const speciesTable& species
184 )
185 :
186  ReactionThermo::thermoType(r),
187  name_(r.name() + "Copy"),
188  species_(species),
189  lhs_(r.lhs_),
190  rhs_(r.rhs_)
191 {}
192 
193 
194 template<class ReactionThermo>
196 (
197  const speciesTable& species,
198  Istream& is
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  // -1 if not found
227  index = species[specieName];
228  }
229  else
230  {
232  << "Expected a word but found " << t.info()
233  << exit(FatalIOError);
234  }
235 }
236 
237 
238 template<class ReactionThermo>
240 (
241  Istream& is,
242  const speciesTable& species,
243  List<specieCoeffs>& lhs,
244  List<specieCoeffs>& rhs
245 )
246 {
248 
249  while (is.good())
250  {
251  dlrhs.append(specieCoeffs(species, is));
252 
253  if (dlrhs.last().index != -1)
254  {
255  token t(is);
256  if (t.isPunctuation())
257  {
258  if (t == token::ADD)
259  {
260  }
261  else if (t == token::ASSIGN)
262  {
263  lhs = dlrhs.shrink();
264  dlrhs.clear();
265  }
266  else
267  {
268  rhs = dlrhs.shrink();
269  is.putBack(t);
270  return;
271  }
272  }
273  else
274  {
275  rhs = dlrhs.shrink();
276  is.putBack(t);
277  return;
278  }
279  }
280  else
281  {
282  dlrhs.remove();
283  if (is.good())
284  {
285  token t(is);
286  if (t.isPunctuation())
287  {
288  if (t == token::ADD)
289  {
290  }
291  else if (t == token::ASSIGN)
292  {
293  lhs = dlrhs.shrink();
294  dlrhs.clear();
295  }
296  else
297  {
298  rhs = dlrhs.shrink();
299  is.putBack(t);
300  return;
301  }
302  }
303  }
304  else
305  {
306  if (!dlrhs.empty())
307  {
308  rhs = dlrhs.shrink();
309  }
310  return;
311  }
312  }
313  }
314 
316  << "Cannot continue reading reaction data from stream"
317  << exit(FatalIOError);
318 }
319 
320 
321 template<class ReactionThermo>
323 (
324  const speciesTable& species,
325  const ReactionTable<ReactionThermo>& thermoDatabase,
326  const dictionary& dict,
327  bool initReactionThermo
328 )
329 :
330  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
331  name_(dict.dictName()),
332  species_(species)
333 {
334  setLRhs
335  (
336  IStringStream(dict.getString("reaction"))(),
337  species_,
338  lhs_,
339  rhs_
340  );
341 
342  if (initReactionThermo)
343  {
344  setThermo(thermoDatabase);
345  }
346 }
347 
348 
349 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
350 
351 template<class ReactionThermo>
354 (
355  const speciesTable& species,
356  const ReactionTable<ReactionThermo>& thermoDatabase,
357  const dictionary& dict
358 )
359 {
360  const word reactionTypeName(dict.get<word>("type"));
361 
362  auto* ctorPtr = dictionaryConstructorTable(reactionTypeName);
363 
364  if (!ctorPtr)
365  {
367  (
368  dict,
369  "reaction",
370  reactionTypeName,
371  *dictionaryConstructorTablePtr_
372  ) << exit(FatalIOError);
373  }
374 
376  (
377  ctorPtr(species, thermoDatabase, dict)
378  );
379 }
380 
381 
382 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
383 
384 template<class ReactionThermo>
386 {
388  os.writeEntry("reaction", reactionStr(reaction));
389 }
390 
391 
392 template<class ReactionThermo>
394 (
395  const scalar p,
396  const scalar T,
397  const scalarField& c
398 ) const
399 {
400  return 0.0;
401 }
402 
403 
404 template<class ReactionThermo>
406 (
407  const scalar kfwd,
408  const scalar p,
409  const scalar T,
410  const scalarField& c
411 ) const
412 {
413  return 0.0;
414 }
415 
416 
417 template<class ReactionThermo>
419 (
420  const scalar p,
421  const scalar T,
422  const scalarField& c
423 ) const
424 {
425  return 0.0;
426 }
427 
428 
429 template<class ReactionThermo>
431 {
433  return NullObjectRef<speciesTable>();
434 }
435 
436 
437 template<class ReactionThermo>
440 {
442  return NullObjectRef<List<specieCoeffs>>();
443 }
444 
445 
446 template<class ReactionThermo>
449 {
451  return NullObjectRef<List<specieCoeffs>>();
452 }
453 
454 
455 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Reaction::setLRhs
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Construct the left- and right-hand-side reaction coefficients.
Definition: Reaction.C:240
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::Reaction::reactionStrLeft
void reactionStrLeft(OStringStream &reaction) const
Return string representation of the left of the reaction.
Definition: Reaction.C:42
Foam::Reaction::specieCoeffs
Definition: Reaction.H:86
Foam::Reaction::name
const word & name() const noexcept
Name of reaction.
Definition: Reaction.H:253
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:434
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::Reaction::gasSpecies
virtual const speciesTable & gasSpecies() const
Access to gas specie list.
Definition: Reaction.C:430
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::Reaction::glhs
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:439
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::Reaction::kf
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:394
Foam::Reaction::reactionStrRight
void reactionStrRight(OStringStream &reaction) const
Return string representation of the right of the reaction.
Definition: Reaction.C:67
Foam::token::isNumber
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:587
Foam::Reaction::kr
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:406
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
Foam::token::number
scalar number() const
Return label, float or double value.
Definition: tokenI.H:593
Foam::Field< scalar >
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::hashedWordList
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
Definition: hashedWordList.H:54
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Reaction::write
virtual void write(Ostream &os) const
Write.
Definition: Reaction.C:385
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Reaction.H
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
reaction
CombustionModel< rhoReactionThermo > & reaction
Definition: setRegionFluidFields.H:3
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::Reaction::specieCoeffs::specieCoeffs
specieCoeffs()
Definition: Reaction.H:92
Foam::HashPtrTable< ThermoType >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::DynamicList::remove
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:704
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Reaction::grhs
virtual const List< specieCoeffs > & grhs() const
Access to gas components of the reaction.
Definition: Reaction.C:448
DynamicList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Reaction::Reaction
Reaction(const speciesTable &species, const List< specieCoeffs > &lhs, const List< specieCoeffs > &rhs, const ReactionTable< ReactionThermo > &thermoDatabase, bool initReactionThermo=true)
Construct from components.
Definition: Reaction.C:158
Foam::Reaction
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:59
Foam::Reaction::New
static autoPtr< Reaction< ReactionThermo > > New(const speciesTable &species, const ReactionTable< ReactionThermo > &thermoDatabase, const dictionary &dict)
Return a pointer to new patchField created on freestore from dict.
Definition: Reaction.C:354