/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2019-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 .
\*---------------------------------------------------------------------------*/
#include "addToRunTimeSelectionTable.H"
#include "fixedValueFvPatchField.H"
#include "fviDdt.H"
#include "fviDiv.H"
#include "fvmDdt.H"
#include "fvmDiv.H"
#include "fvmLaplacian.H"
#include "fvModels.H"
#include "fvConstraints.H"
#include "nonOrthogonalSolutionControl.H"
#include "phaseScalarTransport.H"
#include "surfaceFields.H"
#include "momentumTransportModel.H"
#include "wallFvPatch.H"
#include "zeroGradientFvPatchField.H"
#define PhiDimensionErrorInFunction(phi) \
FatalErrorInFunction \
<< "Incompatible dimensions for " << phi.name() << ": " \
<< phi.dimensions() << nl \
<< "Dimensions should be " << dimensions::massFlux \
<< " or " << dimensions::volume/dimensions::time << exit(FatalError)
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(phaseScalarTransport, 0);
addToRunTimeSelectionTable
(
functionObject,
phaseScalarTransport,
dictionary
);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::volScalarField& Foam::functionObjects::phaseScalarTransport::Phi()
{
if (!PhiPtr_.valid())
{
const surfaceScalarField& phi =
mesh_.lookupObject(phiName_);
const volScalarField& p =
mesh_.lookupObject(pName_);
wordList PhiPatchFieldTypes(mesh_.poly().boundary().size());
forAll(p.boundaryField(), patchi)
{
PhiPatchFieldTypes[patchi] =
p.boundaryField()[patchi].fixesValue()
? fixedValueFvPatchField::typeName
: zeroGradientFvPatchField::typeName;
}
PhiPtr_.set
(
new volScalarField
(
IOobject
(
typedName(IOobject::groupName("Phi", phaseName_)),
time_.name(),
mesh_,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar(phi.dimensions()/dimensions::length, Zero),
PhiPatchFieldTypes
)
);
mesh_.schemes().setFluxRequired(PhiPtr_->name());
}
return PhiPtr_();
}
Foam::tmp
Foam::functionObjects::phaseScalarTransport::alphaPhi()
{
if (!solveAlphaPhi_)
{
return mesh_.lookupObject(alphaPhiName_);
}
const volScalarField& alpha =
mesh_.lookupObject(alphaName_);
const surfaceScalarField& phi =
mesh_.lookupObject(phiName_);
// Make a crude guess of the phase flux using default interpolation
tmp tAlphaPhi
(
new surfaceScalarField
(
alphaPhiName_,
phi*fvc::interpolate(alpha)
)
);
surfaceScalarField& alphaPhi = tAlphaPhi.ref();
// Get the potential field
volScalarField& Phi(this->Phi());
// Construct the Phi Laplacian scheme names
const word PhiLaplacianScheme("laplacian(", pName_, ')');
// Debug writing. Write the material derivative of alpha, before and after
// the solution of the potential and the correction of alphaPhi. Before
// correction the field should be non-zero, and after it should be
// comparable to the solution tolerance.
auto writeDDt = [&](const label i)
{
const volInternalScalarField DDtAlpha
(
word
(
"DDt(",
IOobject::groupName
(
IOobject::member(alpha.name()) + Foam::name(i),
IOobject::group(alpha.name())
),
')'
),
fvi::ddt(alpha) + fvi::div(alphaPhi)
);
Info<< type() << ": Writing " << DDtAlpha.name() << endl;
DDtAlpha.write();
};
if (debug && time_.writeTime())
{
writeDDt(0);
}
// Lookup the non-orthogonal solution control
nonOrthogonalSolutionControl& control =
mesh_.lookupObjectRef
(
solutionControl::typeName
);
// Solve for the potential and correct alphaPhi with the resulting flux
if (phi.dimensions() == dimensions::volumetricFlux)
{
while (control.correctNonOrthogonal())
{
fvScalarMatrix PhiEqn
(
fvm::laplacian(Phi, PhiLaplacianScheme)
+ fvi::ddt(alpha)
+ fvi::div(alphaPhi)
);
PhiEqn.solve(pName_);
if (control.finalNonOrthogonalIter())
{
alphaPhi += PhiEqn.flux();
}
}
}
else if (phi.dimensions() == dimensions::massFlux)
{
const volScalarField& rho =
mesh_.lookupObject(rhoName_);
while (control.correctNonOrthogonal())
{
fvScalarMatrix PhiEqn
(
fvm::laplacian(Phi, PhiLaplacianScheme)
+ fvi::ddt(rho, alpha)
+ fvi::div(alphaPhi)
);
PhiEqn.solve(pName_);
if (control.finalNonOrthogonalIter())
{
alphaPhi += PhiEqn.flux();
}
}
}
else
{
PhiDimensionErrorInFunction(phi);
}
// Debug writing
if (debug && time_.writeTime())
{
writeDDt(1);
}
return tAlphaPhi;
}
Foam::tmp
Foam::functionObjects::phaseScalarTransport::D
(
const surfaceScalarField& alphaPhi
) const
{
if (diffusivity_ == scalarTransport::diffusivityType::field)
{
return mesh_.lookupObject(Dname_);
}
if (diffusivity_ == scalarTransport::diffusivityType::viscosity)
{
const word& nameNoPhase = momentumTransportModel::typeName;
const word namePhase = IOobject::groupName(nameNoPhase, phaseName_);
// Try looking up the phase transport model, then try the mixture
// transport model, then fail with an error relating to the phase
// transport model
const momentumTransportModel& turbulence =
mesh_.foundObject(namePhase)
? mesh_.lookupObject(namePhase)
: mesh_.foundObject(nameNoPhase)
? mesh_.lookupObject(nameNoPhase)
: mesh_.lookupObject(namePhase);
return volScalarField::New
(
"D" + s_.name(),
alphal_*turbulence.nu() + alphat_*turbulence.nut()
);
}
FatalErrorInFunction
<< "Diffusivity field requested for non-field diffusivity option '"
<< scalarTransport::diffusivityTypeNames_[diffusivity_] << "'"
<< exit(FatalError);
return tmp();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::phaseScalarTransport::phaseScalarTransport
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict),
fieldName_(dict.lookup("field")),
phaseName_(IOobject::group(fieldName_)),
D_("D", dimensions::kinematicViscosity, NaN),
Dname_(word::null),
alphal_("alphal", dimless, NaN),
alphat_("alphat", dimless, NaN),
s_
(
IOobject
(
fieldName_,
time_.name(),
mesh_,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
mesh_
),
PhiPtr_(nullptr)
{
if (phaseName_ == word::null)
{
FatalErrorInFunction
<< "Field \"" << fieldName_ << "\" does not have a phase extension "
<< "in its name. If it is associated with \"phaseA\" then it "
<< "should be named \"" << fieldName_ << ".phaseA\"."
<< exit(FatalError);
}
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::phaseScalarTransport::~phaseScalarTransport()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::phaseScalarTransport::read(const dictionary& dict)
{
fvMeshFunctionObject::read(dict);
solveAlphaPhi_ = dict.lookupOrDefault("solveAlphaPhi", false);
alphaName_ =
dict.lookupOrDefault
(
"alpha",
IOobject::groupName("alpha", phaseName_)
);
const word defaultAlphaPhiName =
IOobject::groupName("alphaPhi", phaseName_);
alphaPhiName_ =
solveAlphaPhi_
? typedName(defaultAlphaPhiName)
: dict.lookupOrDefault("alphaPhi", defaultAlphaPhiName);
phiName_ = dict.lookupOrDefault("phi", "phi");
rhoName_ =
dict.lookupOrDefault
(
"rho",
IOobject::groupName("rho", phaseName_)
);
pName_ = dict.lookupOrDefault("p", "p");
schemesField_ = dict.lookupOrDefault("schemesField", fieldName_);
solverField_ = dict.lookupOrDefault("solverField", fieldName_);
diffusivity_ =
scalarTransport::diffusivityTypeNames_.read(dict.lookup("diffusivity"));
switch(diffusivity_)
{
case scalarTransport::diffusivityType::none:
break;
case scalarTransport::diffusivityType::constant:
// This is used instead of D_.read(dict) in order to avoid
// successfully parsing a word as the name of the dimensioned type,
// and then mucking up the subsequent error message
D_.value() = dict.lookup(D_.name(), D_.dimensions());
break;
case scalarTransport::diffusivityType::field:
Dname_ = dict.lookup("D");
break;
case scalarTransport::diffusivityType::viscosity:
alphal_.read(dict);
alphat_.read(dict);
break;
}
residualAlpha_ = dict.lookupOrDefault("residualAlpha", rootSmall);
writeAlphaField_ = dict.lookupOrDefault("writeAlphaField", true);
return true;
}
Foam::wordList Foam::functionObjects::phaseScalarTransport::fields() const
{
return wordList{alphaName_, alphaPhiName_, phiName_, pName_};
}
bool Foam::functionObjects::phaseScalarTransport::execute()
{
Info<< type() << ": Executing" << endl;
const volScalarField& alpha =
mesh_.lookupObject(alphaName_);
// Get the phase flux
tmp tAlphaPhi(this->alphaPhi());
const surfaceScalarField& alphaPhi = tAlphaPhi();
const int nCorr =
mesh_.solution().solverDict(solverField_)
.lookupOrDefaultBackwardsCompatible