STYLE: code cleanup for surfaceCourantNumber (#3422)

- simplify some handling. Use reference (not copy) for internalField
This commit is contained in:
Mark Olesen
2025-08-18 09:17:46 +02:00
parent ac1c41a51b
commit 1fa20428a8
4 changed files with 64 additions and 73 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2013-2016 OpenFOAM Foundation Copyright (C) 2013-2016 OpenFOAM Foundation
Copyright (C) 2020-2024 OpenCFD Ltd. Copyright (C) 2020-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -29,7 +29,6 @@ License
#include "CourantNo.H" #include "CourantNo.H"
#include "surfaceFields.H" #include "surfaceFields.H"
#include "fvcSurfaceIntegrate.H" #include "fvcSurfaceIntegrate.H"
#include "zeroGradientFvPatchFields.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -46,38 +45,25 @@ namespace functionObjects
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::tmp<Foam::volScalarField::Internal>
Foam::functionObjects::CourantNo::byRho
(
const tmp<volScalarField::Internal>& Co
) const
{
if (Co().dimensions() == dimDensity)
{
return Co/obr_.lookupObject<volScalarField>(rhoName_);
}
return Co;
}
bool Foam::functionObjects::CourantNo::calc() bool Foam::functionObjects::CourantNo::calc()
{ {
if (foundObject<surfaceScalarField>(fieldName_)) if (foundObject<surfaceScalarField>(fieldName_))
{ {
const surfaceScalarField& phi = const auto& phi = lookupObject<surfaceScalarField>(fieldName_);
lookupObject<surfaceScalarField>(fieldName_);
tmp<volScalarField::Internal> Coi tmp<volScalarField::Internal> tCo
( (
byRho (0.5*mesh_.time().deltaT())
( * fvc::surfaceSum(Foam::mag(phi))().internalField()
(0.5*mesh_.time().deltaT()) / mesh_.V()
*fvc::surfaceSum(mag(phi))()()
/mesh_.V()
)
); );
if (tCo().dimensions() == dimDensity)
{
tCo.ref() /= obr_.lookupObject<volScalarField>(rhoName_);
}
auto* resultPtr = getObjectPtr<volScalarField>(resultName_); auto* resultPtr = getObjectPtr<volScalarField>(resultName_);
if (!resultPtr) if (!resultPtr)
@ -99,10 +85,10 @@ bool Foam::functionObjects::CourantNo::calc()
); );
regIOobject::store(resultPtr); regIOobject::store(resultPtr);
} }
auto& Co = *resultPtr; auto& result = *resultPtr;
Co.internalFieldRef() = Coi; result.internalFieldRef() = tCo;
Co.correctBoundaryConditions(); result.correctBoundaryConditions();
return true; return true;
} }

View File

@ -86,8 +86,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef functionObjects_CourantNo_H #ifndef Foam_functionObjects_CourantNo_H
#define functionObjects_CourantNo_H #define Foam_functionObjects_CourantNo_H
#include "fieldExpression.H" #include "fieldExpression.H"
#include "volFields.H" #include "volFields.H"
@ -115,12 +115,6 @@ class CourantNo
// Private Member Functions // Private Member Functions
//- Divide the Courant number by rho if required
tmp<volScalarField::Internal> byRho
(
const tmp<volScalarField::Internal>& Co
) const;
//- Calculate the Courant number field and return true if successful //- Calculate the Courant number field and return true if successful
virtual bool calc(); virtual bool calc();

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2024 OpenCFD Ltd. Copyright (C) 2024-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,12 +26,11 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "surfaceCourantNumber.H" #include "surfaceCourantNumber.H"
#include "fvMesh.H"
#include "faMesh.H" #include "faMesh.H"
#include "fvMesh.H"
#include "areaFields.H" #include "areaFields.H"
#include "edgeFields.H" #include "edgeFields.H"
#include "facEdgeIntegrate.H" #include "facEdgeIntegrate.H"
#include "zeroGradientFaPatchFields.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -41,7 +40,12 @@ namespace Foam
namespace functionObjects namespace functionObjects
{ {
defineTypeNameAndDebug(surfaceCourantNumber, 0); defineTypeNameAndDebug(surfaceCourantNumber, 0);
addToRunTimeSelectionTable(functionObject, surfaceCourantNumber, dictionary); addToRunTimeSelectionTable
(
functionObject,
surfaceCourantNumber,
dictionary
);
} }
} }
@ -71,11 +75,12 @@ Foam::functionObjects::surfaceCourantNumber::surfaceCourantNumber
const dictionary& dict const dictionary& dict
) )
: :
fvMeshFunctionObject(name, runTime, dict), functionObjects::fvMeshFunctionObject(name, runTime, dict),
writeFile(mesh_, name, typeName, dict), writeFile(mesh_, name, typeName, dict),
resultName_("surfaceCo"), resultName_("surfaceCo"),
phisName_("phis"), phisName_("phis"),
rhoName_("rho") rhoName_("rho"),
faMeshPtr_(nullptr)
{ {
read(dict); read(dict);
} }
@ -95,9 +100,9 @@ bool Foam::functionObjects::surfaceCourantNumber::read(const dictionary& dict)
dict.readIfPresent("rho", rhoName_); dict.readIfPresent("rho", rhoName_);
// Registry containing all finite-area meshes on the polyMesh // Registry containing all finite-area meshes on the polyMesh
const auto* faRegistryPtr = faMesh::registry(mesh_); const auto* faRegistry = faMesh::registry(mesh_);
if (!faRegistryPtr) if (!faRegistry)
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "No finite-area object registry is available." << "No finite-area object registry is available."
@ -105,36 +110,33 @@ bool Foam::functionObjects::surfaceCourantNumber::read(const dictionary& dict)
} }
word areaName; word areaName;
if (!dict.readIfPresent("area", areaName)) if (!dict.readIfPresent("area", areaName))
{ {
wordList available = faRegistryPtr->sortedNames<faMesh>(); wordList available = faRegistry->sortedNames<faMesh>();
if (!available.empty()) if (!available.empty())
{ {
areaName = available.front(); areaName = available.front();
} }
} }
if (areaName.empty()) faMeshPtr_ = faRegistry->cfindObject<faMesh>(areaName);
if (!faMeshPtr_)
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "No name for finite-area mesh is available." << "No finite-area mesh available."
<< abort(FatalIOError); << abort(FatalIOError);
} }
faMeshPtr_ = std::shared_ptr<const faMesh>
(
faRegistryPtr->cfindObject<faMesh>(areaName),
[](const faMesh*) { /* no-op deleter to avoid double deletion */ }
);
return true; return true;
} }
bool Foam::functionObjects::surfaceCourantNumber::execute() bool Foam::functionObjects::surfaceCourantNumber::execute()
{ {
if (!faMeshPtr_->foundObject<edgeScalarField>(phisName_)) const auto* phiPtr = faMeshPtr_->cfindObject<edgeScalarField>(phisName_);
if (!phiPtr)
{ {
WarningInFunction WarningInFunction
<< "No edge flux field is available. " << "No edge flux field is available. "
@ -144,20 +146,21 @@ bool Foam::functionObjects::surfaceCourantNumber::execute()
return false; return false;
} }
const auto& phis = faMeshPtr_->lookupObject<edgeScalarField>(phisName_); const auto& phi = *phiPtr;
tmp<areaScalarField::Internal> tCo = tmp<areaScalarField::Internal> tCo =
(
(0.5*faMeshPtr_->time().deltaT()) (0.5*faMeshPtr_->time().deltaT())
*fac::edgeSum(mag(phis))()() * fac::edgeSum(Foam::mag(phi))().internalField()
/faMeshPtr_->S(); / faMeshPtr_->S()
);
areaScalarField::Internal Co = tCo.ref(); if (tCo().dimensions() == dimDensity)
if (Co.dimensions() == dimDensity)
{ {
Co /= faMeshPtr_->lookupObject<areaScalarField>(rhoName_); tCo.ref() /= faMeshPtr_->lookupObject<areaScalarField>(rhoName_);
} }
auto* resultPtr = faMeshPtr_->getObjectPtr<areaScalarField>(resultName_); auto* resultPtr = faMeshPtr_->getObjectPtr<areaScalarField>(resultName_);
if (!resultPtr) if (!resultPtr)
@ -168,7 +171,7 @@ bool Foam::functionObjects::surfaceCourantNumber::execute()
( (
resultName_, resultName_,
faMeshPtr_->time().timeName(), faMeshPtr_->time().timeName(),
*faMeshPtr_, *faMeshPtr_,
IOobjectOption() IOobjectOption()
), ),
*faMeshPtr_, *faMeshPtr_,
@ -183,8 +186,8 @@ bool Foam::functionObjects::surfaceCourantNumber::execute()
result.correctBoundaryConditions(); result.correctBoundaryConditions();
const scalarMinMax limits(gMinMax(result)); const scalarMinMax limits = gMinMax(result.primitiveField());
const scalar mean = gAverage(result); const scalar mean = gAverage(result.primitiveField());
Log << "Surface Courant number: " Log << "Surface Courant number: "
<< "mean: " << mean << "mean: " << mean

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2024 OpenCFD Ltd. Copyright (C) 2024-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -63,7 +63,8 @@ Usage
Property | Description | Type | Reqd | Deflt Property | Description | Type | Reqd | Deflt
type | Type name: surfaceCourantNumber | word | yes | - type | Type name: surfaceCourantNumber | word | yes | -
libs | Library name: regionFaModels | word | yes | - libs | Library name: regionFaModels | word | yes | -
area | Name of finite-area region | word | no | region0 region | Name of finite-volume region | word | no | region0
area | Name of finite-area region | word | no |
result | Name of result field | word | no | surfaceCo result | Name of result field | word | no | surfaceCo
phis | Name of edge flux field | word | no | phis phis | Name of edge flux field | word | no | phis
rho | Name of density field | word | no | rho rho | Name of density field | word | no | rho
@ -75,15 +76,16 @@ Usage
Note Note
- The \c surfaceCourantNumber calculates the Courant number at face centers, - The \c surfaceCourantNumber calculates the Courant number at face centers,
rather than at edge centers. rather than at edge centers.
- If the area region is not specified, the first region is taken
SourceFiles SourceFiles
surfaceCourantNumber.C surfaceCourantNumber.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef functionObjects_surfaceCourantNumber_H #ifndef Foam_functionObjects_surfaceCourantNumber_H
#define functionObjects_surfaceCourantNumber_H #define Foam_functionObjects_surfaceCourantNumber_H
#include "fvMeshFunctionObject.H" #include "fvMeshFunctionObject.H"
#include "writeFile.H" #include "writeFile.H"
@ -105,7 +107,7 @@ namespace functionObjects
class surfaceCourantNumber class surfaceCourantNumber
: :
public fvMeshFunctionObject, public functionObjects::fvMeshFunctionObject,
public writeFile public writeFile
{ {
// Private Data // Private Data
@ -120,7 +122,7 @@ class surfaceCourantNumber
word rhoName_; word rhoName_;
//- Reference to finite-area object registry //- Reference to finite-area object registry
std::shared_ptr<const faMesh> faMeshPtr_; const faMesh* faMeshPtr_;
// Private Member Functions // Private Member Functions
@ -145,6 +147,12 @@ public:
const dictionary& dict const dictionary& dict
); );
//- No copy construct
surfaceCourantNumber(const surfaceCourantNumber&) = delete;
//- No copy assignment
void operator=(const surfaceCourantNumber&) = delete;
//- Destructor //- Destructor
virtual ~surfaceCourantNumber() = default; virtual ~surfaceCourantNumber() = default;