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 67 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 257 of file parameters.h.

◆ iterator

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

Parameter map iterator.

Definition at line 252 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 247 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 351 of file parameters.h.

352 {
353  // calls assignment operator
354  *this = p;
355 }

◆ ~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 510 of file parameters.h.

References _values.

511 {
512  return _values.begin();
513 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:284

◆ begin() [2/2]

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

Iterator pointing to the beginning of the set of parameters.

Definition at line 516 of file parameters.h.

References _values.

517 {
518  return _values.begin();
519 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:284

◆ clear()

void libMesh::Parameters::clear ( )
inlinevirtual

Clears internal data structures & frees any allocated memory.

Definition at line 324 of file parameters.h.

References _values.

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

325 {
326  _values.clear();
327 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:284

◆ end() [1/2]

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

Iterator pointing to the end of the set of parameters.

Definition at line 522 of file parameters.h.

References _values.

523 {
524  return _values.end();
525 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:284

◆ end() [2/2]

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

Iterator pointing to the end of the set of parameters.

Definition at line 528 of file parameters.h.

References _values.

529 {
530  return _values.end();
531 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:284

◆ 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 426 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().

427 {
428  if (!this->have_parameter<T>(name))
429  {
430  std::ostringstream oss;
431 
432  oss << "ERROR: no";
433 #ifdef LIBMESH_HAVE_RTTI
434  oss << ' ' << demangle(typeid(T).name());
435 #endif
436  oss << " parameter named \""
437  << name << "\" found.\n\n"
438  << "Known parameters:\n"
439  << *this;
440 
441  libmesh_error_msg(oss.str());
442  }
443 
445 
446  libmesh_assert(it != _values.end());
447  libmesh_assert(it->second);
448 
449  // Get pointer to derived type
450  auto ptr = cast_ptr<Parameter<T> *>(it->second.get());
451 
452  // Return const reference
453  return ptr->get();
454 }
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:284
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:257

◆ 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 395 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().

396 {
398 
399  if (it != _values.end())
400  {
401 #ifdef LIBMESH_HAVE_RTTI
402 
403  if (dynamic_cast<const Parameter<T> *>(it->second.get()))
404  return true;
405 
406 #else // !LIBMESH_HAVE_RTTI
407 
408  // cast_ptr will simply do a static_cast here when RTTI is not
409  // enabled, and it will return a non-nullptr regardless of
410  // whether or not the cast actually succeeds.
411  libmesh_warning("Parameters::have_parameter() may return false positives when RTTI is not enabled.");
412 
413  if (cast_ptr<const Parameter<T> *>(it->second.get()))
414  return true;
415 
416 #endif
417  }
418 
419  return false;
420 }
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:284
Tnew cast_ptr(Told *oldvar)
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:257

◆ 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 458 of file parameters.h.

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

459 {
460  if (!this->have_parameter<T>(name))
461  _values[name] = std::make_unique<Parameter<T>>();
462 
463  set_attributes(name, true);
464 }
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:284
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:138

◆ n_parameters() [1/2]

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

Definition at line 148 of file parameters.h.

References _values.

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

◆ 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 497 of file parameters.h.

References _values.

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

◆ 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 341 of file parameters.h.

References _values, and value.

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

◆ 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 332 of file parameters.h.

References clear().

333 {
334  this->Parameters::clear();
335  *this += source;
336 
337  return *this;
338 }
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:324

◆ print()

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

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

Definition at line 360 of file parameters.h.

References _values.

Referenced by libMesh::operator<<().

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

◆ remove()

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

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

Definition at line 484 of file parameters.h.

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

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

485 {
486  Parameters::iterator it = _values.find(name);
487 
488  if (it != _values.end())
489  _values.erase(it);
490 }
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:284
map_type::iterator iterator
Parameter map iterator.
Definition: parameters.h:252

◆ 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 469 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().

470 {
471  if (!this->have_parameter<T>(name))
472  _values[name] = std::make_unique<Parameter<T>>();
473 
474  set_attributes(name, false);
475 
476  // Get pointer to existing or just-added entry
477  auto ptr = cast_ptr<Parameter<T> *>(_values[name].get());
478 
479  // Return writeable reference
480  return ptr->set();
481 }
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:284
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:138

◆ 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 138 of file parameters.h.

Referenced by insert(), and set().

138 {}

Member Data Documentation

◆ _values

map_type libMesh::Parameters::_values
protected

Data structure to map names with values.

Definition at line 284 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: