www.mooseframework.org
MemoizedFunctionInterface.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
12 template <>
13 InputParameters
15 {
16  InputParameters params = validParams<Function>();
17 
18  params.addClassDescription("The function uses a cache to potentially reduce the computational "
19  "burden of reusing a complex or costly function");
20 
21  params.addParam<bool>("enable_cache",
22  false,
23  "Enables cached function evaluations. Recommended only if this function is "
24  "used directly in a BC or Kernel. This will be enabled automatically if "
25  "any of the FX-based BCs are used.");
26 
27  params.addParam<bool>("respect_time", false, "Enable to clear the cache at each new time step.");
28 
29  return params;
30 }
31 
32 MemoizedFunctionInterface::MemoizedFunctionInterface(const InputParameters & parameters)
33  : Function(parameters),
34  _enable_cache(getParam<bool>("enable_cache")),
35  _respect_time(getParam<bool>("respect_time"))
36 {
37 }
38 
39 void
41 {
42  // The mesh has changed, which invalidates the cache
44 }
45 
46 Real
47 MemoizedFunctionInterface::value(Real time, const Point & point) const
48 {
49  MemoizedFunctionInterface * ptr = const_cast<MemoizedFunctionInterface *>(this);
50 
51  if (_enable_cache)
52  {
53  // Start the cache over if we are at a new time step
54  if (_respect_time && time != _current_time)
55  {
56  _current_time = time;
57  ptr->invalidateCache();
58  }
59 
60  // Try to insert a new value into the cache
61  auto result = _cache.insert({hashing::hashCombine(time, point), 0.0});
62 
63  // Evaluate and apply if the insertion worked, i.e. the element didn't exist
64  if (result.second)
65  result.first->second = ptr->evaluateValue(time, point);
66 
67  // Return the cached value
68  return result.first->second;
69  }
70 
71  return ptr->evaluateValue(time, point);
72 }
73 
74 void
76 {
77  _enable_cache = use;
78 
79  if (!_enable_cache)
81 }
82 
83 void
85 {
86  _cache.clear();
87 }
MemoizedFunctionInterface::_respect_time
bool _respect_time
Flag for whether changes in time invalidate the cache.
Definition: MemoizedFunctionInterface.h:68
MemoizedFunctionInterface
Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash...
Definition: MemoizedFunctionInterface.h:28
hashing::hashCombine
void hashCombine(HashValue &)
Final iteration of the variadic template with no additional arguments.
Definition: Hashing.h:28
MemoizedFunctionInterface::_enable_cache
bool _enable_cache
Flag for whether to cache values.
Definition: MemoizedFunctionInterface.h:65
validParams< MemoizedFunctionInterface >
InputParameters validParams< MemoizedFunctionInterface >()
Definition: MemoizedFunctionInterface.C:14
MemoizedFunctionInterface::meshChanged
virtual void meshChanged() override
Definition: MemoizedFunctionInterface.C:40
MemoizedFunctionInterface::invalidateCache
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change.
Definition: MemoizedFunctionInterface.C:84
MemoizedFunctionInterface.h
MemoizedFunctionInterface::useCache
void useCache(bool use)
Enable/disable the cache.
Definition: MemoizedFunctionInterface.C:75
MemoizedFunctionInterface::_current_time
Real _current_time
Stores the time evaluation of the cache.
Definition: MemoizedFunctionInterface.h:62
MemoizedFunctionInterface::evaluateValue
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()
MemoizedFunctionInterface::MemoizedFunctionInterface
MemoizedFunctionInterface(const InputParameters &parameters)
Definition: MemoizedFunctionInterface.C:32
MemoizedFunctionInterface::value
virtual Real value(Real time, const Point &point) const final
Definition: MemoizedFunctionInterface.C:47
MemoizedFunctionInterface::_cache
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
Definition: MemoizedFunctionInterface.h:59