diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C index 42aeeb20ad..d1f9f6504d 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2016 OpenFOAM Foundation @@ -42,27 +42,23 @@ namespace functionObjects // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // -const Foam::objectRegistry& -Foam::functionObjects::regionFunctionObject::whichSubRegistry -( - const objectRegistry& obr, - const dictionary& dict -) -{ - word subName; - if (dict.readIfPresent("subRegion", subName)) - { - return obr.lookupObject(subName); - } - - return obr; -} - - const Foam::objectRegistry& Foam::functionObjects::regionFunctionObject::obr() const { - return subObr_; + if (!obrPtr_ && !subRegistryName_.empty()) + { + // Recursive - so we also find things registered on Time + obrPtr_ = obr_.cfindObject(subRegistryName_, true); + + // Also search functionObject output ("functionObjectObjects") + if (!obrPtr_) + { + obrPtr_ = + storedObjects().cfindObject(subRegistryName_); + } + } + + return (obrPtr_ ? *obrPtr_ : obr_); } @@ -140,6 +136,7 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject ) : stateFunctionObject(name, runTime), + subRegistryName_(dict.lookupOrDefault("subRegion", word::null)), obr_ ( runTime.lookupObject @@ -147,7 +144,7 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject dict.lookupOrDefault("region", polyMesh::defaultRegion) ) ), - subObr_(whichSubRegistry(obr_, dict)) + obrPtr_(nullptr) {} @@ -159,8 +156,9 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject ) : stateFunctionObject(name, obr.time()), + subRegistryName_(dict.lookupOrDefault("subRegion", word::null)), obr_(obr), - subObr_(whichSubRegistry(obr_, dict)) + obrPtr_(nullptr) {} @@ -168,7 +166,13 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject bool Foam::functionObjects::regionFunctionObject::read(const dictionary& dict) { - return stateFunctionObject::read(dict); + stateFunctionObject::read(dict); + + subRegistryName_ = dict.lookupOrDefault("subRegion", word::null); + + obrPtr_ = nullptr; + + return true; } diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H index d34bfb8b4d..fed75807fc 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H @@ -29,8 +29,17 @@ Class Description Specialization of Foam::functionObject for a region and providing a reference to the region Foam::objectRegistry. - Also provides support for referencing a sub-region, which is typically - needed when dealing with surfMesh and their fields. + + Also provides support for referencing an alternative objectRegistry + that can hold fields. This may be used, for example, to access + stored surfaces and fields. + + Dictionary controls + \table + Property | Description | Required | Default + region | Name of the mesh region | no | region0 + subRegion | Name for alternative objectRegistry | no | "" + \endtable See also Foam::functionObjects::stateFunctionObject @@ -69,25 +78,19 @@ protected: // Protected Member Data + //- Name for alternative object registry + word subRegistryName_; + //- Reference to the region objectRegistry const objectRegistry& obr_; - //- Optional reference to the sub-region objectRegistry. - // If a sub-region is not in effect, this reference is identical - // to the usual region objectRegistry. - const objectRegistry& subObr_; + //- Pointer to alternative (eg, sub-region) objectRegistry. + // If a sub-region is not in effect, this is a nullptr + mutable const objectRegistry* obrPtr_; // Protected Member Functions - //- Selector for alternative sub-registry, - // when the keyword %subRegion is present in the dictionary - static const objectRegistry& whichSubRegistry - ( - const objectRegistry& obr, - const dictionary& dict - ); - //- The region or sub-region registry being used virtual const objectRegistry& obr() const;