Skip to content
Snippets Groups Projects
Commit c1a04abd authored by Andrew Heather's avatar Andrew Heather Committed by Mark OLESEN
Browse files

ENH: Function1 - added optional objectRegistry reference

Function1 can now be created with an object registry, e.g. time or mesh
database. This enables access to other stored objects, e.g. fields,
dictionaries etc. making Function1 much more flexible.

Note: will allow TimeFunction1 to be deprecated
parent aeef9625
Branches
Tags
1 merge request!499Function1 objectRegistry access
...@@ -34,16 +34,25 @@ License ...@@ -34,16 +34,25 @@ License
// * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
template<class Type> 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> 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 ...@@ -65,8 +74,7 @@ Type Foam::Function1<Type>::value(const scalar x) const
template<class Type> template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>> Foam::Function1<Type>::value
Foam::Function1<Type>::value
( (
const scalarField& x const scalarField& x
) const ) const
...@@ -85,8 +93,7 @@ Type Foam::Function1<Type>::integrate(const scalar x1, const scalar x2) const ...@@ -85,8 +93,7 @@ Type Foam::Function1<Type>::integrate(const scalar x1, const scalar x2) const
template<class Type> template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>> Foam::Function1<Type>::integrate
Foam::Function1<Type>::integrate
( (
const scalarField& x1, const scalarField& x1,
const scalarField& x2 const scalarField& x2
......
...@@ -84,8 +84,6 @@ namespace Foam ...@@ -84,8 +84,6 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
class Time;
template<class Type> class Function1;
template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&); template<class Type> Ostream& operator<<(Ostream&, const Function1<Type>&);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
...@@ -142,10 +140,19 @@ public: ...@@ -142,10 +140,19 @@ public:
// Constructors // Constructors
//- Construct from entry name //- 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) //- 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 //- Copy construct
explicit Function1(const Function1<Type>& rhs); explicit Function1(const Function1<Type>& rhs);
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -26,37 +26,112 @@ License ...@@ -26,37 +26,112 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "function1Base.H" #include "function1Base.H"
#include "objectRegistry.H"
#include "Time.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 * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
Foam::function1Base::function1Base(const word& entryName) Foam::function1Base::function1Base
(
const word& entryName,
const objectRegistry* obrPtr
)
: :
refCount(), refCount(),
name_(entryName) name_(entryName),
obrPtr_(obrPtr)
{} {}
Foam::function1Base::function1Base Foam::function1Base::function1Base
( (
const word& entryName, const word& entryName,
const dictionary& dict const dictionary& dict,
const objectRegistry* obrPtr
) )
: :
refCount(), refCount(),
name_(entryName) name_(entryName),
obrPtr_(obrPtr)
{} {}
Foam::function1Base::function1Base(const function1Base& rhs) Foam::function1Base::function1Base(const function1Base& rhs)
: :
refCount(), refCount(),
name_(rhs.name_) name_(rhs.name_),
obrPtr_(rhs.obrPtr_)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
// NOTE : do not delete obrPtr_ (no ownership)
Foam::function1Base::~function1Base()
{} {}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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) void Foam::function1Base::convertTimeBase(const Time& t)
{} {}
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -27,12 +27,7 @@ Class ...@@ -27,12 +27,7 @@ Class
Foam::function1Base Foam::function1Base
Description Description
Top level data entry class for use in dictionaries. Provides a mechanism Base class for template-invariant parts of Function1
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
SourceFiles SourceFiles
function1Base.C function1Base.C
...@@ -43,6 +38,7 @@ SourceFiles ...@@ -43,6 +38,7 @@ SourceFiles
#define function1Base_H #define function1Base_H
#include "dictionary.H" #include "dictionary.H"
#include "objectRegistry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
...@@ -50,7 +46,7 @@ namespace Foam ...@@ -50,7 +46,7 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
class Time; template<class Type> class Function1;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class function1Base Declaration Class function1Base Declaration
...@@ -60,6 +56,12 @@ class function1Base ...@@ -60,6 +56,12 @@ class function1Base
: :
public refCount public refCount
{ {
// Private Member Functions
//- The associated registry, the time registry or nullptr
const objectRegistry* whichDb(const bool useTime) const noexcept;
protected: protected:
// Protected Data // Protected Data
...@@ -67,6 +69,9 @@ protected: ...@@ -67,6 +69,9 @@ protected:
//- Name of entry //- Name of entry
const word name_; const word name_;
//- Pointer to an object registry
const objectRegistry* obrPtr_;
// Protected Member Functions // Protected Member Functions
...@@ -78,18 +83,27 @@ public: ...@@ -78,18 +83,27 @@ public:
// Constructors // Constructors
//- Construct from entry name //- Construct from entry name and optional registry
explicit function1Base(const word& entryName); explicit function1Base
(
//- Construct from entry name and dictionary (unused) const word& entryName,
function1Base(const word& entryName, const dictionary& dict); 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 //- Copy construct
explicit function1Base(const function1Base& rhs); explicit function1Base(const function1Base& rhs);
//- Destructor //- Destructor
virtual ~function1Base() = default; virtual ~function1Base();
// Member Functions // Member Functions
...@@ -97,11 +111,58 @@ public: ...@@ -97,11 +111,58 @@ public:
// Access // Access
//- The name of the entry //- The name of the entry
const word& name() const const word& name() const noexcept
{ {
return name_; 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 // Manipulation
......
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