libMesh
rb_eim_construction.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_EIM_CONSTRUCTION_H
21 #define LIBMESH_RB_EIM_CONSTRUCTION_H
22 
23 // rbOOmit includes
24 #include "libmesh/rb_construction.h"
25 #include "libmesh/rb_assembly_expansion.h"
26 #include "libmesh/rb_eim_assembly.h"
27 #include "libmesh/rb_eim_evaluation.h"
28 
29 // libMesh includes
30 #include "libmesh/mesh_function.h"
31 #include "libmesh/coupling_matrix.h"
32 
33 // C++ includes
34 #include <unordered_map>
35 #include <map>
36 #include <string>
37 #include <memory>
38 #include <vector>
39 
40 namespace libMesh
41 {
42 
49 {
52  unsigned int side_index;
53  unsigned int comp_index;
54  unsigned int qp_index;
55 };
56 
67 class RBEIMConstruction : public RBConstructionBase<System>
68 {
69 public:
70 
72 
78  const std::string & name,
79  const unsigned int number);
80 
86  RBEIMConstruction (RBEIMConstruction &&) = default;
87  RBEIMConstruction (const RBEIMConstruction &) = delete;
90  virtual ~RBEIMConstruction ();
91 
96 
101 
106 
110  virtual void clear() override;
111 
115  void set_rb_eim_evaluation(RBEIMEvaluation & rb_eim_eval_in);
116 
121 
125  const RBEIMEvaluation & get_rb_eim_evaluation() const;
126 
132 
137  virtual void process_parameters_file (const std::string & parameters_filename);
138 
143  void set_rb_construction_parameters(unsigned int n_training_samples_in,
144  bool deterministic_training_in,
145  int training_parameters_random_seed_in,
146  bool quiet_mode_in,
147  unsigned int Nmax_in,
148  Real rel_training_tolerance_in,
149  Real abs_training_tolerance_in,
150  const RBParameters & mu_min_in,
151  const RBParameters & mu_max_in,
152  const std::map<std::string, std::vector<Real>> & discrete_parameter_values_in,
153  const std::map<std::string,bool> & log_scaling,
154  std::map<std::string, std::vector<RBParameter>> * training_sample_list=nullptr);
155 
160  virtual void set_best_fit_type_flag (const std::string & best_fit_type_string);
161 
165  virtual void print_info();
166 
171  virtual Real train_eim_approximation();
172 
180 
188 
195  virtual void initialize_eim_assembly_objects();
196 
200  std::vector<std::unique_ptr<ElemAssembly>> & get_eim_assembly_objects();
201 
208  virtual std::unique_ptr<ElemAssembly> build_eim_assembly(unsigned int bf_index) = 0;
209 
213  virtual void init_context(FEMContext &);
214 
218  void set_rel_training_tolerance(Real new_training_tolerance);
220 
224  void set_abs_training_tolerance(Real new_training_tolerance);
226 
231  unsigned int get_Nmax() const;
232  virtual void set_Nmax(unsigned int Nmax);
233 
239 
249 
254  const QpDataMap & get_parametrized_function_from_training_set(unsigned int training_index) const;
255  const SideQpDataMap & get_side_parametrized_function_from_training_set(unsigned int training_index) const;
256  const NodeDataMap & get_node_parametrized_function_from_training_set(unsigned int training_index) const;
257 
261  const std::unordered_map<dof_id_type, std::vector<Real> > & get_local_quad_point_JxW();
262  const std::map<std::pair<dof_id_type,unsigned int>, std::vector<Real> > & get_local_side_quad_point_JxW();
263 
267  unsigned int get_n_parametrized_functions_for_training() const;
268 
273 
281 
282 protected:
283 
296  bool add_basis_function,
297  EimPointData * eim_point_data);
298 
302  void enrich_eim_approximation_on_nodes(const NodeDataMap & node_pf,
303  bool add_basis_function,
304  EimPointData * eim_point_data);
305 
309  void enrich_eim_approximation_on_interiors(const QpDataMap & interior_pf,
310  bool add_basis_function,
311  EimPointData * eim_point_data);
312 
319  void update_eim_matrices(bool set_eim_error_indicator);
320 
321 private:
322 
328  std::pair<Real, unsigned int> compute_max_eim_error();
329 
335 
340  void initialize_qp_data();
341 
351  Number inner_product(const QpDataMap & v, const QpDataMap & w, bool apply_comp_scaling);
352 
356  Number side_inner_product(const SideQpDataMap & v, const SideQpDataMap & w, bool apply_comp_scaling);
357 
361  Number node_inner_product(const NodeDataMap & v, const NodeDataMap & w, bool apply_comp_scaling);
362 
367  template <class DataMap>
368  Real get_max_abs_value(const DataMap & v) const
369  {
370  Real max_value = 0.;
371 
372  for (const auto & pr : v)
373  {
374  const auto & v_comp_and_qp = pr.second;
375 
376  for (const auto & comp : index_range(v_comp_and_qp))
377  {
378  Real comp_scaling = 1.;
379  if (get_rb_eim_evaluation().scale_components_in_enrichment().count(comp))
380  {
381  // Make sure that _component_scaling_in_training_set is initialized
382  libmesh_error_msg_if(comp >= _component_scaling_in_training_set.size(),
383  "Invalid vector index");
384  comp_scaling = _component_scaling_in_training_set[comp];
385  }
386 
387  const std::vector<Number> & v_qp = v_comp_and_qp[comp];
388  for (Number value : v_qp)
389  max_value = std::max(max_value, std::abs(value * comp_scaling));
390  }
391  }
392 
393  comm().max(max_value);
394  return max_value;
395  }
396 
401  Real get_node_max_abs_value(const NodeDataMap & v) const;
402 
406  void enrich_eim_approximation(unsigned int training_index,
407  bool add_basis_function,
408  EimPointData * eim_point_data);
409 
413  template<class DataMap>
414  static void scale_parametrized_function(DataMap & local_pf,
415  Number scaling_factor)
416  {
417  for (auto & pr : local_pf)
418  {
419  auto & comp_and_qp = pr.second;
420 
421  for (unsigned int comp : index_range(comp_and_qp))
422  {
423  std::vector<Number> & qp_values = comp_and_qp[comp];
424 
425  for (unsigned int qp : index_range(qp_values))
426  {
427  qp_values[qp] *= scaling_factor;
428  }
429  }
430  }
431  }
432 
438  static void scale_node_parametrized_function(NodeDataMap & local_pf,
439  Number scaling_factor);
440 
444  static unsigned int get_random_int_0_to_n(unsigned int n);
445 
452 
458 
462  unsigned int _Nmax;
463 
469 
475 
480 
485  std::vector<std::unique_ptr<ElemAssembly>> _rb_eim_assembly_objects;
486 
499 
506 
513 
519 
524 
533 
545  std::unordered_map<dof_id_type, std::vector<Point> > _local_quad_point_locations;
546  std::unordered_map<dof_id_type, std::vector<Real> > _local_quad_point_JxW;
547  std::unordered_map<dof_id_type, subdomain_id_type > _local_quad_point_subdomain_ids;
548 
556  std::unordered_map<dof_id_type, std::vector<std::vector<Point>> > _local_quad_point_locations_perturbations;
557 
561  std::map<std::pair<dof_id_type,unsigned int>, std::vector<Point> > _local_side_quad_point_locations;
562  std::map<std::pair<dof_id_type,unsigned int>, std::vector<Real> > _local_side_quad_point_JxW;
563  std::map<std::pair<dof_id_type,unsigned int>, subdomain_id_type > _local_side_quad_point_subdomain_ids;
564  std::map<std::pair<dof_id_type,unsigned int>, boundary_id_type > _local_side_quad_point_boundary_ids;
565  std::map<std::pair<dof_id_type,unsigned int>, std::vector<std::vector<Point>> > _local_side_quad_point_locations_perturbations;
566 
570  std::unordered_map<dof_id_type, Point > _local_node_locations;
571  std::unordered_map<dof_id_type, boundary_id_type > _local_node_boundary_ids;
572 
580  std::map<std::pair<dof_id_type,unsigned int>, unsigned int > _local_side_quad_point_side_types;
581 
582 };
583 
584 } // namespace libMesh
585 
586 #endif // LIBMESH_RB_EIM_CONSTRUCTION_H
Real get_max_abs_value(const DataMap &v) const
Get the maximum absolute value from a vector stored in the format that we use for basis functions...
This is the EquationSystems class.
std::map< std::pair< dof_id_type, unsigned int >, std::vector< std::vector< Number > > > SideQpDataMap
Type of the data structure used to map from (elem id, side index) -> [n_vars][n_qp] data...
Real get_max_abs_value_in_training_set() const
Get the maximum value (across all processors) from the parametrized functions in the training set...
std::unordered_map< dof_id_type, boundary_id_type > _local_node_boundary_ids
std::map< dof_id_type, std::vector< Number > > NodeDataMap
Type of the data structure used to map from (node id) -> [n_vars] data.
void initialize_eim_construction()
Perform initialization of this object to prepare for running train_eim_approximation().
RBEIMEvaluation & get_rb_eim_evaluation()
Get a reference to the RBEvaluation object.
virtual Real train_eim_approximation()
Generate the EIM approximation for the specified parametrized function using either POD or the Greedy...
std::vector< SideQpDataMap > _local_side_parametrized_functions_for_training
Same as _local_parametrized_functions_for_training except for side data.
Number side_inner_product(const SideQpDataMap &v, const SideQpDataMap &w, bool apply_comp_scaling)
Same as inner_product() except for side data.
This class is part of the rbOOmit framework.
std::map< std::pair< dof_id_type, unsigned int >, unsigned int > _local_side_quad_point_side_types
For side data, we also store "side type" info.
std::unordered_map< dof_id_type, std::vector< Real > > _local_quad_point_JxW
This struct is used to encapsulate the arguments required to specify an EIM point that we may add to ...
virtual void set_best_fit_type_flag(const std::string &best_fit_type_string)
Specify which type of "best fit" we use to guide the EIM greedy algorithm.
void reinit_eim_projection_matrix()
Zero the _eim_projection_matrix and resize it to be get_Nmax() x get_Nmax().
virtual void process_parameters_file(const std::string &parameters_filename)
Read parameters in from file and set up this system accordingly.
Real _max_abs_value_in_training_set
Maximum value in _local_parametrized_functions_for_training across all processors.
const Parallel::Communicator & comm() const
RBEIMEvaluation * _rb_eim_eval
The RBEIMEvaluation object that we use to perform the EIM training.
const QpDataMap & get_parametrized_function_from_training_set(unsigned int training_index) const
Get a const reference to the specified parametrized function from the training set.
RBEIMConstruction(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
void enrich_eim_approximation_on_interiors(const QpDataMap &interior_pf, bool add_basis_function, EimPointData *eim_point_data)
Implementation of enrich_eim_approximation() for the case of element interiors.
virtual void clear() override
Clear this object.
EimPointData get_random_point(const QpDataMap &v)
Helper function that identifies a random EIM point from v.
The libMesh namespace provides an interface to certain functionality in the library.
void set_abs_training_tolerance(Real new_training_tolerance)
Get/set the absolute tolerance for the basis training.
void update_eim_matrices(bool set_eim_error_indicator)
Update the matrices used in training the EIM approximation.
virtual Real train_eim_approximation_with_greedy()
Generate the EIM approximation for the specified parametrized function using the Greedy Algorithm...
Number node_inner_product(const NodeDataMap &v, const NodeDataMap &w, bool apply_comp_scaling)
Same as inner_product() except for node data.
virtual void set_Nmax(unsigned int Nmax)
virtual void print_info()
Print out info that describes the current setup of this RBConstruction.
std::map< std::pair< dof_id_type, unsigned int >, subdomain_id_type > _local_side_quad_point_subdomain_ids
Real get_node_max_abs_value(const NodeDataMap &v) const
Get the maximum absolute value from a vector stored in the format that we use for basis functions...
const std::unordered_map< dof_id_type, std::vector< Real > > & get_local_quad_point_JxW()
Get the interior and side quadrature weights.
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:57
void enrich_eim_approximation_on_nodes(const NodeDataMap &node_pf, bool add_basis_function, EimPointData *eim_point_data)
Implementation of enrich_eim_approximation() for the case of element nodes.
unsigned int number() const
Definition: system.h:2269
std::vector< QpDataMap > _local_parametrized_functions_for_training
The parametrized functions that are used for training.
int8_t boundary_id_type
Definition: id_types.h:51
std::vector< NodeDataMap > _local_node_parametrized_functions_for_training
Same as _local_parametrized_functions_for_training except for node data.
void enrich_eim_approximation_on_sides(const SideQpDataMap &side_pf, bool add_basis_function, EimPointData *eim_point_data)
Implementation of enrich_eim_approximation() for the case of element sides.
unsigned int _Nmax
Maximum number of EIM basis functions we are willing to use.
std::map< dof_id_type, std::vector< std::vector< Number > > > QpDataMap
Type of the data structure used to map from (elem id) -> [n_vars][n_qp] data.
std::map< std::pair< dof_id_type, unsigned int >, std::vector< Real > > _local_side_quad_point_JxW
virtual void init_context(FEMContext &)
Pre-request FE data needed for calculations.
void store_eim_solutions_for_training_set()
Get the EIM solution vector at all parametrized functions in the training set.
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:62
unsigned int _max_abs_value_in_training_set_index
The training sample index at which we found _max_abs_value_in_training_set.
RBEIMConstruction & operator=(const RBEIMConstruction &)=delete
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:52
virtual std::unique_ptr< ElemAssembly > build_eim_assembly(unsigned int bf_index)=0
Build an element assembly object that will access basis function bf_index.
BEST_FIT_TYPE best_fit_type_flag
Enum that indicates which type of "best fit" algorithm we should use.
DenseMatrix< Number > _eim_projection_matrix
The matrix we use in order to perform L2 projections of parametrized functions as part of EIM trainin...
EimPointData get_random_point_from_training_sample()
Get a random point using the 0^th training sample as input to get_random_point(). ...
static void scale_parametrized_function(DataMap &local_pf, Number scaling_factor)
Scale all values in pf by scaling_factor.
void set_rb_eim_evaluation(RBEIMEvaluation &rb_eim_eval_in)
Set the RBEIMEvaluation object.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::map< std::pair< dof_id_type, unsigned int >, boundary_id_type > _local_side_quad_point_boundary_ids
std::unordered_map< dof_id_type, Point > _local_node_locations
Same as above except for node data.
const SideQpDataMap & get_side_parametrized_function_from_training_set(unsigned int training_index) const
virtual void initialize_eim_assembly_objects()
Build a vector of ElemAssembly objects that accesses the basis functions stored in this RBEIMConstruc...
void max(const T &r, T &o, Request &req) const
void set_rel_training_tolerance(Real new_training_tolerance)
Get/set the relative tolerance for the basis training.
std::vector< std::unique_ptr< ElemAssembly > > & get_eim_assembly_objects()
Number inner_product(const QpDataMap &v, const QpDataMap &w, bool apply_comp_scaling)
Evaluate the inner product of vec1 and vec2 which specify values at quadrature points.
static const bool value
Definition: xdr_io.C:54
const std::map< std::pair< dof_id_type, unsigned int >, std::vector< Real > > & get_local_side_quad_point_JxW()
void initialize_parametrized_functions_in_training_set()
Compute and store the parametrized function for each parameter in the training set at all the stored ...
unsigned int get_Nmax() const
Get/set Nmax, the maximum number of RB functions we are willing to compute.
std::pair< Real, unsigned int > compute_max_eim_error()
Find the training sample that has the largest EIM approximation error based on the current EIM approx...
RBEIMEvaluation::NodeDataMap NodeDataMap
Type of the data structure used to map from node id -> [n_vars] data.
This class is part of the rbOOmit framework.
std::vector< Real > _component_scaling_in_training_set
Keep track of a scaling factor for each component of the parametrized functions in the training set w...
void enrich_eim_approximation(unsigned int training_index, bool add_basis_function, EimPointData *eim_point_data)
Add a new basis function to the EIM approximation.
Real _rel_training_tolerance
Relative and absolute tolerances for training the EIM approximation.
RBEIMEvaluation::QpDataMap QpDataMap
Type of the data structure used to map from (elem id) -> [n_vars][n_qp] data.
unsigned int get_n_parametrized_functions_for_training() const
Get the number of parametrized functions used for training.
virtual Real train_eim_approximation_with_POD()
Generate the EIM approximation for the specified parametrized function using Proper Orthogonal Decomp...
const std::string & name() const
Definition: system.h:2261
std::unordered_map< dof_id_type, std::vector< Point > > _local_quad_point_locations
The quadrature point locations, quadrature point weights (JxW), and subdomain IDs on every element lo...
const NodeDataMap & get_node_parametrized_function_from_training_set(unsigned int training_index) const
std::map< std::pair< dof_id_type, unsigned int >, std::vector< Point > > _local_side_quad_point_locations
Same as above except for side data.
std::vector< std::unique_ptr< ElemAssembly > > _rb_eim_assembly_objects
The vector of assembly objects that are created to point to this RBEIMConstruction.
RBEIMEvaluation::SideQpDataMap SideQpDataMap
Type of the data structure used to map from (elem id,side_index) -> [n_vars][n_qp] data...
std::map< std::pair< dof_id_type, unsigned int >, std::vector< std::vector< Point > > > _local_side_quad_point_locations_perturbations
This class enables evaluation of an Empirical Interpolation Method (EIM) approximation.
void initialize_qp_data()
Initialize the data associated with each quad point (location, JxW, etc.) so that we can use this in ...
std::unordered_map< dof_id_type, std::vector< std::vector< Point > > > _local_quad_point_locations_perturbations
EIM approximations often arise when applying a geometric mapping to a Reduced Basis formulation...
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:111
static void scale_node_parametrized_function(NodeDataMap &local_pf, Number scaling_factor)
Scale all values in pf by scaling_factor The templated function above handles the elem and side cases...
void set_rb_construction_parameters(unsigned int n_training_samples_in, bool deterministic_training_in, int training_parameters_random_seed_in, bool quiet_mode_in, unsigned int Nmax_in, Real rel_training_tolerance_in, Real abs_training_tolerance_in, const RBParameters &mu_min_in, const RBParameters &mu_max_in, const std::map< std::string, std::vector< Real >> &discrete_parameter_values_in, const std::map< std::string, bool > &log_scaling, std::map< std::string, std::vector< RBParameter >> *training_sample_list=nullptr)
Set the state of this RBConstruction object based on the arguments to this function.
std::unordered_map< dof_id_type, subdomain_id_type > _local_quad_point_subdomain_ids
static unsigned int get_random_int_0_to_n(unsigned int n)
Static helper function that is used by get_random_point().
uint8_t dof_id_type
Definition: id_types.h:67