ENH: DimensionedField, GeometricField enhancements and style changes

- constCast() convenience methods.
  This simplies coding in places, especially in solvers

- inline some methods within headers, for better overview/maintenance

- only set DimensionedField default value on construct if not read

- storeOldTimes() does not require const_cast
This commit is contained in:
Mark Olesen
2025-05-20 07:59:23 +02:00
parent ae3adc1007
commit 4e513cec18
31 changed files with 96 additions and 197 deletions

View File

@ -135,7 +135,7 @@ public:
virtual volScalarField& he()
{
NotImplemented;
return const_cast<volScalarField&>(volScalarField::null());
return volScalarField::null().constCast();
}
//- Enthalpy/Internal energy [J/kg]

View File

@ -243,7 +243,7 @@ public:
virtual volScalarField& he()
{
NotImplemented;
return const_cast<volScalarField&>(volScalarField::null());
return volScalarField::null().constCast();
}
//- Enthalpy/Internal energy [J/kg]

View File

@ -86,7 +86,7 @@ public:
virtual volScalarField& he()
{
NotImplemented;
return const_cast<volScalarField&>(volScalarField::null());
return volScalarField::null().constCast();
}
//- Return access to the internal energy field [J/Kg]

View File

@ -79,8 +79,7 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
regIOobject(io),
Field<Type>(field),
mesh_(mesh),
dimensions_(dims),
oriented_()
dimensions_(dims)
{
checkFieldSize();
}
@ -134,8 +133,7 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
regIOobject(io),
Field<Type>(tfield.constCast(), tfield.movable()),
mesh_(mesh),
dimensions_(dims),
oriented_()
dimensions_(dims)
{
tfield.clear();
checkFieldSize();
@ -154,8 +152,7 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
regIOobject(io),
Field<Type>(GeoMesh::size(mesh)),
mesh_(mesh),
dimensions_(dims),
oriented_()
dimensions_(dims)
{
if (checkIOFlags)
{
@ -175,14 +172,14 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
)
:
regIOobject(io),
Field<Type>(GeoMesh::size(mesh), value),
Field<Type>(GeoMesh::size(mesh)),
mesh_(mesh),
dimensions_(dims),
oriented_()
dimensions_(dims)
{
if (checkIOFlags)
if (!checkIOFlags || !readIfPresent())
{
readIfPresent();
// Set default value (if not read)
this->field() = value;
}
}

View File

@ -83,6 +83,9 @@ public:
// Public Typedefs
//- The template instance used for this DimensionedField type
typedef DimensionedField<Type, GeoMesh> this_type;
//- The mesh type for the DimensionedField
typedef typename GeoMesh::Mesh Mesh;
@ -116,7 +119,7 @@ private:
void checkFieldSize() const;
//- Read from file if it is present. Checks the readOption flag
void readIfPresent(const word& fieldDictEntry = "value");
bool readIfPresent(const word& fieldDictEntry = "value");
//- Read the field - create the field dictionary on-the-fly
void readField(const word& fieldDictEntry = "value");
@ -509,20 +512,26 @@ public:
const word& fieldDictEntry = "value"
);
//- Return mesh
inline const Mesh& mesh() const noexcept;
//- Return non-const reference to this field
this_type& constCast() const noexcept
{
return const_cast<this_type&>(*this);
}
//- Return const reference to mesh
const Mesh& mesh() const noexcept { return mesh_; }
//- Return dimensions
inline const dimensionSet& dimensions() const noexcept;
const dimensionSet& dimensions() const noexcept { return dimensions_; }
//- Return non-const access to dimensions
inline dimensionSet& dimensions() noexcept;
dimensionSet& dimensions() noexcept { return dimensions_; }
//- Return oriented type
inline orientedType oriented() const noexcept;
orientedType oriented() const noexcept { return oriented_; }
//- Return non-const access to the oriented type
inline orientedType& oriented() noexcept;
orientedType& oriented() noexcept { return oriented_; }
//- True if field is ORIENTED
inline bool is_oriented() const noexcept;
@ -530,11 +539,11 @@ public:
//- Set the oriented flag: on/off
inline void setOriented(bool on = true) noexcept;
//- Return const-reference to the field values
inline const Field<Type>& field() const noexcept;
//- Return const-reference to the primitive field values
const Field<Type>& field() const noexcept { return *this; }
//- Return reference to the field values
inline Field<Type>& field() noexcept;
//- Return reference to the primitive field values
Field<Type>& field() noexcept { return *this; }
//- Return a component field of the field
tmp<DimensionedField<cmptType, GeoMesh>> component

View File

@ -28,46 +28,6 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type, class GeoMesh>
inline const typename GeoMesh::Mesh&
Foam::DimensionedField<Type, GeoMesh>::mesh() const noexcept
{
return mesh_;
}
template<class Type, class GeoMesh>
inline const Foam::dimensionSet&
Foam::DimensionedField<Type, GeoMesh>::dimensions() const noexcept
{
return dimensions_;
}
template<class Type, class GeoMesh>
inline Foam::dimensionSet&
Foam::DimensionedField<Type, GeoMesh>::dimensions() noexcept
{
return dimensions_;
}
template<class Type, class GeoMesh>
inline Foam::orientedType
Foam::DimensionedField<Type, GeoMesh>::oriented() const noexcept
{
return oriented_;
}
template<class Type, class GeoMesh>
inline Foam::orientedType&
Foam::DimensionedField<Type, GeoMesh>::oriented() noexcept
{
return oriented_;
}
template<class Type, class GeoMesh>
inline bool Foam::DimensionedField<Type, GeoMesh>::is_oriented() const noexcept
{
@ -82,20 +42,4 @@ inline void Foam::DimensionedField<Type, GeoMesh>::setOriented(bool on) noexcept
}
template<class Type, class GeoMesh>
inline const Foam::Field<Type>&
Foam::DimensionedField<Type, GeoMesh>::field() const noexcept
{
return *this;
}
template<class Type, class GeoMesh>
inline Foam::Field<Type>&
Foam::DimensionedField<Type, GeoMesh>::field() noexcept
{
return *this;
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -90,7 +90,7 @@ void Foam::DimensionedField<Type, GeoMesh>::readField
template<class Type, class GeoMesh>
void Foam::DimensionedField<Type, GeoMesh>::readIfPresent
bool Foam::DimensionedField<Type, GeoMesh>::readIfPresent
(
const word& fieldDictEntry
)
@ -102,7 +102,10 @@ void Foam::DimensionedField<Type, GeoMesh>::readIfPresent
)
{
readField(fieldDictEntry);
return true;
}
return false;
}
@ -117,10 +120,7 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
)
:
regIOobject(io),
Field<Type>(),
mesh_(mesh),
dimensions_(dimless),
oriented_()
mesh_(mesh)
{
readField(fieldDictEntry);
}
@ -136,10 +136,7 @@ Foam::DimensionedField<Type, GeoMesh>::DimensionedField
)
:
regIOobject(io),
Field<Type>(),
mesh_(mesh),
dimensions_(dimless),
oriented_()
mesh_(mesh)
{
readField(fieldDict, fieldDictEntry);
}

