www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
MutableCoefficientsFunctionInterface Class Referenceabstract

Interface for a type of functions using coefficients that may be changed before or after a solve. More...

#include <MutableCoefficientsFunctionInterface.h>

Inheritance diagram for MutableCoefficientsFunctionInterface:
[legend]

Public Member Functions

 MutableCoefficientsFunctionInterface (const MooseObject *moose_object, const InputParameters &parameters)
 
virtual void meshChanged () override
 
void useCache (bool use)
 Enable/disable the cache. More...
 
virtual Real value (Real time, const Point &point) const final
 
Real operator[] (std::size_t index) const
 Get the value of the coefficient at the corresponding index. More...
 
const std::vector< std::size_t > & getCharacteristics () const
 Get a reference to the characteristics array. More...
 
const std::vector< Real > & getCoefficients () const
 Get a read-only reference to the vector of coefficients. More...
 
std::vector< Real > & getCoefficients ()
 Get a writeable reference to the vector of coefficients. More...
 
std::string getCoefficientsTable () const
 Get a formatted string of the coefficients. More...
 
std::size_t getSize () const
 Get the size, aka number of coefficients. More...
 
bool isCompatibleWith (const MutableCoefficientsInterface &other) const
 Checks to see if another instance is compatible. More...
 
bool isSizeEnforced () const
 Returns true if the size of the coefficient array is fixed and enforced. More...
 
void enforceSize (bool enforce)
 Toggle whether the size of the coefficient array can be changed. More...
 
void importCoefficients (const MutableCoefficientsInterface &other)
 Import the coefficients from another instance. More...
 
void resize (std::size_t size, Real fill=0.0, bool fill_out_to_size=true)
 Resize the array, using the value for fill if the new size is larger. More...
 
void setCharacteristics (const std::vector< std::size_t > &new_characteristics)
 Sets the characteristics array. More...
 
void setCoefficients (const std::vector< Real > &new_coefficients)
 Set the coefficients using a copy operation. More...
 
void setCoefficients (std::vector< Real > &&dropin_coefficients)
 Set the coefficients using a move operation (only works with temp objects) More...
 

Protected Member Functions

virtual void coefficientsChanged () override
 Called when the coefficients have been changed. More...
 
virtual Real evaluateValue (Real time, const Point &point)=0
 Used in derived classes, equivalent to Function::value() More...
 
void invalidateCache ()
 Called by derived classes to invalidate the cache, perhaps due to a state change. More...
 

Protected Attributes

std::vector< std::size_t > & _characteristics
 An array of integer characteristics that can be used to check compatibility. More...
 
std::vector< Real > & _coefficients
 The coefficient array. More...
 
bool _enforce_size
 Boolean that locks or allows resizing of the coefficient array. More...
 
const bool _print_coefficients
 Boolean to flag if the coefficients should be printed when set. More...
 

Private Attributes

std::unordered_map< hashing::HashValue, Real > _cache
 Cached evaluations for each point. More...
 
Real _current_time
 Stores the time evaluation of the cache. More...
 
bool _enable_cache
 Flag for whether to cache values. More...
 
bool _respect_time
 Flag for whether changes in time invalidate the cache. More...
 
const ConsoleStream & _console
 MooseObject instance of this to provide access to _console More...
 

Detailed Description

Interface for a type of functions using coefficients that may be changed before or after a solve.

Definition at line 25 of file MutableCoefficientsFunctionInterface.h.

Constructor & Destructor Documentation

◆ MutableCoefficientsFunctionInterface()

MutableCoefficientsFunctionInterface::MutableCoefficientsFunctionInterface ( const MooseObject *  moose_object,
const InputParameters &  parameters 
)

Definition at line 28 of file MutableCoefficientsFunctionInterface.C.

30  : MemoizedFunctionInterface(parameters),
31  FunctionInterface(this),
32  MutableCoefficientsInterface(moose_object, parameters)
33 {
34  if (isParamValid("coefficients"))
35  setCoefficients(getParam<std::vector<Real>>("coefficients")), enforceSize(true);
36 }

Member Function Documentation

◆ coefficientsChanged()

void MutableCoefficientsFunctionInterface::coefficientsChanged ( )
overrideprotectedvirtual

