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 (
68  OStringStream& reaction
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()
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 {
247  DynamicList<specieCoeffs> dlrhs;
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 // ************************************************************************* //
Foam::token::ASSIGN
@ ASSIGN
Assignment/equals [isseparator].
Definition: token.H:133
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::Reaction::setLRhs
void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Definition: Reaction.C:233
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:51
Foam::Reaction::reactionStrLeft
void reactionStrLeft(OStringStream &reaction) const
Definition: Reaction.C:35
dictName
const word dictName("faMeshDefinition")
Foam::Reaction::specieCoeffs
Definition: Reaction.H:82
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:64
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:100
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:72
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Definition: DynamicListI.H:427
Foam::DynamicList::clear
void clear() noexcept
Definition: DynamicListI.H:384
Foam::Reaction::gasSpecies
virtual const speciesTable & gasSpecies() const
Definition: Reaction.C:423
Foam::token::isWord
bool isWord() const noexcept
Definition: tokenI.H:602
Foam::Reaction::glhs
virtual const List< specieCoeffs > & glhs() const
Definition: Reaction.C:432
Foam::IOstream::good
bool good() const noexcept
Definition: IOstream.H:229
Foam::Reaction::kf
virtual scalar kf(const scalar p, const scalar T, const scalarField &c) const
Definition: Reaction.C:387
Foam::Reaction::reactionStrRight
void reactionStrRight(OStringStream &reaction) const
Definition: Reaction.C:60
Foam::token::isNumber
bool isNumber() const noexcept
Definition: tokenI.H:580
Foam::Reaction::kr
virtual scalar kr(const scalar kfwd, const scalar p, const scalar T, const scalarField &c) const
Definition: Reaction.C:399
NotImplemented
#define NotImplemented
Definition: error.H:553
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Definition: error.H:502
Foam::token::number
scalar number() const
Definition: tokenI.H:586
Foam::Field
Generic templated field type.
Definition: Field.H:59
Foam::token::info
InfoProxy< token > info() const
Definition: token.H:582
Foam::hashedWordList
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
Definition: hashedWordList.H:50
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::speciesTable
hashedWordList speciesTable
A table of species as a hashedWordList.
Definition: speciesTable.H:37
Foam::Reaction::write
virtual void write(Ostream &os) const
Definition: Reaction.C:378
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Definition: DynamicListI.H:504
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:51
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Definition: tokenI.H:452
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:119
os
OBJstream os(runTime.globalPath()/outputName)
Reaction.H
Foam::IStringStream
Definition: StringStream.H:108
Foam
Definition: atmBoundaryLayer.C:26
Foam::token::wordToken
const word & wordToken() const
Definition: tokenI.H:624
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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:49
Foam::Reaction::specieCoeffs::specieCoeffs
specieCoeffs()
Definition: Reaction.H:88
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:50
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:58
Foam::OStringStream
Definition: StringStream.H:229
Foam::Istream::putBack
void putBack(const token &tok)
Definition: Istream.C:63
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::DynamicList::remove
T remove()
Definition: DynamicListI.H:697
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Definition: Ostream.H:232
Foam::constant::universal::c
const dimensionedScalar c
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Definition: error.H:494
Foam::Reaction::grhs
virtual const List< specieCoeffs > & grhs() const
Definition: Reaction.C:441
DynamicList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::Reaction::Reaction
Reaction(const speciesTable &species, const List< specieCoeffs > &lhs, const List< specieCoeffs > &rhs, const ReactionTable< ReactionThermo > &thermoDatabase, bool initReactionThermo=true)
Foam::Reaction
Simple extension of ReactionThermo to handle reaction kinetics in addition to the equilibrium thermod...
Definition: Reaction.H:55
Foam::Reaction::New
static autoPtr< Reaction< ReactionThermo > > New(const speciesTable &species, const ReactionTable< ReactionThermo > &thermoDatabase, const dictionary &dict)
Definition: Reaction.C:347
Foam::token::ADD
@ ADD
Addition [isseparator].
Definition: token.H:148