https://mooseframework.inl.gov
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Moose::MFEM::CoefficientMap< T, Tpw > Class Template Reference

Class to manage MFEM coefficient objects representing material properties. More...

#include <CoefficientMap.h>

Public Member Functions

 CoefficientMap ()=default
 
template<class P , class... Args>
std::shared_ptr< P > make (Args &&... args)
 Make arbitrary coefficients which will be tracked by this object. More...
 
void addCoefficient (const std::string &name, std::shared_ptr< T > coeff)
 Add a named global coefficient. More...
 
void addPiecewiseBlocks (const std::string &name, std::shared_ptr< T > coeff, const std::vector< std::string > &blocks)
 Add piecewise material property. More...
 
T & getCoefficient (const std::string &name)
 
std::shared_ptr< T > getCoefficientPtr (const std::string &name)
 
bool hasCoefficient (const std::string &name) const
 
bool propertyDefinedOnBlock (const std::string &name, const std::string &block) const
 
void setTime (const mfem::real_t time)
 

Private Types

using PWData = std::tuple< std::shared_ptr< Tpw >, std::map< const std::string, std::shared_ptr< T > >>
 

Private Member Functions

PWData emptyPWData (std::shared_ptr< T >)
 
void checkPWData (std::shared_ptr< T >, std::shared_ptr< Tpw >, const std::string &)
 

Private Attributes

std::map< const std::string, std::variant< std::shared_ptr< T >, PWData > > _coefficients
 
std::vector< std::shared_ptr< T > > _iterable_coefficients
 

Detailed Description

template<class T, class Tpw>
class Moose::MFEM::CoefficientMap< T, Tpw >

Class to manage MFEM coefficient objects representing material properties.

It can build up piecewise coefficients representing properties defined across multiple materials.

Definition at line 35 of file CoefficientMap.h.

Member Typedef Documentation

◆ PWData

template<class T, class Tpw>
using Moose::MFEM::CoefficientMap< T, Tpw >::PWData = std::tuple<std::shared_ptr<Tpw>, std::map<const std::string, std::shared_ptr<T> >>
private

Definition at line 147 of file CoefficientMap.h.

Constructor & Destructor Documentation

◆ CoefficientMap()

template<class T, class Tpw>
Moose::MFEM::CoefficientMap< T, Tpw >::CoefficientMap ( )
default

Member Function Documentation

◆ addCoefficient()

template<class T, class Tpw>
void Moose::MFEM::CoefficientMap< T, Tpw >::addCoefficient ( const std::string &  name,
std::shared_ptr< T >  coeff 
)
inline

Add a named global coefficient.

It must have been created with the make method on this object.

Definition at line 51 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientMap< mfem::MatrixCoefficient, mfem::PWMatrixCoefficient >::addPiecewiseBlocks(), Moose::MFEM::CoefficientManager::declareMatrix(), Moose::MFEM::CoefficientManager::declareScalar(), and Moose::MFEM::CoefficientManager::declareVector().

52  {
53  mooseAssert(std::find(this->_iterable_coefficients.cbegin(),
54  this->_iterable_coefficients.cend(),
55  coeff) != this->_iterable_coefficients.cend(),
56  "Coefficient object was not created by this CoefficientMap.");
57 
58  const auto [_, inserted] = this->_coefficients.emplace(name, std::move(coeff));
59  if (!inserted)
60  mooseError("Coefficient with name '" + name + "' already present in CoefficientMap object");
61  }
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
std::vector< std::shared_ptr< T > > _iterable_coefficients
std::map< const std::string, std::variant< std::shared_ptr< T >, PWData > > _coefficients

◆ addPiecewiseBlocks()

template<class T, class Tpw>
void Moose::MFEM::CoefficientMap< T, Tpw >::addPiecewiseBlocks ( const std::string &  name,
std::shared_ptr< T >  coeff,
const std::vector< std::string > &  blocks 
)
inline

Add piecewise material property.

The coefficient must have been created with the make method on this object.

Note: If you attempt to overwrite an existing block then an exception will be thrown and data for that property will be left in an undefined state.

Definition at line 67 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientManager::declareMatrixProperty(), Moose::MFEM::CoefficientManager::declareScalarProperty(), and Moose::MFEM::CoefficientManager::declareVectorProperty().

70  {
71  // If list of blocks is empty then treat as a global coefficient
72  if (blocks.size() == 0)
73  {
74  this->addCoefficient(name, coeff);
75  return;
76  }
77 
78  // Initialise property with empty coefficients, if it does not already exist
79  if (!this->hasCoefficient(name))
80  this->_coefficients.insert({name, this->emptyPWData(coeff)});
81  PWData * data = std::get_if<PWData>(&this->_coefficients[name]);
82  // Throw an exception if the data is not piecewise
83  if (!data)
84  mooseError("Global coefficient with name '" + name + "' already present in CoefficientMap");
85  mooseAssert(std::find(this->_iterable_coefficients.cbegin(),
86  this->_iterable_coefficients.cend(),
87  coeff) != this->_iterable_coefficients.cend(),
88  "Coefficient object was not created by the appropriate coefficient manager.");
89  auto & [pw_coeff, coeff_map] = *data;
90  this->checkPWData(coeff, pw_coeff, name);
91 
92  for (const auto & block : blocks)
93  {
94  if (coeff_map.count(block) > 0)
95  mooseError("Property with name '" + name + "' already assigned to block " + block +
96  " in CoefficientMap object");
97  coeff_map[block] = coeff;
98  pw_coeff->UpdateCoefficient(std::stoi(block), *coeff);
99  }
100  }
std::string name(const ElemQuality q)
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
PWData emptyPWData(std::shared_ptr< T >)
char ** blocks
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
std::tuple< std::shared_ptr< Tpw >, std::map< const std::string, std::shared_ptr< T > >> PWData
void addCoefficient(const std::string &name, std::shared_ptr< T > coeff)
Add a named global coefficient.
std::vector< std::shared_ptr< T > > _iterable_coefficients
std::map< const std::string, std::variant< std::shared_ptr< T >, PWData > > _coefficients
void checkPWData(std::shared_ptr< T >, std::shared_ptr< Tpw >, const std::string &)
bool hasCoefficient(const std::string &name) const

