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 0cddd5cc56..a18bff7070 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; diff --git a/src/functionObjects/field/PecletNo/PecletNo.C b/src/functionObjects/field/PecletNo/PecletNo.C index da4018b54b..c8eb2e8f67 100644 --- a/src/functionObjects/field/PecletNo/PecletNo.C +++ b/src/functionObjects/field/PecletNo/PecletNo.C @@ -157,7 +157,7 @@ Foam::functionObjects::PecletNo::~PecletNo() bool Foam::functionObjects::PecletNo::read(const dictionary& dict) { - dict.readIfPresent("rho", rhoName_); + rhoName_ = dict.lookupOrDefault("rho", "rho"); return true; } diff --git a/src/functionObjects/field/blendingFactor/blendingFactor.C b/src/functionObjects/field/blendingFactor/blendingFactor.C index 3861e809a2..f46374f29c 100644 --- a/src/functionObjects/field/blendingFactor/blendingFactor.C +++ b/src/functionObjects/field/blendingFactor/blendingFactor.C @@ -117,8 +117,13 @@ bool Foam::functionObjects::blendingFactor::read(const dictionary& dict) writeFile::read(dict); phiName_ = dict.lookupOrDefault("phi", "phi"); - dict.readIfPresent("tolerance", tolerance_); - if ((tolerance_ < 0) || (tolerance_ > 1)) + + tolerance_ = 0.001; + if + ( + dict.readIfPresent("tolerance", tolerance_) + && (tolerance_ < 0 || tolerance_ > 1) + ) { FatalErrorInFunction << "tolerance must be in the range 0 to 1. Supplied value: " diff --git a/src/functionObjects/field/fieldAverage/fieldAverage.C b/src/functionObjects/field/fieldAverage/fieldAverage.C index 5ce702d66a..12ffdcd9a5 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverage.C +++ b/src/functionObjects/field/fieldAverage/fieldAverage.C @@ -302,17 +302,39 @@ bool Foam::functionObjects::fieldAverage::read(const dictionary& dict) dict.readIfPresent("periodicRestart", periodicRestart_); dict.lookup("fields") >> faItems_; + const scalar currentTime = obr().time().value(); + if (periodicRestart_) { dict.lookup("restartPeriod") >> restartPeriod_; - Info<< " Restart period " << restartPeriod_ - << nl << endl; + + if (restartPeriod_ > 0) + { + // Determine the appropriate interval for the next restart + periodIndex_ = 1; + while (currentTime > restartPeriod_*periodIndex_) + { + ++periodIndex_; + } + + Info<< " Restart period " << restartPeriod_ + << " - next restart at " << (restartPeriod_*periodIndex_) + << nl << endl; + } + else + { + periodicRestart_ = false; + + Info<< " Restart period " << restartPeriod_ + << " - ignored" + << nl << endl; + } } restartTime_ = GREAT; if (dict.readIfPresent("restartTime", restartTime_)) { - if (restartTime_ < obr_.time().value()) + if (currentTime > restartTime_) { // The restart time is already in the past - ignore restartTime_ = GREAT; diff --git a/src/functionObjects/field/fieldValues/fieldValue/fieldValue.C b/src/functionObjects/field/fieldValues/fieldValue/fieldValue.C index 42f57bb9f7..4ebe86bb24 100644 --- a/src/functionObjects/field/fieldValues/fieldValue/fieldValue.C +++ b/src/functionObjects/field/fieldValues/fieldValue/fieldValue.C @@ -97,7 +97,7 @@ bool Foam::functionObjects::fieldValue::read(const dictionary& dict) dict.lookup("fields") >> fields_; dict.lookup("writeFields") >> writeFields_; - dict.readIfPresent("scaleFactor", scaleFactor_); + scaleFactor_ = dict.lookupOrDefault("scaleFactor", 1.0); return true; } diff --git a/src/functionObjects/field/fieldValues/fieldValue/fieldValue.H b/src/functionObjects/field/fieldValues/fieldValue/fieldValue.H index 6278eea50a..823363316a 100644 --- a/src/functionObjects/field/fieldValues/fieldValue/fieldValue.H +++ b/src/functionObjects/field/fieldValues/fieldValue/fieldValue.H @@ -70,7 +70,7 @@ protected: // Protected data - //- Optional scale value + //- Optional scaling factor scalar scaleFactor_; //- Construction dictionary diff --git a/src/functionObjects/field/fluxSummary/fluxSummary.C b/src/functionObjects/field/fluxSummary/fluxSummary.C index cfe7a3e18c..dc403701a4 100644 --- a/src/functionObjects/field/fluxSummary/fluxSummary.C +++ b/src/functionObjects/field/fluxSummary/fluxSummary.C @@ -79,21 +79,19 @@ void Foam::functionObjects::fluxSummary::initialiseFaceZone DynamicList>& faceSign ) const { - const fvMesh& mesh = refCast(obr_); - - label zonei = mesh.faceZones().findZoneID(faceZoneName); + label zonei = mesh_.faceZones().findZoneID(faceZoneName); if (zonei == -1) { FatalErrorInFunction << "Unable to find faceZone " << faceZoneName - << ". Valid faceZones are: " << mesh.faceZones().names() + << ". Valid faceZones are: " << mesh_.faceZones().names() << exit(FatalError); } faceZoneNames.append(faceZoneName); - const faceZone& fZone = mesh.faceZones()[zonei]; + const faceZone& fZone = mesh_.faceZones()[zonei]; DynamicList