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-2014 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  {
225  FatalIOErrorIn("Reaction<ReactionThermo>::lrhs(Istream& is)", is)
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 
309  FatalIOErrorIn("Reaction<ReactionThermo>::setLRhs(Istream& is)", is)
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  "Reaction<ReactionThermo>::New(const speciesTable&, "
371  " const HashPtrTable<ReactionThermo>&, Istream&)",
372  is
373  ) << "Reaction type not specified" << nl << nl
374  << "Valid Reaction types are :" << nl
375  << IstreamConstructorTablePtr_->sortedToc()
376  << exit(FatalIOError);
377  }
378 
379  const word reactionTypeName(is);
380 
381  typename IstreamConstructorTable::iterator cstrIter
382  = IstreamConstructorTablePtr_->find(reactionTypeName);
383 
384  if (cstrIter == IstreamConstructorTablePtr_->end())
385  {
387  (
388  "Reaction<ReactionThermo>::New(const speciesTable&, "
389  " const HashPtrTable<ReactionThermo>&, Istream&)",
390  is
391  ) << "Unknown reaction type "
392  << reactionTypeName << nl << nl
393  << "Valid reaction types are :" << nl
394  << IstreamConstructorTablePtr_->sortedToc()
395  << exit(FatalIOError);
396  }
397 
399  (
400  cstrIter()(species, thermoDatabase, is)
401  );
402 }
403 
404 
405 template<class ReactionThermo>
408 (
409  const speciesTable& species,
410  const HashPtrTable<ReactionThermo>& thermoDatabase,
411  const dictionary& dict
412 )
413 {
414  const word& reactionTypeName = dict.lookup("type");
415 
416  typename dictionaryConstructorTable::iterator cstrIter
417  = dictionaryConstructorTablePtr_->find(reactionTypeName);
418 
419  if (cstrIter == dictionaryConstructorTablePtr_->end())
420  {
422  (
423  "Reaction<ReactionThermo>::New"
424  "("
425  "const speciesTable&, "
426  "const HashPtrTable<ReactionThermo>&, "
427  "const dictionary&"
428  ")"
429  ) << "Unknown reaction type "
430  << reactionTypeName << nl << nl
431  << "Valid reaction types are :" << nl
432  << dictionaryConstructorTablePtr_->sortedToc()
433  << exit(FatalError);
434  }
435 
437  (
438  cstrIter()(species, thermoDatabase, dict)
439  );
440 }
441 
442 
443 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
444 
445 template<class ReactionThermo>
447 {
449  os.writeKeyword("reaction") << reactionStr(reaction)
450  << token::END_STATEMENT << nl;
451 }
452 
453 
454 template<class ReactionThermo>
456 (
457  const scalar p,
458  const scalar T,
459  const scalarField& c
460 ) const
461 {
462  return 0.0;
463 }
464 
465 
466 template<class ReactionThermo>
468 (
469  const scalar kfwd,
470  const scalar p,
471  const scalar T,
472  const scalarField& c
473 ) const
474 {
475  return 0.0;
476 }
477 
478 
479 template<class ReactionThermo>
481 (
482  const scalar p,
483  const scalar T,
484  const scalarField& c
485 ) const
486 {
487  return 0.0;
488 }
489 
490 
491 template<class ReactionThermo>
493 {
494  return species_;
495 }
496 
497 
498 template<class ReactionThermo>
500 {
502  (
503  "const speciesTable& gasSpecies() const"
504  " for this reaction"
505  );
506  return NullObjectRef<speciesTable>();
507 }
508 
509 
510 template<class ReactionThermo>
513 {
515  (
516  "inline const List<typename Reaction<ReactionThermo>::specieCoeffs>&"
517  "Reaction<ReactionThermo>::glhs()"
518  );
519  return NullObjectRef<List<specieCoeffs> >();
520 }
521 
522 
523 template<class ReactionThermo>
526 {
528  (
529  "inline const List<typename Reaction<ReactionThermo>::specieCoeffs>&"
530  "Reaction<ReactionThermo>::grhs()"
531  );
532  return NullObjectRef<List<specieCoeffs> >();
533 }
534 
535 
536 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:49
virtual const List< specieCoeffs > & grhs() const
Definition: Reaction.C:525
scalar number() const
Definition: tokenI.H:345
const speciesTable & species() const
Definition: Reaction.C:492
bool isWord() const
Definition: tokenI.H:221
dimensioned< scalar > mag(const dimensioned< Type > &)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Forward rate constant.
Definition: Reaction.C:456
T & last()
Return the last element of the list.
Definition: UListI.H:131
void reactionStrLeft(OStringStream &reaction) const
Return string representation of the left of the reaction.
Definition: Reaction.C:38
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual const speciesTable & gasSpecies() const
Definition: Reaction.C:499
string str() const
Return the string.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:242
A class for handling words, derived from string.
Definition: word.H:59
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
A class for handling character strings derived from std::string.
Definition: string.H:74
bool isPunctuation() const
Definition: tokenI.H:203
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
T remove()
Remove and return the top element.
Definition: DynamicListI.H:368
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const word & wordToken() const
Definition: tokenI.H:226
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
bool isNumber() const
Definition: tokenI.H:340
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
dictionary dict
static const char nl
Definition: Ostream.H:260
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:512
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
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:468
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
Output to memory buffer stream.
Definition: OStringStream.H:49
A token holds items read from Istream.
Definition: token.H:67
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Construct the left- and right-hand-side reaction coefficients.
Definition: Reaction.C:234
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
void reactionStrRight(OStringStream &reaction) const
Return string representation of the right of the reaction.
Definition: Reaction.C:63
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:53
virtual void write(Ostream &) const
Write.
Definition: Reaction.C:446
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
Class to hold the specie index and its coefficients in the.
Definition: Reaction.H:91
Info<< "Creating reaction model\n"<< endl;autoPtr< combustionModels::psiCombustionModel > reaction(combustionModels::psiCombustionModel::New(mesh))
A wordList with hashed indices for faster lookup by name.
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
bool contains(const word &) const
Does the list contain the specified name.
Reaction(const speciesTable &species, const List< specieCoeffs > &lhs, const List< specieCoeffs > &rhs, const HashPtrTable< ReactionThermo > &thermoDatabase)
Construct from components.
Definition: Reaction.C:145
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:356
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
word & name()
Definition: ReactionI.H:36