libMesh
rb_parametrized.C
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 // libMesh includes
21 #include "libmesh/xdr_cxx.h"
22 
23 // rbOOmit includes
24 #include "libmesh/rb_parametrized.h"
25 
26 // C++ includes
27 #include <sstream>
28 #include <fstream>
29 #include <algorithm> // std::min_element
30 
31 namespace libMesh
32 {
33 
34 // ------------------------------------------------------------
35 // RBParameters implementation
36 
38  :
39  verbose_mode(false),
40  parameters_initialized(false)
41 {
42  parameters.clear();
45 }
46 
48 {
49  this->clear();
50 }
51 
53 {
54  parameters.clear();
57  parameters_initialized = false;
58 }
59 
61  const RBParameters & mu_max_in,
62  const std::map<std::string, std::vector<Real>> & discrete_parameter_values)
63 {
64  // Check that the min/max vectors are valid
65  {
66  const std::string err_string = "Error: Invalid mu_min/mu_max in RBParameters constructor.";
67  bool valid_min_max = (mu_min_in.n_parameters() == mu_max_in.n_parameters());
68  if (!valid_min_max)
69  libmesh_error_msg(err_string);
70 
71  else
72  {
73  RBParameters::const_iterator it = mu_min_in.begin();
74  RBParameters::const_iterator it_end = mu_min_in.end();
75  for ( ; it != it_end; ++it)
76  {
77  std::string param_name = it->first;
78  if (mu_min_in.get_value(param_name) > mu_max_in.get_value(param_name))
79  libmesh_error_msg(err_string);
80  }
81  }
82  }
83 
84  parameters_min = mu_min_in;
85  parameters_max = mu_max_in;
86 
87  // Add in min/max values due to the discrete parameters
88  {
89  std::map<std::string, std::vector<Real>>::const_iterator it =
90  discrete_parameter_values.begin();
91  const std::map<std::string, std::vector<Real>>::const_iterator it_end =
92  discrete_parameter_values.end();
93  for ( ; it != it_end; ++it)
94  {
95  if (it->second.empty())
96  libmesh_error_msg("Error: List of discrete parameters for " << it->first << " is empty.");
97 
98  Real min_val = *std::min_element(it->second.begin(), it->second.end());
99  Real max_val = *std::max_element(it->second.begin(), it->second.end());
100 
101  libmesh_assert_less_equal(min_val,max_val);
102 
103  parameters_min.set_value(it->first, min_val);
104  parameters_max.set_value(it->first, max_val);
105  }
106 
107  _discrete_parameter_values = discrete_parameter_values;
108  }
109 
110  parameters_initialized = true;
111 
112  // Initialize the current parameters to parameters_min
114 }
115 
117 {
118  initialize_parameters(rb_parametrized.get_parameters_min(),
119  rb_parametrized.get_parameters_max(),
120  rb_parametrized.get_discrete_parameter_values());
121 }
122 
123 unsigned int RBParametrized::get_n_params() const
124 {
126  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_n_params");
127 
128  libmesh_assert_equal_to ( parameters_min.n_parameters(), parameters_max.n_parameters() );
129 
130  return parameters_min.n_parameters();
131 }
132 
134 {
136  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_n_continuous_params");
137 
139 
140  return static_cast<unsigned int>(get_n_params() - get_n_discrete_params());
141 }
142 
144 {
146  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_n_discrete_params");
147 
148  return cast_int<unsigned int>
150 }
151 
152 std::set<std::string> RBParametrized::get_parameter_names() const
153 {
155  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_parameter_names");
156 
157  std::set<std::string> parameter_names;
158  parameters_min.get_parameter_names(parameter_names);
159 
160  return parameter_names;
161 }
162 
164 {
166  libmesh_error_msg("Error: parameters not initialized in RBParametrized::set_current_parameters");
167 
168  valid_params(params); // Terminates if params has the wrong number of parameters
169 
170  // Make a copy of params (default assignment operator just does memberwise copy, which is sufficient here)
171  this->parameters = params;
172 }
173 
175 {
177  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_current_parameters");
178 
179  return parameters;
180 }
181 
183 {
185  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_parameters_min");
186 
187  return parameters_min;
188 }
189 
191 {
193  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_parameters_max");
194 
195  return parameters_max;
196 }
197 
198 Real RBParametrized::get_parameter_min(const std::string & param_name) const
199 {
201  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_parameter_min");
202 
203  return parameters_min.get_value(param_name);
204 }
205 
206 Real RBParametrized::get_parameter_max(const std::string & param_name) const
207 {
209  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_parameter_max");
210 
211  return parameters_max.get_value(param_name);
212 }
213 
215 {
217  libmesh_error_msg("Error: parameters not initialized in RBParametrized::print_current_parameters");
218 
219  get_parameters().print();
220 }
221 
222 void RBParametrized::write_parameter_data_to_files(const std::string & continuous_param_file_name,
223  const std::string & discrete_param_file_name,
224  const bool write_binary_data)
225 {
226  write_parameter_ranges_to_file(continuous_param_file_name, write_binary_data);
227  write_discrete_parameter_values_to_file(discrete_param_file_name, write_binary_data);
228 }
229 
230 void RBParametrized::write_parameter_ranges_to_file(const std::string & file_name,
231  const bool write_binary_data)
232 {
233  // The writing mode: ENCODE for binary, WRITE for ASCII
234  XdrMODE mode = write_binary_data ? ENCODE : WRITE;
235 
236  // Write out the parameter ranges
237  Xdr parameter_ranges_out(file_name, mode);
238  unsigned int n_continuous_params = get_n_continuous_params();
239  parameter_ranges_out << n_continuous_params;
240 
243  it = get_parameters_min().begin();
244  it_end = get_parameters_min().end();
245  for ( ; it != it_end; ++it)
246  {
247  std::string param_name = it->first;
248  if (!is_discrete_parameter(param_name))
249  {
250  Real param_value = it->second;
251  parameter_ranges_out << param_name << param_value;
252  }
253  }
254  it = get_parameters_max().begin();
255  it_end = get_parameters_max().end();
256  for ( ; it != it_end; ++it)
257  {
258  std::string param_name = it->first;
259  if (!is_discrete_parameter(param_name))
260  {
261  Real param_value = it->second;
262  parameter_ranges_out << param_name << param_value;
263  }
264  }
265  parameter_ranges_out.close();
266 }
267 
269  const bool write_binary_data)
270 {
271  // write out the discrete parameters, if we have any
272  if (get_n_discrete_params() > 0)
273  {
274  // The writing mode: ENCODE for binary, WRITE for ASCII
275  XdrMODE mode = write_binary_data ? ENCODE : WRITE;
276 
277  Xdr discrete_parameters_out(file_name, mode);
278  unsigned int n_discrete_params = get_n_discrete_params();
279  discrete_parameters_out << n_discrete_params;
280 
281  std::map<std::string, std::vector<Real>>::const_iterator discrete_it =
283  const std::map<std::string, std::vector<Real>>::const_iterator discrete_it_end =
285  for ( ; discrete_it != discrete_it_end; ++discrete_it)
286  {
287  std::string param_name = discrete_it->first;
288  unsigned int n_discrete_values = cast_int<unsigned int>
289  (discrete_it->second.size());
290  discrete_parameters_out << param_name << n_discrete_values;
291 
292  for (unsigned int i=0; i<n_discrete_values; i++)
293  {
294  Real discrete_value = discrete_it->second[i];
295  discrete_parameters_out << discrete_value;
296  }
297  }
298  }
299 }
300 
301 void RBParametrized::read_parameter_data_from_files(const std::string & continuous_param_file_name,
302  const std::string & discrete_param_file_name,
303  const bool read_binary_data)
304 {
305  RBParameters param_min;
306  RBParameters param_max;
307  read_parameter_ranges_from_file(continuous_param_file_name,
308  read_binary_data,
309  param_min,
310  param_max);
311 
312  std::map<std::string, std::vector<Real>> discrete_parameter_values_in;
313  read_discrete_parameter_values_from_file(discrete_param_file_name,
314  read_binary_data,
315  discrete_parameter_values_in);
316 
317  initialize_parameters(param_min, param_max, discrete_parameter_values_in);
318 }
319 
320 void RBParametrized::read_parameter_ranges_from_file(const std::string & file_name,
321  const bool read_binary_data,
322  RBParameters & param_min,
323  RBParameters & param_max)
324 {
325  // The reading mode: DECODE for binary, READ for ASCII
326  XdrMODE mode = read_binary_data ? DECODE : READ;
327 
328  // Read in the parameter ranges
329  Xdr parameter_ranges_in(file_name, mode);
330  unsigned int n_continuous_params;
331  parameter_ranges_in >> n_continuous_params;
332 
333  for (unsigned int i=0; i<n_continuous_params; i++)
334  {
335  std::string param_name;
336  Real param_value;
337 
338  parameter_ranges_in >> param_name;
339  parameter_ranges_in >> param_value;
340 
341  param_min.set_value(param_name, param_value);
342  }
343  for (unsigned int i=0; i<n_continuous_params; i++)
344  {
345  std::string param_name;
346  Real param_value;
347 
348  parameter_ranges_in >> param_name;
349  parameter_ranges_in >> param_value;
350 
351  param_max.set_value(param_name, param_value);
352  }
353 
354  parameter_ranges_in.close();
355 }
356 
358  const bool read_binary_data,
359  std::map<std::string, std::vector<Real>> & discrete_parameter_values)
360 {
361  // read in the discrete parameters, if we have any
362  std::ifstream check_if_file_exists(file_name.c_str());
363  if (check_if_file_exists.good())
364  {
365  // The reading mode: DECODE for binary, READ for ASCII
366  XdrMODE mode = read_binary_data ? DECODE : READ;
367 
368  // Read in the parameter ranges
369  Xdr discrete_parameter_values_in(file_name, mode);
370  unsigned int n_discrete_params;
371  discrete_parameter_values_in >> n_discrete_params;
372 
373  for (unsigned int i=0; i<n_discrete_params; i++)
374  {
375  std::string param_name;
376  discrete_parameter_values_in >> param_name;
377 
378  unsigned int n_discrete_values;
379  discrete_parameter_values_in >> n_discrete_values;
380 
381  std::vector<Real> discrete_values(n_discrete_values);
382  for (std::size_t j=0; j<discrete_values.size(); j++)
383  {
384  Real param_value;
385  discrete_parameter_values_in >> param_value;
386 
387  discrete_values[j] = param_value;
388  }
389 
390  discrete_parameter_values[param_name] = discrete_values;
391  }
392  }
393 }
394 
395 bool RBParametrized::is_discrete_parameter(const std::string & mu_name) const
396 {
398  libmesh_error_msg("Error: parameters not initialized in RBParametrized::is_discrete_parameter");
399 
400  return (_discrete_parameter_values.find(mu_name) != _discrete_parameter_values.end());
401 }
402 
403 const std::map<std::string, std::vector<Real>> & RBParametrized::get_discrete_parameter_values() const
404 {
406  libmesh_error_msg("Error: parameters not initialized in RBParametrized::get_discrete_parameter_values");
407 
409 }
410 
412 {
413  std::map<std::string, std::vector<Real>>::const_iterator it =
415  const std::map<std::string, std::vector<Real>>::const_iterator it_end =
417 
418  for ( ; it != it_end; ++it)
419  {
420  libMesh::out << "Discrete parameter " << it->first << ", values: ";
421 
422  std::vector<Real> values = it->second;
423  for (std::size_t i=0; i<values.size(); i++)
424  libMesh::out << values[i] << " ";
425  libMesh::out << std::endl;
426  }
427 }
428 
430 {
431  if (params.n_parameters() != get_n_params())
432  libmesh_error_msg("Error: Number of parameters don't match");
433 
434  else
435  {
436  bool valid = true;
437  RBParameters::const_iterator it = params.begin();
438  RBParameters::const_iterator it_end = params.end();
439  for ( ; it != it_end; ++it)
440  {
441  std::string param_name = it->first;
442  valid = valid && ( (get_parameter_min(param_name) <= params.get_value(param_name)) &&
443  (params.get_value(param_name) <= get_parameter_max(param_name)) );
444 
445  if (is_discrete_parameter(param_name))
446  {
447  // make sure params.get_value(param_name) is sufficiently close
448  // to one of the discrete parameter values
449  valid = valid && is_value_in_list(params.get_value(param_name),
450  get_discrete_parameter_values().find(param_name)->second,
451  TOLERANCE);
452  }
453  }
454 
455  if (!valid && verbose_mode)
456  {
457  libMesh::out << "Warning: parameter is outside parameter range" << std::endl;
458  }
459 
460  return valid;
461  }
462 }
463 
464 Real RBParametrized::get_closest_value(Real value, const std::vector<Real> & list_of_values)
465 {
466  if (list_of_values.empty())
467  libmesh_error_msg("Error: list_of_values is empty.");
468 
469  std::vector<Real>::const_iterator it = list_of_values.begin();
470  std::vector<Real>::const_iterator it_end = list_of_values.end();
471 
472  Real min_distance = std::numeric_limits<Real>::max();
473  Real closest_val = 0.;
474  for ( ; it != it_end; ++it)
475  {
476  Real distance = std::abs(value - *it);
477  if (distance < min_distance)
478  {
479  min_distance = distance;
480  closest_val = *it;
481  }
482  }
483 
484  return closest_val;
485 }
486 
487 bool RBParametrized::is_value_in_list(Real value, const std::vector<Real> & list_of_values, Real tol)
488 {
489  Real closest_value = get_closest_value(value, list_of_values);
490 
491  // Check if relative tolerance is satisfied
492  Real rel_error = std::abs(value - closest_value) / std::abs(value);
493  if (rel_error <= tol)
494  {
495  return true;
496  }
497 
498  // If relative tolerance isn't satisfied, we should still check an absolute
499  // error, since relative tolerance can be misleading if value is close to zero
500  Real abs_error = std::abs(value - closest_value);
501  return (abs_error <= tol);
502 }
503 
504 } // namespace libMesh
virtual void clear()
Clear all the data structures associated with the system.
bool parameters_initialized
Flag indicating whether the parameters have been initialized.
const RBParameters & get_parameters_max() const
Get an RBParameters object that specifies the maximum allowable value for each parameter.
double abs(double a)
bool valid_params(const RBParameters &params)
Helper function to check that params is valid.
void write_parameter_data_to_files(const std::string &continuous_param_file_name, const std::string &discrete_param_file_name, const bool write_binary_data)
Write out the parameter ranges to files.
void read_parameter_ranges_from_file(const std::string &file_name, const bool read_binary, RBParameters &param_min, RBParameters &param_max)
Read in the parameter ranges from file.
Real get_value(const std::string &param_name) const
Get the value of the specific parameter.
Definition: rb_parameters.C:45
void print_discrete_parameter_values() const
Print out all the discrete parameter values.
void write_discrete_parameter_values_to_file(const std::string &file_name, const bool write_binary_data)
Write out the discrete parameter values to file.
void close()
Closes the file if it is open.
Definition: xdr_cxx.C:271
unsigned int get_n_params() const
Get the number of parameters.
virtual ~RBParametrized()
Destructor.
unsigned int n_parameters() const
Get the number of parameters that have been added.
Definition: rb_parameters.C:62
bool is_discrete_parameter(const std::string &mu_name) const
Is parameter mu_name discrete?
void read_discrete_parameter_values_from_file(const std::string &file_name, const bool read_binary_data, std::map< std::string, std::vector< Real >> &discrete_parameter_values_in)
Read in the discrete parameter values from file, if we have any.
void get_parameter_names(std::set< std::string > &param_names) const
Fill param_names with the names of the parameters.
Definition: rb_parameters.C:68
static const Real TOLERANCE
void read_parameter_data_from_files(const std::string &continuous_param_file_name, const std::string &discrete_param_file_name, const bool read_binary_data)
Read in the parameter ranges from files.
The libMesh namespace provides an interface to certain functionality in the library.
static Real get_closest_value(Real value, const std::vector< Real > &list_of_values)
long double max(long double a, double b)
Real distance(const Point &p)
static bool is_value_in_list(Real value, const std::vector< Real > &list_of_values, Real tol)
Helper function to check if the specified value is in the list of values (within a tolerance given by...
libmesh_assert(j)
RBParameters parameters_min
Vectors that define the ranges (min and max) for the parameters.
Real get_parameter_max(const std::string &param_name) const
Get maximum allowable value of parameter param_name.
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:32
void set_parameters(const RBParameters &params)
Set the current parameters to params.
void write_parameter_ranges_to_file(const std::string &file_name, const bool write_binary)
Write out the parameter ranges to file.
RBParametrized()
Constructor.
const_iterator end() const
Get a constant iterator to the end of this RBParameters object.
Definition: rb_parameters.C:85
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:42
std::map< std::string, std::vector< Real > > _discrete_parameter_values
Map that defines the allowable values of any discrete parameters.
bool verbose_mode
Public boolean to toggle verbose mode.
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:68
const RBParameters & get_parameters_min() const
Get an RBParameters object that specifies the minimum allowable value for each parameter.
unsigned int get_n_discrete_params() const
Get the number of discrete parameters.
void print() const
Print the parameters.
unsigned int get_n_continuous_params() const
Get the number of continuous parameters.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
Definition: rb_parameters.C:57
void print_parameters() const
Print the current parameters.
This class is part of the rbOOmit framework.
OStreamProxy out
void clear()
Clear this object.
Definition: rb_parameters.C:40
static const bool value
Definition: xdr_io.C:108
const std::map< std::string, std::vector< Real > > & get_discrete_parameter_values() const
Get a const reference to the discrete parameter values.
const_iterator begin() const
Get a constant iterator to beginning of this RBParameters object.
Definition: rb_parameters.C:80
const RBParameters & get_parameters() const
Get the current parameters.
std::vector< ElemQuality > valid(const ElemType t)
Returns all valid quality metrics for element type t.
Definition: elem_quality.C:321
std::map< std::string, Real >::const_iterator const_iterator
Definition: rb_parameters.h:57
void initialize_parameters(const RBParameters &mu_min_in, const RBParameters &mu_max_in, const std::map< std::string, std::vector< Real >> &discrete_parameter_values)
Initialize the parameter ranges and set current_parameters.
RBParameters parameters
Vector storing the current parameters.
std::set< std::string > get_parameter_names() const
Get a set that stores the parameter names.
Real get_parameter_min(const std::string &param_name) const
Get minimum allowable value of parameter param_name.