21 #include "libmesh/int_range.h" 22 #include "libmesh/simple_range.h" 23 #include "libmesh/xdr_cxx.h" 26 #include "libmesh/rb_parametrized.h" 39 parameters_initialized(false)
55 const std::map<std::string, std::vector<Real>> & discrete_parameter_values)
59 "Error: Invalid mu_min/mu_max in initialize_parameters(), different number of parameters.");
60 libmesh_error_msg_if(mu_min_in.
n_samples() != 1 ||
62 "Error: Invalid mu_min/mu_max in initialize_parameters(), only 1 sample supported.");
68 libmesh_error_msg_if((*pr_min).second > (*pr_max).second,
69 "Error: Invalid mu_min/mu_max in RBParameters constructor.");
75 for (
const auto & [
name, vals] : discrete_parameter_values)
77 libmesh_error_msg_if(vals.empty(),
"Error: List of discrete parameters for " <<
name <<
" is empty.");
79 Real min_val = *std::min_element(vals.begin(), vals.end());
80 Real max_val = *std::max_element(vals.begin(), vals.end());
82 libmesh_assert_less_equal(min_val, max_val);
105 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_n_params");
114 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_n_continuous_params");
123 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_n_discrete_params");
125 return cast_int<unsigned int>
129 #ifdef LIBMESH_ENABLE_DEPRECATED 132 libmesh_deprecated();
133 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameter_names");
135 std::set<std::string> parameter_names;
137 parameter_names.insert(pr.first);
139 return parameter_names;
141 #endif // LIBMESH_ENABLE_DEPRECATED 145 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::set_parameters");
159 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameters");
166 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameters_min");
173 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameters_max");
180 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameter_min");
187 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_parameter_max");
194 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::print_current_parameters");
200 const std::string & discrete_param_file_name,
201 const bool write_binary_data)
208 const bool write_binary_data)
214 Xdr parameter_ranges_out(file_name, mode);
216 parameter_ranges_out << n_continuous_params;
224 std::string param_name = pr.first;
228 parameter_ranges_out << param_name << param_value;
233 std::string param_name = pr.first;
237 parameter_ranges_out << param_name << param_value;
241 parameter_ranges_out.
close();
245 const bool write_binary_data)
253 Xdr discrete_parameters_out(file_name, mode);
255 discrete_parameters_out << n_discrete_params;
263 std::string param_name = pr.first;
264 auto n_discrete_values = cast_int<unsigned int>(pr.second.size());
265 discrete_parameters_out << param_name << n_discrete_values;
267 for (
unsigned int i=0; i<n_discrete_values; i++)
269 Real discrete_value = pr.second[i];
270 discrete_parameters_out << discrete_value;
277 const std::string & discrete_param_file_name,
278 const bool read_binary_data)
287 std::map<std::string, std::vector<Real>> discrete_parameter_values_in;
290 discrete_parameter_values_in);
296 const bool read_binary_data,
304 Xdr parameter_ranges_in(file_name, mode);
305 unsigned int n_continuous_params;
306 parameter_ranges_in >> n_continuous_params;
308 for (
unsigned int i=0; i<n_continuous_params; i++)
310 std::string param_name;
313 parameter_ranges_in >> param_name;
314 parameter_ranges_in >> param_value;
316 param_min.
set_value(param_name, param_value);
318 for (
unsigned int i=0; i<n_continuous_params; i++)
320 std::string param_name;
323 parameter_ranges_in >> param_name;
324 parameter_ranges_in >> param_value;
326 param_max.
set_value(param_name, param_value);
329 parameter_ranges_in.
close();
333 const bool read_binary_data,
334 std::map<std::string, std::vector<Real>> & discrete_parameter_values)
337 std::ifstream check_if_file_exists(file_name.c_str());
338 if (check_if_file_exists.good())
344 Xdr discrete_parameter_values_in(file_name, mode);
345 unsigned int n_discrete_params;
346 discrete_parameter_values_in >> n_discrete_params;
348 for (
unsigned int i=0; i<n_discrete_params; i++)
350 std::string param_name;
351 discrete_parameter_values_in >> param_name;
353 unsigned int n_discrete_values;
354 discrete_parameter_values_in >> n_discrete_values;
356 std::vector<Real> discrete_values(n_discrete_values);
357 for (
auto & val : discrete_values)
358 discrete_parameter_values_in >> val;
360 discrete_parameter_values[param_name] = discrete_values;
368 "Error: parameters not initialized in RBParametrized::is_discrete_parameter");
375 libmesh_error_msg_if(!
parameters_initialized,
"Error: parameters not initialized in RBParametrized::get_discrete_parameter_values");
386 for (
const auto &
value : values)
396 "Error: Number of parameters don't match; found " 400 bool is_valid =
true;
401 std::string prev_param_name =
"";
402 for (
const auto & [param_name, sample_vec] : params)
404 std::size_t sample_idx = 0;
407 for (
const auto & value_vec : sample_vec)
409 for (
const auto &
value : value_vec)
413 const bool outside_range = ((value < min_value) || (value > max_value));
414 is_valid = is_valid && !outside_range;
417 libMesh::out <<
"Warning: parameter " << param_name <<
" value=" 418 <<
value <<
" outside acceptable range: (" 419 << min_value <<
", " << max_value <<
")";
430 const bool is_value_discrete =
434 is_valid = is_valid && is_value_discrete;
436 libMesh::out <<
"Warning: parameter " << param_name <<
" value=" 437 <<
value <<
" is not in discrete value list.";
448 libmesh_error_msg_if(list_of_values.empty(),
"Error: list_of_values is empty.");
450 Real min_distance = std::numeric_limits<Real>::max();
451 Real closest_val = 0.;
452 for (
const auto & current_value : list_of_values)
458 closest_val = current_value;
470 Real rel_error = std::abs(
value - closest_value) / std::abs(
value);
471 if (rel_error <= tol)
478 Real abs_error = std::abs(
value - closest_value);
479 return (abs_error <= tol);
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Real get_value(const std::string ¶m_name) const
Get the value of the specified parameter, throw an error if it does not exist.
virtual void clear()
Clear all the data structures associated with the system.
bool parameters_initialized
Flag indicating whether the parameters have been initialized.
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 ¶m_min, RBParameters ¶m_max)
Read in the parameter ranges from file.
static constexpr Real TOLERANCE
const_iterator begin_serialized() const
Get const_iterator access to the parameters stored in this RBParameters object.
Real get_parameter_min(const std::string ¶m_name) const
Get minimum allowable value of parameter param_name.
Real get_parameter_max(const std::string ¶m_name) const
Get maximum allowable value of parameter param_name.
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.
const RBParameters & get_parameters_max() const
Get an RBParameters object that specifies the maximum allowable value for each parameter.
virtual ~RBParametrized()
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 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)
void print(unsigned precision=6, int max_values=5) const
Print the parameters.
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...
unsigned int get_n_discrete_params() const
Get the number of discrete parameters.
XdrMODE
Defines an enum for read/write mode in Xdr format.
RBParameters parameters_min
Vectors that define the ranges (min and max) for the parameters.
unsigned int n_parameters() const
Get the number of parameters that have been added.
const RBParameters & get_parameters_min() const
Get an RBParameters object that specifies the minimum allowable value for each parameter.
std::set< std::string > get_parameter_names() const
Get a set that stores the parameter names.
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 std::map< std::string, std::vector< Real > > & get_discrete_parameter_values() const
Get a const reference to the discrete parameter values.
const RBParameters & get_parameters() const
Get the current parameters.
void print_parameters() const
Print the current parameters.
This class is part of the rbOOmit framework.
std::map< std::string, std::vector< Real > > _discrete_parameter_values
Map that defines the allowable values of any discrete parameters.
unsigned int get_n_continuous_params() const
Get the number of continuous parameters.
bool verbose_mode
Public boolean to toggle verbose mode.
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
bool set_parameters(const RBParameters ¶ms)
Set the current parameters to params The parameters are checked for validity; an error is thrown if t...
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_value(const std::string ¶m_name, Real value)
Set the value of the specified parameter.
This class is part of the rbOOmit framework.
void clear()
Clear this object.
const_iterator end_serialized() const
RBParameters parameters_max
bool check_if_valid_params(const RBParameters ¶ms) const
Helper function to check that params is valid:
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.
unsigned int get_n_params() const
Get the number of parameters.
bool is_discrete_parameter(const std::string &mu_name) const
Is parameter mu_name discrete?
void print_discrete_parameter_values() const
Print out all the discrete parameter values.
RBParameters parameters
Vector storing the current parameters.