https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MemoizedFunctionInterface.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11
14{
16
17 params.addClassDescription("The function uses a cache to potentially reduce the computational "
18 "burden of reusing a complex or costly function");
19
20 params.addParam<bool>("enable_cache",
21 false,
22 "Enables cached function evaluations. Recommended only if this function is "
23 "used directly in a BC or Kernel. This will be enabled automatically if "
24 "any of the FX-based BCs are used.");
25
26 params.addParam<bool>("respect_time", false, "Enable to clear the cache at each new time step.");
27
28 return params;
29}
30
32 : Function(parameters),
33 _enable_cache(getParam<bool>("enable_cache")),
34 _respect_time(getParam<bool>("respect_time"))
35{
36}
37
38void
40{
41 // The mesh has changed, which invalidates the cache
43}
44
45Real
46MemoizedFunctionInterface::value(Real time, const Point & point) const
47{
48 MemoizedFunctionInterface * ptr = const_cast<MemoizedFunctionInterface *>(this);
49
50 if (_enable_cache)
51 {
52 // Start the cache over if we are at a new time step
53 if (_respect_time && time != _current_time)
54 {
55 _current_time = time;
56 ptr->invalidateCache();
57 }
58
59 // Try to insert a new value into the cache
60 auto result = _cache.insert({hashing::hashCombine(time, point), 0.0});
61
62 // Evaluate and apply if the insertion worked, i.e. the element didn't exist
63 if (result.second)
64 result.first->second = ptr->evaluateValue(time, point);
65
66 // Return the cached value
67 return result.first->second;
68 }
69
70 return ptr->evaluateValue(time, point);
71}
72
75{
76 mooseError("Not implemented");
77}
78
79void
87
88void
DualNumber< Real, DNDerivativeType, true > ADReal
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash...
virtual Real value(Real time, const Point &point) const final
bool _respect_time
Flag for whether changes in time invalidate the cache.
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change.
void useCache(bool use)
Enable/disable the cache.
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()
Real _current_time
Stores the time evaluation of the cache.
virtual void meshChanged() override
bool _enable_cache
Flag for whether to cache values.
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
MemoizedFunctionInterface(const InputParameters &parameters)
static InputParameters validParams()
void mooseError(Args &&... args) const
void hashCombine(HashValue &)
Final iteration of the variadic template with no additional arguments.
Definition Hashing.h:28