libMesh
Classes | Public Types | Public Member Functions | Protected Attributes | List of all members
libMesh::Parameters Class Reference

This class provides the ability to map between arbitrary, user-defined strings and several data types. More...

#include <parameters.h>

Classes

class  Parameter
 Concrete definition of a parameter value for a specified type. More...
 
class  Value
 Abstract definition of a parameter value. More...
 

Public Types

typedef std::map< std::string, std::unique_ptr< Value >, std::less<> > map_type
 The type of the map that we store internally. More...
 
typedef map_type::iterator iterator
 Parameter map iterator. More...
 
typedef map_type::const_iterator const_iterator
 Constant parameter map iterator. More...
 

Public Member Functions

 Parameters ()=default
 Default constructor. More...
 
 Parameters (const Parameters &)
 Copy constructor. More...
 
virtual ~Parameters ()=default
 Destructor. More...
 
virtual Parametersoperator= (const Parameters &source)
 Assignment operator. More...
 
virtual Parametersoperator+= (const Parameters &source)
 Addition/Assignment operator. More...
 
template<typename T >
bool have_parameter (std::string_view) const
 
template<typename T >
const T & get (std::string_view) const
 
template<typename T >
void insert (const std::string &)
 Inserts a new Parameter into the object but does not return a writable reference. More...
 
template<typename T >
T & set (const std::string &)
 
virtual void set_attributes (const std::string &, bool)
 Overridable function to set any extended attributes for classes inheriting from this class. More...
 
void remove (std::string_view)
 Removes the specified parameter from the list, if it exists. More...
 
std::size_t n_parameters () const
 
template<typename T >
unsigned int n_parameters () const
 
virtual void clear ()
 Clears internal data structures & frees any allocated memory. More...
 
void print (std::ostream &os=libMesh::out) const
 Prints the contents, by default to libMesh::out. More...
 
iterator begin ()
 Iterator pointing to the beginning of the set of parameters. More...
 
const_iterator begin () const
 Iterator pointing to the beginning of the set of parameters. More...
 
iterator end ()
 Iterator pointing to the end of the set of parameters. More...
 
const_iterator end () const
 Iterator pointing to the end of the set of parameters. More...
 

Protected Attributes

map_type _values
 Data structure to map names with values. More...
 

Detailed Description

This class provides the ability to map between arbitrary, user-defined strings and several data types.

This can be used to provide arbitrary user-specified options.

Author
Benjamin S. Kirk
Date
2004

Definition at line 71 of file parameters.h.

Member Typedef Documentation

◆ const_iterator

typedef map_type::const_iterator libMesh::Parameters::const_iterator

Constant parameter map iterator.

Definition at line 261 of file parameters.h.

◆ iterator

typedef map_type::iterator libMesh::Parameters::iterator

Parameter map iterator.

Definition at line 256 of file parameters.h.

◆ map_type

typedef std::map<std::string, std::unique_ptr<Value>, std::less<> > libMesh::Parameters::map_type

The type of the map that we store internally.

Definition at line 251 of file parameters.h.

Constructor & Destructor Documentation

◆ Parameters() [1/2]

libMesh::Parameters::Parameters ( )
default

Default constructor.

◆ Parameters() [2/2]

libMesh::Parameters::Parameters ( const Parameters p)
inline

Copy constructor.

Makes an independent copy by cloning the contents of the passed-in Parameters object.

Definition at line 355 of file parameters.h.

356 {
357  // calls assignment operator
358  *this = p;
359 }

◆ ~Parameters()

virtual libMesh::Parameters::~Parameters ( )
virtualdefault

Destructor.

Member Function Documentation

◆ begin() [1/2]

Parameters::iterator libMesh::Parameters::begin ( )
inline

Iterator pointing to the beginning of the set of parameters.

Definition at line 514 of file parameters.h.

References _values.

