ENH: lazy evaluation of subRegion in regionFunctionObject (#1202)

- delay dereferencing of optional subRegion entries until an
  objectRegistry is required.

  This improves usabilty when reference objects do not yet exist
  at the time of construction.
This commit is contained in:
Mark Olesen
2019-02-12 13:54:02 +01:00
parent 2aca6b35ce
commit 43cdc8fac1
2 changed files with 43 additions and 36 deletions

View File

@ -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<objectRegistry>(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<objectRegistry>(subRegistryName_, true);
// Also search functionObject output ("functionObjectObjects")
if (!obrPtr_)
{
obrPtr_ =
storedObjects().cfindObject<objectRegistry>(subRegistryName_);
}
}
return (obrPtr_ ? *obrPtr_ : obr_);
}
@ -140,6 +136,7 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject
)
:
stateFunctionObject(name, runTime),
subRegistryName_(dict.lookupOrDefault<word>("subRegion", word::null)),
obr_
(
runTime.lookupObject<objectRegistry>
@ -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<word>("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<word>("subRegion", word::null);
obrPtr_ = nullptr;
return true;
}

View File

@ -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;