https://mooseframework.inl.gov
MemoizedFunctionInterface.h
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 
10 #pragma once
11 
12 #include <map>
13 
14 #include "Function.h"
15 
16 #include "Hashing.h"
17 
24 {
25 public:
27 
29 
30  // Override from MeshChangedInterface
31  virtual void meshChanged() override;
32 
36  void useCache(bool use);
37 
38  using Function::value;
39  // Make this implementation of Function::value() final so derived classes cannot bypass the
40  // memoization functionality it implements. Instead, deriving classes should implement
41  // evaluateValue().
42  virtual Real value(Real time, const Point & point) const final;
43  virtual ADReal value(const ADReal & time, const ADPoint & point) const final;
44 
45 protected:
49  virtual Real evaluateValue(Real time, const Point & point) = 0;
50 
54  void invalidateCache();
55 
56 private:
58  mutable std::unordered_map<hashing::HashValue, Real> _cache;
59 
62 
65 
68 };
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...
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.
DualNumber< Real, DNDerivativeType, true > ADReal
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)
virtual void meshChanged() override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void useCache(bool use)
Enable/disable the cache.
const InputParameters & parameters() const
Implementation of Function that memoizes (caches) former evaluations in an unordered map using a hash...
virtual Real value(Real t, const Point &p) const
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()