◆ checkPWData()

template<class T, class Tpw>
void Moose::MFEM::CoefficientMap< T, Tpw >::checkPWData ( std::shared_ptr< T >  ,
std::shared_ptr< Tpw >  ,
const std::string &   
)
inlineprivate

◆ emptyPWData()

template<class T, class Tpw>
PWData Moose::MFEM::CoefficientMap< T, Tpw >::emptyPWData ( std::shared_ptr< T >  )
inlineprivate

Definition at line 151 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientMap< mfem::MatrixCoefficient, mfem::PWMatrixCoefficient >::addPiecewiseBlocks().

152  {
153  return std::make_tuple(this->template make<Tpw>(),
154  std::map<const std::string, std::shared_ptr<T>>());
155  }

◆ getCoefficient()

template<class T, class Tpw>
T& Moose::MFEM::CoefficientMap< T, Tpw >::getCoefficient ( const std::string &  name)
inline

Definition at line 102 of file CoefficientMap.h.

102 { return *this->getCoefficientPtr(name); }
std::shared_ptr< T > getCoefficientPtr(const std::string &name)

◆ getCoefficientPtr()

template<class T, class Tpw>
std::shared_ptr<T> Moose::MFEM::CoefficientMap< T, Tpw >::getCoefficientPtr ( const std::string &  name)
inline

Definition at line 104 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientMap< mfem::MatrixCoefficient, mfem::PWMatrixCoefficient >::getCoefficient(), Moose::MFEM::CoefficientManager::getMatrixCoefficientPtr(), Moose::MFEM::CoefficientManager::getScalarCoefficientPtr(), and Moose::MFEM::CoefficientManager::getVectorCoefficientPtr().

105  {
106  try
107  {
108  auto & coeff = this->_coefficients.at(name);
109  try
110  {
111  return std::get<std::shared_ptr<T>>(coeff);
112  }
113  catch (const std::bad_variant_access &)
114  {
115  return std::get<0>(std::get<PWData>(coeff));
116  }
117  }
118  catch (const std::out_of_range &)
119  {
120  mooseError("Property with name '" + name + "' has not been declared.");
121  }
122  }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
std::map< const std::string, std::variant< std::shared_ptr< T >, PWData > > _coefficients

◆ hasCoefficient()

template<class T, class Tpw>
bool Moose::MFEM::CoefficientMap< T, Tpw >::hasCoefficient ( const std::string &  name) const
inline

◆ make()

template<class T, class Tpw>
template<class P , class... Args>
std::shared_ptr<P> Moose::MFEM::CoefficientMap< T, Tpw >::make ( Args &&...  args)
inline

Make arbitrary coefficients which will be tracked by this object.

Definition at line 42 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientManager::declareMatrix(), Moose::MFEM::CoefficientManager::declareMatrixProperty(), Moose::MFEM::CoefficientManager::declareScalar(), Moose::MFEM::CoefficientManager::declareScalarProperty(), Moose::MFEM::CoefficientManager::declareVector(), and Moose::MFEM::CoefficientManager::declareVectorProperty().

43  {
44  auto result = std::make_shared<P>(args...);
45  this->_iterable_coefficients.push_back(result);
46  return result;
47  }
std::vector< std::shared_ptr< T > > _iterable_coefficients

◆ propertyDefinedOnBlock()

template<class T, class Tpw>
bool Moose::MFEM::CoefficientMap< T, Tpw >::propertyDefinedOnBlock ( const std::string &  name,
const std::string &  block 
) const
inline

Definition at line 129 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientManager::matrixPropertyIsDefined(), Moose::MFEM::CoefficientManager::scalarPropertyIsDefined(), and Moose::MFEM::CoefficientManager::vectorPropertyIsDefined().

130  {
131  if (!this->hasCoefficient(name))
132  return false;
133  auto & coeff = libmesh_map_find(this->_coefficients, name);
134  if (std::holds_alternative<std::shared_ptr<T>>(coeff))
135  return true;
136  auto block_map = std::get<1>(std::get<PWData>(coeff));
137  return block_map.count(block) > 0;
138  }
std::map< const std::string, std::variant< std::shared_ptr< T >, PWData > > _coefficients
bool hasCoefficient(const std::string &name) const

◆ setTime()

template<class T, class Tpw>
void Moose::MFEM::CoefficientMap< T, Tpw >::setTime ( const mfem::real_t  time)
inline

Definition at line 140 of file CoefficientMap.h.

Referenced by Moose::MFEM::CoefficientManager::setTime().

141  {
142  for (auto & coef : this->_iterable_coefficients)
143  coef->SetTime(time);
144  }
std::vector< std::shared_ptr< T > > _iterable_coefficients

Member Data Documentation

◆ _coefficients

template<class T, class Tpw>
std::map<const std::string, std::variant<std::shared_ptr<T>, PWData> > Moose::MFEM::CoefficientMap< T, Tpw >::_coefficients
private

◆ _iterable_coefficients

template<class T, class Tpw>
std::vector<std::shared_ptr<T> > Moose::MFEM::CoefficientMap< T, Tpw >::_iterable_coefficients
private

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