massFractions.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) 2023-2026 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 "massFractions.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace functionObjects
35 {
38 }
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 (
46  const word& name,
47  const Time& runTime,
48  const dictionary& dict
49 )
50 :
51  fvMeshFunctionObject(name, runTime, dict),
52  phaseName_(dict.lookupOrDefault<word>("phase", word::null)),
53  Y_()
54 {}
55 
56 
57 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
58 
60 {}
61 
62 
63 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 
66 {
67  return true;
68 }
69 
70 
72 {
73  // A multicomponent thermo should not exist for this context, as the
74  // following would create a different, inconsistent definition of the
75  // composition of an existing thermo model
76  const word thermoName =
78  if (mesh_.foundObject<fluidMulticomponentThermo>(thermoName))
79  {
81  << "Cannot create mass fractions. Mass fractions already exist "
82  << "within the multicomponent thermo model, \"" << thermoName
83  << "\"." << exit(FatalError);
84  }
85 
86  // Construct a multicomponent thermo
89  fluidMulticomponentThermo& thermo = thermoPtr();
90  const PtrList<volScalarField>& Y = thermo.Y();
91 
92  // One-mole constant for conversions
93  static const dimensionedScalar oneMole(dimMoles, 1);
94 
95  // Construct lists of specie molar mass, fields of specie mass, and a field
96  // of total mass
98  PtrList<volScalarField> m(Y.size());
99  volScalarField mTotal
100  (
101  IOobject("mTotal", time_.name(), mesh_),
102  mesh_,
104  );
105  bool fromMoleFractions = false, fromMoles = false;
106  forAll(Y, i)
107  {
108  W.set(i, new dimensionedScalar(thermo.Wi(i)));
109 
111  (
112  Y[i].name(),
113  time_.name(),
114  mesh_,
116  );
118  (
119  "X_" + Y[i].name(),
120  time_.name(),
121  mesh_,
123  );
125  (
126  "n_" + Y[i].name(),
127  time_.name(),
128  mesh_,
130  );
131 
132  // Check consistency of input
133  if (YIo.headerOk())
134  {
136  << "Mass fraction field " << YIo.name()
137  << " already exists on disk" << exit(FatalError);
138  }
139  fromMoleFractions = fromMoleFractions || XIo.headerOk();
140  fromMoles = fromMoles || nIo.headerOk();
141  if (fromMoleFractions && fromMoles)
142  {
144  << "Mole fraction fields and moles fields "
145  << " both found on disk"
146  << exit(FatalError);
147  }
148 
149  // Sum the contributions
150  if (XIo.headerOk())
151  {
152  m.set(i, W[i]*oneMole*volScalarField(XIo, mesh_));
153  }
154  if (nIo.headerOk())
155  {
156  m.set(i, W[i]*volScalarField(nIo, mesh_));
157  }
158  if (XIo.headerOk() || nIo.headerOk())
159  {
160  mTotal += m[i];
161  }
162  }
163 
164  // Steal the thermo's mass fraction fields and delete the thermo
165  Y_.transfer(thermo.Y());
166  thermoPtr.clear();
167 
168  // Divide the specie masses by the total mass to get the mass fractions
169  forAll(Y_, i)
170  {
171  if (m.set(i))
172  {
173  Y_[i] == m[i]/mTotal;
174  }
175  else
176  {
177  Y_.set(i, nullptr);
178  }
179  }
180 
181  return true;
182 }
183 
184 
186 {
187  forAll(Y_, i)
188  {
189  if (Y_.set(i))
190  {
191  Y_[i].write();
192  }
193  }
194 
195  return true;
196 }
197 
198 
199 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into run-time selection tables.
Generic GeometricField class.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const word & name() const
Return name.
Definition: IOobject.H:307
static word groupName(Name name, const word &group)
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
bool set(const label) const
Is element set.
Definition: PtrListI.H:62
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:213
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Base-class for multi-component fluid thermodynamic properties.
static autoPtr< fluidMulticomponentThermo > New(const fvMesh &, const word &phaseName=word::null)
Standard selection based on fvMesh.
Abstract base-class for Time/database functionObjects.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
This function object calculates mass-fraction fields from mole-fraction or moles fields present on di...
Definition: massFractions.H:74
virtual ~massFractions()
Destructor.
Definition: massFractions.C:59
massFractions(const word &name, const Time &t, const dictionary &dict)
Construct from Time and dictionary.
Definition: massFractions.C:45
virtual bool execute()
Calculate the mass-fraction fields.
Definition: massFractions.C:71
virtual bool write()
The mass-fraction fields auto-write.
virtual bool read(const dictionary &)
Read the massFractions data.
Definition: massFractions.C:65
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:545
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
A class for handling words, derived from string.
Definition: word.H:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, fvModel, dictionary)
Namespace for OpenFOAM.
const dimensionSet & dimMoles
Definition: dimensions.C:144
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const dimensionSet & dimMass
Definition: dimensions.C:140
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
scalarList W(const fluidMulticomponentThermo &thermo)
VolField< scalar > volScalarField
Definition: volFieldsFwd.H:62
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
dictionary dict
PtrList< volScalarField > & Y
fluidMulticomponentThermo & thermo
Definition: createFields.H:15