www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected 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) 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...
 

Detailed Description

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

Definition at line 26 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 }
void setCoefficients(const std::vector< Real > &new_coefficients)
Set the coefficients using a copy operation.
MutableCoefficientsInterface(const MooseObject *moose_object, const InputParameters &parameters)
MemoizedFunctionInterface(const InputParameters &parameters)
void enforceSize(bool enforce)
Toggle whether the size of the coefficient array can be changed.

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 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...

◆ enforceSize()

void MutableCoefficientsInterface::enforceSize ( bool  enforce)
inherited

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

Definition at line 110 of file MutableCoefficientsInterface.C.

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

111 {
112  _enforce_size = enforce;
113 }
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.

◆ 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 50 of file MutableCoefficientsInterface.C.

51 {
52  return _characteristics;
53 }
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.

◆ getCoefficients() [1/2]

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

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

Definition at line 56 of file MutableCoefficientsInterface.C.

57 {
58  return _coefficients;
59 }
std::vector< Real > & _coefficients
The coefficient array.

◆ getCoefficients() [2/2]

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

Get a writeable reference to the vector of coefficients.

Definition at line 62 of file MutableCoefficientsInterface.C.

63 {
64  return _coefficients;
65 }
std::vector< Real > & _coefficients
The coefficient array.

◆ getCoefficientsTable()

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

Get a formatted string of the coefficients.

Definition at line 68 of file MutableCoefficientsInterface.C.

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

◆ getSize()

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

Get the size, aka number of coefficients.

Definition at line 79 of file MutableCoefficientsInterface.C.

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

80 {
81  return _coefficients.size();
82 }
std::vector< Real > & _coefficients
The coefficient array.

◆ importCoefficients()

void MutableCoefficientsInterface::importCoefficients ( const MutableCoefficientsInterface other)
inherited

Import the coefficients from another instance.

Definition at line 116 of file MutableCoefficientsInterface.C.

Referenced by MultiAppFXTransfer::execute().

117 {
118  if (!isCompatibleWith(other))
119  mooseError("Cannot import coefficients from incompatible MutableCoefficientsInterface");
120 
122 
124  _console << *this;
125 
127 }
virtual void coefficientsChanged()
Called when the coefficients have been changed.
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
bool isCompatibleWith(const MutableCoefficientsInterface &other) const
Checks to see if another instance is compatible.
std::vector< Real > & _coefficients
The coefficient array.
const ConsoleStream & _console
MooseObject instance of this to provide access to _console

◆ invalidateCache()

void MemoizedFunctionInterface::invalidateCache ( )
protectedinherited

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

Definition at line 82 of file MemoizedFunctionInterface.C.

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

83 {
84  _cache.clear();
85 }
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.

◆ isCompatibleWith()

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

Checks to see if another instance is compatible.

Definition at line 85 of file MutableCoefficientsInterface.C.

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

86 {
87  // Check the coefficient sizes if requested
88  if ((_enforce_size && other._enforce_size) && getSize() != other.getSize())
89  return false;
90 
91  // Check the size of the characteristics array
92  if (_characteristics.size() != other._characteristics.size())
93  return false;
94 
95  // Check the values of the characteristics array
96  for (std::size_t i = 0; i < _characteristics.size(); ++i)
97  if (_characteristics[i] != other._characteristics[i])
98  return false;
99 
100  return true;
101 }
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.
std::size_t getSize() const
Get the size, aka number of coefficients.

◆ isSizeEnforced()

bool MutableCoefficientsInterface::isSizeEnforced ( ) const
inherited

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

Definition at line 104 of file MutableCoefficientsInterface.C.

105 {
106  return _enforce_size;
107 }
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.

◆ meshChanged()

void MemoizedFunctionInterface::meshChanged ( )
overridevirtualinherited

Definition at line 40 of file MemoizedFunctionInterface.C.

