File size: 5,768 Bytes
5a600cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2023-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "massFractions.H"
#include "fluidMulticomponentThermo.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(massFractions, 0);
addToRunTimeSelectionTable(functionObject, massFractions, dictionary);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::massFractions::massFractions
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict),
phaseName_(dict.lookupOrDefault<word>("phase", word::null)),
Y_()
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::massFractions::~massFractions()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::massFractions::read(const dictionary& dict)
{
return true;
}
bool Foam::functionObjects::massFractions::execute()
{
// A multicomponent thermo should not exist for this context, as the
// following would create a different, inconsistent definition of the
// composition of an existing thermo model
const word thermoName =
IOobject::groupName(physicalProperties::typeName, phaseName_);
if (mesh_.foundObject<fluidMulticomponentThermo>(thermoName))
{
FatalErrorInFunction
<< "Cannot create mass fractions. Mass fractions already exist "
<< "within the multicomponent thermo model, \"" << thermoName
<< "\"." << exit(FatalError);
}
// Construct a multicomponent thermo
autoPtr<fluidMulticomponentThermo> thermoPtr =
fluidMulticomponentThermo::New(mesh_);
fluidMulticomponentThermo& thermo = thermoPtr();
const PtrList<volScalarField>& Y = thermo.Y();
// One-mole constant for conversions
static const dimensionedScalar oneMole(dimensions::moles, 1);
// Construct lists of specie molar mass, fields of specie mass, and a field
// of total mass
PtrList<dimensionedScalar> W(Y.size());
PtrList<volScalarField> m(Y.size());
volScalarField mTotal
(
IOobject("mTotal", time_.name(), mesh_),
mesh_,
dimensionedScalar(dimensions::mass, 0)
);
bool fromMoleFractions = false, fromMoles = false;
forAll(Y, i)
{
W.set(i, new dimensionedScalar(thermo.Wi(i)));
typeIOobject<volScalarField> YIo
(
Y[i].name(),
time_.name(),
mesh_,
IOobject::MUST_READ
);
typeIOobject<volScalarField> XIo
(
"X_" + Y[i].name(),
time_.name(),
mesh_,
IOobject::MUST_READ
);
typeIOobject<volScalarField> nIo
(
"n_" + Y[i].name(),
time_.name(),
mesh_,
IOobject::MUST_READ
);
// Check consistency of input
if (YIo.headerOk())
{
FatalErrorInFunction
<< "Mass fraction field " << YIo.name()
<< " already exists on disk" << exit(FatalError);
}
fromMoleFractions = fromMoleFractions || XIo.headerOk();
fromMoles = fromMoles || nIo.headerOk();
if (fromMoleFractions && fromMoles)
{
FatalErrorInFunction
<< "Mole fraction fields and moles fields "
<< " both found on disk"
<< exit(FatalError);
}
// Sum the contributions
if (XIo.headerOk())
{
m.set(i, W[i]*oneMole*volScalarField(XIo, mesh_));
}
if (nIo.headerOk())
{
m.set(i, W[i]*volScalarField(nIo, mesh_));
}
if (XIo.headerOk() || nIo.headerOk())
{
mTotal += m[i];
}
}
// Steal the thermo's mass fraction fields and delete the thermo
Y_.transfer(thermo.Y());
thermoPtr.clear();
// Divide the specie masses by the total mass to get the mass fractions
forAll(Y_, i)
{
if (m.set(i))
{
Y_[i] == m[i]/mTotal;
}
else
{
Y_.set(i, nullptr);
}
}
return true;
}
bool Foam::functionObjects::massFractions::write()
{
forAll(Y_, i)
{
if (Y_.set(i))
{
Y_[i].write();
}
}
return true;
}
// ************************************************************************* //
|