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 74 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 275 of file parameters.h.

◆ iterator

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

Parameter map iterator.

Definition at line 270 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 265 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 376 of file parameters.h.

377 {
378  // calls assignment operator
379  *this = p;
380 }

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

References _values.

536 {
537  return _values.begin();
538 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

◆ begin() [2/2]

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

Iterator pointing to the beginning of the set of parameters.

Definition at line 541 of file parameters.h.

References _values.

542 {
543  return _values.begin();
544 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

◆ clear()

void libMesh::Parameters::clear ( )
inlinevirtual

Clears internal data structures & frees any allocated memory.

Definition at line 349 of file parameters.h.

References _values.

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

350 {
351  _values.clear();
352 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

◆ end() [1/2]

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

Iterator pointing to the end of the set of parameters.

Definition at line 547 of file parameters.h.

References _values.

548 {
549  return _values.end();
550 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

◆ end() [2/2]

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

Iterator pointing to the end of the set of parameters.

Definition at line 553 of file parameters.h.

References _values.

554 {
555  return _values.end();
556 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

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

452 {
453  if (!this->have_parameter<T>(name))
454  {
455  std::ostringstream oss;
456 
457  oss << "ERROR: no";
458 #ifdef LIBMESH_HAVE_RTTI
459  oss << ' ' << demangle(typeid(T).name());
460 #endif
461  oss << " parameter named \""
462  << name << "\" found.\n\n"
463  << "Known parameters:\n"
464  << *this;
465 
466  libmesh_error_msg(oss.str());
467  }
468 
470 
471  libmesh_assert(it != _values.end());
472  libmesh_assert(it->second);
473 
474  // Get pointer to derived type
475  auto ptr = cast_ptr<Parameter<T> *>(it->second.get());
476 
477  // Return const reference
478  return ptr->get();
479 }
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:302
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:275

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

421 {
423 
424  if (it != _values.end())
425  {
426 #ifdef LIBMESH_HAVE_RTTI
427 
428  if (dynamic_cast<const Parameter<T> *>(it->second.get()))
429  return true;
430 
431 #else // !LIBMESH_HAVE_RTTI
432 
433  // cast_ptr will simply do a static_cast here when RTTI is not
434  // enabled, and it will return a non-nullptr regardless of
435  // whether or not the cast actually succeeds.
436  libmesh_warning("Parameters::have_parameter() may return false positives when RTTI is not enabled.");
437 
438  if (cast_ptr<const Parameter<T> *>(it->second.get()))
439  return true;
440 
441 #endif
442  }
443 
444  return false;
445 }
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:302
Tnew cast_ptr(Told *oldvar)
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:275

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

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

484 {
485  if (!this->have_parameter<T>(name))
486  _values[name] = std::make_unique<Parameter<T>>();
487 
488  set_attributes(name, true);
489 }
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:302
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:145

◆ n_parameters() [1/2]

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

Definition at line 155 of file parameters.h.

References _values.

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

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

References _values.

523 {
524  unsigned int cnt = 0;
525 
526  for (const auto & pr : _values)
527  if (dynamic_cast<Parameter<T> *>(pr.second.get()))
528  cnt++;
529 
530  return cnt;
531 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302

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

References _values, and value.

367 {
368  // Overwrite each value (if it exists) or create a new entry
369  for (const auto & [key, value] : source._values)
370  _values[key] = value->clone();
371 
372  return *this;
373 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302
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 357 of file parameters.h.

References clear().

358 {
359  this->Parameters::clear();
360  *this += source;
361 
362  return *this;
363 }
virtual void clear()
Clears internal data structures & frees any allocated memory.
Definition: parameters.h:349

◆ print()

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

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

Definition at line 385 of file parameters.h.

References _values.

Referenced by libMesh::operator<<().

386 {
387  Parameters::const_iterator it = _values.begin();
388 
389  os << "Name\t Type\t Value\n"
390  << "---------------------\n";
391  while (it != _values.end())
392  {
393  os << " " << it->first
394 #ifdef LIBMESH_HAVE_RTTI
395  << "\t " << it->second->type()
396 #endif // LIBMESH_HAVE_RTTI
397  << "\t "; it->second->print(os);
398  os << '\n';
399 
400  ++it;
401  }
402 }
map_type _values
Data structure to map names with values.
Definition: parameters.h:302
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition: parameters.h:275

◆ remove()

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

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

Definition at line 509 of file parameters.h.

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

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

510 {
511  Parameters::iterator it = _values.find(name);
512 
513  if (it != _values.end())
514  _values.erase(it);
515 }
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:302
map_type::iterator iterator
Parameter map iterator.
Definition: parameters.h:270

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

495 {
496  if (!this->have_parameter<T>(name))
497  _values[name] = std::make_unique<Parameter<T>>();
498 
499  set_attributes(name, false);
500 
501  // Get pointer to existing or just-added entry
502  auto ptr = cast_ptr<Parameter<T> *>(_values[name].get());
503 
504  // Return writeable reference
505  return ptr->set();
506 }
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:302
virtual void set_attributes(const std::string &, bool)
Overridable function to set any extended attributes for classes inheriting from this class...
Definition: parameters.h:145

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

Referenced by insert(), and set().

145 {}

Member Data Documentation

◆ _values

map_type libMesh::Parameters::_values
protected

Data structure to map names with values.

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