specieCoeffs.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2019 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 "specieCoeffs.H"
27 #include "DynamicList.H"
28 #include "OStringStream.H"
29 
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const speciesTable& species,
36  Istream& is
37 )
38 {
39  token t(is);
40  if (t.isNumber())
41  {
42  stoichCoeff = t.number();
43  is >> t;
44  }
45  else
46  {
47  stoichCoeff = 1;
48  }
49 
50  exponent = stoichCoeff;
51 
52  if (t.isWord())
53  {
54  word specieName = t.wordToken();
55 
56  size_t i = specieName.find('^');
57 
58  if (i != word::npos)
59  {
60  string exponentStr = specieName
61  (
62  i + 1,
63  specieName.size() - i - 1
64  );
65  exponent = atof(exponentStr.c_str());
66  specieName = specieName(0, i);
67  }
68 
69  if (species.contains(specieName))
70  {
71  index = species[specieName];
72  }
73  else
74  {
75  // In order to support the convoluted way in which
76  // the solidChemistry currently handles gaseous species
77  // an error cannot be generated here.
78  // We will re-introduce this check after solidChemistry is
79  // re-written in a rational manner.
80  // FatalIOErrorInFunction(is)
81  // << "Specie " << specieName
82  // << " not found in table " << species
83  // << exit(FatalIOError);
84 
85  index = -1;
86  }
87  }
88  else
89  {
91  << "Expected a word but found " << t.info()
92  << exit(FatalIOError);
93  }
94 }
95 
96 
97 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
98 
100 (
101  Istream& is,
102  const speciesTable& species,
103  List<specieCoeffs>& lhs,
104  List<specieCoeffs>& rhs
105 )
106 {
108 
109  while (is.good())
110  {
111  dlrhs.append(specieCoeffs(species, is));
112 
113  if (dlrhs.last().index != -1)
114  {
115  token t(is);
116  if (t.isPunctuation())
117  {
118  if (t == token::ADD)
119  {
120  }
121  else if (t == token::ASSIGN)
122  {
123  lhs = dlrhs.shrink();
124  dlrhs.clear();
125  }
126  else
127  {
128  rhs = dlrhs.shrink();
129  is.putBack(t);
130  return;
131  }
132  }
133  else
134  {
135  rhs = dlrhs.shrink();
136  is.putBack(t);
137  return;
138  }
139  }
140  else
141  {
142  dlrhs.remove();
143  if (is.good())
144  {
145  token t(is);
146  if (t.isPunctuation())
147  {
148  if (t == token::ADD)
149  {
150  }
151  else if (t == token::ASSIGN)
152  {
153  lhs = dlrhs.shrink();
154  dlrhs.clear();
155  }
156  else
157  {
158  rhs = dlrhs.shrink();
159  is.putBack(t);
160  return;
161  }
162  }
163  }
164  else
165  {
166  if (!dlrhs.empty())
167  {
168  rhs = dlrhs.shrink();
169  }
170  return;
171  }
172  }
173  }
174 
176  << "Cannot continue reading reaction data from stream"
177  << exit(FatalIOError);
178 }
179 
180 
182 (
183  OStringStream& reaction,
184  const speciesTable& species,
185  const List<specieCoeffs>& scs
186 )
187 {
188  for (label i = 0; i < scs.size(); ++i)
189  {
190  if (i > 0)
191  {
192  reaction << " + ";
193  }
194  if (mag(scs[i].stoichCoeff - 1) > small)
195  {
196  reaction << scs[i].stoichCoeff;
197  }
198  reaction << species[scs[i].index];
199  if (mag(scs[i].exponent - scs[i].stoichCoeff) > small)
200  {
201  reaction << "^" << scs[i].exponent;
202  }
203  }
204 }
205 
206 
208 (
209  OStringStream& reaction,
210  const speciesTable& species,
211  const List<specieCoeffs>& lhs,
212  const List<specieCoeffs>& rhs
213 )
214 {
215  reactionStr(reaction, species, lhs);
216  reaction << " = ";
217  reactionStr(reaction, species, rhs);
218  return reaction.str();
219 }
220 
221 
222 // ************************************************************************* //
bool isWord() const
Definition: tokenI.H:230
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
Class to hold the specie index and its coefficients in the reaction rate expression.
Definition: specieCoeffs.H:54
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
scalar number() const
Definition: tokenI.H:382
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:371
const word & wordToken() const
Definition: tokenI.H:235
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
A token holds items read from Istream.
Definition: token.H:69
void putBack(const token &)
Put back token.
Definition: Istream.C:30
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
bool isNumber() const
Definition: tokenI.H:377
static void reactionStr(OStringStream &reaction, const speciesTable &, const List< specieCoeffs > &scs)
Write the string representation of the specieCoeffs list.
Definition: specieCoeffs.C:182
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:296
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:252
T remove()
Remove and return the top element.
Definition: DynamicListI.H:351
static void setLRhs(Istream &, const speciesTable &, List< specieCoeffs > &lhs, List< specieCoeffs > &rhs)
Construct the left- and right-hand-side reaction coefficients.
Definition: specieCoeffs.C:100
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
A wordList with hashed indices for faster lookup by name.
string str() const
Return the string.
dimensioned< scalar > mag(const dimensioned< Type > &)
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:236
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:212
IOerror FatalIOError