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-2016 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  if (rhs_.size() > 0)
115  {
116  ReactionThermo::thermoType::operator=
117  (
118  rhs_[0].stoichCoeff*(*thermoDatabase[species_[rhs_[0].index]])
119  );
120 
121  for (label i=1; i<rhs_.size(); ++i)
122  {
123  this->operator+=
124  (
125  rhs_[i].stoichCoeff*(*thermoDatabase[species_[rhs_[i].index]])
126  );
127  }
128  }
129 
130  forAll(lhs_, i)
131  {
132  this->operator-=
133  (
134  lhs_[i].stoichCoeff*(*thermoDatabase[species_[lhs_[i].index]])
135  );
136  }
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141 
142 
143 template<class ReactionThermo>
145 (
146  const speciesTable& species,
147  const List<specieCoeffs>& lhs,
148  const List<specieCoeffs>& rhs,
149  const HashPtrTable<ReactionThermo>& thermoDatabase
150 )
151 :
152  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
153  name_("un-named-reaction-" + Foam::name(getNewReactionID())),
154  species_(species),
155  lhs_(lhs),
156  rhs_(rhs)
157 {
158  setThermo(thermoDatabase);
159 }
160 
161 
162 template<class ReactionThermo>
164 (
165  const Reaction<ReactionThermo>& r,
166  const speciesTable& species
167 )
168 :
169  ReactionThermo::thermoType(r),
170  name_(r.name() + "Copy"),
171  species_(species),
172  lhs_(r.lhs_),
173  rhs_(r.rhs_)
174 {}
175 
176 
177 template<class ReactionThermo>
179 (
180  const speciesTable& species,
181  Istream& is
182 )
183 {
184  token t(is);
185  if (t.isNumber())
186  {
187  stoichCoeff = t.number();
188  is >> t;
189  }
190  else
191  {
192  stoichCoeff = 1.0;
193  }
194 
195  exponent = stoichCoeff;
196 
197  if (t.isWord())
198  {
199  word specieName = t.wordToken();
200 
201  size_t i = specieName.find('^');
202 
203  if (i != word::npos)
204  {
205  string exponentStr = specieName
206  (
207  i + 1,
208  specieName.size() - i - 1
209  );
210  exponent = atof(exponentStr.c_str());
211  specieName = specieName(0, i);
212  }
213 
214  if (species.contains(specieName))
215  {
216  index = species[specieName];
217  }
218  else
219  {
220  index = -1;
221  }
222  }
223  else
224  {
226  << "Expected a word but found " << t.info()
227  << exit(FatalIOError);
228  }
229 }
230 
231 
232 template<class ReactionThermo>
234 (
235  Istream& is,
236  const speciesTable& species,
237  List<specieCoeffs>& lhs,
238  List<specieCoeffs>& rhs
239 )
240 {
242 
243  while (is.good())
244  {
245  dlrhs.append(specieCoeffs(species, is));
246 
247  if (dlrhs.last().index != -1)
248  {
249  token t(is);
250  if (t.isPunctuation())
251  {
252  if (t == token::ADD)
253  {
254  }
255  else if (t == token::ASSIGN)
256  {
257  lhs = dlrhs.shrink();
258  dlrhs.clear();
259  }
260  else
261  {
262  rhs = dlrhs.shrink();
263  is.putBack(t);
264  return;
265  }
266  }
267  else
268  {
269  rhs = dlrhs.shrink();
270  is.putBack(t);
271  return;
272  }
273  }
274  else
275  {
276  dlrhs.remove();
277  if (is.good())
278  {
279  token t(is);
280  if (t.isPunctuation())
281  {
282  if (t == token::ADD)
283  {
284  }
285  else if (t == token::ASSIGN)
286  {
287  lhs = dlrhs.shrink();
288  dlrhs.clear();
289  }
290  else
291  {
292  rhs = dlrhs.shrink();
293  is.putBack(t);
294  return;
295  }
296  }
297  }
298  else
299  {
300  if (!dlrhs.empty())
301  {
302  rhs = dlrhs.shrink();
303  }
304  return;
305  }
306  }
307  }
308 
310  << "Cannot continue reading reaction data from stream"
311  << exit(FatalIOError);
312 }
313 
314 
315 template<class ReactionThermo>
317 (
318  const speciesTable& species,
319  const HashPtrTable<ReactionThermo>& thermoDatabase,
320  Istream& is
321 )
322 :
323  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
324  name_("un-named-reaction" + Foam::name(getNewReactionID())),
325  species_(species)
326 {
327  setLRhs(is, species, lhs_, rhs_);
328  setThermo(thermoDatabase);
329 }
330 
331 
332 template<class ReactionThermo>
334 (
335  const speciesTable& species,
336  const HashPtrTable<ReactionThermo>& thermoDatabase,
337  const dictionary& dict
338 )
339 :
340  ReactionThermo::thermoType(*thermoDatabase[species[0]]),
341  name_(dict.dictName()),
342  species_(species)
343 {
344  setLRhs
345  (
346  IStringStream(dict.lookup("reaction"))(),
347  species_,
348  lhs_,
349  rhs_
350  );
351  setThermo(thermoDatabase);
352 }
353 
354 
355 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
356 
357 template<class ReactionThermo>
360 (
361  const speciesTable& species,
362  const HashPtrTable<ReactionThermo>& thermoDatabase,
363  Istream& is
364 )
365 {
366  if (is.eof())
367  {
369  (
370  is
371  ) << "Reaction type not specified" << nl << nl
372  << "Valid Reaction types are :" << nl
373  << IstreamConstructorTablePtr_->sortedToc()
374  << exit(FatalIOError);
375  }
376 
377  const word reactionTypeName(is);
378 
379  typename IstreamConstructorTable::iterator cstrIter
380  = IstreamConstructorTablePtr_->find(reactionTypeName);
381 
382  if (cstrIter == IstreamConstructorTablePtr_->end())
383  {
385  (
386  is
387  ) << "Unknown reaction type "
388  << reactionTypeName << nl << nl
389  << "Valid reaction types are :" << nl
390  << IstreamConstructorTablePtr_->sortedToc()
391  << exit(FatalIOError);
392  }
393 
395  (
396  cstrIter()(species, thermoDatabase, is)
397  );
398 }
399 
400 
401 template<class ReactionThermo>
404 (
405  const speciesTable& species,
406  const HashPtrTable<ReactionThermo>& thermoDatabase,
407  const dictionary& dict
408 )
409 {
410  const word& reactionTypeName = dict.lookup("type");
411 
412  typename dictionaryConstructorTable::iterator cstrIter
413  = dictionaryConstructorTablePtr_->find(reactionTypeName);
414 
415  if (cstrIter == dictionaryConstructorTablePtr_->end())
416  {
418  << "Unknown reaction type "
419  << reactionTypeName << nl << nl
420  << "Valid reaction types are :" << nl
421  << dictionaryConstructorTablePtr_->sortedToc()
422  << exit(FatalError);
423  }
424 
426  (
427  cstrIter()(species, thermoDatabase, dict)
428  );
429 }
430 
431 
432 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
433 
434 template<class ReactionThermo>
436 {
438  os.writeKeyword("reaction") << reactionStr(reaction)
439  << token::END_STATEMENT << nl;
440 }
441 
442 
443 template<class ReactionThermo>
445 (
446  const scalar p,
447  const scalar T,
448  const scalarField& c
449 ) const
450 {
451  return 0.0;
452 }
453 
454 
455 template<class ReactionThermo>
457 (
458  const scalar kfwd,
459  const scalar p,
460  const scalar T,
461  const scalarField& c
462 ) const
463 {
464  return 0.0;
465 }
466 
467 
468 template<class ReactionThermo>
470 (
471  const scalar p,
472  const scalar T,
473  const scalarField& c
474 ) const
475 {
476  return 0.0;
477 }
478 
479 
480 template<class ReactionThermo>
482 {
483  return species_;
484 }
485 
486 
487 template<class ReactionThermo>
489 {
491  return NullObjectRef<speciesTable>();
492 }
493 
494 
495 template<class ReactionThermo>
498 {
500  return NullObjectRef<List<specieCoeffs>>();
501 }
502 
503 
504 template<class ReactionThermo>
507 {
509  return NullObjectRef<List<specieCoeffs>>();
510 }
511 
512 
513 // ************************************************************************* //
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
string str() const
Return the string.
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
virtual const List< specieCoeffs > & grhs() const
Definition: Reaction.C:506
static autoPtr< Reaction< ReactionThermo > > New(const speciesTable &species, const HashPtrTable< ReactionThermo > &thermoDatabase, Istream &is)
Return a pointer to new patchField created on freestore from input.
Definition: Reaction.C:360
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
word & name()
Definition: ReactionI.H:36
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
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
A token holds items read from Istream.
Definition: token.H:69
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
void putBack(const token &)
Put back token.
Definition: Istream.C:30
void reactionStrRight(OStringStream &reaction) const
Return string representation of the right of the reaction.
Definition: Reaction.C:63
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
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:445
bool isWord() const
Definition: tokenI.H:213
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
void reactionStrLeft(OStringStream &reaction) const
Return string representation of the left of the reaction.
Definition: Reaction.C:38
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
virtual void write(Ostream &) const
Write.
Definition: Reaction.C:435
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
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:457
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 List< specieCoeffs > & glhs() const
Definition: Reaction.C:497
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:145
scalar number() const
Definition: tokenI.H:337
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Construct the left- and right-hand-side reaction coefficients.
Definition: Reaction.C:234
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
A wordList with hashed indices for faster lookup by name.
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:374
Info<< "Creating reaction model\n"<< endl;autoPtr< combustionModels::psiCombustionModel > reaction(combustionModels::psiCombustionModel::New(mesh))
Input from memory buffer stream.
Definition: IStringStream.H:49
const word & wordToken() const
Definition: tokenI.H:218
dimensioned< scalar > mag(const dimensioned< Type > &)
bool contains(const word &) const
Does the list contain the specified name.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
bool isPunctuation() const
Definition: tokenI.H:195
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
const speciesTable & species() const
Definition: Reaction.C:481
#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 isNumber() const
Definition: tokenI.H:332
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451
IOerror FatalIOError
virtual const speciesTable & gasSpecies() const
Definition: Reaction.C:488