From 1b9576df0a62b2178e0af575e730e84a092562de Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 6 Feb 2019 12:01:29 +0100 Subject: [PATCH 1/3] ENH: support objectRegistry::checkOut(const word& name) - similar to what erase() does, but as a mutable operation (#1180) - replace basicThermo lookupAndCheckout (commit 880c98757d91) with the new objectRegistry::checkOut() method. --- .../db/objectRegistry/objectRegistry.C | 6 +++++ .../db/objectRegistry/objectRegistry.H | 4 ++++ .../fieldAverageItem/fieldAverageItem.C | 17 ++++++------- src/functionObjects/field/momentum/momentum.C | 8 +++---- .../surfMeshSamplers/surfMeshSamplers.C | 24 ++++--------------- .../surfMeshSamplers/surfMeshSamplers.H | 8 ------- .../basic/basicThermo/basicThermo.C | 11 +-------- .../basic/basicThermo/basicThermo.H | 5 +--- 8 files changed, 27 insertions(+), 56 deletions(-) diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index cc745f61e6..f9a51bbc2f 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -285,6 +285,12 @@ bool Foam::objectRegistry::checkOut(regIOobject& io) const } +bool Foam::objectRegistry::checkOut(const word& key) const +{ + return const_cast(*this).erase(key); +} + + void Foam::objectRegistry::clear() { // Free anything owned by the registry diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index 7eb7d0c467..a606f8049c 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -438,6 +438,10 @@ public: //- object is ownedByRegistry bool checkOut(regIOobject& io) const; + //- Remove a regIOobject by name from registry and frees memory if the + //- object is ownedByRegistry + bool checkOut(const word& key) const; + //- Clear all entries from the registry // Performs a checkOut() for all objects that are ownedByRegistry void clear(); diff --git a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C index e21c881939..7864b12bbb 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C +++ b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -152,7 +152,7 @@ void Foam::functionObjects::fieldAverageItem::evolve(const objectRegistry& obr) const word fieldName = windowFieldNames_.pop(); //Info<< "evolve: removing field: " << fieldName << endl; - obr.checkOut(*obr[fieldName]); + obr.checkOut(fieldName); } } } @@ -164,22 +164,19 @@ void Foam::functionObjects::fieldAverageItem::clear bool fullClean ) { - if (mean_ && obr.found(meanFieldName_)) + if (mean_) { - obr.checkOut(*obr[meanFieldName_]); + obr.checkOut(meanFieldName_); } - if (prime2Mean_ && obr.found(prime2MeanFieldName_)) + if (prime2Mean_) { - obr.checkOut(*obr[prime2MeanFieldName_]); + obr.checkOut(prime2MeanFieldName_); } for (const word& fieldName : windowFieldNames_) { - if (obr.found(fieldName)) - { - obr.checkOut(*obr[fieldName]); - } + obr.checkOut(fieldName); } if (totalTime_ < 0 || fullClean) diff --git a/src/functionObjects/field/momentum/momentum.C b/src/functionObjects/field/momentum/momentum.C index 342b46eb97..260253bf00 100644 --- a/src/functionObjects/field/momentum/momentum.C +++ b/src/functionObjects/field/momentum/momentum.C @@ -46,11 +46,9 @@ namespace functionObjects void Foam::functionObjects::momentum::purgeFields() { - objectRegistry& obr = const_cast(obr_); - - obr.erase(scopedName("momentum")); - obr.erase(scopedName("angularMomentum")); - obr.erase(scopedName("angularVelocity")); + obr_.checkOut(scopedName("momentum")); + obr_.checkOut(scopedName("angularMomentum")); + obr_.checkOut(scopedName("angularVelocity")); } diff --git a/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.C b/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.C index 5cf3391d5d..42cc4509b6 100644 --- a/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.C +++ b/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.C @@ -49,24 +49,6 @@ namespace Foam bool Foam::surfMeshSamplers::verbose_ = false; -void Foam::surfMeshSamplers::checkOutNames -( - const objectRegistry& registry, - const UList& names -) -{ - objectRegistry& reg = const_cast(registry); - - for (const word& fldName : names) - { - objectRegistry::iterator iter = reg.find(fldName); - if (iter.found()) - { - registry.checkOut(*iter()); - } - } -} - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -297,7 +279,11 @@ bool Foam::surfMeshSamplers::execute() } } - checkOutNames(db, cleanup); + // Cleanup any locally introduced names + for (const word& fieldName : cleanup) + { + db.checkOut(fieldName); + } return true; } diff --git a/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.H b/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.H index 19cd1bb8fa..ecd729dabe 100644 --- a/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.H +++ b/src/sampling/surfMeshSample/surfMeshSamplers/surfMeshSamplers.H @@ -157,14 +157,6 @@ class surfMeshSamplers // Private Member Functions - //- Remove items by name from objectRegistry - static void checkOutNames - ( - const objectRegistry& registry, - const UList& names - ); - - //- Hard-coded derived field (rho * U) // \return true if field did not previously exist bool add_rhoU(const word& derivedName); diff --git a/src/thermophysicalModels/basic/basicThermo/basicThermo.C b/src/thermophysicalModels/basic/basicThermo/basicThermo.C index 59425ecd3d..bbd4780020 100644 --- a/src/thermophysicalModels/basic/basicThermo/basicThermo.C +++ b/src/thermophysicalModels/basic/basicThermo/basicThermo.C @@ -160,15 +160,6 @@ Foam::volScalarField& Foam::basicThermo::lookupOrConstruct } -void Foam::basicThermo::lookupAndCheckout(const char* name) const -{ - if (db().foundObject(name)) - { - db().checkOut(*db()[name]); - } -} - - Foam::basicThermo::basicThermo ( const fvMesh& mesh, @@ -326,7 +317,7 @@ Foam::autoPtr Foam::basicThermo::New Foam::basicThermo::~basicThermo() { - lookupAndCheckout("p"); + db().checkOut("p"); } diff --git a/src/thermophysicalModels/basic/basicThermo/basicThermo.H b/src/thermophysicalModels/basic/basicThermo/basicThermo.H index e8be30ec96..337373c685 100644 --- a/src/thermophysicalModels/basic/basicThermo/basicThermo.H +++ b/src/thermophysicalModels/basic/basicThermo/basicThermo.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2010, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -105,9 +105,6 @@ protected: bool& isOwner ); - //- Lookup and check out field - void lookupAndCheckout(const char* name) const; - //- Return the enthalpy/internal energy field boundary types // by interrogating the temperature field boundary types wordList heBoundaryTypes(); From 6d6f43d72e229f8f4b28a3b7b75c41a2b421d878 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 6 Feb 2019 12:01:29 +0100 Subject: [PATCH 2/3] ENH: refactored regIOobject searching in object registry - The findObject() methods are template-typed and used to locate a particular Type/name combination. Eg, volScalarField* ptr = obr.findObject("xyz"); - The findIOobject() methods are un-typed and use the name only. Eg, regIOobject* ptr = obr.findIOobject("xyz"); The typed versions will be most commonly used, but the un-typed lookup can be useful in a templating. - Simplified findObject* methods to use findIOobject* as the backend. --- .../db/objectRegistry/objectRegistry.C | 61 +++++++++++++++++++ .../db/objectRegistry/objectRegistry.H | 53 ++++++++++++++++ .../objectRegistry/objectRegistryTemplates.C | 28 ++------- 3 files changed, 118 insertions(+), 24 deletions(-) diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index f9a51bbc2f..f93d0c1f7d 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -373,6 +373,67 @@ void Foam::objectRegistry::rename(const word& newName) } +bool Foam::objectRegistry::found +( + const word& name, + const bool recursive +) const +{ + return cfindIOobject(name, recursive); +} + + +const Foam::regIOobject* Foam::objectRegistry::cfindIOobject +( + const word& name, + const bool recursive +) const +{ + const_iterator iter = cfind(name); + + if (iter.found()) + { + return iter.val(); + } + else if (recursive && this->parentNotTime()) + { + return parent_.cfindIOobject(name, recursive); + } + + return nullptr; +} + + +const Foam::regIOobject* Foam::objectRegistry::findIOobject +( + const word& name, + const bool recursive +) const +{ + return cfindIOobject(name, recursive); +} + + +Foam::regIOobject* Foam::objectRegistry::findIOobject +( + const word& name, + const bool recursive +) +{ + return const_cast(cfindIOobject(name, recursive)); +} + + +Foam::regIOobject* Foam::objectRegistry::getIOobjectPtr +( + const word& name, + const bool recursive +) const +{ + return const_cast(cfindIOobject(name, recursive)); +} + + bool Foam::objectRegistry::modified() const { for (const_iterator iter = cbegin(); iter != cend(); ++iter) diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index a606f8049c..d3d05761a8 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -340,6 +340,59 @@ public: template HashTable lookupClass(const bool strict = false); + + //- Can the regIOobject object be found (by name). + // + // \param recursive search parent registries + bool found(const word& name, const bool recursive = false) const; + + + //- Return const pointer to the regIOobject. + // + // \param recursive search parent registries + // + // \return nullptr if the object was not found. + const regIOobject* cfindIOobject + ( + const word& name, + const bool recursive = false + ) const; + + //- Return const pointer to the regIOobject. + // + // \param recursive search parent registries + // + // \return nullptr if the object was not found. + const regIOobject* findIOobject + ( + const word& name, + const bool recursive = false + ) const; + + //- Return non-const pointer to the regIOobject. + // + // \param recursive search parent registries + // + // \return nullptr if the object was not found. + regIOobject* findIOobject + ( + const word& name, + const bool recursive = false + ); + + //- Return non-const pointer to the regIOobject, + //- using a const-cast to have it behave like a mutable. + // Exercise caution when using. + // + // \param recursive search parent registries. + // + // \return nullptr if the object was not found. + regIOobject* getIOobjectPtr + ( + const word& name, + const bool recursive = false + ) const; + //- Is the named Type found? // // \param recursive search parent registries diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index 4d41233290..127786b74f 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -391,23 +391,7 @@ const Type* Foam::objectRegistry::cfindObject const bool recursive ) const { - const_iterator iter = cfind(name); - - if (iter.found()) - { - const Type* ptr = dynamic_cast(iter()); - - if (ptr) - { - return ptr; - } - } - else if (recursive && this->parentNotTime()) - { - return parent_.cfindObject(name, recursive); - } - - return nullptr; + return dynamic_cast(this->cfindIOobject(name, recursive)); } @@ -429,9 +413,7 @@ Type* Foam::objectRegistry::findObject const bool recursive ) { - const Type* ptr = this->cfindObject(name, recursive); - - return const_cast(ptr); + return const_cast(this->cfindObject(name, recursive)); } @@ -442,9 +424,7 @@ Type* Foam::objectRegistry::getObjectPtr const bool recursive ) const { - const Type* ptr = this->cfindObject(name, recursive); - - return const_cast(ptr); + return const_cast(this->cfindObject(name, recursive)); } From 97994734d2865a5a79d8b3acfa18422c5f7370f9 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 6 Feb 2019 12:01:29 +0100 Subject: [PATCH 3/3] CONFIG: bump API version number to 1901 to register recent changes - objectRegistry search, erase methods - clip, minMax - function object triggering ... --- META-INFO/api-info | 4 ++-- wmake/rules/General/general | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/META-INFO/api-info b/META-INFO/api-info index 0821b0ef51..996a322b6a 100644 --- a/META-INFO/api-info +++ b/META-INFO/api-info @@ -1,2 +1,2 @@ -api=1812 -patch=190129 +api=1901 +patch=0 diff --git a/wmake/rules/General/general b/wmake/rules/General/general index e9b10c18aa..97253e62fd 100644 --- a/wmake/rules/General/general +++ b/wmake/rules/General/general @@ -1,5 +1,5 @@ #-------------------------------*- makefile -*--------------------------------- -WM_VERSION = OPENFOAM=1812 +WM_VERSION = OPENFOAM=1901 AR = ar ARFLAGS = cr