www.mooseframework.org
MemoizedFunctionInterface.h
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 
10 #pragma once
11 
12 #include <map>
13 
14 #include "Function.h"
15 
16 #include "Hashing.h"
17 
19 
20 template <>
22 
28 class MemoizedFunctionInterface : public Function
29 {
30 public:
31  MemoizedFunctionInterface(const InputParameters & parameters);
32 
33  // Override from MeshChangedInterface
34  virtual void meshChanged() override;
35 
39  void useCache(bool use);
40 
41  // Make this implementation of Function::value() final so derived classes cannot bypass the
42  // memoization functionality it implements. Instead, deriving classes should implement
43  // evaluateValue().
44  virtual Real value(Real time, const Point & point) const final;
45 
46 protected:
50  virtual Real evaluateValue(Real time, const Point & point) = 0;
51 
55  void invalidateCache();
56 
57 private:
59  mutable std::unordered_map<hashing::HashValue, Real> _cache;
60 
62  mutable Real _current_time;
63 
66 
69 };
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.h
MemoizedFunctionInterface::_enable_cache
bool _enable_cache
Flag for whether to cache values.
Definition: MemoizedFunctionInterface.h:65
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
validParams< MemoizedFunctionInterface >
InputParameters validParams< MemoizedFunctionInterface >()
Definition: MemoizedFunctionInterface.C:14
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