41 {
42  // The mesh has changed, which invalidates the cache
44 }
void invalidateCache()
Called by derived classes to invalidate the cache, perhaps due to a state change. ...

◆ operator[]()

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

Get the value of the coefficient at the corresponding index.

Definition at line 44 of file MutableCoefficientsInterface.C.

45 {
46  return _coefficients[index];
47 }
std::vector< Real > & _coefficients
The coefficient array.

◆ 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 130 of file MutableCoefficientsInterface.C.

Referenced by FunctionSeries::FunctionSeries().

131 {
132  if (size != _coefficients.size())
133  {
134  if (_enforce_size &&
135  (size > _coefficients.size() || (size < _coefficients.size() && !fill_out_to_size)))
136  mooseError("Cannot resize coefficient array with size enforcement enabled.");
137 
138  _coefficients.resize(size, fill);
139 
141  _console << *this;
142 
144  }
145 }
virtual void coefficientsChanged()
Called when the coefficients have been changed.
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
std::vector< Real > & _coefficients
The coefficient array.
const ConsoleStream & _console
MooseObject instance of this to provide access to _console

◆ setCharacteristics()

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

Sets the characteristics array.

Definition at line 148 of file MutableCoefficientsInterface.C.

Referenced by FunctionSeries::FunctionSeries().

150 {
151  _characteristics = new_characteristics;
152 }
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.

◆ setCoefficients() [1/2]

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

Set the coefficients using a copy operation.

Definition at line 155 of file MutableCoefficientsInterface.C.

Referenced by MutableCoefficientsFunctionInterface().

156 {
157  if (_enforce_size && new_coefficients.size() != _coefficients.size())
158  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
159  "enabled.");
160 
161  _coefficients = new_coefficients;
162 
164  _console << *this;
165 
167 }
virtual void coefficientsChanged()
Called when the coefficients have been changed.
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
std::vector< Real > & _coefficients
The coefficient array.
const ConsoleStream & _console
MooseObject instance of this to provide access to _console

◆ 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 170 of file MutableCoefficientsInterface.C.

171 {
172  if (_enforce_size && dropin_coefficients.size() != _coefficients.size())
173  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
174  "enabled.");
175 
176  _coefficients = dropin_coefficients;
177 
179  _console << *this;
180 
182 }
virtual void coefficientsChanged()
Called when the coefficients have been changed.
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
std::vector< Real > & _coefficients
The coefficient array.
const ConsoleStream & _console
MooseObject instance of this to provide access to _console

◆ useCache()

void MemoizedFunctionInterface::useCache ( bool  use)
inherited

Enable/disable the cache.

Definition at line 73 of file MemoizedFunctionInterface.C.

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

74 {
75  _enable_cache = use;
76 
77  if (!_enable_cache)
79 }
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.

◆ value()

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

Definition at line 47 of file MemoizedFunctionInterface.C.

48 {
49  if (_enable_cache)
50  {
51  // Start the cache over if we are at a new time step
52  if (_respect_time && time != _current_time)
53  {
54  _current_time = time;
56  }
57 
58  // Try to insert a new value into the cache
59  auto result = _cache.insert({hashing::hashCombine(time, point), 0.0});
60 
61  // Evaluate and apply if the insertion worked, i.e. the element didn't exist
62  if (result.second)
63  result.first->second = evaluateValue(time, point);
64 
65  // Return the cached value
66  return result.first->second;
67  }
68 
69  return evaluateValue(time, point);
70 }
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.
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:27
bool _respect_time
Flag for whether changes in time invalidate the cache.
std::unordered_map< hashing::HashValue, Real > _cache
Cached evaluations for each point.
virtual Real evaluateValue(Real time, const Point &point)=0
Used in derived classes, equivalent to Function::value()

Member Data Documentation

◆ _characteristics

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

◆ _coefficients

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

◆ _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 120 of file MutableCoefficientsInterface.h.

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


The documentation for this class was generated from the following files: