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

View File

@ -86,8 +86,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef functionObjects_CourantNo_H
#define functionObjects_CourantNo_H
#ifndef Foam_functionObjects_CourantNo_H
#define Foam_functionObjects_CourantNo_H
#include "fieldExpression.H"
#include "volFields.H"
@ -115,12 +115,6 @@ class CourantNo
// 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
virtual bool calc();

View File

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

View File

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