libMesh
rb_parameters.h
Go to the documentation of this file.
1 // rbOOmit: An implementation of the Certified Reduced Basis method.
2 // Copyright (C) 2009, 2010 David J. Knezevic
3 
4 // This file is part of rbOOmit.
5 
6 // rbOOmit is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 
11 // rbOOmit is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 #ifndef LIBMESH_RB_PARAMETERS_H
21 #define LIBMESH_RB_PARAMETERS_H
22 
23 // libMesh includes
24 #include "libmesh/libmesh_common.h"
25 
26 // C++ includes
27 #include <string>
28 #include <map>
29 #include <set>
30 #include <vector>
31 
32 namespace libMesh
33 {
34 
39 using RBParameter = std::vector<Real>;
40 
53 {
54 public:
55 
61  RBParameters();
62 
67  RBParameters (RBParameters &&) = default;
68  RBParameters (const RBParameters &) = default;
69  RBParameters & operator= (const RBParameters &) = default;
70  RBParameters & operator= (RBParameters &&) = default;
71  ~RBParameters() = default;
72 
79  RBParameters(const std::map<std::string, Real> & parameter_map);
80 
86  std::map<std::string,std::vector<RBParameter>>::const_iterator begin() const;
87  std::map<std::string,std::vector<RBParameter>>::const_iterator end() const;
88  std::map<std::string,std::vector<RBParameter>>::const_iterator extra_begin() const;
89  std::map<std::string,std::vector<RBParameter>>::const_iterator extra_end() const;
90 
98  {
99  public:
100  // Typedefs needed for interoperating with other STL algorithms and containers.
101  typedef std::forward_iterator_tag iterator_category;
102  typedef std::pair<std::string, Real> value_type;
103  typedef std::ptrdiff_t difference_type;
104  typedef value_type* pointer;
106 
107  // Underlying iterator type, must match the container type of _parameters.
108  typedef std::map<std::string, std::vector<RBParameter>>::const_iterator MapIter;
109 
110  // Constructor
112  const std::size_t sample_vec_index,
113  const std::size_t value_vec_index)
114  : _it(it),
115  _sample_vec_index(sample_vec_index),
116  _value_vec_index(value_vec_index)
117  {}
118 
119  // Copy ctor
120  const_iterator(const const_iterator & i) = default;
121 
122  // Prefix increment operator "++it"
124  {
125  // First increment the value-vector index.
126  // If _value_vec_index goes beyond the current value-vector, reset to the next one.
128  if (_value_vec_index >= _it->second[_sample_vec_index].size())
129  {
130  _value_vec_index = 0;
131  // Now increment the sample-vector index, and do the same check.
132  // If we go beyond the current sample-vector, reset to the next one.
134  if (_sample_vec_index >= _it->second.size())
135  {
136  _sample_vec_index = 0;
137  ++_it;
138  }
139  }
140 
141  return *this;
142  }
143 
144  // Postfix increment operator "it++". This is actually less
145  // efficient than doing the prefix increment, so if nothing
146  // requires it, let's skip defining it.
147  // const_iterator operator++(int)
148  // {
149  // const_iterator i = *this;
150  // ++(*this);
151  // return i;
152  // }
153 
154  // Dereference operator - returns a const reference to the value
155  // indexed by it and _vec_index. Note: this is needed for backwards
156  // compatibility but it is not the most efficient thing since we
157  // need to construct a std::pair every time we dereference a
158  // const_iterator.
159  const value_type & operator*() const
160  {
161  _emulator = std::make_pair(_it->first, _it->second[_sample_vec_index][_value_vec_index]);
162  return _emulator;
163  }
164 
165  // Equivalence comparison operator.
166  bool operator==(const const_iterator & other) const
167  {
168  return (_it == other._it && _sample_vec_index == other._sample_vec_index);
169  }
170 
171  // Non-equvialence comparison operator
172  bool operator!=(const const_iterator & other) const
173  {
174  return !(other == *this);
175  }
176 
177  private:
178  // Give RBParameters access to our private members. At the moment
179  // this is not needed since the RBParameters class does not really
180  // need to know anything about the const_iterator once it has been
181  // created.
182  // friend class RBParameters;
183 
184  // Iterator into real container
186 
187  // Accompanying current sample-vector index into it->second
188  std::size_t _sample_vec_index;
189 
190  // Accompanying current value-vector index into it->second[_sample_vec_index]
191  std::size_t _value_vec_index;
192 
193  // Returned by the operator* function. Emulates dereferencing a
194  // map<string, Real> iterator.
196  }; // end const_iterator
197 
207 
217 
221  void clear();
222 
227  bool has_value(const std::string & param_name) const;
228 
233  bool has_extra_value(const std::string & param_name) const;
234 
241  Real get_value(const std::string & param_name) const;
242  const RBParameter & get_vector_value(const std::string & param_name) const;
243 
252  Real get_value(const std::string & param_name, const Real & default_val) const;
253  const RBParameter & get_vector_value(const std::string & param_name, const RBParameter & default_val) const;
254 
261  Real get_sample_value(const std::string & param_name, std::size_t sample_idx) const;
262  const RBParameter & get_sample_vector_value(const std::string & param_name, std::size_t sample_idx) const;
263 
271  Real get_sample_value(const std::string & param_name, std::size_t sample_idx, const Real & default_val) const;
272  const RBParameter & get_sample_vector_value(const std::string & param_name, std::size_t sample_idx, const RBParameter & default_val) const;
273 
281  void set_value(const std::string & param_name, Real value);
282  void set_value(const std::string & param_name, const RBParameter & value);
283 
288  void set_value(const std::string & param_name, std::size_t index, Real value);
289  void set_value(const std::string & param_name, std::size_t index, const RBParameter & value);
290 
297  void push_back_value(const std::string & param_name, Real value);
298  void push_back_value(const std::string & param_name, const RBParameter & value);
299 
303  void push_back_extra_value(const std::string & param_name, Real value);
304  void push_back_extra_value(const std::string & param_name, const RBParameter & value);
305 
310  Real get_extra_value(const std::string & param_name) const;
311  const RBParameter & get_extra_vector_value(const std::string & param_name) const;
312 
317  Real get_extra_value(const std::string & param_name, const Real & default_val) const;
318  const RBParameter & get_extra_vector_value(const std::string & param_name, const RBParameter & default_val) const;
319 
324  Real get_extra_sample_value(const std::string & param_name, std::size_t sample_idx) const;
325  const RBParameter & get_extra_sample_vector_value(const std::string & param_name, std::size_t sample_idx) const;
326 
332  Real get_extra_sample_value(const std::string & param_name, std::size_t sample_idx, const Real & default_val) const;
333  const RBParameter & get_extra_sample_vector_value(const std::string & param_name, std::size_t sample_idx, const RBParameter & default_val) const;
334 
339  void set_extra_value(const std::string & param_name, Real value);
340  void set_extra_value(const std::string & param_name, const RBParameter & value);
341 
346  void set_extra_value(const std::string & param_name, std::size_t index, Real value);
347  void set_extra_value(const std::string & param_name, std::size_t index, const RBParameter & value);
348 
352  unsigned int n_parameters() const;
353 
362  void set_n_samples(unsigned int n_samples);
363 
370  unsigned int n_samples() const;
371 
380  std::set<std::string> get_parameter_names() const;
381 
390  std::set<std::string> get_extra_parameter_names() const;
391 
396  void erase_parameter(const std::string & param_name);
397 
402  void erase_extra_parameter(const std::string & param_name);
403 
407  bool operator== (const RBParameters & rhs) const;
408 
412  bool operator!= (const RBParameters & rhs) const;
413 
420  RBParameters & operator+= (const RBParameters & rhs);
421 
427  std::string get_string(unsigned int precision=6) const;
428 
432  void print() const;
433 
434 private:
435 
440  void set_value_helper(std::map<std::string, std::vector<RBParameter>> & map,
441  const std::string & param_name,
442  const std::size_t index,
444 
451  unsigned int _n_samples;
452 
460  std::map<std::string, std::vector<RBParameter>> _parameters;
461 
466  std::map<std::string, std::vector<RBParameter>> _extra_parameters;
467 };
468 
469 } // namespace libMesh
470 
471 
472 #endif // LIBMESH_RB_PARAMETERS_H
Real get_value(const std::string &param_name) const
Get the value of the specified parameter, throw an error if it does not exist.
Definition: rb_parameters.C:64
bool operator!=(const RBParameters &rhs) const
Real get_extra_sample_value(const std::string &param_name, std::size_t sample_idx) const
Get the value of the specified "extra" parameter at the specified sample index, throwing an error if ...
const_iterator(const MapIter &it, const std::size_t sample_vec_index, const std::size_t value_vec_index)
bool has_value(const std::string &param_name) const
Definition: rb_parameters.C:54
std::set< std::string > get_parameter_names() const
RBParameters & operator=(const RBParameters &)=default
void erase_extra_parameter(const std::string &param_name)
Erase param_name from _extra_parameters.
std::string get_string(unsigned int precision=6) const
Get a string that specifies the contents of this RBParameters object.
const RBParameter & get_extra_sample_vector_value(const std::string &param_name, std::size_t sample_idx) const
const_iterator begin_serialized() const
Get const_iterator access to the parameters stored in this RBParameters object.
std::map< std::string, std::vector< RBParameter > >::const_iterator extra_begin() const
std::map< std::string, std::vector< RBParameter > >::const_iterator end() const
std::map< std::string, std::vector< RBParameter > > _parameters
Actual parameter values (in std::vector<RBParameter> form) across a vector of samples.
unsigned int _n_samples
The number of samples represented by this RBParameters object, in the case where there are no paramet...
void print() const
Print the parameters.
std::map< std::string, std::vector< RBParameter > >::const_iterator MapIter
bool operator==(const RBParameters &rhs) const
Two RBParameters are equal if they have the same _parameters map.
const RBParameter & get_vector_value(const std::string &param_name) const
Definition: rb_parameters.C:72
std::map< std::string, std::vector< RBParameter > > _extra_parameters
Extra parameter vectors not used for RB training.
The libMesh namespace provides an interface to certain functionality in the library.
RBParameters & operator+=(const RBParameters &rhs)
Append "rhs" to "*this".
const RBParameter & get_extra_vector_value(const std::string &param_name) const
std::forward_iterator_tag iterator_category
RBParameters()
Constructor.
Definition: rb_parameters.C:32
std::map< std::string, std::vector< RBParameter > >::const_iterator extra_end() const
unsigned int n_parameters() const
Get the number of parameters that have been added.
const value_type & operator*() const
std::set< std::string > get_extra_parameter_names() const
Define a constant iterator for iterating over the map of parameters.
Definition: rb_parameters.h:97
const RBParameter & get_sample_vector_value(const std::string &param_name, std::size_t sample_idx) const
std::vector< Real > RBParameter
Typedef for an individual RB parameter.
Definition: rb_parameters.h:39
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:52
const_iterator end_serialized_extra() const
bool has_extra_value(const std::string &param_name) const
Definition: rb_parameters.C:59
void set_value_helper(std::map< std::string, std::vector< RBParameter >> &map, const std::string &param_name, const std::size_t index, RBParameter value)
Helper function for the 3-parameter versions of set_value() and set_extra_value().
unsigned int n_samples() const
Returns the number of samples stored for all parameters.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_extra_value(const std::string &param_name, Real value)
Set the value of the specified extra parameter.
Real get_sample_value(const std::string &param_name, std::size_t sample_idx) const
Get the value of the specified parameter at the specified sample, throwing an error if it does not ex...
Definition: rb_parameters.C:96
const_iterator begin_serialized_extra() const
Get const_iterator access to the extra parameters stored in this RBParameters object.
void push_back_extra_value(const std::string &param_name, Real value)
Same as push_back_value(), but for "extra" parameters.
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
void clear()
Clear this object.
Definition: rb_parameters.C:47
static const bool value
Definition: xdr_io.C:54
const_iterator end_serialized() const
void erase_parameter(const std::string &param_name)
Erase param_name from _parameters.
Real get_extra_value(const std::string &param_name) const
Get the value of the specified extra parameter, throwing an error if it does not exist.
void push_back_value(const std::string &param_name, Real value)
Similar to set_value(name, index, value) but instead of specifying a particular index, just appends one more.
void set_n_samples(unsigned int n_samples)
Set the number of samples this RBParameters object is intended to represent, in the case that there a...
std::pair< std::string, Real > value_type
bool operator==(const const_iterator &other) const
std::map< std::string, std::vector< RBParameter > >::const_iterator begin() const
Return const_iterators to the internal parameter map, as a convenient way to access the parameter nam...
bool operator!=(const const_iterator &other) const