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-2019 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 
196 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
197 
199 {}
200 
201 
202 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
203 
204 void Foam::phaseProperties::reorder
205 (
206  const wordList& gasNames,
207  const wordList& liquidNames,
208  const wordList& solidNames
209 )
210 {
211  // Determine the addressing to map between species listed in the phase
212  // with those given in the (main) thermo properties
213  switch (phase_)
214  {
215  case GAS:
216  {
217  // The list of gaseous species in the mixture may be a sub-set of
218  // the gaseous species in the carrier phase
219  setCarrierIds(gasNames);
220  break;
221  }
222  case LIQUID:
223  {
224  // Set the list of liquid species to correspond to the complete list
225  // defined in the thermodynamics package.
226  reorder(liquidNames);
227  // Set the ids of the corresponding species in the carrier phase
228  setCarrierIds(gasNames);
229  break;
230  }
231  case SOLID:
232  {
233  // Set the list of solid species to correspond to the complete list
234  // defined in the thermodynamics package.
235  reorder(solidNames);
236  // Assume there is no correspondence between the solid species and
237  // the species in the carrier phase (no sublimation).
238  break;
239  }
240  default:
241  {
243  << "Invalid phase: " << phaseTypeNames[phase_] << nl
244  << " phase must be gas, liquid or solid" << nl
245  << exit(FatalError);
246  }
247  }
248 }
249 
250 
252 {
253  return phase_;
254 }
255 
256 
258 {
259  return stateLabel_;
260 }
261 
262 
264 {
265  return phaseTypeNames[phase_];
266 }
267 
268 
270 {
271  return names_;
272 }
273 
274 
275 const Foam::word& Foam::phaseProperties::name(const label speciei) const
276 {
277  if (speciei >= names_.size())
278  {
280  << "Requested specie " << speciei << "out of range" << nl
281  << "Available phase species:" << nl << names_ << nl
282  << exit(FatalError);
283  }
284 
285  return names_[speciei];
286 }
287 
288 
290 {
291  return Y_;
292 }
293 
294 
295 Foam::scalar& Foam::phaseProperties::Y(const label speciei)
296 {
297  if (speciei >= Y_.size())
298  {
300  << "Requested specie " << speciei << "out of range" << nl
301  << "Available phase species:" << nl << names_ << nl
302  << exit(FatalError);
303  }
304 
305  return Y_[speciei];
306 }
307 
308 
310 {
311  return carrierIds_;
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:319
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
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
phaseType
Phase type enumeration.
const List< word > & names() const
Return the list of specie names.
const labelList & carrierIds() const
Return const access to the map to the carrier ids.
phaseType phase() const
Return const access to the phase type.
static const char nl
Definition: Ostream.H:265
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.