phaseProperties.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 | Copyright (C) 2011-2015 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, initialize 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  (
95  "void phaseProperties::reorder(const wordList&)"
96  ) << "Could not find specie " << names0[i]
97  << " in list " << names_
98  << " for phase " << phaseTypeNames[phase_]
99  << exit(FatalError);
100  }
101  }
102 }
103 
104 
105 void Foam::phaseProperties::setCarrierIds
106 (
107  const wordList& carrierNames
108 )
109 {
110  carrierIds_ = -1;
111 
112  forAll(names_, i)
113  {
114  forAll(carrierNames, j)
115  {
116  if (carrierNames[j] == names_[i])
117  {
118  carrierIds_[i] = j;
119  break;
120  }
121  }
122  if (carrierIds_[i] == -1)
123  {
125  (
126  "void phaseProperties::setCarrierIds"
127  "(const wordList& carrierNames)"
128  ) << "Could not find carrier specie " << names_[i]
129  << " in species list" << nl
130  << "Available species are: " << nl << carrierNames << nl
131  << exit(FatalError);
132  }
133  }
134 }
135 
136 
137 void Foam::phaseProperties::checkTotalMassFraction() const
138 {
139  scalar total = 0.0;
140  forAll(Y_, speciei)
141  {
142  total += Y_[speciei];
143  }
144 
145  if (Y_.size() != 0 && mag(total - 1.0) > SMALL)
146  {
148  (
149  "void phaseProperties::checkTotalMassFraction() const"
150  ) << "Specie fractions must total to unity for phase "
151  << phaseTypeNames[phase_] << nl
152  << "Species: " << nl << names_ << nl
153  << exit(FatalError);
154  }
155 }
156 
157 
158 Foam::word Foam::phaseProperties::phaseToStateLabel(const phaseType pt) const
159 {
160  word state = "(unknown)";
161  switch (pt)
162  {
163  case GAS:
164  {
165  state = "(g)";
166  break;
167  }
168  case LIQUID:
169  {
170  state = "(l)";
171  break;
172  }
173  case SOLID:
174  {
175  state = "(s)";
176  break;
177  }
178  default:
179  {
181  (
182  "phaseProperties::phaseToStateLabel(phaseType pt)"
183  ) << "Invalid phase: " << phaseTypeNames[pt] << nl
184  << " phase must be gas, liquid or solid" << nl
185  << exit(FatalError);
186  }
187  }
188 
189  return state;
190 }
191 
192 
193 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
194 
196 :
197  phase_(UNKNOWN),
198  stateLabel_("(unknown)"),
199  names_(0),
200  Y_(0),
201  carrierIds_(0)
202 {}
203 
204 
206 :
207  phase_(pp.phase_),
208  stateLabel_(pp.stateLabel_),
209  names_(pp.names_),
210  Y_(pp.Y_),
211  carrierIds_(pp.carrierIds_)
212 {}
213 
214 
215 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
216 
218 {}
219 
220 
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222 
223 void Foam::phaseProperties::reorder
224 (
225  const wordList& gasNames,
226  const wordList& liquidNames,
227  const wordList& solidNames
228 )
229 {
230  // Determine the addressing to map between species listed in the phase
231  // with those given in the (main) thermo properties
232  switch (phase_)
233  {
234  case GAS:
235  {
236  // The list of gaseous species in the mixture may be a sub-set of
237  // the gaseous species in the carrier phase
238  setCarrierIds(gasNames);
239  break;
240  }
241  case LIQUID:
242  {
243  // Set the list of liquid species to correspond to the complete list
244  // defined in the thermodynamics package.
245  reorder(liquidNames);
246  // Set the ids of the corresponding species in the carrier phase
247  setCarrierIds(gasNames);
248  break;
249  }
250  case SOLID:
251  {
252  // Set the list of solid species to correspond to the complete list
253  // defined in the thermodynamics package.
254  reorder(solidNames);
255  // Assume there is no correspondence between the solid species and
256  // the species in the carrier phase (no sublimation).
257  break;
258  }
259  default:
260  {
262  (
263  "phaseProperties::reorder"
264  "("
265  "const wordList& gasNames, "
266  "const wordList& liquidNames, "
267  "const wordList& solidNames"
268  ")"
269  ) << "Invalid phase: " << phaseTypeNames[phase_] << nl
270  << " phase must be gas, liquid or solid" << nl
271  << exit(FatalError);
272  }
273  }
274 }
275 
276 
278 {
279  return phase_;
280 }
281 
282 
284 {
285  return stateLabel_;
286 }
287 
288 
290 {
291  return phaseTypeNames[phase_];
292 }
293 
294 
296 {
297  return names_;
298 }
299 
300 
301 const Foam::word& Foam::phaseProperties::name(const label speciei) const
302 {
303  if (speciei >= names_.size())
304  {
306  (
307  "const word& phaseProperties::name(const label) const"
308  ) << "Requested specie " << speciei << "out of range" << nl
309  << "Available phase species:" << nl << names_ << nl
310  << exit(FatalError);
311  }
312 
313  return names_[speciei];
314 }
315 
316 
318 {
319  return Y_;
320 }
321 
322 
323 Foam::scalar& Foam::phaseProperties::Y(const label speciei)
324 {
325  if (speciei >= Y_.size())
326  {
328  (
329  "const scalar& phaseProperties::Y(const label) const"
330  ) << "Requested specie " << speciei << "out of range" << nl
331  << "Available phase species:" << nl << names_ << nl
332  << exit(FatalError);
333  }
334 
335  return Y_[speciei];
336 }
337 
338 
340 {
341  return carrierIds_;
342 }
343 
344 
346 {
347  forAll(names_, speciei)
348  {
349  if (names_[speciei] == specieName)
350  {
351  return speciei;
352  }
353  }
354 
355  return -1;
356 }
357 
358 
359 // ************************************************************************* //
const word & name(const label speciei) const
Return const access to a specie name.
phaseProperties()
Null constructor.
dimensioned< scalar > mag(const dimensioned< Type > &)
A class for handling words, derived from string.
Definition: word.H:59
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
~phaseProperties()
Destructor.
Namespace for OpenFOAM.
phaseType phase() const
Return const access to the phase type.
const word & stateLabel() const
Return const access to the phase state label.
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
scalarList Y0(nSpecie, 0.0)
#define forAll(list, i)
Definition: UList.H:421
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:52
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
bool found
Helper class to manage multi-specie phase properties.
const List< word > & names() const
Return the list of specie names.
error FatalError
const labelList & carrierIds() const
Return const access to the map to the carrier ids.
label id(const word &specieName) const
Return the id of a specie in the local list by name.
static const NamedEnum< phaseType, 4 > phaseTypeNames
Corresponding word representations for phase type enumerations.
word phaseTypeName() const
Return word representation of the phase type.
phaseType
Phase type enumeration.