fvConstraints::limitMag: Generalised replacement for limitVelocity

limitMag limits the magnitude of a specified field of any rank to a specified
maximum value.  To limit the velocity field U using limitMag rather than
limitVelocity specify

    limitU
    {
        type            limitMag;
        selectionMode   all;
        field           U;
        max             100;
    }

in the system/fvConstraints dictionary.
This commit is contained in:
Henry Weller
2022-01-25 11:21:14 +00:00
parent e5ac86bd5e
commit 01f72b4c82
4 changed files with 96 additions and 71 deletions

View File

@ -2,7 +2,7 @@ fixedValueConstraint/fixedValueConstraint.C
fixedTemperatureConstraint/fixedTemperatureConstraint.C
limitTemperature/limitTemperature.C
limitPressure/limitPressure.C
limitVelocity/limitVelocity.C
limitMag/limitMag.C
meanVelocityForce/meanVelocityForce.C
meanVelocityForce/patchMeanVelocityForce/patchMeanVelocityForce.C

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -89,7 +89,11 @@ class fixedValueConstraint
//- Set value on a field
template<class Type>
bool constrainType(fvMatrix<Type>& eqn, const word& fieldName) const;
inline bool constrainType
(
fvMatrix<Type>& eqn,
const word& fieldName
) const;
public:

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -23,7 +23,7 @@ License
\*---------------------------------------------------------------------------*/
#include "limitVelocity.H"
#include "limitMag.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"
@ -33,11 +33,11 @@ namespace Foam
{
namespace fv
{
defineTypeNameAndDebug(limitVelocity, 0);
defineTypeNameAndDebug(limitMag, 0);
addToRunTimeSelectionTable
(
fvConstraint,
limitVelocity,
limitMag,
dictionary
);
}
@ -46,45 +46,22 @@ namespace fv
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::fv::limitVelocity::readCoeffs()
void Foam::fv::limitMag::readCoeffs()
{
UName_ = coeffs().lookupOrDefault<word>("U", "U");
fieldName_ = coeffs().lookup<word>("field");
max_ = coeffs().lookup<scalar>("max");
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::limitVelocity::limitVelocity
template<class Type>
inline bool Foam::fv::limitMag::constrainType
(
const word& name,
const word& modelType,
const dictionary& dict,
const fvMesh& mesh
)
:
fvConstraint(name, modelType, dict, mesh),
set_(coeffs(), mesh),
UName_(word::null),
max_(vGreat)
GeometricField<Type, fvPatchField, volMesh>& psi
) const
{
readCoeffs();
}
const scalar maxSqrPsi = sqr(max_);
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::wordList Foam::fv::limitVelocity::constrainedFields() const
{
return wordList(1, UName_);
}
bool Foam::fv::limitVelocity::constrain(volVectorField& U) const
{
const scalar maxSqrU = sqr(max_);
vectorField& Uif = U.primitiveFieldRef();
Field<Type>& psiif = psi.primitiveFieldRef();
const labelList& cells = set_.cells();
@ -92,32 +69,33 @@ bool Foam::fv::limitVelocity::constrain(volVectorField& U) const
{
const label celli = cells[i];
const scalar magSqrUi = magSqr(Uif[celli]);
const scalar magSqrPsii = magSqr(psiif[celli]);
if (magSqrUi > maxSqrU)
if (magSqrPsii > maxSqrPsi)
{
Uif[celli] *= sqrt(maxSqrU/magSqrUi);
psiif[celli] *= sqrt(maxSqrPsi/magSqrPsii);
}
}
// handle boundaries in the case of 'all'
if (set_.selectionMode() == fvCellSet::selectionModeType::all)
{
volVectorField::Boundary& Ubf = U.boundaryFieldRef();
typename GeometricField<Type, fvPatchField, volMesh>::Boundary& psibf =
psi.boundaryFieldRef();
forAll(Ubf, patchi)
forAll(psibf, patchi)
{
fvPatchVectorField& Up = Ubf[patchi];
fvPatchField<Type>& psip = psibf[patchi];
if (!Up.fixesValue())
if (!psip.fixesValue())
{
forAll(Up, facei)
forAll(psip, facei)
{
const scalar magSqrUi = magSqr(Up[facei]);
const scalar magSqrPsii = magSqr(psip[facei]);
if (magSqrUi > maxSqrU)
if (magSqrPsii > maxSqrPsi)
{
Up[facei] *= sqrt(maxSqrU/magSqrUi);
psip[facei] *= sqrt(maxSqrPsi/magSqrPsii);
}
}
}
@ -128,26 +106,60 @@ bool Foam::fv::limitVelocity::constrain(volVectorField& U) const
}
void Foam::fv::limitVelocity::updateMesh(const mapPolyMesh& map)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::limitMag::limitMag
(
const word& name,
const word& modelType,
const dictionary& dict,
const fvMesh& mesh
)
:
fvConstraint(name, modelType, dict, mesh),
set_(coeffs(), mesh),
fieldName_(word::null),
max_(vGreat)
{
readCoeffs();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::wordList Foam::fv::limitMag::constrainedFields() const
{
return wordList(1, fieldName_);
}
FOR_ALL_FIELD_TYPES
(
IMPLEMENT_FV_CONSTRAINT_CONSTRAIN_FIELD,
fv::limitMag
);
void Foam::fv::limitMag::updateMesh(const mapPolyMesh& map)
{
set_.updateMesh(map);
}
void Foam::fv::limitVelocity::distribute(const mapDistributePolyMesh& map)
void Foam::fv::limitMag::distribute(const mapDistributePolyMesh& map)
{
set_.distribute(map);
}
bool Foam::fv::limitVelocity::movePoints()
bool Foam::fv::limitMag::movePoints()
{
set_.movePoints();
return true;
}
bool Foam::fv::limitVelocity::read(const dictionary& dict)
bool Foam::fv::limitMag::read(const dictionary& dict)
{
if (fvConstraint::read(dict))
{

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -22,31 +22,33 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::fv::limitVelocity
Foam::fv::limitMag
Description
Limits the velocity magnitude to the specified \c max value.
Limits the magnitude of the specified field to the specified \c max value.
Usage
Example usage:
\verbatim
limitU
{
type limitVelocity;
type limitMag;
selectionMode all;
field U;
max 100;
}
\endverbatim
SourceFiles
limitVelocity.C
limitMag.C
\*---------------------------------------------------------------------------*/
#ifndef limitVelocity_H
#define limitVelocity_H
#ifndef limitMag_H
#define limitMag_H
#include "fvConstraint.H"
#include "fvCellSet.H"
@ -59,10 +61,10 @@ namespace fv
{
/*---------------------------------------------------------------------------*\
Class limitVelocity Declaration
Class limitMag Declaration
\*---------------------------------------------------------------------------*/
class limitVelocity
class limitMag
:
public fvConstraint
{
@ -71,8 +73,8 @@ class limitVelocity
//- The set of cells the fvConstraint applies to
fvCellSet set_;
//- Velocity field name, default = U
word UName_;
//- Field name
word fieldName_;
//- Maximum velocity magnitude
scalar max_;
@ -83,17 +85,24 @@ class limitVelocity
//- Non-virtual read
void readCoeffs();
//- Limit the field
template<class Type>
inline bool constrainType
(
GeometricField<Type, fvPatchField, volMesh>& psi
) const;
public:
//- Runtime type information
TypeName("limitVelocity");
TypeName("limitMag");
// Constructors
//- Construct from components
limitVelocity
limitMag
(
const word& name,
const word& modelType,
@ -102,11 +111,11 @@ public:
);
//- Disallow default bitwise copy construction
limitVelocity(const limitVelocity&) = delete;
limitMag(const limitMag&) = delete;
//- Destructor
virtual ~limitVelocity()
virtual ~limitMag()
{}
@ -115,8 +124,8 @@ public:
//- Return the list of fields constrained by the fvConstraint
virtual wordList constrainedFields() const;
//- Constrain the velocity field
virtual bool constrain(volVectorField& U) const;
//- Add a constraint to an equation
FOR_ALL_FIELD_TYPES(DEFINE_FV_CONSTRAINT_CONSTRAIN_FIELD);
//- Update for mesh changes
virtual void updateMesh(const mapPolyMesh&);
@ -134,7 +143,7 @@ public:
// Member Operators
//- Disallow default bitwise assignment
void operator=(const limitVelocity&) = delete;
void operator=(const limitMag&) = delete;
};