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-2021 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  }
75 
76  // Set the mass-fraction for each specie in the list to the corresponding
77  // value in the original list
78  forAll(names0, i)
79  {
80  bool found = false;
81  forAll(names_, j)
82  {
83  if (names_[j] == names0[i])
84  {
85  Y_[j] = Y0[i];
86  found = true;
87  break;
88  }
89  }
90 
91  if (!found)
92  {
94  << "Could not find specie " << names0[i]
95  << " in list " << names_
96  << " for phase " << phaseTypeNames[phase_]
97  << exit(FatalError);
98  }
99  }
100 }
101 
102 
103 void Foam::phaseProperties::setCarrierIds
104 (
105  const wordList& carrierNames
106 )
107 {
108  carrierIds_ = -1;
109 
110  forAll(names_, i)
111  {
112  forAll(carrierNames, j)
113  {
114  if (carrierNames[j] == names_[i])
115  {
116  carrierIds_[i] = j;
117  break;
118  }
119  }
120  }
121 }
122 
123 
124 void Foam::phaseProperties::checkTotalMassFraction() const
125 {
126  scalar total = 0.0;
127  forAll(Y_, speciei)
128  {
129  total += Y_[speciei];
130  }
131 
132  if (Y_.size() != 0 && mag(total - 1.0) > small)
133  {
135  << "Specie fractions must total to unity for phase "
136  << phaseTypeNames[phase_] << nl
137  << "Species: " << nl << names_ << nl
138  << exit(FatalError);
139  }
140 }
141 
142 
143 Foam::word Foam::phaseProperties::phaseToStateLabel(const phaseType pt) const
144 {
145  word state = "(unknown)";
146  switch (pt)
147  {
148  case GAS:
149  {
150  state = "(g)";
151  break;
152  }
153  case LIQUID:
154  {
155  state = "(l)";
156  break;
157  }
158  case SOLID:
159  {
160  state = "(s)";
161  break;
162  }
163  default:
164  {
166  << "Invalid phase: " << phaseTypeNames[pt] << nl
167  << " phase must be gas, liquid or solid" << nl
168  << exit(FatalError);
169  }
170  }
171 
172  return state;
173 }
174 
175 
176 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
177 
179 :
180  phase_(UNKNOWN),
181  stateLabel_("(unknown)"),
182  names_(0),
183  Y_(0),
184  carrierIds_(0)
185 {}
186 
187 
188 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
189 
191 {}
192 
193 
194 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
195 
196 void Foam::phaseProperties::reorder
197 (
198  const wordList& gasNames,
199  const wordList& liquidNames,
200  const wordList& solidNames
201 )
202 {
203  // Determine the addressing to map between species listed in the phase
204  // with those given in the (main) thermo properties
205  switch (phase_)
206  {
207  case GAS:
208  {
209  // The list of gaseous species in the mixture may be a sub-set of
210  // the gaseous species in the carrier phase
211  setCarrierIds(gasNames);
212  break;
213  }
214  case LIQUID:
215  {
216  // Set the list of liquid species to correspond to the complete list
217  // defined in the thermodynamics package.
218  reorder(liquidNames);
219  // Set the ids of the corresponding species in the carrier phase
220  setCarrierIds(gasNames);
221  break;
222  }
223  case SOLID:
224  {
225  // Set the list of solid species to correspond to the complete list
226  // defined in the thermodynamics package.
227  reorder(solidNames);
228  // Set the ids of the corresponding species in the carrier phase
229  setCarrierIds(gasNames);
230  break;
231  }
232  default:
233  {
235  << "Invalid phase: " << phaseTypeNames[phase_] << nl
236  << " phase must be gas, liquid or solid" << nl
237  << exit(FatalError);
238  }
239  }
240 }
241 
242 
244 {
245  return phase_;
246 }
247 
248 
250 {
251  return stateLabel_;
252 }
253 
254 
256 {
257  return phaseTypeNames[phase_];
258 }
259 
260 
262 {
263  return names_;
264 }
265 
266 
267 const Foam::word& Foam::phaseProperties::name(const label speciei) const
268 {
269  if (speciei >= names_.size())
270  {
272  << "Requested specie " << speciei << "out of range" << nl
273  << "Available phase species:" << nl << names_ << nl
274  << exit(FatalError);
275  }
276 
277  return names_[speciei];
278 }
279 
280 
282 {
283  return Y_;
284 }
285 
286 
287 Foam::scalar& Foam::phaseProperties::Y(const label speciei)
288 {
289  if (speciei >= Y_.size())
290  {
292  << "Requested specie " << speciei << "out of range" << nl
293  << "Available phase species:" << nl << names_ << nl
294  << exit(FatalError);
295  }
296 
297  return Y_[speciei];
298 }
299 
300 
302 {
303  if (carrierIds_[speciei] == -1)
304  {
306  << "Could not find specie " << names_[speciei]
307  << " in carrier " << nl
308  << exit(FatalError);
309  }
310 
311  return carrierIds_[speciei];
312 }
313 
314 
316 {
317  forAll(names_, speciei)
318  {
319  if (names_[speciei] == specieName)
320  {
321  return speciei;
322  }
323  }
324 
325  return -1;
326 }
327 
328 
329 // ************************************************************************* //
const word & stateLabel() const
Return const access to the phase state label.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
label id(const word &specieName) const
Return the id of a specie in the local list by name.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
const scalarField & Y() const
Return const access to all specie mass fractions.
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
~phaseProperties()
Destructor.
scalarList Y0(nSpecie, 0.0)
static const NamedEnum< phaseType, 4 > phaseTypeNames
Corresponding word representations for phase type enumerations.
A class for handling words, derived from string.
Definition: word.H:59
label carrierId(const label speciei) const
Return the carrier id for a given specie.
phaseType
Phase type enumeration.
const List< word > & names() const
Return the list of specie names.
phaseType phase() const
Return const access to the phase type.
static const char nl
Definition: Ostream.H:260
const word & name(const label speciei) const
Return const access to a specie name.
void setSize(const label)
Reset size of List.
Definition: List.C:281
phaseProperties()
Null constructor.
dimensioned< scalar > mag(const dimensioned< Type > &)
bool found
word phaseTypeName() const
Return word representation of the phase type.
Namespace for OpenFOAM.