Called when the coefficients have been changed.

Reimplemented from MutableCoefficientsInterface.

Definition at line 39 of file MutableCoefficientsFunctionInterface.C.

40 {
41  // The coefficients have changed, which invalidates the cache
43 }

◆ enforceSize()

void MutableCoefficientsInterface::enforceSize ( bool  enforce)
inherited

Toggle whether the size of the coefficient array can be changed.

Definition at line 111 of file MutableCoefficientsInterface.C.

112 {
113  _enforce_size = enforce;
114 }

Referenced by FunctionSeries::FunctionSeries(), and MutableCoefficientsFunctionInterface().

◆ evaluateValue()

virtual Real MemoizedFunctionInterface::evaluateValue ( Real  time,
const Point &  point 
)
protectedpure virtualinherited

Used in derived classes, equivalent to Function::value()

Implemented in FunctionSeries.

Referenced by MemoizedFunctionInterface::value().

◆ getCharacteristics()

const std::vector< std::size_t > & MutableCoefficientsInterface::getCharacteristics ( ) const
inherited

Get a reference to the characteristics array.

Definition at line 51 of file MutableCoefficientsInterface.C.

52 {
53  return _characteristics;
54 }

◆ getCoefficients() [1/2]

std::vector< Real > & MutableCoefficientsInterface::getCoefficients ( )
inherited

Get a writeable reference to the vector of coefficients.

Definition at line 63 of file MutableCoefficientsInterface.C.

64 {
65  return _coefficients;
66 }

◆ getCoefficients() [2/2]

const std::vector< Real > & MutableCoefficientsInterface::getCoefficients ( ) const
inherited

Get a read-only reference to the vector of coefficients.

Definition at line 57 of file MutableCoefficientsInterface.C.

58 {
59  return _coefficients;
60 }

◆ getCoefficientsTable()

std::string MutableCoefficientsInterface::getCoefficientsTable ( ) const
inherited

Get a formatted string of the coefficients.

Definition at line 69 of file MutableCoefficientsInterface.C.

70 {
71  std::stringbuf string;
72  std::ostream table(&string);
73 
74  table << *this;
75 
76  return string.str();
77 }

◆ getSize()

std::size_t MutableCoefficientsInterface::getSize ( ) const
inherited

Get the size, aka number of coefficients.

Definition at line 80 of file MutableCoefficientsInterface.C.

81 {
82  return _coefficients.size();
83 }

Referenced by MutableCoefficientsInterface::isCompatibleWith(), and operator<<().

◆ importCoefficients()

void MutableCoefficientsInterface::importCoefficients ( const MutableCoefficientsInterface other)
inherited

Import the coefficients from another instance.

Definition at line 117 of file MutableCoefficientsInterface.C.

118 {
119  if (!isCompatibleWith(other))
120  mooseError("Cannot import coefficients from incompatible MutableCoefficientsInterface");
121 
123 
125  _console << *this;
126 
128 }

Referenced by MultiAppFXTransfer::execute().

◆ invalidateCache()

void MemoizedFunctionInterface::invalidateCache ( )
protectedinherited

Called by derived classes to invalidate the cache, perhaps due to a state change.

Definition at line 84 of file MemoizedFunctionInterface.C.

85 {
86  _cache.clear();
87 }

Referenced by coefficientsChanged(), MemoizedFunctionInterface::meshChanged(), MemoizedFunctionInterface::useCache(), and MemoizedFunctionInterface::value().

◆ isCompatibleWith()

bool MutableCoefficientsInterface::isCompatibleWith ( const MutableCoefficientsInterface other) const
inherited

Checks to see if another instance is compatible.

Definition at line 86 of file MutableCoefficientsInterface.C.

87 {
88  // Check the coefficient sizes if requested
89  if ((_enforce_size && other._enforce_size) && getSize() != other.getSize())
90  return false;
91 
92  // Check the size of the characteristics array
93  if (_characteristics.size() != other._characteristics.size())
94  return false;
95 
96  // Check the values of the characteristics array
97  for (std::size_t i = 0; i < _characteristics.size(); ++i)
98  if (_characteristics[i] != other._characteristics[i])
99  return false;
100 
101  return true;
102 }

