From b30d42c391cc3ea8ba94f982503f2b458f497b28 Mon Sep 17 00:00:00 2001 From: Kutalmis Bercin Date: Sat, 20 Sep 2025 20:00:39 +0100 Subject: [PATCH] ENH: age: improve field access pattern - cache the repeated evaluations --- src/functionObjects/field/age/age.C | 110 ++++++++++++++++------------ src/functionObjects/field/age/age.H | 11 ++- 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/src/functionObjects/field/age/age.C b/src/functionObjects/field/age/age.C index d63e452b9c..c0c0957d8b 100644 --- a/src/functionObjects/field/age/age.C +++ b/src/functionObjects/field/age/age.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2018-2021 OpenFOAM Foundation - Copyright (C) 2021-2023 OpenCFD Ltd. + Copyright (C) 2021-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -53,15 +53,17 @@ namespace functionObjects Foam::wordList Foam::functionObjects::age::patchTypes() const { + const fvBoundaryMesh& patches = mesh_.boundary(); + wordList result ( - mesh_.boundary().size(), + patches.size(), inletOutletFvPatchField::typeName ); - forAll(mesh_.boundary(), patchi) + forAll(patches, patchi) { - if (isA(mesh_.boundary()[patchi])) + if (isA(patches[patchi])) { result[patchi] = fvPatchFieldBase::zeroGradientType(); } @@ -109,42 +111,67 @@ Foam::functionObjects::age::age bool Foam::functionObjects::age::read(const dictionary& dict) { - if (fvMeshFunctionObject::read(dict)) + if (!fvMeshFunctionObject::read(dict)) { - phiName_ = dict.getOrDefault("phi", "phi"); - rhoName_ = dict.getOrDefault("rho", "rho"); - schemesField_ = dict.getOrDefault("schemesField", typeName); - tolerance_ = dict.getOrDefault("tolerance", 1e-5); - nCorr_ = dict.getOrDefault("nCorr", 5); - diffusion_ = dict.getOrDefault("diffusion", false); - - return true; + return false; } - return false; + phiName_ = dict.getOrDefault("phi", "phi"); + rhoName_ = dict.getOrDefault("rho", "rho"); + schemesField_ = dict.getOrDefault("schemesField", typeName); + tolerance_ = dict.getOrDefault("tolerance", 1e-5); + nCorr_ = dict.getOrDefault("nCorr", 5); + diffusion_ = dict.getOrDefault("diffusion", false); + + + // Detect if compressible or incompressible + const auto& phi = mesh_.lookupObject(phiName_); + isCompressible_ = (phi.dimensions() == dimMass/dimTime); + + // Store the divergence scheme + divScheme_ = word("div(phi," + schemesField_ + ")"); + + // Store the Laplacian scheme + if (diffusion_) + { + if (isCompressible_) + { + laplacianScheme_ = "laplacian(muEff," + schemesField_ + ")"; + } + else + { + laplacianScheme_ = "laplacian(nuEff," + schemesField_ + ")"; + } + } + + return true; } bool Foam::functionObjects::age::execute() { - auto tage = tmp::New - ( - IOobject + auto* agePtr = getObjectPtr(typeName); + if (!agePtr) + { + agePtr = new volScalarField ( - typeName, - mesh_.time().timeName(), + IOobject + ( + typeName, + obr().time().timeName(), + obr(), + IOobject::READ_IF_PRESENT, + IOobject::AUTO_WRITE, + IOobject::NO_REGISTER + ), mesh_, - IOobject::READ_IF_PRESENT, - IOobject::AUTO_WRITE, - IOobject::NO_REGISTER - ), - mesh_, - dimensionedScalar(dimTime, Zero), - patchTypes() - ); - volScalarField& age = tage.ref(); + dimensionedScalar(dimTime, Zero), + patchTypes() + ); + regIOobject::store(agePtr); + } + auto& age = *agePtr; - const word divScheme("div(phi," + schemesField_ + ")"); // Set under-relaxation coeff scalar relaxCoeff = 0; @@ -161,12 +188,11 @@ bool Foam::functionObjects::age::execute() const auto& phi = mesh_.lookupObject(phiName_); - if (phi.dimensions() == dimMass/dimTime) + if (isCompressible_) { const auto& rho = mesh_.lookupObject(rhoName_); tmp tmuEff; - word laplacianScheme; if (diffusion_) { @@ -175,21 +201,18 @@ bool Foam::functionObjects::age::execute() ( turbulenceModel::propertiesName ).muEff(); - - laplacianScheme = - "laplacian(" + tmuEff().name() + ',' + schemesField_ + ")"; } for (int i = 0; i <= nCorr_; ++i) { fvScalarMatrix ageEqn ( - fvm::div(phi, age, divScheme) == rho //+ fvOptions(rho, age) + fvm::div(phi, age, divScheme_) == rho //+ fvOptions(rho, age) ); if (diffusion_) { - ageEqn -= fvm::laplacian(tmuEff(), age, laplacianScheme); + ageEqn -= fvm::laplacian(tmuEff(), age, laplacianScheme_); } ageEqn.relax(relaxCoeff); @@ -199,7 +222,7 @@ bool Foam::functionObjects::age::execute() if (converged(i, ageEqn.solve().initialResidual())) { break; - }; + } fvOptions.correct(age); } @@ -207,7 +230,6 @@ bool Foam::functionObjects::age::execute() else { tmp tnuEff; - word laplacianScheme; if (diffusion_) { @@ -216,22 +238,19 @@ bool Foam::functionObjects::age::execute() ( turbulenceModel::propertiesName ).nuEff(); - - laplacianScheme = - "laplacian(" + tnuEff().name() + ',' + schemesField_ + ")"; } for (int i = 0; i <= nCorr_; ++i) { fvScalarMatrix ageEqn ( - fvm::div(phi, age, divScheme) + fvm::div(phi, age, divScheme_) == dimensionedScalar(1) + fvOptions(age) ); if (diffusion_) { - ageEqn -= fvm::laplacian(tnuEff(), age, laplacianScheme); + ageEqn -= fvm::laplacian(tnuEff(), age, laplacianScheme_); } ageEqn.relax(relaxCoeff); @@ -252,10 +271,7 @@ bool Foam::functionObjects::age::execute() << max(age).value() << endl; - // Workaround - word fieldName = typeName; - - return store(fieldName, tage); + return true; } diff --git a/src/functionObjects/field/age/age.H b/src/functionObjects/field/age/age.H index daba0c401d..ccc3596823 100644 --- a/src/functionObjects/field/age/age.H +++ b/src/functionObjects/field/age/age.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2018-2021 OpenFOAM Foundation - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -123,6 +123,12 @@ class age //- Name of field from which schemes are taken word schemesField_; + //- Name of the divergence scheme + word divScheme_; + + //- Name of the Laplacian scheme + word laplacianScheme_; + //- Convergence tolerance scalar tolerance_; @@ -132,6 +138,9 @@ class age //- Flag to turn on/off the diffusion term bool diffusion_; + //- Flag that indicates if flux is mass-based or volume-based + bool isCompressible_; + // Private Member Functions