diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C index 6fdfe07eb1..b1d9f5f59f 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,19 +40,46 @@ 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); + } + else + { + return obr; + } +} + + +const Foam::objectRegistry& +Foam::functionObjects::regionFunctionObject::obr() const +{ + return subObr_; +} + + bool Foam::functionObjects::regionFunctionObject::writeObject ( const word& fieldName ) { - if (obr_.foundObject(fieldName)) + const regIOobject* objPtr = + this->lookupObjectPtr(fieldName); + + if (objPtr) { - const regIOobject& field = obr_.lookupObject(fieldName); - Log << " functionObjects::" << type() << " " << name() - << " writing field: " << field.name() << endl; + << " writing field: " << objPtr->name() << endl; - field.write(); + objPtr->write(); return true; } @@ -68,13 +95,12 @@ bool Foam::functionObjects::regionFunctionObject::clearObject const word& fieldName ) { - if (foundObject(fieldName)) + regIOobject* objPtr = lookupObjectRefPtr(fieldName); + if (objPtr) { - const regIOobject& resultObject = lookupObject(fieldName); - - if (resultObject.ownedByRegistry()) + if (objPtr->ownedByRegistry()) { - return const_cast(resultObject).checkOut(); + return objPtr->checkOut(); } else { @@ -104,7 +130,8 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject ( dict.lookupOrDefault("region", polyMesh::defaultRegion) ) - ) + ), + subObr_(whichSubRegistry(obr_, dict)) {} @@ -116,7 +143,8 @@ Foam::functionObjects::regionFunctionObject::regionFunctionObject ) : stateFunctionObject(name, obr.time()), - obr_(obr) + obr_(obr), + subObr_(whichSubRegistry(obr_, dict)) {} diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H index e9be6e3487..70be4fccfe 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H @@ -27,6 +27,8 @@ 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. See also Foam::functionObjects::stateFunctionObject @@ -68,18 +70,51 @@ protected: //- 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_; + // Protected member functions - //- Find field in the objectRegistry + //- 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 + const objectRegistry& obr() const; + + + //- Find object (eg, a field) in the (sub) objectRegistry template bool foundObject(const word& fieldName) const; - //- Lookup field from the objectRegistry + //- Lookup and return object (eg, a field) from the (sub) objectRegistry template const ObjectType& lookupObject(const word& fieldName) const; - //- Store the given field in the objectRegistry under the given name + //- Lookup and return object (eg, a field) from the (sub) objectRegistry + template + ObjectType& lookupObjectRef(const word& fieldName) const; + + //- Lookup and return pointer to the object, + // otherwise nullptr if the object was not found, + // or had the incorrect type. + template + const ObjectType* lookupObjectPtr(const word& fieldName) const; + + //- Lookup and return non-const pointer to the object, + // otherwise nullptr if the object was not found, + // or had the incorrect type. + template + ObjectType* lookupObjectRefPtr(const word& fieldName) const; + + //- Store the given field in the (sub) objectRegistry under the given name // Note: sets the fieldName to tfield().name() if not already set template bool store @@ -89,10 +124,10 @@ protected: bool cacheable = false ); - //- Write field if present in objectRegistry + //- Write field if present in the (sub) objectRegistry bool writeObject(const word& fieldName); - //- Clear field from the objectRegistry if present + //- Clear field from the (sub) objectRegistry if present bool clearObject(const word& fieldName); @@ -101,10 +136,10 @@ private: // Private Member Functions //- Disallow default bitwise copy construct - regionFunctionObject(const regionFunctionObject&); + regionFunctionObject(const regionFunctionObject&) = delete; //- Disallow default bitwise assignment - void operator=(const regionFunctionObject&); + void operator=(const regionFunctionObject&) = delete; public: diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C index 5c803a73df..c318770cc0 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C @@ -34,7 +34,7 @@ bool Foam::functionObjects::regionFunctionObject::foundObject const word& fieldName ) const { - return obr_.foundObject(fieldName); + return obr().foundObject(fieldName); } @@ -44,7 +44,37 @@ const ObjectType& Foam::functionObjects::regionFunctionObject::lookupObject const word& fieldName ) const { - return obr_.lookupObject(fieldName); + return obr().lookupObject(fieldName); +} + + +template +ObjectType& Foam::functionObjects::regionFunctionObject::lookupObjectRef +( + const word& fieldName +) const +{ + return obr().lookupObjectRef(fieldName); +} + + +template +const ObjectType* Foam::functionObjects::regionFunctionObject::lookupObjectPtr +( + const word& fieldName +) const +{ + return obr().lookupObjectPtr(fieldName); +} + + +template +ObjectType* Foam::functionObjects::regionFunctionObject::lookupObjectRefPtr +( + const word& fieldName +) const +{ + return obr().lookupObjectRefPtr(fieldName); } @@ -72,8 +102,8 @@ bool Foam::functionObjects::regionFunctionObject::store { const ObjectType& field = lookupObject(fieldName); - // If there is a result field already registered assign to the new - // result field otherwise transfer ownership of the new result field to + // If there is a result field already registered, assign to the new + // result field. Otherwise transfer ownership of the new result field to // the object registry if (&field != &tfield()) { @@ -81,7 +111,7 @@ bool Foam::functionObjects::regionFunctionObject::store } else { - obr_.objectRegistry::store(tfield.ptr()); + obr().objectRegistry::store(tfield.ptr()); } } else @@ -95,7 +125,7 @@ bool Foam::functionObjects::regionFunctionObject::store fieldName = tfield().name(); } - obr_.objectRegistry::store(tfield.ptr()); + obr().objectRegistry::store(tfield.ptr()); } return true;