Referenced by MultiAppFXTransfer::execute(), and MutableCoefficientsInterface::importCoefficients().

◆ isSizeEnforced()

bool MutableCoefficientsInterface::isSizeEnforced ( ) const
inherited

Returns true if the size of the coefficient array is fixed and enforced.

Definition at line 105 of file MutableCoefficientsInterface.C.

106 {
107  return _enforce_size;
108 }

◆ meshChanged()

void MemoizedFunctionInterface::meshChanged ( )
overridevirtualinherited

Definition at line 40 of file MemoizedFunctionInterface.C.

41 {
42  // The mesh has changed, which invalidates the cache
44 }

◆ operator[]()

Real MutableCoefficientsInterface::operator[] ( std::size_t  index) const
inherited

Get the value of the coefficient at the corresponding index.

Definition at line 45 of file MutableCoefficientsInterface.C.

46 {
47  return _coefficients[index];
48 }

◆ resize()

void MutableCoefficientsInterface::resize ( std::size_t  size,
Real  fill = 0.0,
bool  fill_out_to_size = true 
)
inherited

Resize the array, using the value for fill if the new size is larger.

Definition at line 131 of file MutableCoefficientsInterface.C.

132 {
133  if (size != _coefficients.size())
134  {
135  if (_enforce_size &&
136  (size > _coefficients.size() || (size < _coefficients.size() && !fill_out_to_size)))
137  mooseError("Cannot resize coefficient array with size enforcement enabled.");
138 
139  _coefficients.resize(size, fill);
140 
142  _console << *this;
143 
145  }
146 }

Referenced by FunctionSeries::FunctionSeries().

◆ setCharacteristics()

void MutableCoefficientsInterface::setCharacteristics ( const std::vector< std::size_t > &  new_characteristics)
inherited

Sets the characteristics array.

Definition at line 149 of file MutableCoefficientsInterface.C.

151 {
152  _characteristics = new_characteristics;
153 }

Referenced by FunctionSeries::FunctionSeries().

◆ setCoefficients() [1/2]

void MutableCoefficientsInterface::setCoefficients ( const std::vector< Real > &  new_coefficients)
inherited

Set the coefficients using a copy operation.

Definition at line 156 of file MutableCoefficientsInterface.C.

157 {
158  if (_enforce_size && new_coefficients.size() != _coefficients.size())
159  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
160  "enabled.");
161 
162  _coefficients = new_coefficients;
163 
165  _console << *this;
166 
168 }

Referenced by MutableCoefficientsFunctionInterface().

◆ setCoefficients() [2/2]

void MutableCoefficientsInterface::setCoefficients ( std::vector< Real > &&  dropin_coefficients)
inherited

Set the coefficients using a move operation (only works with temp objects)

Definition at line 171 of file MutableCoefficientsInterface.C.

172 {
173  if (_enforce_size && dropin_coefficients.size() != _coefficients.size())
174  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
175  "enabled.");
176 
177  _coefficients = dropin_coefficients;
178 
180  _console << *this;
181 
183 }

◆ useCache()

void MemoizedFunctionInterface::useCache ( bool  use)
inherited

Enable/disable the cache.

Definition at line 75 of file MemoizedFunctionInterface.C.

76 {
77  _enable_cache = use;
78 
79  if (!_enable_cache)
81 }

Referenced by FXFluxBC::FXFluxBC(), FXValueBC::FXValueBC(), and FXValuePenaltyBC::FXValuePenaltyBC().

◆ value()

Real MemoizedFunctionInterface::value ( Real  time,
const Point &  point 
) const
finalvirtualinherited

Definition at line 47 of file MemoizedFunctionInterface.C.

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 }

Member Data Documentation

◆ _cache

std::unordered_map<hashing::HashValue, Real> MemoizedFunctionInterface::_cache
mutableprivateinherited

Cached evaluations for each point.

Definition at line 59 of file MemoizedFunctionInterface.h.

Referenced by MemoizedFunctionInterface::invalidateCache(), and MemoizedFunctionInterface::value().

◆ _characteristics

std::vector<std::size_t>& MutableCoefficientsInterface::_characteristics
protectedinherited

