Skip to content
Snippets Groups Projects
Commit 43cdc8fa authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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.
parent 2aca6b35
No related merge requests found
......@@ -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
......@@ -43,26 +43,22 @@ namespace functionObjects
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
const Foam::objectRegistry&
Foam::functionObjects::regionFunctionObject::whichSubRegistry
(
const objectRegistry& obr,
const dictionary& dict
)
Foam::functionObjects::regionFunctionObject::obr() const
{
word subName;
if (dict.readIfPresent("subRegion", subName))
if (!obrPtr_ && !subRegistryName_.empty())
{
return obr.lookupObject<objectRegistry>(subName);
}
return obr;
}
// 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_);
}
}
const Foam::objectRegistry&
Foam::functionObjects::regionFunctionObject::obr() const
{
return subObr_;
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;
}
......
......@@ -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;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment