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 | Copyright (C) 2011-2017 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Reaction.H"
27 #include "DynamicList.H"
28 
29 // * * * * * * * * * * * * * * * * Static Data * * * * * * * * * * * * * * * //
30 
31 template<class ReactionThermo>
33 
34 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
35 
36 template<class ReactionThermo>
38 (
39  OStringStream& reaction
40 ) const
41 {
42  for (label i = 0; i < lhs_.size(); ++i)
43  {
44  if (i > 0)
45  {
46  reaction << " + ";
47  }
48  if (mag(lhs_[i].stoichCoeff - 1) > SMALL)
49  {
50  reaction << lhs_[i].stoichCoeff;
51  }
52  reaction << species_[lhs_[i].index];
53  if (mag(lhs_[i].exponent - lhs_[i].stoichCoeff) > SMALL)
54  {
55  reaction << "^" << lhs_[i].exponent;
56  }
57  }
58 }
59 
60 
61 template<class ReactionThermo>
63 (
64  OStringStream& reaction
65 ) const
66 {
67  for (label i = 0; i < rhs_.size(); ++i)
68  {
69  if (i > 0)
70  {
71  reaction << " + ";
72  }
73  if (mag(rhs_[i].stoichCoeff - 1) > SMALL)
74  {
75  reaction << rhs_[i].stoichCoeff;
76  }
77  reaction << species_[rhs_[i].index];
78  if (mag(rhs_[i].exponent - rhs_[i].stoichCoeff) > SMALL)
79  {
80  reaction << "^" << rhs_[i].exponent;
81  }
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
87 
88 template<class ReactionThermo>
90 {
91  return nUnNamedReactions++;
92 }
93 
94 
95 template<class ReactionThermo>
97 (
98  OStringStream& reaction
99 ) const
100 {
101  reactionStrLeft(reaction);
102  reaction << " = ";
103  reactionStrRight(reaction);
104  return reaction.str();
105 }
106 
107 
108 template<class ReactionThermo>
110 (
111  const HashPtrTable<ReactionThermo>& thermoDatabase
112 )
113 {
114  typename ReactionThermo::thermoType rhsThermo
115  (
116  rhs_[0].stoichCoeff
117  *(*thermoDatabase[species_[rhs_[0].index]]).W()
118  *(*thermoDatabase[species_[rhs_[0].index]])
119  );
120 
121  for (label i=1; i<rhs_.size(); ++i)
122  {
123  rhsThermo +=
124  rhs_[i].stoichCoeff
125  *(*thermoDatabase[species_[rhs_[i].index]]).W()
126  *(*thermoDatabase[species_[rhs_[i].index]]);
127  }
128 
129  typename ReactionThermo::thermoType lhsThermo
130  (
131  lhs_[0].stoichCoeff
132  *(*thermoDatabase[species_[lhs_[0].index]]).W()
133  *(*thermoDatabase[species_[lhs_[0].index]])
134  );
135 
136  for (label i=1; i<lhs_.size(); ++i)
137  {
138  lhsThermo +=
139  lhs_[i].stoichCoeff
140  *(*thermoDatabase[species_[lhs_[i].index]]).W()
141  *(*thermoDatabase[species_[lhs_[i].index]]);
142  }
143 
144  ReactionThermo::thermoType::operator=(lhsThermo == rhsThermo);
145 }
146 
147 
148 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
149 
150 
151 template<class ReactionThermo>
153 (
154  const speciesTable& species,
155  const List<specieCoeffs>& lhs,
156  const List<specieCoeffs>& rhs,
157  const HashPtrTable<ReactionThermo>& thermoDatabase
158 )
159 :
160  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
161  name_("un-named-reaction-" + Foam::name(getNewReactionID())),
162  species_(species),
163  lhs_(lhs),
164  rhs_(rhs)
165 {
166  setThermo(thermoDatabase);
167 }
168 
169 
170 template<class ReactionThermo>
172 (
173  const Reaction<ReactionThermo>& r,
174  const speciesTable& species
175 )
176 :
177  ReactionThermo::thermoType(r),
178  name_(r.name() + "Copy"),
179  species_(species),
180  lhs_(r.lhs_),
181  rhs_(r.rhs_)
182 {}
183 
184 
185 template<class ReactionThermo>
187 (
188  const speciesTable& species,
189  Istream& is
190 )
191 {
192  token t(is);
193  if (t.isNumber())
194  {
195  stoichCoeff = t.number();
196  is >> t;
197  }
198  else
199  {
200  stoichCoeff = 1.0;
201  }
202 
203  exponent = stoichCoeff;
204 
205  if (t.isWord())
206  {
207  word specieName = t.wordToken();
208 
209  size_t i = specieName.find('^');
210 
211  if (i != word::npos)
212  {
213  string exponentStr = specieName
214  (
215  i + 1,
216  specieName.size() - i - 1
217  );
218  exponent = atof(exponentStr.c_str());
219  specieName = specieName(0, i);
220  }
221 
222  if (species.contains(specieName))
223  {
224  index = species[specieName];
225  }
226  else
227  {
228  index = -1;
229  }
230  }
231  else
232  {
234  << "Expected a word but found " << t.info()
235  << exit(FatalIOError);
236  }
237 }
238 
239 
240 template<class ReactionThermo>
242 (
243  Istream& is,
244  const speciesTable& species,
245  List<specieCoeffs>& lhs,
246  List<specieCoeffs>& rhs
247 )
248 {
250 
251  while (is.good())
252  {
253  dlrhs.append(specieCoeffs(species, is));
254 
255  if (dlrhs.last().index != -1)
256  {
257  token t(is);
258  if (t.isPunctuation())
259  {
260  if (t == token::ADD)
261  {
262  }
263  else if (t == token::ASSIGN)
264  {
265  lhs = dlrhs.shrink();
266  dlrhs.clear();
267  }
268  else
269  {
270  rhs = dlrhs.shrink();
271  is.putBack(t);
272  return;
273  }
274  }
275  else
276  {
277  rhs = dlrhs.shrink();
278  is.putBack(t);
279  return;
280  }
281  }
282  else
283  {
284  dlrhs.remove();
285  if (is.good())
286  {
287  token t(is);
288  if (t.isPunctuation())
289  {
290  if (t == token::ADD)
291  {
292  }
293  else if (t == token::ASSIGN)
294  {
295  lhs = dlrhs.shrink();
296  dlrhs.clear();
297  }
298  else
299  {
300  rhs = dlrhs.shrink();
301  is.putBack(t);
302  return;
303  }
304  }
305  }
306  else
307  {
308  if (!dlrhs.empty())
309  {
310  rhs = dlrhs.shrink();
311  }
312  return;
313  }
314  }
315  }
316 
318  << "Cannot continue reading reaction data from stream"
319  << exit(FatalIOError);
320 }
321 
322 
323 template<class ReactionThermo>
325 (
326  const speciesTable& species,
327  const HashPtrTable<ReactionThermo>& thermoDatabase,
328  const dictionary& dict
329 )
330 :
331  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
332  name_(dict.dictName()),
333  species_(species)
334 {
335  setLRhs
336  (
337  IStringStream(dict.lookup("reaction"))(),
338  species_,
339  lhs_,
340  rhs_
341  );
342  setThermo(thermoDatabase);
343 }
344 
345 
346 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
347 
348 template<class ReactionThermo>
351 (
352  const speciesTable& species,
353  const HashPtrTable<ReactionThermo>& thermoDatabase,
354  const dictionary& dict
355 )
356 {
357  const word& reactionTypeName = dict.lookup("type");
358 
359  typename dictionaryConstructorTable::iterator cstrIter
360  = dictionaryConstructorTablePtr_->find(reactionTypeName);
361 
362  if (cstrIter == dictionaryConstructorTablePtr_->end())
363  {
365  << "Unknown reaction type "
366  << reactionTypeName << nl << nl
367  << "Valid reaction types are :" << nl
368  << dictionaryConstructorTablePtr_->sortedToc()
369  << exit(FatalError);
370  }
371 
373  (
374  cstrIter()(species, thermoDatabase, dict)
375  );
376 }
377 
378 
379 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
380 
381 template<class ReactionThermo>
383 {
385  os.writeKeyword("reaction") << reactionStr(reaction)
386  << token::END_STATEMENT << nl;
387 }
388 
389 
390 template<class ReactionThermo>
392 (
393  const scalar p,
394  const scalar T,
395  const scalarField& c
396 ) const
397 {
398  return 0.0;
399 }
400 
401 
402 template<class ReactionThermo>
404 (
405  const scalar kfwd,
406  const scalar p,
407  const scalar T,
408  const scalarField& c
409 ) const
410 {
411  return 0.0;
412 }
413 
414 
415 template<class ReactionThermo>
417 (
418  const scalar p,
419  const scalar T,
420  const scalarField& c
421 ) const
422 {
423  return 0.0;
424 }
425 
426 
427 template<class ReactionThermo>
429 {
430  return species_;
431 }
432 
433 
434 template<class ReactionThermo>
436 {
438  return NullObjectRef<speciesTable>();
439 }
440 
441 
442 template<class ReactionThermo>
445 {
447  return NullObjectRef<List<specieCoeffs>>();
448 }
449 
450 
451 template<class ReactionThermo>
454 {
456  return NullObjectRef<List<specieCoeffs>>();
457 }
458 
459 
460 // ************************************************************************* //
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:404
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:392
dictionary dict
bool isWord() const
Definition: tokenI.H:213
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
word & name()
Definition: ReactionI.H:36
virtual const List< specieCoeffs > & grhs() const
Definition: Reaction.C:453
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
scalar number() const
Definition: tokenI.H:337
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:374
const word & wordToken() const
Definition: tokenI.H:218
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
virtual void write(Ostream &) const
Write.
Definition: Reaction.C:382
void putBack(const token &)
Put back token.
Definition: Istream.C:30
A HashTable specialization for hashing pointers.
Definition: HashPtrTable.H:50
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Class to hold the specie index and its coefficients in the.
Definition: Reaction.H:91
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:53
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
bool isNumber() const
Definition: tokenI.H:332
const speciesTable & species() const
Access to specie list.
Definition: Reaction.C:428
void reactionStrLeft(OStringStream &reaction) const
Return string representation of the left of the reaction.
Definition: Reaction.C:38
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
A class for handling words, derived from string.
Definition: word.H:59
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:444
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:240
static const char nl
Definition: Ostream.H:262
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual const speciesTable & gasSpecies() const
Access to gas specie list.
Definition: Reaction.C:435
void reactionStrRight(OStringStream &reaction) const
Return string representation of the right of the reaction.
Definition: Reaction.C:63
T remove()
Remove and return the top element.
Definition: DynamicListI.H:347
Reaction(const speciesTable &species, const List< specieCoeffs > &lhs, const List< specieCoeffs > &rhs, const HashPtrTable< ReactionThermo > &thermoDatabase)
Construct from components.
Definition: Reaction.C:153
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Construct the left- and right-hand-side reaction coefficients.
Definition: Reaction.C:242
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
A wordList with hashed indices for faster lookup by name.
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:351
Info<< "Creating reaction model\"<< endl;autoPtr< combustionModels::psiCombustionModel > reaction(combustionModels::psiCombustionModel::New(mesh))
Input from memory buffer stream.
Definition: IStringStream.H:49
string str() const
Return the string.
dimensioned< scalar > mag(const dimensioned< Type > &)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
bool contains(const word &) const
Does the list contain the specified name.
T & last()
Return the last element of the list.
Definition: UListI.H:128
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:224
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:366
A class for handling character strings derived from std::string.
Definition: string.H:74
Output to memory buffer stream.
Definition: OStringStream.H:49
bool isPunctuation() const
Definition: tokenI.H:195
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
IOerror FatalIOError