◆ _coefficients

std::vector<Real>& MutableCoefficientsInterface::_coefficients
protectedinherited

◆ _console

const ConsoleStream& MutableCoefficientsInterface::_console
privateinherited

MooseObject instance of this to provide access to _console

Definition at line 123 of file MutableCoefficientsInterface.h.

Referenced by MutableCoefficientsInterface::importCoefficients(), MutableCoefficientsInterface::resize(), and MutableCoefficientsInterface::setCoefficients().

◆ _current_time

Real MemoizedFunctionInterface::_current_time
mutableprivateinherited

Stores the time evaluation of the cache.

Definition at line 62 of file MemoizedFunctionInterface.h.

Referenced by MemoizedFunctionInterface::value().

◆ _enable_cache

bool MemoizedFunctionInterface::_enable_cache
privateinherited

Flag for whether to cache values.

Definition at line 65 of file MemoizedFunctionInterface.h.

Referenced by MemoizedFunctionInterface::useCache(), and MemoizedFunctionInterface::value().

◆ _enforce_size

bool MutableCoefficientsInterface::_enforce_size
protectedinherited

◆ _print_coefficients

const bool MutableCoefficientsInterface::_print_coefficients
protectedinherited

Boolean to flag if the coefficients should be printed when set.

Definition at line 119 of file MutableCoefficientsInterface.h.

Referenced by MutableCoefficientsInterface::importCoefficients(), MutableCoefficientsInterface::resize(), and MutableCoefficientsInterface::setCoefficients().

◆ _respect_time

bool MemoizedFunctionInterface::_respect_time
privateinherited

Flag for whether changes in time invalidate the cache.

Definition at line 68 of file MemoizedFunctionInterface.h.

Referenced by MemoizedFunctionInterface::value().


The documentation for this class was generated from the following files:
MutableCoefficientsInterface::_print_coefficients
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
Definition: MutableCoefficientsInterface.h:119
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
MutableCoefficientsInterface::_console
const ConsoleStream & _console
MooseObject instance of this to provide access to _console
Definition: MutableCoefficientsInterface.h:123
MemoizedFunctionInterface::_enable_cache
bool _enable_cache
Flag for whether to cache values.
Definition: MemoizedFunctionInterface.h:65
MutableCoefficientsInterface::_coefficients
std::vector< Real > & _coefficients
The coefficient array.
Definition: MutableCoefficientsInterface.h:113
MutableCoefficientsInterface::_characteristics
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.
Definition: MutableCoefficientsInterface.h:107
MutableCoefficientsInterface::getSize
std::size_t getSize() const
Get the size, aka number of coefficients.
Definition: MutableCoefficientsInterface.C:80
MemoizedFunctionInterface::invalidateCache
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change.
Definition: MemoizedFunctionInterface.C:84
MutableCoefficientsInterface::enforceSize
void enforceSize(bool enforce)
Toggle whether the size of the coefficient array can be changed.
Definition: MutableCoefficientsInterface.C:111
MutableCoefficientsInterface::coefficientsChanged
virtual void coefficientsChanged()
Called when the coefficients have been changed.
Definition: MutableCoefficientsInterface.h:107
MutableCoefficientsInterface::setCoefficients
void setCoefficients(const std::vector< Real > &new_coefficients)
Set the coefficients using a copy operation.
Definition: MutableCoefficientsInterface.C:156
MutableCoefficientsInterface::MutableCoefficientsInterface
MutableCoefficientsInterface(const MooseObject *moose_object, const InputParameters &parameters)
Definition: MutableCoefficientsInterface.C:31
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()
MutableCoefficientsInterface::isCompatibleWith
bool isCompatibleWith(const MutableCoefficientsInterface &other) const
Checks to see if another instance is compatible.
Definition: MutableCoefficientsInterface.C:86
MemoizedFunctionInterface::MemoizedFunctionInterface
MemoizedFunctionInterface(const InputParameters &parameters)
Definition: MemoizedFunctionInterface.C:32
MutableCoefficientsInterface::_enforce_size
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
Definition: MutableCoefficientsInterface.h:116
MemoizedFunctionInterface::_cache
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
Definition: MemoizedFunctionInterface.h:59