View File

@ -62,6 +62,9 @@ public:
// Public Typedefs
//- The template instance used for this GeometricBoundaryField type
typedef GeometricBoundaryField<Type, PatchField, GeoMesh> this_type;
//- The boundary mesh type for the boundary fields
typedef typename GeoMesh::BoundaryMesh BoundaryMesh;
@ -173,6 +176,12 @@ public:
// Member Functions
//- Return non-const reference to this boundary field
this_type& constCast() const noexcept
{
return const_cast<this_type&>(*this);
}
//- Read the boundary field
void readField(const Internal& iField, const dictionary& dict);

View File

@ -81,6 +81,9 @@ public:
// Public Typedefs
//- The template instance used for this GeometricField type
typedef GeometricField<Type, PatchField, GeoMesh> this_type;
//- The mesh type for the GeometricField
typedef typename GeoMesh::Mesh Mesh;
@ -102,12 +105,6 @@ public:
private:
// Private Typedefs
//- The GeometricField type
typedef GeometricField<Type, PatchField, GeoMesh> this_type;
// Private Data
//- Current time index.
@ -794,6 +791,12 @@ public:
// Member Functions
//- Return non-const reference to this field
this_type& constCast() const noexcept
{
return const_cast<this_type&>(*this);
}
//- Return a const-reference to the dimensioned internal field.
inline const Internal& internalField() const noexcept;
@ -841,11 +844,11 @@ public:
// \note Should avoid using updateAccessTime = true within loops.
Boundary& boundaryFieldRef(const bool updateAccessTime = true);
//- Return the time index of the field
inline label timeIndex() const noexcept;
//- The time index of the field
label timeIndex() const noexcept { return timeIndex_; }
//- Write-access to the time index of the field
inline label& timeIndex() noexcept;
label& timeIndex() noexcept { return timeIndex_; }
//- The number of old time fields stored
label nOldTimes() const noexcept;

