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