Skip to content
Snippets Groups Projects
Commit ba95b89e authored by mattijs's avatar mattijs Committed by Mark OLESEN
Browse files

ENH: add Function1 caching selector

parent fd82f3e4
Branches
Tags
1 merge request!497ENH: fvSolution: allow Function1 for all scalars
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -76,6 +76,7 @@ SourceFiles ...@@ -76,6 +76,7 @@ SourceFiles
#include "function1Base.H" #include "function1Base.H"
#include "Field.H" #include "Field.H"
#include "HashPtrTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
...@@ -191,6 +192,32 @@ public: ...@@ -191,6 +192,32 @@ public:
); );
// Caching Selectors - accept wildcards in dictionary
//- Selector with external storage of Function1.
//- This also allows wildcard matches in a dictionary
static refPtr<Function1<Type>> New
(
HashPtrTable<Function1<Type>>& cache,
const word& entryName,
const dictionary& dict,
enum keyType::option matchOpt = keyType::LITERAL,
const bool mandatory = true
);
/// //- Selector with external storage of Function1.
/// //- This also allows wildcard matches in a dictionary.
/// // If the default value is used, stores an entry as "default"
/// static refPtr<Function1<Type>> NewOrDefault
/// (
/// HashPtrTable<Function1<Type>>& cache,
/// const word& entryName,
/// const dictionary& dict,
/// const Type& deflt,
/// enum keyType::option matchOpt = keyType::LITERAL
/// );
//- Destructor //- Destructor
virtual ~Function1() = default; virtual ~Function1() = default;
......
...@@ -209,4 +209,104 @@ Foam::Function1<Type>::NewIfPresent ...@@ -209,4 +209,104 @@ Foam::Function1<Type>::NewIfPresent
} }
template<class Type>
Foam::refPtr<Foam::Function1<Type>>
Foam::Function1<Type>::New
(
HashPtrTable<Function1<Type>>& cache,
const word& entryName,
const dictionary& dict,
enum keyType::option matchOpt,
const bool mandatory
)
{
// Use the dictionary to find the keyword (allowing wildcards).
// Alternative would be to have
// a HashTable where the key type uses a wildcard match
refPtr<Function1<Type>> fref; // return value
// Try for direct cache hit
fref.cref(cache.get(entryName));
if (fref)
{
return fref;
}
// Lookup from dictionary
const entry* eptr = dict.findEntry(entryName, matchOpt);
if (eptr)
{
// Use keyword (potentially a wildcard) instead of entry name
const auto& kw = eptr->keyword();
// Try for a cache hit
fref.cref(cache.get(kw));
if (!fref)
{
// Create new entry
auto fauto
(
Function1<Type>::New
(
kw,
eptr, // Already resolved
dict,
word::null,
mandatory
)
);
if (fauto)
{
// Cache the newly created function
fref.cref(fauto.get());
cache.set(kw, fauto);
}
}
}
if (mandatory && !fref)
{
FatalIOErrorInFunction(dict)
<< "No match for " << entryName << nl
<< exit(FatalIOError);
}
return fref;
}
/// template<class Type>
/// Foam::refPtr<Foam::Function1<Type>>
/// Foam::Function1<Type>::NewOrDefault
/// (
/// HashPtrTable<Function1<Type>>& cache,
///
/// const word& entryName,
/// const dictionary& dict,
/// const Type& deflt,
/// enum keyType::option matchOpt
/// )
/// {
/// auto fref
/// (
/// Function1<Type>::New(entryName, dict, cache, matchOpt, false)
/// );
///
/// if (!fref)
/// {
/// fref.reset(new Function1Types::Constant<Type>("default", deflt));
/// }
///
/// return fref;
/// }
// ************************************************************************* // // ************************************************************************* //
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