libMesh
Loading...
Searching...
No Matches
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.
 
typedef map_type::iterator iterator
 Parameter map iterator.
 
typedef map_type::const_iterator const_iterator
 Constant parameter map iterator.
 

Public Member Functions

 Parameters ()=default
 Default constructor.
 
 Parameters (const Parameters &)
 Copy constructor.
 
virtual ~Parameters ()=default
 Destructor.
 
virtual Parametersoperator= (const Parameters &source)
 Assignment operator.
 
virtual Parametersoperator+= (const Parameters &source)
 Addition/Assignment operator.
 
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.
 
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.
 
void remove (std::string_view)
 Removes the specified parameter from the list, if it exists.
 
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.
 
void print (std::ostream &os=libMesh::out) const
 Prints the contents, by default to libMesh::out.
 
iterator begin ()
 Iterator pointing to the beginning of the set of parameters.
 
const_iterator begin () const
 Iterator pointing to the beginning of the set of parameters.
 
iterator end ()
 Iterator pointing to the end of the set of parameters.
 
const_iterator end () const
 Iterator pointing to the end of the set of parameters.
 

Protected Attributes

map_type _values
 Data structure to map names with values.
 

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.

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

References _values.

Referenced by print().

◆ 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.

542{
543 return _values.begin();
544}

References _values.

◆ clear()

void libMesh::Parameters::clear ( )
inlinevirtual

Clears internal data structures & frees any allocated memory.

Definition at line 349 of file parameters.h.

350{
351 _values.clear();
352}

References _values.

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

◆ 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.

548{
549 return _values.end();
550}

References _values.

◆ 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.

554{
555 return _values.end();
556}

References _values.

◆ 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.

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
469 Parameters::const_iterator it = _values.find(name);
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}
map_type::const_iterator const_iterator
Constant parameter map iterator.
Definition parameters.h:275
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
std::string demangle(const char *name)
Mostly system independent demangler.
libmesh_assert(ctx)

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

Referenced by add_M_C_K_helmholtz(), assemble_cd(), assemble_cd(), assemble_divgrad(), assemble_ellipticdg(), assemble_func(), assemble_helmholtz(), assemble_poisson(), assemble_SchroedingerEquation(), assemble_shell(), assemble_shell(), assemble_stokes(), assemble_wave(), assemble_wave(), libMesh::FrequencySystem::clear_all(), libMesh::compute_error(), compute_jacobian(), compute_residual(), LargeDeformationElasticity::compute_stresses(), LinearElasticityWithContact::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(), right_solution_fn(), 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().

◆ 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.

421{
422 Parameters::const_iterator it = _values.find(name);
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}
Tnew cast_ptr(Told *oldvar)

References _values, and libMesh::cast_ptr().

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::CondensedEigenSystem::solve(), libMesh::EigenSystem::solve(), and libMesh::EigenSystem::solve_helper().

◆ 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.

484{
485 if (!this->have_parameter<T>(name))
486 _values[name] = std::make_unique<Parameter<T>>();
487
488 set_attributes(name, true);
489}
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

References _values, and set_attributes().

◆ 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.

155{ return _values.size(); }

References _values.

◆ 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.

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}
const T & get(std::string_view) const
Definition parameters.h:451

References _values.

◆ 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.

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}
static const bool value
Definition xdr_io.C:55

References _values, and value.

◆ 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.

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

References clear().

◆ 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.

386{
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}

References _values, and begin().

Referenced by libMesh::operator<<().

◆ 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.

510{
511 Parameters::iterator it = _values.find(name);
512
513 if (it != _values.end())
514 _values.erase(it);
515}
map_type::iterator iterator
Parameter map iterator.
Definition parameters.h:270

References _values.

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

◆ 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.

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}

References _values, 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(), SystemsTest::testSetSystemParameterOverEquationSystem(), DisjointNeighborTest::testTempJump(), and DisjointNeighborTest::testTempJumpRefine().

◆ 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.

145{}

Referenced by insert(), and set().

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(), begin(), clear(), end(), end(), get(), have_parameter(), insert(), n_parameters(), operator+=(), print(), remove(), and set().


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