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