View File

@ -55,20 +55,4 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryField() const noexcept
}
template<class Type, template<class> class PatchField, class GeoMesh>
inline Foam::label
Foam::GeometricField<Type, PatchField, GeoMesh>::timeIndex() const noexcept
{
return timeIndex_;
}
template<class Type, template<class> class PatchField, class GeoMesh>
inline Foam::label&
Foam::GeometricField<Type, PatchField, GeoMesh>::timeIndex() noexcept
{
return timeIndex_;
}
// ************************************************************************* //

View File

@ -92,13 +92,13 @@ void MapGeometricFields
// the pointMesh instead of now on the polyMesh!
if (&field.mesh() == &mapper.mesh())
{
const_cast<FieldType&>(field).storeOldTimes();
field.storeOldTimes();
}
}
for (const auto& field : fields)
{
FieldType& fld = const_cast<FieldType&>(field);
auto& fld = field.constCast();
if (&field.mesh() == &mapper.mesh())
{

View File

@ -100,7 +100,7 @@ Foam::UniformDimensionedField<Type>::UniformDimensionedField
)
:
regIOobject(io),
dimensioned<Type>(regIOobject::name(), dimless, Zero)
dimensioned<Type>(regIOobject::name(), dimless, Foam::zero{})
{
// For if MUST_READ_IF_MODIFIED
addWatch();

View File

@ -131,8 +131,7 @@ tmp<volScalarField>
singleStepCombustion<ReactionThermo, ThermoType>::Qdot() const
{
const label fuelI = singleMixturePtr_->fuelIndex();
volScalarField& YFuel =
const_cast<volScalarField&>(this->thermo().composition().Y(fuelI));
volScalarField& YFuel = this->thermo().composition().Y(fuelI).constCast();
return -singleMixturePtr_->qFuel()*(R(YFuel) & YFuel);
}

View File

@ -288,13 +288,13 @@ void Foam::fvMeshAdder::MapVolFields
<< "MapVolFields : Storing old time for "
<< field.name() << endl;
const_cast<fldType&>(field).storeOldTimes();
field.storeOldTimes();
}
for (const auto& field : fields)
{
fldType& fld = const_cast<fldType&>(field);
fldType& fld = field.constCast();
const auto* toAdd =
meshToAdd.objectRegistry::cfindObject<fldType>(field.name());
@ -592,12 +592,12 @@ void Foam::fvMeshAdder::MapSurfaceFields
<< "MapSurfaceFields : Storing old time for "
<< field.name() << endl;
const_cast<fldType&>(field).storeOldTimes();
field.storeOldTimes();
}
for (const auto& field : fields)
{
fldType& fld = const_cast<fldType&>(field);
fldType& fld = field.constCast();
const auto* toAdd =
meshToAdd.objectRegistry::cfindObject<fldType>(field.name());
@ -1114,7 +1114,7 @@ void Foam::fvMeshAdder::MapVolFields
<< "MapVolFields : Storing old time for "
<< field.name() << endl;
const_cast<fldType&>(field).storeOldTimes();
field.storeOldTimes();
}
@ -1240,7 +1240,7 @@ void Foam::fvMeshAdder::MapSurfaceFields
<< "MapSurfaceFields : Storing old time for "
<< field.name() << endl;
const_cast<fldType&>(field).storeOldTimes();
field.storeOldTimes();
}

