From 30cceb42c02fb98c9d01dbb4ed53fc652ead5395 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Sun, 1 Sep 2019 10:18:45 +0100 Subject: [PATCH] objectRegistry: Corrected caching of registered temporary objects For example the generation term in the k-epsilon turbulence kEpsilon:G is a temporary field that is specifically named and registered so that it can be looked-up be the wall-function boundary conditions and requires slightly different handling compared to normal temporary fields which are not registered. The tutorials/incompressible/simpleFoam/pitzDaily case now demostrates this functionality with the addition of cacheTemporaryObjects ( kEpsilon:G ); functions { #includeFunc writeObjects(objects = (kEpsilon:G)) } in controlDict which caches kEpsilon:G and writes it at every write time. --- .../db/objectRegistry/objectRegistry.C | 37 +++++++++++++++++++ .../db/objectRegistry/objectRegistry.H | 4 ++ .../objectRegistry/objectRegistryTemplates.C | 5 +-- .../simpleFoam/pitzDaily/system/controlDict | 10 +++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index 729fa9b957..15eafc4631 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -82,6 +82,15 @@ void Foam::objectRegistry::readCacheTemporaryObjects() const } +void Foam::objectRegistry::deleteCachedObject(regIOobject& cachedOb) const +{ + cachedOb.release(); + cachedOb.checkOut(); + cachedOb.rename(cachedOb.name() + "Cached"); + delete &cachedOb; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::objectRegistry::objectRegistry @@ -255,6 +264,34 @@ bool Foam::objectRegistry::checkIn(regIOobject& io) const << endl; } + // Delete cached object with the same name as io and if it is in the + // cacheTemporaryObjects list + if (cacheTemporaryObjects_.size()) + { + HashTable>::iterator cacheIter + ( + cacheTemporaryObjects_.find(io.name()) + ); + + if (cacheIter != cacheTemporaryObjects_.end()) + { + iterator iter = const_cast(*this).find(io.name()); + + if (iter != end() && iter() != &io && iter()->ownedByRegistry()) + { + if (objectRegistry::debug) + { + Pout<< "objectRegistry::checkIn(regIOobject&) : " + << name() << " : deleting cached object " << iter.key() + << endl; + } + + cacheIter().first() = false; + deleteCachedObject(*iter()); + } + } + } + return const_cast(*this).insert(io.name(), &io); } diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index b090195f8a..bf42608f84 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -87,8 +87,12 @@ class objectRegistry // Used to terminate searching within the ancestors bool parentNotTime() const; + //- Read the cacheTemporaryObjects list from controlDict void readCacheTemporaryObjects() const; + //- Delete the current cached object before caching a new object + void deleteCachedObject(regIOobject& cachedOb) const; + public: diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index d12c595bb2..dbcd505707 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -240,10 +240,7 @@ bool Foam::objectRegistry::cacheTemporaryObject(Object& ob) const // If the object is already cached in the database delete it if (&cachedOb != &ob && cachedOb.ownedByRegistry()) { - cachedOb.release(); - cachedOb.checkOut(); - cachedOb.rename(cachedOb.name() + "Cached"); - delete &cachedOb; + deleteCachedObject(cachedOb); } } diff --git a/tutorials/incompressible/simpleFoam/pitzDaily/system/controlDict b/tutorials/incompressible/simpleFoam/pitzDaily/system/controlDict index a266569049..4adb8dca8e 100644 --- a/tutorials/incompressible/simpleFoam/pitzDaily/system/controlDict +++ b/tutorials/incompressible/simpleFoam/pitzDaily/system/controlDict @@ -50,4 +50,14 @@ functions #includeFunc streamlines } +cacheTemporaryObjects +( + kEpsilon:G +); + +functions +{ + #includeFunc writeObjects(objects = (kEpsilon:G)) +} + // ************************************************************************* //