From e62ded842f1e6edffb5d3fab568d00e28dbca8ea Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 10 Oct 2019 16:16:24 +0100 Subject: [PATCH] functionObjects::age: Added caching of the age field to allow post-processing, e.g. sampling, cutting planes, averaging etc. --- .../regionFunctionObject.H | 6 ++- .../regionFunctionObjectTemplates.C | 39 ++++++++++++++++--- src/functionObjects/field/age/age.C | 39 +++++++++++-------- src/functionObjects/field/age/age.H | 2 +- .../simpleFoam/roomResidenceTime/system/age | 3 ++ 5 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H index 148b9909b5..96d11f1198 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H @@ -86,11 +86,15 @@ protected: template ObjectType& lookupObjectRef(const word& fieldName); + //- Store the given field in the objectRegistry + template + bool store(const tmp& tfield); + //- Store the given field in the objectRegistry under the given name template bool store ( - word& fieldName, + const word& fieldName, const tmp& tfield, bool cacheable = false ); diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C index c3fe21408b..cbb89d8ec1 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2016-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -61,7 +61,38 @@ ObjectType& Foam::functionObjects::regionFunctionObject::lookupObjectRef template bool Foam::functionObjects::regionFunctionObject::store ( - word& fieldName, + const tmp& tfield +) +{ + if (obr_.foundObject(tfield->name())) + { + ObjectType& field = obr_.lookupObjectRef(tfield->name()); + + // If there is a result field already registered assign to the new + // result field otherwise transfer ownership of the new result field to + // the object registry + if (&field != &tfield()) + { + field = tfield; + } + else + { + obr_.objectRegistry::store(tfield.ptr()); + } + } + else + { + obr_.objectRegistry::store(tfield.ptr()); + } + + return true; +} + + +template +bool Foam::functionObjects::regionFunctionObject::store +( + const word& fieldName, const tmp& tfield, bool cacheable ) @@ -104,10 +135,6 @@ bool Foam::functionObjects::regionFunctionObject::store { tfield.ref().rename(fieldName); } - else - { - fieldName = tfield().name(); - } obr_.objectRegistry::store(tfield.ptr()); } diff --git a/src/functionObjects/field/age/age.C b/src/functionObjects/field/age/age.C index 0222e7816f..65f87f08ee 100644 --- a/src/functionObjects/field/age/age.C +++ b/src/functionObjects/field/age/age.C @@ -124,26 +124,25 @@ bool Foam::functionObjects::age::read(const dictionary& dict) bool Foam::functionObjects::age::execute() { - return true; -} - - -bool Foam::functionObjects::age::write() -{ - volScalarField age + tmp tage ( - IOobject + new volScalarField ( - typeName, - mesh_.time().timeName(), + IOobject + ( + typeName, + mesh_.time().timeName(), + mesh_, + IOobject::READ_IF_PRESENT, + IOobject::AUTO_WRITE, + false + ), mesh_, - IOobject::READ_IF_PRESENT, - IOobject::AUTO_WRITE - ), - mesh_, - dimensionedScalar(dimTime, 0), - patchTypes() + dimensionedScalar(dimTime, 0), + patchTypes() + ) ); + volScalarField& age = tage.ref(); const word divScheme("div(phi," + schemesField_ + ")"); @@ -244,10 +243,16 @@ bool Foam::functionObjects::age::write() Info<< "Min/max age:" << min(age).value() << ' ' << max(age).value() << endl; - age.write(); + store(tage); return true; } +bool Foam::functionObjects::age::write() +{ + return writeObject(typeName); +} + + // ************************************************************************* // diff --git a/src/functionObjects/field/age/age.H b/src/functionObjects/field/age/age.H index f8a5088efe..d4f3fe31e5 100644 --- a/src/functionObjects/field/age/age.H +++ b/src/functionObjects/field/age/age.H @@ -56,8 +56,8 @@ Usage type age; libs ("libsolverFunctionObjects.so"); + executeControl writeTime; writeControl writeTime; - writeInterval 1; schemesField k; } diff --git a/tutorials/incompressible/simpleFoam/roomResidenceTime/system/age b/tutorials/incompressible/simpleFoam/roomResidenceTime/system/age index 605ae78f7e..336ce3a707 100644 --- a/tutorials/incompressible/simpleFoam/roomResidenceTime/system/age +++ b/tutorials/incompressible/simpleFoam/roomResidenceTime/system/age @@ -25,4 +25,7 @@ libs ("libfieldFunctionObjects.so"); diffusion true; +executeControl writeTime; +writeControl writeTime; + // ************************************************************************* //