View File

@ -213,8 +213,7 @@ Foam::faMatrix<Type>::faMatrix
}
// Update the boundary coefficients of psi without changing its event No.
auto& psiRef =
const_cast<GeometricField<Type, faPatchField, areaMesh>&>(psi_);
auto& psiRef = psi_.constCast();
const label currentStatePsi = psiRef.eventNo();
psiRef.boundaryFieldRef().updateCoeffs();
@ -313,11 +312,7 @@ void Foam::faMatrix<Type>::setValuesFromList
const labelUList& nei = mesh.neighbour();
scalarField& Diag = diag();
Field<Type>& psi =
const_cast
<
GeometricField<Type, faPatchField, areaMesh>&
>(psi_).primitiveFieldRef();
Field<Type>& psi = psi_.constCast().primitiveFieldRef();
// Following actions:
// - adjust local field psi

View File

@ -67,8 +67,7 @@ Foam::SolverPerformance<Type> Foam::faMatrix<Type>::solve
SolverPerformance<Type>::debug
);
auto& psi =
const_cast<GeometricField<Type, faPatchField, areaMesh>&>(psi_);
auto& psi = psi_.constCast();
SolverPerformance<Type> solverPerfVec
(

View File

@ -67,8 +67,7 @@ Foam::solverPerformance Foam::faMatrix<Foam::scalar>::solve
solverPerformance::debug
);
auto& psi =
const_cast<GeometricField<scalar, faPatchField, areaMesh>&>(psi_);
auto& psi = psi_.constCast();
scalarField saveDiag(diag());
addBoundaryDiag(diag(), 0);

View File

@ -218,10 +218,7 @@ void Foam::mappedVelocityFluxFixedValueFvPatchField::updateCoeffs()
}
operator==(newUValues);
const_cast<surfaceScalarField&>
(
phiField
).boundaryFieldRef()[patch().index()] == newPhiValues;
phiField.constCast().boundaryFieldRef()[patch().index()] == newPhiValues;
UPstream::msgType(oldTag); // Restore tag

View File

@ -150,10 +150,7 @@ void Foam::matchedFlowRateOutletVelocityFvPatchVectorField::updateValues
// Lookup non-const access to velocity field
volVectorField& U
(
const_cast<volVectorField&>
(
dynamic_cast<const volVectorField&>(internalField())
)
dynamic_cast<const volVectorField&>(internalField()).constCast()
);
// Get the corresponding inlet velocity patch field

View File

@ -136,8 +136,7 @@ Foam::SolverPerformance<Type> Foam::fvMatrix<Type>::solveSegregated
SolverPerformance<Type>::debug
);
auto& psi =
const_cast<GeometricField<Type, fvPatchField, volMesh>&>(psi_);
auto& psi = psi_.constCast();
SolverPerformance<Type> solverPerfVec
(
@ -269,8 +268,7 @@ Foam::SolverPerformance<Type> Foam::fvMatrix<Type>::solveCoupled
SolverPerformance<Type>::debug
);
auto& psi =
const_cast<GeometricField<Type, fvPatchField, volMesh>&>(psi_);
auto& psi = psi_.constCast();
LduMatrix<Type, scalar, scalar> coupledMatrix(psi.mesh());
coupledMatrix.diag() = diag();

