diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C index 84900b63f74cf60a617fc04761042f4b04680bb6..e5a3834661a604774df8d391b40cfc2183b7656a 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C @@ -34,16 +34,25 @@ License // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * // template<class Type> -Foam::Function1<Type>::Function1(const word& entryName) +Foam::Function1<Type>::Function1 +( + const word& entryName, + const objectRegistry* obrPtr +) : - function1Base(entryName) + function1Base(entryName, obrPtr) {} template<class Type> -Foam::Function1<Type>::Function1(const word& entryName, const dictionary& dict) +Foam::Function1<Type>::Function1 +( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr +) : - function1Base(entryName, dict) + function1Base(entryName, dict, obrPtr) {} @@ -65,8 +74,7 @@ Type Foam::Function1<Type>::value(const scalar x) const template<class Type> -Foam::tmp<Foam::Field<Type>> -Foam::Function1<Type>::value +Foam::tmp<Foam::Field<Type>> Foam::Function1<Type>::value ( const scalarField& x ) const @@ -85,8 +93,7 @@ Type Foam::Function1<Type>::integrate(const scalar x1, const scalar x2) const template<class Type> -Foam::tmp<Foam::Field<Type>> -Foam::Function1<Type>::integrate +Foam::tmp<Foam::Field<Type>> Foam::Function1<Type>::integrate ( const scalarField& x1, const scalarField& x2 diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H index dacf265cd9015d6f789d5d1cf0731d91584954f9..0e2cffdf31a34fae25a1e9f2a691789e06c724ed 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H @@ -84,8 +84,6 @@ namespace Foam { // Forward Declarations -class Time; -template<class Type> class Function1; template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&); /*---------------------------------------------------------------------------*\ @@ -142,10 +140,19 @@ public: // Constructors //- Construct from entry name - explicit Function1(const word& entryName); + explicit Function1 + ( + const word& entryName, + const objectRegistry* obrPtr = nullptr + ); //- Construct from entry name and dictionary (unused) - Function1(const word& entryName, const dictionary& dict); + Function1 + ( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr = nullptr + ); //- Copy construct explicit Function1(const Function1<Type>& rhs); diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.C b/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.C index c4a6e0a19403ebd198665bb000dd73325f8f5efe..72494af3052cd56fdaa02359d4d00673ed10d6c8 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,37 +26,112 @@ License \*---------------------------------------------------------------------------*/ #include "function1Base.H" +#include "objectRegistry.H" #include "Time.H" +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +const Foam::objectRegistry* Foam::function1Base::whichDb +( + const bool useTime +) const noexcept +{ + if (obrPtr_ && useTime) + { + return &(obrPtr_->time()); + } + + return obrPtr_; +} + + // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * // -Foam::function1Base::function1Base(const word& entryName) +Foam::function1Base::function1Base +( + const word& entryName, + const objectRegistry* obrPtr +) : refCount(), - name_(entryName) + name_(entryName), + obrPtr_(obrPtr) {} Foam::function1Base::function1Base ( const word& entryName, - const dictionary& dict + const dictionary& dict, + const objectRegistry* obrPtr ) : refCount(), - name_(entryName) + name_(entryName), + obrPtr_(obrPtr) {} Foam::function1Base::function1Base(const function1Base& rhs) : refCount(), - name_(rhs.name_) + name_(rhs.name_), + obrPtr_(rhs.obrPtr_) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +// NOTE : do not delete obrPtr_ (no ownership) +Foam::function1Base::~function1Base() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +const Foam::objectRegistry& Foam::function1Base::obr() const +{ + if (!obrPtr_) + { + FatalErrorInFunction + << "Object registry not set" + << abort(FatalError); + } + return *obrPtr_; +} + + +const Foam::Time& Foam::function1Base::time() const +{ + if (!obrPtr_) + { + FatalErrorInFunction + << "Object registry not set" + << abort(FatalError); + } + + return obrPtr_->time(); +} + + +bool Foam::function1Base::isTime() const noexcept +{ + return (obrPtr_ && obrPtr_->isTimeDb()); +} + + +void Foam::function1Base::resetDb(const objectRegistry* obrPtr) noexcept +{ + obrPtr_ = obrPtr; +} + + +void Foam::function1Base::resetDb(const objectRegistry& db) noexcept +{ + obrPtr_ = &db; +} + + void Foam::function1Base::convertTimeBase(const Time& t) {} diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.H b/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.H index a3191c7924e4e5ccb4a5ec834b8b92081b327748..85188de82c45f4d8ac9ab1c9e590aa93984146c1 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/function1Base.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,12 +27,7 @@ Class Foam::function1Base Description - Top level data entry class for use in dictionaries. Provides a mechanism - to specify a variable as a certain type, e.g. constant or time varying, and - provide functions to return the (interpolated) value, and integral between - limits. - - Extends the Function1 class by adding autoMap and rMap functions + Base class for template-invariant parts of Function1 SourceFiles function1Base.C @@ -43,6 +38,7 @@ SourceFiles #define function1Base_H #include "dictionary.H" +#include "objectRegistry.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -50,7 +46,7 @@ namespace Foam { // Forward Declarations -class Time; +template<class Type> class Function1; /*---------------------------------------------------------------------------*\ Class function1Base Declaration @@ -60,6 +56,12 @@ class function1Base : public refCount { + // Private Member Functions + + //- The associated registry, the time registry or nullptr + const objectRegistry* whichDb(const bool useTime) const noexcept; + + protected: // Protected Data @@ -67,6 +69,9 @@ protected: //- Name of entry const word name_; + //- Pointer to an object registry + const objectRegistry* obrPtr_; + // Protected Member Functions @@ -78,18 +83,27 @@ public: // Constructors - //- Construct from entry name - explicit function1Base(const word& entryName); - - //- Construct from entry name and dictionary (unused) - function1Base(const word& entryName, const dictionary& dict); + //- Construct from entry name and optional registry + explicit function1Base + ( + const word& entryName, + const objectRegistry* obrPtr = nullptr + ); + + //- Construct from entry name, dictionary (unused) and optional registry + function1Base + ( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr = nullptr + ); //- Copy construct explicit function1Base(const function1Base& rhs); //- Destructor - virtual ~function1Base() = default; + virtual ~function1Base(); // Member Functions @@ -97,11 +111,58 @@ public: // Access //- The name of the entry - const word& name() const + const word& name() const noexcept { return name_; } + //- Return the associated registry or nullptr. + const objectRegistry* whichDb() const noexcept + { + return obrPtr_; + } + + //- Reset the associated objectRegistry + void resetDb(const objectRegistry* obrPtr = nullptr) noexcept; + + //- Reset the associated objectRegistry + void resetDb(const objectRegistry& db) noexcept; + + //- Return the object registry + // FatalError if object registry is not set + const objectRegistry& obr() const; + + //- Return true if this function was created with the time database + bool isTime() const noexcept; + + //- Return the time database + // FatalError if object registry is not set + const Time& time() const; + + //- Return the mesh database if this Function1 was created using a mesh + // Note: relies on refCast failure if the type is not correct + template<class MeshType> + const MeshType& mesh(const word& regionName = word::null) const + { + const objectRegistry* ptr = whichDb(!regionName.empty()); + + if (!ptr) + { + FatalErrorInFunction + << "Object registry not set" + << abort(FatalError); + } + + if (regionName.empty()) + { + return refCast<const MeshType>(*ptr); + } + else + { + return ptr->lookupObject<MeshType>(regionName); + } + } + // Manipulation