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-2020 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.found(specieName))
70  {
71  index = species[specieName];
72  }
73  else
74  {
76  << "Specie " << specieName
77  << " not found in table " << species
78  << exit(FatalIOError);
79 
80  index = -1;
81  }
82  }
83  else
84  {
86  << "Expected a word but found " << t.info()
87  << exit(FatalIOError);
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
93 
95 (
96  Istream& is,
97  const speciesTable& species,
98  List<specieCoeffs>& lhs,
100 )
101 {
103 
104  while (is.good())
105  {
106  dlrhs.append(specieCoeffs(species, is));
107 
108  if (dlrhs.last().index != -1)
109  {
110  token t(is);
111  if (t.isPunctuation())
112  {
113  if (t == token::ADD)
114  {
115  }
116  else if (t == token::ASSIGN)
117  {
118  lhs = dlrhs.shrink();
119  dlrhs.clear();
120  }
121  else
122  {
123  rhs = dlrhs.shrink();
124  is.putBack(t);
125  return;
126  }
127  }
128  else
129  {
130  rhs = dlrhs.shrink();
131  is.putBack(t);
132  return;
133  }
134  }
135  else
136  {
137  dlrhs.remove();
138  if (is.good())
139  {
140  token t(is);
141  if (t.isPunctuation())
142  {
143  if (t == token::ADD)
144  {
145  }
146  else if (t == token::ASSIGN)
147  {
148  lhs = dlrhs.shrink();
149  dlrhs.clear();
150  }
151  else
152  {
153  rhs = dlrhs.shrink();
154  is.putBack(t);
155  return;
156  }
157  }
158  }
159  else
160  {
161  if (!dlrhs.empty())
162  {
163  rhs = dlrhs.shrink();
164  }
165  return;
166  }
167  }
168  }
169 
171  << "Cannot continue reading reaction data from stream"
172  << exit(FatalIOError);
173 }
174 
175 
177 (
178  OStringStream& reaction,
179  const speciesTable& species,
180  const List<specieCoeffs>& scs
181 )
182 {
183  for (label i = 0; i < scs.size(); ++i)
184  {
185  if (i > 0)
186  {
187  reaction << " + ";
188  }
189  if (mag(scs[i].stoichCoeff - 1) > small)
190  {
191  reaction << scs[i].stoichCoeff;
192  }
193  reaction << species[scs[i].index];
194  if (mag(scs[i].exponent - scs[i].stoichCoeff) > small)
195  {
196  reaction << "^" << scs[i].exponent;
197  }
198  }
199 }
200 
201 
203 (
204  OStringStream& reaction,
205  const speciesTable& species,
206  const List<specieCoeffs>& lhs,
207  const List<specieCoeffs>& rhs
208 )
209 {
210  reactionStr(reaction, species, lhs);
211  reaction << " = ";
212  reactionStr(reaction, species, rhs);
213  return reaction.str();
214 }
215 
216 
217 // ************************************************************************* //
bool isWord() const
Definition: tokenI.H:261
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
bool found(const word &) const
Does the list contain the specified name.
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:503
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
const word & wordToken() const
Definition: tokenI.H:266
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:164
A token holds items read from Istream.
Definition: token.H:72
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:498
static void reactionStr(OStringStream &reaction, const speciesTable &, const List< specieCoeffs > &scs)
Write the string representation of the specieCoeffs list.
Definition: specieCoeffs.C:177
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:95
#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 > &)
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:76
Output to memory buffer stream.
Definition: OStringStream.H:49
bool isPunctuation() const
Definition: tokenI.H:243
IOerror FatalIOError