/*---------------------------------------------------------------------------*\
========= |
\\ / 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 .
\*---------------------------------------------------------------------------*/
#include "zeroDimensionalFixedPressureConstraint.H"
#include "zeroDimensionalFixedPressureModel.H"
#include "fvModels.H"
#include "fvMatrix.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace fv
{
defineTypeNameAndDebug(zeroDimensionalFixedPressureConstraint, 0);
addToRunTimeSelectionTable
(
fvConstraint,
zeroDimensionalFixedPressureConstraint,
dictionary
);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
const Foam::fv::zeroDimensionalFixedPressureModel&
Foam::fv::zeroDimensionalFixedPressureConstraint::model() const
{
const fvModels& models = fvModels::New(mesh());
forAll(models, i)
{
if (isA(models[i]))
{
return refCast
(
models[i]
);
}
}
FatalErrorInFunction
<< "The " << typeName << " fvConstraint requires a corresponding "
<< zeroDimensionalFixedPressureModel::typeName << " fvModel"
<< exit(FatalError);
return NullObjectRef();
}
template
Foam::tmp
Foam::fv::zeroDimensionalFixedPressureConstraint::massSource
(
const AlphaFieldType& alpha,
const volInternalScalarField& rho
) const
{
// Source does not exist yet. Return zero.
if (!sourcePtr_.valid())
{
return
volInternalScalarField::New
(
typedName("source"),
mesh(),
dimensionedScalar(dimensions::density/dimensions::time, 0)
);
}
// Source for mass-based pressure equations
if (sourcePtr_->dimensions() == dimensions::density/dimensions::time)
{
return alpha*sourcePtr_();
}
// Source for volume-based pressure equations
if (sourcePtr_->dimensions() == dimensions::rate)
{
return alpha*rho*sourcePtr_();
}
FatalErrorInFunction
<< "Pressure equation dimensions not recognised"
<< exit(FatalError);
return tmp(nullptr);
}
void Foam::fv::zeroDimensionalFixedPressureConstraint::readCoeffs
(
const dictionary& dict
)
{
pName_ = dict.lookupOrDefault("p", "p");
rhoName_ = dict.lookupOrDefault("rho", "rho");
p_.reset
(
Function1::New
(
"pressure",
mesh().time().userUnits(),
dimensions::pressure,
dict
).ptr()
);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::zeroDimensionalFixedPressureConstraint::
zeroDimensionalFixedPressureConstraint
(
const word& name,
const word& modelType,
const fvMesh& mesh,
const dictionary& dict
)
:
fvConstraint(name, modelType, mesh, dict),
pName_(word::null),
rhoName_(word::null),
p_(nullptr),
sourcePtr_(nullptr)
{
if (mesh.nGeometricD() != 0)
{
FatalIOErrorInFunction(dict)
<< "Zero-dimensional fvConstraint applied to a "
<< mesh.nGeometricD() << "-dimensional mesh"
<< exit(FatalIOError);
}
readCoeffs(coeffs(dict));
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fv::zeroDimensionalFixedPressureConstraint::
~zeroDimensionalFixedPressureConstraint()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::wordList
Foam::fv::zeroDimensionalFixedPressureConstraint::constrainedFields() const
{
return wordList(1, pName_);
}
Foam::tmp
Foam::fv::zeroDimensionalFixedPressureConstraint::pEqnSource
(
const volScalarField& rho,
fvMatrix& pEqn
) const
{
// Ensure the corresponding fvModel exits
model();
// Return zero if the source does not yet exist
if (!sourcePtr_.valid())
{
return
volInternalScalarField::New
(
typedName("source"),
mesh(),
dimensionedScalar(pEqn.dimensions()/dimensions::volume, 0)
);
}
// Return the source, multiplying by density if needed
if (sourcePtr_->dimensions() == pEqn.dimensions()/dimensions::volume)
{
return sourcePtr_();
}
else if (sourcePtr_->dimensions() == pEqn.dimensions()/dimensions::mass)
{
return rho()*sourcePtr_();
}
else
{
FatalErrorInFunction
<< "Dimensions of equation for pressure "
<< pEqn.psi().name() << " not recognised"
<< exit(FatalError);
return tmp(nullptr);
}
}
Foam::tmp
Foam::fv::zeroDimensionalFixedPressureConstraint::massSource
(
const volInternalScalarField& rho
) const
{
return massSource(geometricOneField(), rho);
}
Foam::tmp
Foam::fv::zeroDimensionalFixedPressureConstraint::massSource
(
const volInternalScalarField& alpha,
const volInternalScalarField& rho
) const
{
return massSource(alpha, rho);
}
bool Foam::fv::zeroDimensionalFixedPressureConstraint::constrain
(
fvMatrix& pEqn,
const word& fieldName
) const
{
// Create the source field if it does not already exist
if (!sourcePtr_.valid())
{
sourcePtr_.set
(
new volInternalScalarField
(
IOobject
(
typedName("source"),
mesh().time().name(),
mesh(),
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE
),
mesh(),
dimensionedScalar(pEqn.dimensions()/dimensions::volume, 0)
)
);
}
// Remove the previous iteration's source from the pressure equation
pEqn += sourcePtr_();
// Set the source as the residual of the pressure equation when evaluated
// at the desired pressure
sourcePtr_() =
pEqn
& volInternalScalarField::New
(
"p",
mesh(),
dimensionedScalar
(
dimensions::pressure,
p_->value(mesh().time().value())
)
);
// Add the source to the pressure equation to force the pressure towards
// the desired value
pEqn -= sourcePtr_();
return true;
}
bool Foam::fv::zeroDimensionalFixedPressureConstraint::movePoints()
{
return true;
}
void Foam::fv::zeroDimensionalFixedPressureConstraint::topoChange
(
const polyTopoChangeMap& map
)
{}
void Foam::fv::zeroDimensionalFixedPressureConstraint::mapMesh
(
const polyMeshMap& map
)
{}
void Foam::fv::zeroDimensionalFixedPressureConstraint::distribute
(
const polyDistributionMap& map
)
{}
bool Foam::fv::zeroDimensionalFixedPressureConstraint::read
(
const dictionary& dict
)
{
if (fvConstraint::read(dict))
{
readCoeffs(coeffs(dict));
return true;
}
else
{
return false;
}
}
// ************************************************************************* //