515 {
516  return _values.begin();
517 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ begin() [2/2]

Parameters::const_iterator libMesh::Parameters::begin ( ) const
inline

Iterator pointing to the beginning of the set of parameters.

Definition at line 520 of file parameters.h.

References _values.

521 {
522  return _values.begin();
523 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ clear()

void libMesh::Parameters::clear ( )
inlinevirtual

Clears internal data structures & frees any allocated memory.

Definition at line 328 of file parameters.h.

References _values.

Referenced by libMesh::EquationSystems::clear(), and operator=().

329 {
330  _values.clear();
331 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ end() [1/2]

Parameters::iterator libMesh::Parameters::end ( )
inline

Iterator pointing to the end of the set of parameters.

Definition at line 526 of file parameters.h.

References _values.

527 {
528  return _values.end();
529 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ end() [2/2]

Parameters::const_iterator libMesh::Parameters::end ( ) const
inline

Iterator pointing to the end of the set of parameters.

Definition at line 532 of file parameters.h.

References _values.

533 {
534  return _values.end();
535 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ get()

template<typename T >
const T & libMesh::Parameters::get ( std::string_view  name) const
inline
Returns
A constant reference to the specified parameter value. Requires, of course, that the parameter exists.

Definition at line 430 of file parameters.h.

References _values, libMesh::demangle(), libMesh::libmesh_assert(), and libMesh::Quality::name().

Referenced by add_M_C_K_helmholtz(), assemble_cd(), assemble_divgrad(), assemble_ellipticdg(), assemble_func(), assemble_helmholtz(), assemble_poisson(), assemble_SchroedingerEquation(), assemble_shell(), assemble_stokes(), assemble_wave(), libMesh::FrequencySystem::clear_all(), libMesh::compute_error(), compute_jacobian(), compute_residual(), LinearElasticityWithContact::compute_stresses(), LargeDeformationElasticity::compute_stresses(), designed_for_side_elems(), exact_derivative(), exact_solution(), exact_value(), fe_assembly(), fill_dirichlet_bc(), libMesh::ImplicitSystem::get_linear_solve_parameters(), libMesh::FEComputeData::init(), libMesh::FrequencySystem::init_data(), Biharmonic::JR::InitialDensityBall(), Biharmonic::JR::InitialDensityRod(), Biharmonic::JR::InitialDensityStrip(), LargeDeformationElasticity::jacobian(), line_print(), main(), libMesh::FrequencySystem::n_frequencies(), LargeDeformationElasticity::residual(), LinearElasticityWithContact::residual_and_jacobian(), libMesh::FrequencySystem::set_current_frequency(), libMesh::NonlinearImplicitSystem::set_solver_parameters(), SolidSystem::side_time_derivative(), libMesh::RBConstruction::solve_for_matrix_and_rhs(), libMesh::EigenSystem::solve_helper(), ParametersTest::testMap(), and ParametersTest::testScalar().

431 {
432  if (!this->have_parameter<T>(name))
433  {
434  std::ostringstream oss;
435 
436  oss << "ERROR: no";
437 #ifdef LIBMESH_HAVE_RTTI
438  oss << ' ' << demangle(typeid(T).name());
439 #endif
440  oss << " parameter named \""
441  << name << "\" found.\n\n"
442  << "Known parameters:\n"
443  << *this;
444 
445  libmesh_error_msg(oss.str());
446  }
447 
449 
450  libmesh_assert(it != _values.end());
451  libmesh_assert(it->second);
452 
453  // Get pointer to derived type
454  auto ptr = cast_ptr<Parameter<T> *>(it->second.get());
455 
456  // Return const reference
457  return ptr->get();
458 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
libmesh_assert(ctx)
std::string demangle(const char *name)
Mostly system independent demangler.
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:261

◆ have_parameter()

template<typename T >
bool libMesh::Parameters::have_parameter ( std::string_view  name) const
inline
Returns
true if a parameter of type T with a specified name exists, false otherwise.

If RTTI has been disabled then we return true if a parameter of specified name exists regardless of its type.

Definition at line 399 of file parameters.h.

References _values, libMesh::cast_ptr(), and libMesh::Quality::name().

Referenced by libMesh::FrequencySystem::clear_all(), designed_for_side_elems(), libMesh::ImplicitSystem::get_linear_solve_parameters(), libMesh::FEComputeData::init(), libMesh::FrequencySystem::init_data(), libMesh::NonlinearImplicitSystem::set_solver_parameters(), libMesh::EigenSystem::solve(), libMesh::CondensedEigenSystem::solve(), and libMesh::EigenSystem::solve_helper().

400 {
402 
403  if (it != _values.end())
404  {
405 #ifdef LIBMESH_HAVE_RTTI
406 
407  if (dynamic_cast<const Parameter<T> *>(it->second.get()))
408  return true;
409 
410 #else // !LIBMESH_HAVE_RTTI
411 
412  // cast_ptr will simply do a static_cast here when RTTI is not
413  // enabled, and it will return a non-nullptr regardless of
414  // whether or not the cast actually succeeds.
415  libmesh_warning("Parameters::have_parameter() may return false positives when RTTI is not enabled.");
416 
417  if (cast_ptr<const Parameter<T> *>(it->second.get()))
418  return true;
419 
420 #endif
421  }
422 
423  return false;
424 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
Tnew cast_ptr(Told *oldvar)
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:261

◆ insert()

template<typename T >
void libMesh::Parameters::insert ( const std::string &  name)
inline

Inserts a new Parameter into the object but does not return a writable reference.

The value of the newly inserted parameter may not be valid.

Definition at line 462 of file parameters.h.

References _values, libMesh::Quality::name(), and set_attributes().

463 {
464  if (!this->have_parameter<T>(name))
465  _values[name] = std::make_unique<Parameter<T>>();
466 
467  set_attributes(name, true);
468 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:142

◆ n_parameters() [1/2]

std::size_t libMesh::Parameters::n_parameters ( ) const
inline
Returns
The total number of parameters.

Definition at line 152 of file parameters.h.

References _values.

152 { return _values.size(); }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ n_parameters() [2/2]

template<typename T >
unsigned int libMesh::Parameters::n_parameters ( ) const
inline
Returns
The number of parameters of the requested type.

Definition at line 501 of file parameters.h.

References _values.

502 {
503  unsigned int cnt = 0;
504 
505  for (const auto & pr : _values)
506  if (dynamic_cast<Parameter<T> *>(pr.second.get()))
507  cnt++;
508 
509  return cnt;
510 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288

◆ operator+=()

Parameters & libMesh::Parameters::operator+= ( const Parameters source)
inlinevirtual

Addition/Assignment operator.

Inserts copies of all parameters from source. Any parameters of the same name already in this are replaced.

Definition at line 345 of file parameters.h.

References _values, and value.

346 {
347  // Overwrite each value (if it exists) or create a new entry
348  for (const auto & [key, value] : source._values)
349  _values[key] = value->clone();
350 
351  return *this;
352 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
static const bool value
Definition: xdr_io.C:55

◆ operator=()

Parameters & libMesh::Parameters::operator= ( const Parameters source)
inlinevirtual

Assignment operator.

Removes all parameters in this and inserts copies of all parameters from source

Definition at line 336 of file parameters.h.

References clear().

337 {
338  this->Parameters::clear();
339  *this += source;
340 
341  return *this;
342 }
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:328

◆ print()

void libMesh::Parameters::print ( std::ostream &  os = libMesh::out) const
inline

Prints the contents, by default to libMesh::out.

Definition at line 364 of file parameters.h.

References _values.

Referenced by libMesh::operator<<().

365 {
366  Parameters::const_iterator it = _values.begin();
367 
368  os << "Name\t Type\t Value\n"
369  << "---------------------\n";
370  while (it != _values.end())
371  {
372  os << " " << it->first
373 #ifdef LIBMESH_HAVE_RTTI
374  << "\t " << it->second->type()
375 #endif // LIBMESH_HAVE_RTTI
376  << "\t "; it->second->print(os);
377  os << '\n';
378 
379  ++it;
380  }
381 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:261

◆ remove()

void libMesh::Parameters::remove ( std::string_view  name)
inline

Removes the specified parameter from the list, if it exists.

Definition at line 488 of file parameters.h.

References _values, and libMesh::Quality::name().

Referenced by libMesh::FrequencySystem::clear_all().

489 {
490  Parameters::iterator it = _values.find(name);
491 
492  if (it != _values.end())
493  _values.erase(it);
494 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
map_type::iterator iterator
Parameter map iterator.
Definition: parameters.h:256

◆ set()

template<typename T >
T & libMesh::Parameters::set ( const std::string &  name)
inline
Returns
A writable reference to the specified parameter. This method will create the parameter if it does not exist, so it can be used to define parameters which will later be accessed with the get() member.

Definition at line 473 of file parameters.h.

References _values, libMesh::Quality::name(), and set_attributes().

Referenced by assemble_SchroedingerEquation(), libMesh::NewmarkSystem::clear(), libMesh::EquationSystems::EquationSystems(), init_cd(), HeatSystem::init_data(), Biharmonic::JR::initialize(), initialize(), main(), libMesh::NewmarkSystem::NewmarkSystem(), libMesh::NonlinearImplicitSystem::NonlinearImplicitSystem(), run_timestepping(), libMesh::FrequencySystem::set_current_frequency(), libMesh::FrequencySystem::set_frequencies(), libMesh::FrequencySystem::set_frequencies_by_range(), libMesh::FrequencySystem::set_frequencies_by_steps(), libMesh::NewmarkSystem::set_newmark_parameters(), setup(), ParametersTest::testMap(), ParametersTest::testScalar(), and SystemsTest::testSetSystemParameterOverEquationSystem().

474 {
475  if (!this->have_parameter<T>(name))
476  _values[name] = std::make_unique<Parameter<T>>();
477 
478  set_attributes(name, false);
479 
480  // Get pointer to existing or just-added entry
481  auto ptr = cast_ptr<Parameter<T> *>(_values[name].get());
482 
483  // Return writeable reference
484  return ptr->set();
485 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
map_type _values
Data structure to map names with values.
Definition: parameters.h:288
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:142

◆ set_attributes()

virtual void libMesh::Parameters::set_attributes ( const std::string &  ,
bool   
)
inlinevirtual

Overridable function to set any extended attributes for classes inheriting from this class.

Definition at line 142 of file parameters.h.

Referenced by insert(), and set().

142 {}

Member Data Documentation

◆ _values

map_type libMesh::Parameters::_values
protected

Data structure to map names with values.

Definition at line 288 of file parameters.h.

Referenced by begin(), clear(), end(), get(), have_parameter(), insert(), n_parameters(), operator+=(), print(), remove(), and set().


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