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-2019 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 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
38 
39 template<class ReactionThermo>
41 (
43 ) const
44 {
45  for (label i = 0; i < lhs_.size(); ++i)
46  {
47  if (i > 0)
48  {
49  reaction << " + ";
50  }
51  if (mag(lhs_[i].stoichCoeff - 1) > SMALL)
52  {
53  reaction << lhs_[i].stoichCoeff;
54  }
55  reaction << species_[lhs_[i].index];
56  if (mag(lhs_[i].exponent - lhs_[i].stoichCoeff) > SMALL)
57  {
58  reaction << "^" << lhs_[i].exponent;
59  }
60  }
61 }
62 
63 
64 template<class ReactionThermo>
66 (
68 ) const
69 {
70  for (label i = 0; i < rhs_.size(); ++i)
71  {
72  if (i > 0)
73  {
74  reaction << " + ";
75  }
76  if (mag(rhs_[i].stoichCoeff - 1) > SMALL)
77  {
78  reaction << rhs_[i].stoichCoeff;
79  }
80  reaction << species_[rhs_[i].index];
81  if (mag(rhs_[i].exponent - rhs_[i].stoichCoeff) > SMALL)
82  {
83  reaction << "^" << rhs_[i].exponent;
84  }
85  }
86 }
87 
88 
89 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
90 
91 template<class ReactionThermo>
93 {
94  return nUnNamedReactions++;
95 }
96 
97 
98 template<class ReactionThermo>
100 (
101  OStringStream& reaction
102 ) const
103 {
104  reactionStrLeft(reaction);
105  reaction << " = ";
106  reactionStrRight(reaction);
107  return reaction.str();
108 }
109 
110 
111 template<class ReactionThermo>
113 (
114  const HashPtrTable<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 
155 template<class ReactionThermo>
157 (
158  const speciesTable& species,
159  const List<specieCoeffs>& lhs,
160  const List<specieCoeffs>& rhs,
161  const HashPtrTable<ReactionThermo>& thermoDatabase,
162  bool initReactionThermo
163 )
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);
174  }
175 }
176 
177 
178 template<class ReactionThermo>
180 (
181  const Reaction<ReactionThermo>& r,
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 
193 template<class ReactionThermo>
195 (
196  const speciesTable& species,
197  Istream& is
198 )
199 {
200  token t(is);
201  if (t.isNumber())
202  {
203  stoichCoeff = t.number();
204  is >> t;
205  }
206  else
207  {
208  stoichCoeff = 1.0;
209  }
210 
211  exponent = stoichCoeff;
212 
213  if (t.isWord())
214  {
215  word specieName = t.wordToken();
216 
217  const size_t i = specieName.find('^');
218 
219  if (i != word::npos)
220  {
221  exponent = atof(specieName.substr(i + 1).c_str());
222  specieName.resize(i);
223  }
224 
225  // -1 if not found
226  index = species[specieName];
227  }
228  else
229  {
231  << "Expected a word but found " << t.info()
232  << exit(FatalIOError);
233  }
234 }
235 
236 
237 template<class ReactionThermo>
239 (
240  Istream& is,
241  const speciesTable& species,
242  List<specieCoeffs>& lhs,
243  List<specieCoeffs>& rhs
244 )
245 {
247 
248  while (is.good())
249  {
250  dlrhs.append(specieCoeffs(species, is));
251 
252  if (dlrhs.last().index != -1)
253  {
254  token t(is);
255  if (t.isPunctuation())
256  {
257  if (t == token::ADD)
258  {
259  }
260  else if (t == token::ASSIGN)
261  {
262  lhs = dlrhs.shrink();
263  dlrhs.clear();
264  }
265  else
266  {
267  rhs = dlrhs.shrink();
268  is.putBack(t);
269  return;
270  }
271  }
272  else
273  {
274  rhs = dlrhs.shrink();
275  is.putBack(t);
276  return;
277  }
278  }
279  else
280  {
281  dlrhs.remove();
282  if (is.good())
283  {
284  token t(is);
285  if (t.isPunctuation())
286  {
287  if (t == token::ADD)
288  {
289  }
290  else if (t == token::ASSIGN)
291  {
292  lhs = dlrhs.shrink();
293  dlrhs.clear();
294  }
295  else
296  {
297  rhs = dlrhs.shrink();
298  is.putBack(t);
299  return;
300  }
301  }
302  }
303  else
304  {
305  if (!dlrhs.empty())
306  {
307  rhs = dlrhs.shrink();
308  }
309  return;
310  }
311  }
312  }
313 
315  << "Cannot continue reading reaction data from stream"
316  << exit(FatalIOError);
317 }
318 
319 
320 template<class ReactionThermo>
322 (
323  const speciesTable& species,
324  const HashPtrTable<ReactionThermo>& thermoDatabase,
325  const dictionary& dict,
326  bool initReactionThermo
327 )
328 :
329  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
330  name_(dict.dictName()),
331  species_(species)
332 {
333  setLRhs
334  (
335  IStringStream(dict.getString("reaction"))(),
336  species_,
337  lhs_,
338  rhs_
339  );
340 
341  if (initReactionThermo)
342  {
343  setThermo(thermoDatabase);
344  }
345 }
346 
347 
348 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
349 
350 template<class ReactionThermo>
353 (
354  const speciesTable& species,
355  const HashPtrTable<ReactionThermo>& thermoDatabase,
356  const dictionary& dict
357 )
358 {
359  const word reactionTypeName(dict.get<word>("type"));
360 
361  auto cstrIter = dictionaryConstructorTablePtr_->cfind(reactionTypeName);
362 
363  if (!cstrIter.found())
364  {
366  (
367  dict,
368  "reaction",
369  reactionTypeName,
370  *dictionaryConstructorTablePtr_
371  ) << exit(FatalIOError);
372  }
373 
375  (
376  cstrIter()(species, thermoDatabase, dict)
377  );
378 }
379 
380 
381 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
382 
383 template<class ReactionThermo>
385 {
387  os.writeEntry("reaction", reactionStr(reaction));
388 }
389 
390 
391 template<class ReactionThermo>
393 (
394  const scalar p,
395  const scalar T,
396  const scalarField& c
397 ) const
398 {
399  return 0.0;
400 }
401 
402 
403 template<class ReactionThermo>
405 (
406  const scalar kfwd,
407  const scalar p,
408  const scalar T,
409  const scalarField& c
410 ) const
411 {
412  return 0.0;
413 }
414 
415 
416 template<class ReactionThermo>
418 (
419  const scalar p,
420  const scalar T,
421  const scalarField& c
422 ) const
423 {
424  return 0.0;
425 }
426 
427 
428 template<class ReactionThermo>
430 {
431  return species_;
432 }
433 
434 
435 template<class ReactionThermo>
437 {
439  return NullObjectRef<speciesTable>();
440 }
441 
442 
443 template<class ReactionThermo>
446 {
448  return NullObjectRef<List<specieCoeffs>>();
449 }
450 
451 
452 template<class ReactionThermo>
455 {
457  return NullObjectRef<List<specieCoeffs>>();
458 }
459 
460 
461 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:239
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::species
const speciesTable & species() const
Access to specie list.
Definition: Reaction.C:429
Foam::Reaction::reactionStrLeft
void reactionStrLeft(OStringStream &reaction) const
Return string representation of the left of the reaction.
Definition: Reaction.C:41
Foam::Reaction::specieCoeffs
Class to hold the specie index and its coefficients in the.
Definition: Reaction.H:94
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:73
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:376
Foam::Reaction::gasSpecies
virtual const speciesTable & gasSpecies() const
Access to gas specie list.
Definition: Reaction.C:436
Foam::token::isWord
bool isWord() const noexcept
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:583
Foam::Reaction::glhs
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:445
Foam::Reaction::kf
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:393
Foam::Reaction::reactionStrRight
void reactionStrRight(OStringStream &reaction) const
Return string representation of the right of the reaction.
Definition: Reaction.C:66
Foam::token::isNumber
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:561
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:405
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
Foam::Reaction::New
static autoPtr< Reaction< ReactionThermo > > New(const speciesTable &species, const HashPtrTable< ReactionThermo > &thermoDatabase, const dictionary &dict)
Return a pointer to new patchField created on freestore from dict.
Definition: Reaction.C:353
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:406
Foam::token::number
scalar number() const
Return label, float or double value.
Definition: tokenI.H:567
Foam::Field< scalar >
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:551
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::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::Reaction::write
virtual void write(Ostream &) const
Write.
Definition: Reaction.C:384
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:453
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:121
Reaction.H
Foam::IStringStream
Input from string buffer, using a ISstream.
Definition: StringStream.H:111
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:599
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:100
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
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.
Definition: StringStream.H:196
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
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:653
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::Reaction::name
const word & name() const
Definition: ReactionI.H:38
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Reaction::grhs
virtual const List< specieCoeffs > & grhs() const
Definition: Reaction.C:454
DynamicList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::Reaction
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:56
Foam::Reaction::Reaction
Reaction(const speciesTable &species, const List< specieCoeffs > &lhs, const List< specieCoeffs > &rhs, const HashPtrTable< ReactionThermo > &thermoDatabase, bool initReactionThermo=true)
Construct from components.
Definition: Reaction.C:157