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 
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 
38 void
40 {
41  // The mesh has changed, which invalidates the cache
43 }
44 
45 Real
46 MemoizedFunctionInterface::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 
73 ADReal
75 {
76  mooseError("Not implemented");
77 }
78 
79 void
81 {
82  _enable_cache = use;
83 
84  if (!_enable_cache)
86 }
87 
88 void
90 {
91  _cache.clear();
92 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
bool _enable_cache
Flag for whether to cache values.
virtual Real value(Real time, const Point &point) const final
Real _current_time
Stores the time evaluation of the cache.
void hashCombine(HashValue &)
Final iteration of the variadic template with no additional arguments.
Definition: Hashing.h:28
static InputParameters validParams()
bool _respect_time
Flag for whether changes in time invalidate the cache.
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
MemoizedFunctionInterface(const InputParameters &parameters)
DualReal ADReal
virtual void meshChanged() override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void mooseError(Args &&... args) const
void useCache(bool use)
Enable/disable the cache.
void addClassDescription(const std::string &doc_string)
Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash...
static InputParameters validParams()
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()