View File

@ -125,11 +125,7 @@ Foam::solverPerformance Foam::fvMatrix<Foam::scalar>::fvSolver::solve
solverPerformance::debug
);
auto& psi =
const_cast<GeometricField<scalar, fvPatchField, volMesh>&>
(
fvMat_.psi()
);
auto& psi = fvMat_.psi().constCast();
scalarField saveDiag(fvMat_.diag());
fvMat_.addBoundaryDiag(fvMat_.diag(), 0);
@ -227,13 +223,7 @@ Foam::solverPerformance Foam::fvMatrix<Foam::scalar>::solveSegregated
tmp<scalarField> tpsi;
if (!useImplicit_)
{
tpsi.ref
(
const_cast<GeometricField<scalar, fvPatchField, volMesh>&>
(
psi_
).primitiveFieldRef()
);
tpsi.ref(psi_.constCast().primitiveFieldRef());
}
else
{
@ -269,10 +259,7 @@ Foam::solverPerformance Foam::fvMatrix<Foam::scalar>::solveSegregated
for (label fieldi = 0; fieldi < nMatrices(); fieldi++)
{
auto& psiInternal =
const_cast<GeometricField<scalar, fvPatchField, volMesh>&>
(
this->psi(fieldi)
).primitiveFieldRef();
this->psi(fieldi).constCast().primitiveFieldRef();
const label cellOffset = lduMeshPtr()->cellOffsets()[fieldi];
@ -310,11 +297,7 @@ Foam::solverPerformance Foam::fvMatrix<Foam::scalar>::solveSegregated
for (label fieldi = 0; fieldi < nMatrices(); fieldi++)
{
auto& localPsi =
const_cast<GeometricField<scalar, fvPatchField, volMesh>&>
(
this->psi(fieldi)
);
auto& localPsi = this->psi(fieldi).constCast();
localPsi.correctBoundaryConditions();
localPsi.mesh().data().setSolverPerformance

View File

@ -69,7 +69,7 @@ Foam::patchDistMethods::Poisson::Poisson
bool Foam::patchDistMethods::Poisson::correct(volScalarField& y)
{
return correct(y, const_cast<volVectorField&>(volVectorField::null()));
return correct(y, volVectorField::null().constCast());
}

View File

@ -87,7 +87,7 @@ Foam::patchDistMethods::advectionDiffusion::advectionDiffusion
bool Foam::patchDistMethods::advectionDiffusion::correct(volScalarField& y)
{
return correct(y, const_cast<volVectorField&>(volVectorField::null()));
return correct(y, volVectorField::null().constCast());
}

View File

@ -73,7 +73,7 @@ Foam::patchDistMethods::meshWaveAddressing::meshWaveAddressing
bool Foam::patchDistMethods::meshWaveAddressing::correct(volScalarField& y)
{
return correct(y, const_cast<volVectorField&>(volVectorField::null()));
return correct(y, volVectorField::null().constCast());
}

View File

@ -63,10 +63,7 @@ SpalartAllmaras::SpalartAllmaras
// The wall dist name can vary depending on how wallDist was
// constructed. Grab the field directly from wallDist
distPtr_.ref
(
const_cast<volScalarField&>(wallDist::New(mesh_).y())
);
distPtr_.ref(wallDist::New(mesh_).y().constCast());
allocateInitValues();
allocateMeanFields();

View File

@ -91,7 +91,7 @@ kOmegaSST::kOmegaSST
TMVar2Ptr_.ref(mesh_.lookupObjectRef<volScalarField>(TMVar2BaseName_));
nutPtr_.ref(mesh_.lookupObjectRef<volScalarField>(nutBaseName_));
distPtr_.ref(const_cast<volScalarField&>(wallDist::New(mesh_).y()));
distPtr_.ref(wallDist::New(mesh_).y().constCast());
allocateInitValues();
allocateMeanFields();

View File

@ -367,10 +367,8 @@ void Foam::oversetFvMeshBase::addInterpolation
// 1c. Adapt field for additional interfaceFields (note: solver uses
// GeometricField::scalarInterfaces() to get hold of interfaces)
typedef GeometricField<Type, fvPatchField, volMesh> GeoField;
typename GeoField::Boundary& bfld =
const_cast<GeoField&>(m.psi()).boundaryFieldRef();
auto& bfld = m.psi().constCast().boundaryFieldRef();
bfld.setSize(interfaces.size());
@ -717,8 +715,7 @@ Foam::SolverPerformance<Type> Foam::oversetFvMeshBase::solveOverset
{
typedef GeometricField<Type, fvPatchField, volMesh> GeoField;
// Check we're running with bcs that can handle implicit matrix manipulation
typename GeoField::Boundary& bpsi =
const_cast<GeoField&>(m.psi()).boundaryFieldRef();
auto& bpsi = m.psi().constCast().boundaryFieldRef();
bool hasOverset = false;
forAll(bpsi, patchi)
@ -834,8 +831,7 @@ Foam::SolverPerformance<Type> Foam::oversetFvMeshBase::solveOverset
const labelList& own = mesh_.faceOwner();
const labelList& nei = mesh_.faceNeighbour();
auto& psi =
const_cast<GeometricField<Type, fvPatchField, volMesh>&>(m.psi());
auto& psi = m.psi().constCast();
// Mirror hole cell values next to calculated cells
for (label facei = 0; facei < mesh_.nInternalFaces(); facei++)

View File

@ -148,7 +148,7 @@ Foam::patchDistMethods::exact::exact
bool Foam::patchDistMethods::exact::correct(volScalarField& y)
{
return correct(y, const_cast<volVectorField&>(volVectorField::null()));
return correct(y, volVectorField::null().constCast());
}

View File

@ -282,7 +282,7 @@ public:
virtual volScalarField& he()
{
NotImplemented;
return const_cast<volScalarField&>(volScalarField::null());
return volScalarField::null().constCast();
}
//- Return access to the internal energy field [J/Kg]

View File

@ -154,7 +154,7 @@ Foam::StationaryPhaseModel<BasePhaseModel>::URef()
<< "Cannot access the velocity of a stationary phase"
<< exit(FatalError);
return const_cast<volVectorField&>(volVectorField::null());
return volVectorField::null().constCast();
}
@ -174,7 +174,7 @@ Foam::StationaryPhaseModel<BasePhaseModel>::phiRef()
<< "Cannot access the flux of a stationary phase"
<< exit(FatalError);
return const_cast<surfaceScalarField&>(surfaceScalarField::null());
return surfaceScalarField::null().constCast();
}
@ -194,7 +194,7 @@ Foam::StationaryPhaseModel<BasePhaseModel>::alphaPhiRef()
<< "Cannot access the volumetric flux of a stationary phase"
<< exit(FatalError);
return const_cast<surfaceScalarField&>(surfaceScalarField::null());
return surfaceScalarField::null().constCast();
}
@ -214,7 +214,7 @@ Foam::StationaryPhaseModel<BasePhaseModel>::alphaRhoPhiRef()
<< "Cannot access the mass flux of a stationary phase"
<< exit(FatalError);
return const_cast<surfaceScalarField&>(surfaceScalarField::null());
return surfaceScalarField::null().constCast();
}

View File

@ -67,11 +67,7 @@ Foam::DimensionedField<Foam::scalar, Foam::volMesh>&
Foam::basicSolidChemistryModel::RR(const label i)
{
NotImplemented;
return
const_cast<volScalarField::Internal&>
(
volScalarField::Internal::null()
);
return volScalarField::Internal::null().constCast();
}