phaseProperties.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) 2011-2023 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 "phaseProperties.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32  template<>
33  const char* Foam::NamedEnum
34  <
36  4
37  >::names[] =
38  {
39  "gas",
40  "liquid",
41  "solid",
42  "unknown"
43  };
44 }
45 
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::phaseProperties::reorder(const wordList& specieNames)
53 {
54  // ***HGW Unfortunately in the current implementation it is assumed that
55  // if no species are specified the phase is not present and this MUST
56  // be checked at the point of use. This needs a rewrite.
57  if (!names_.size())
58  {
59  return;
60  }
61 
62  // Store the current sames and mass-fractions
63  List<word> names0(names_);
64  scalarField Y0(Y_);
65 
66  // Update the specie names to those given
67  names_ = specieNames;
68 
69  // Re-size mass-fractions if necessary, initialise to 0
70  if (names_.size() != names0.size())
71  {
72  Y_.setSize(names_.size());
73  Y_ = 0;
74  carrierIds_.setSize(names_.size(), -1);
75  }
76 
77  // Set the mass-fraction for each specie in the list to the corresponding
78  // value in the original list
79  forAll(names0, i)
80  {
81  bool found = false;
82  forAll(names_, j)
83  {
84  if (names_[j] == names0[i])
85  {
86  Y_[j] = Y0[i];
87  found = true;
88  break;
89  }
90  }
91 
92  if (!found)
93  {
95  << "Could not find specie " << names0[i]
96  << " in list " << names_
97  << " for phase " << phaseTypeNames[phase_]
98  << exit(FatalError);
99  }
100  }
101 }
102 
103 
104 void Foam::phaseProperties::setCarrierIds
105 (
106  const wordList& carrierNames
107 )
108 {
109  carrierIds_ = -1;
110 
111  forAll(names_, i)
112  {
113  forAll(carrierNames, j)
114  {
115  if (carrierNames[j] == names_[i])
116  {
117  carrierIds_[i] = j;
118  break;
119  }
120  }
121  }
122 }
123 
124 
125 void Foam::phaseProperties::checkTotalMassFraction() const
126 {
127  scalar total = 0.0;
128  forAll(Y_, speciei)
129  {
130  total += Y_[speciei];
131  }
132 
133  if (Y_.size() != 0 && mag(total - 1.0) > small)
134  {
136  << "Specie fractions must total to unity for phase "
137  << phaseTypeNames[phase_] << nl
138  << "Species: " << nl << names_ << nl
139  << exit(FatalError);
140  }
141 }
142 
143 
144 Foam::word Foam::phaseProperties::phaseToStateLabel(const phaseType pt) const
145 {
146  word state = "(unknown)";
147  switch (pt)
148  {
149  case GAS:
150  {
151  state = "(g)";
152  break;
153  }
154  case LIQUID:
155  {
156  state = "(l)";
157  break;
158  }
159  case SOLID:
160  {
161  state = "(s)";
162  break;
163  }
164  default:
165  {
167  << "Invalid phase: " << phaseTypeNames[pt] << nl
168  << " phase must be gas, liquid or solid" << nl
169  << exit(FatalError);
170  }
171  }
172 
173  return state;
174 }
175 
176 
177 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
178 
180 :
181  phase_(UNKNOWN),
182  stateLabel_("(unknown)"),
183  names_(0),
184  Y_(0),
185  carrierIds_(0)
186 {}
187 
188 
189 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
190 
192 {}
193 
194 
195 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
196 
197 void Foam::phaseProperties::reorder
198 (
199  const wordList& gasNames,
200  const wordList& liquidNames,
201  const wordList& solidNames
202 )
203 {
204  // Determine the addressing to map between species listed in the phase
205  // with those given in the (main) thermo properties
206  switch (phase_)
207  {
208  case GAS:
209  {
210  // The list of gaseous species in the mixture may be a sub-set of
211  // the gaseous species in the carrier phase
212  setCarrierIds(gasNames);
213  break;
214  }
215  case LIQUID:
216  {
217  // Set the list of liquid species to correspond to the complete list
218  // defined in the thermodynamics package.
219  reorder(liquidNames);
220  // Set the ids of the corresponding species in the carrier phase
221  setCarrierIds(gasNames);
222  break;
223  }
224  case SOLID:
225  {
226  // Set the list of solid species to correspond to the complete list
227  // defined in the thermodynamics package.
228  reorder(solidNames);
229  // Set the ids of the corresponding species in the carrier phase
230  setCarrierIds(gasNames);
231  break;
232  }
233  default:
234  {
236  << "Invalid phase: " << phaseTypeNames[phase_] << nl
237  << " phase must be gas, liquid or solid" << nl
238  << exit(FatalError);
239  }
240  }
241 }
242 
243 
245 {
246  return phase_;
247 }
248 
249 
251 {
252  return stateLabel_;
253 }
254 
255 
257 {
258  return phaseTypeNames[phase_];
259 }
260 
261 
263 {
264  return names_;
265 }
266 
267 
268 const Foam::word& Foam::phaseProperties::name(const label speciei) const
269 {
270  if (speciei >= names_.size())
271  {
273  << "Requested specie " << speciei << "out of range" << nl
274  << "Available phase species:" << nl << names_ << nl
275  << exit(FatalError);
276  }
277 
278  return names_[speciei];
279 }
280 
281 
283 {
284  return Y_;
285 }
286 
287 
288 Foam::scalar& Foam::phaseProperties::Y(const label speciei)
289 {
290  if (speciei >= Y_.size())
291  {
293  << "Requested specie " << speciei << "out of range" << nl
294  << "Available phase species:" << nl << names_ << nl
295  << exit(FatalError);
296  }
297 
298  return Y_[speciei];
299 }
300 
301 
303 {
304  if (carrierIds_[speciei] == -1)
305  {
307  << "Could not find specie " << names_[speciei]
308  << " in carrier " << nl
309  << exit(FatalError);
310  }
311 
312  return carrierIds_[speciei];
313 }
314 
315 
317 {
318  forAll(names_, speciei)
319  {
320  if (names_[speciei] == specieName)
321  {
322  return speciei;
323  }
324  }
325 
326  return -1;
327 }
328 
329 
330 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:54
static const NamedEnum< phaseType, 4 > phaseTypeNames
Corresponding word representations for phase type enumerations.
const List< word > & names() const
Return the list of specie names.
const word & name(const label speciei) const
Return const access to a specie name.
phaseType
Phase type enumeration.
label carrierId(const label speciei) const
Return the carrier id for a given specie.
phaseProperties()
Null constructor.
phaseType phase() const
Return const access to the phase type.
const scalarField & Y() const
Return const access to all specie mass fractions.
~phaseProperties()
Destructor.
const word & stateLabel() const
Return const access to the phase state label.
label id(const word &specieName) const
Return the id of a specie in the local list by name.
word phaseTypeName() const
Return word representation of the phase type.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
List< word > wordList
A List of words.
Definition: fileName.H:54
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
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
ListType reorder(const label size, const typename ListType::value_type &defaultValue, const labelUList &oldToNew, const ListType &lst)
dimensioned< scalar > mag(const dimensioned< Type > &)
error FatalError
static const char nl
Definition: Ostream.H:266
scalarList Y0(nSpecie, 0.0)