Go to the documentation of this file.
   21 #include "libmesh/rb_evaluation.h" 
   22 #include "libmesh/rb_theta_expansion.h" 
   25 #include "libmesh/libmesh_version.h" 
   26 #include "libmesh/system.h" 
   27 #include "libmesh/numeric_vector.h" 
   28 #include "libmesh/libmesh_logging.h" 
   29 #include "libmesh/xdr_cxx.h" 
   30 #include "libmesh/mesh_tools.h" 
   31 #include "libmesh/utility.h" 
   37 #include <sys/types.h> 
   50   evaluate_RB_error_bound(true),
 
   51   compute_RB_inner_product(false),
 
   52   rb_theta_expansion(nullptr)
 
   64   LOG_SCOPE(
"clear()", 
"RBEvaluation");
 
   91     libmesh_error_msg(
"Error: rb_theta_expansion hasn't been initialized yet");
 
  109                                           bool resize_error_bound_data)
 
  111   LOG_SCOPE(
"resize_data_structures()", 
"RBEvaluation");
 
  114     libmesh_error_msg(
"Error: Cannot set Nmax to be less than the current number of basis functions.");
 
  153   if (resize_error_bound_data)
 
  172       for (
unsigned int i=0; i<Q_a_hat; i++)
 
  175           for (
unsigned int j=0; j<Nmax; j++)
 
  211   LOG_SCOPE(
"rb_solve()", 
"RBEvaluation");
 
  214     libmesh_error_msg(
"ERROR: N cannot be larger than the number of basis functions in rb_solve");
 
  223   RB_system_matrix.
zero();
 
  271       libmesh_assert_greater ( alpha_LB, 0. );
 
  282       return abs_error_bound;
 
  299   return normalization;
 
  304   LOG_SCOPE(
"compute_residual_dual_norm()", 
"RBEvaluation");
 
  310   Number residual_norm_sq = 0.;
 
  317           Real delta = (q_f1==q_f2) ? 1. : 2.;
 
  330           for (
unsigned int i=0; i<N; i++)
 
  346           Real delta = (q_a1==q_a2) ? 1. : 2.;
 
  348           for (
unsigned int i=0; i<N; i++)
 
  350               for (
unsigned int j=0; j<N; j++)
 
  371       residual_norm_sq = 
std::abs(residual_norm_sq);
 
  394   Number output_bound_sq = 0.;
 
  400           Real delta = (q_l1==q_l2) ? 1. : 2.;
 
  417                                                       const bool write_binary_data)
 
  419   LOG_SCOPE(
"legacy_write_offline_data_to_files()", 
"RBEvaluation");
 
  428   const std::string suffix = write_binary_data ? 
".xdr" : 
".dat";
 
  442       std::ostringstream file_name;
 
  444         file_name << directory_name << 
"/n_bfs" << suffix;
 
  445         Xdr n_bfs_out(file_name.str(), mode);
 
  452       file_name << directory_name << 
"/parameter_ranges" << suffix;
 
  453       std::string continuous_param_file_name = file_name.str();
 
  457       file_name << directory_name << 
"/discrete_parameter_values" << suffix;
 
  458       std::string discrete_param_file_name = file_name.str();
 
  461                                     discrete_param_file_name,
 
  466       file_name << directory_name << 
"/Fq_innerprods" << suffix;
 
  467       Xdr RB_Fq_innerprods_out(file_name.str(), mode);
 
  469       for (
unsigned int i=0; i<Q_f_hat; i++)
 
  473       RB_Fq_innerprods_out.close();
 
  479           file_name << directory_name << 
"/output_";
 
  480           file_name << std::setw(3)
 
  481                     << std::setprecision(0)
 
  486           file_name << 
"_dual_innerprods" << suffix;
 
  487           Xdr output_dual_innerprods_out(file_name.str(), mode);
 
  490           for (
unsigned int q=0; q<Q_l_hat; q++)
 
  494           output_dual_innerprods_out.close();
 
  504               file_name << directory_name << 
"/output_";
 
  505               file_name << std::setw(3)
 
  506                         << std::setprecision(0)
 
  511               file_name << std::setw(3)
 
  512                         << std::setprecision(0)
 
  517               Xdr output_n_out(file_name.str(), mode);
 
  519               for (
unsigned int j=0; j<n_bfs; j++)
 
  523               output_n_out.close();
 
  531           file_name << directory_name << 
"/RB_inner_product_matrix" << suffix;
 
  532           Xdr RB_inner_product_matrix_out(file_name.str(), mode);
 
  533           for (
unsigned int i=0; i<n_bfs; i++)
 
  535               for (
unsigned int j=0; j<n_bfs; j++)
 
  540           RB_inner_product_matrix_out.close();
 
  547           file_name << directory_name << 
"/RB_F_";
 
  548           file_name << std::setw(3)
 
  549                     << std::setprecision(0)
 
  554           Xdr RB_Fq_f_out(file_name.str(), mode);
 
  556           for (
unsigned int i=0; i<n_bfs; i++)
 
  567           file_name << directory_name << 
"/RB_A_";
 
  568           file_name << std::setw(3)
 
  569                     << std::setprecision(0)
 
  574           Xdr RB_Aq_a_out(file_name.str(), mode);
 
  576           for (
unsigned int i=0; i<n_bfs; i++)
 
  578               for (
unsigned int j=0; j<n_bfs; j++)
 
  588       file_name << directory_name << 
"/Fq_Aq_innerprods" << suffix;
 
  589       Xdr RB_Fq_Aq_innerprods_out(file_name.str(), mode);
 
  595               for (
unsigned int i=0; i<n_bfs; i++)
 
  601       RB_Fq_Aq_innerprods_out.close();
 
  605       file_name << directory_name << 
"/Aq_Aq_innerprods" << suffix;
 
  606       Xdr RB_Aq_Aq_innerprods_out(file_name.str(), mode);
 
  609       for (
unsigned int i=0; i<Q_a_hat; i++)
 
  611           for (
unsigned int j=0; j<n_bfs; j++)
 
  613               for (
unsigned int l=0; l<n_bfs; l++)
 
  619       RB_Aq_Aq_innerprods_out.close();
 
  624         file_name << directory_name << 
"/greedy_params" << suffix;
 
  625         Xdr greedy_params_out(file_name.str(), mode);
 
  628           for (
const auto & pr : param)
 
  632               Real param_value = pr.second;
 
  633               greedy_params_out << param_value;
 
  635         greedy_params_out.close();
 
  642                                                        bool read_error_bound_data,
 
  643                                                        const bool read_binary_data)
 
  645   LOG_SCOPE(
"legacy_read_offline_data_from_files()", 
"RBEvaluation");
 
  651   const std::string suffix = read_binary_data ? 
".xdr" : 
".dat";
 
  654   std::ostringstream file_name;
 
  659     file_name << directory_name << 
"/n_bfs" << suffix;
 
  662     Xdr n_bfs_in(file_name.str(), mode);
 
  670   file_name << directory_name << 
"/parameter_ranges" << suffix;
 
  671   std::string continuous_param_file_name = file_name.str();
 
  675   file_name << directory_name << 
"/discrete_parameter_values" << suffix;
 
  676   std::string discrete_param_file_name = file_name.str();
 
  678                                  discrete_param_file_name,
 
  687           file_name << directory_name << 
"/output_";
 
  688           file_name << std::setw(3)
 
  689                     << std::setprecision(0)
 
  694           file_name << std::setw(3)
 
  695                     << std::setprecision(0)
 
  702           Xdr output_n_in(file_name.str(), mode);
 
  704           for (
unsigned int j=0; j<n_bfs; j++)
 
  707               output_n_in >> 
value;
 
  718       file_name << directory_name << 
"/RB_inner_product_matrix" << suffix;
 
  721       Xdr RB_inner_product_matrix_in(file_name.str(), mode);
 
  723       for (
unsigned int i=0; i<n_bfs; i++)
 
  725           for (
unsigned int j=0; j<n_bfs; j++)
 
  728               RB_inner_product_matrix_in >> 
value;
 
  732       RB_inner_product_matrix_in.close();
 
  739       file_name << directory_name << 
"/RB_F_";
 
  740       file_name << std::setw(3)
 
  741                 << std::setprecision(0)
 
  748       Xdr RB_Fq_f_in(file_name.str(), mode);
 
  750       for (
unsigned int i=0; i<n_bfs; i++)
 
  763       file_name << directory_name << 
"/RB_A_";
 
  764       file_name << std::setw(3)
 
  765                 << std::setprecision(0)
 
  772       Xdr RB_Aq_a_in(file_name.str(), mode);
 
  774       for (
unsigned int i=0; i<n_bfs; i++)
 
  776           for (
unsigned int j=0; j<n_bfs; j++)
 
  787   if (read_error_bound_data)
 
  791       file_name << directory_name << 
"/Fq_innerprods" << suffix;
 
  794       Xdr RB_Fq_innerprods_in(file_name.str(), mode);
 
  797       for (
unsigned int i=0; i<Q_f_hat; i++)
 
  801       RB_Fq_innerprods_in.close();
 
  807           file_name << directory_name << 
"/output_";
 
  808           file_name << std::setw(3)
 
  809                     << std::setprecision(0)
 
  813           file_name << 
"_dual_innerprods" << suffix;
 
  816           Xdr output_dual_innerprods_in(file_name.str(), mode);
 
  819           for (
unsigned int q=0; q<Q_l_hat; q++)
 
  823           output_dual_innerprods_in.close();
 
  829       file_name << directory_name << 
"/Fq_Aq_innerprods" << suffix;
 
  832       Xdr RB_Fq_Aq_innerprods_in(file_name.str(), mode);
 
  838               for (
unsigned int i=0; i<n_bfs; i++)
 
  844       RB_Fq_Aq_innerprods_in.close();
 
  848       file_name << directory_name << 
"/Aq_Aq_innerprods" << suffix;
 
  851       Xdr RB_Aq_Aq_innerprods_in(file_name.str(), mode);
 
  854       for (
unsigned int i=0; i<Q_a_hat; i++)
 
  856           for (
unsigned int j=0; j<n_bfs; j++)
 
  858               for (
unsigned int l=0; l<n_bfs; l++)
 
  864       RB_Aq_Aq_innerprods_in.close();
 
  876   if (!std::ifstream(file_name.c_str()))
 
  877     libmesh_error_msg(
"File missing: " + file_name);
 
  881                                              const std::string & directory_name,
 
  882                                              const bool write_binary_basis_functions)
 
  884   LOG_SCOPE(
"write_out_basis_functions()", 
"RBEvaluation");
 
  886   std::vector<NumericVector<Number>*> basis_functions_ptrs;
 
  893                     basis_functions_ptrs,
 
  896                     write_binary_basis_functions);
 
  901                                      const std::string & directory_name,
 
  902                                      const std::string & data_name,
 
  903                                      const bool write_binary_vectors)
 
  905   LOG_SCOPE(
"write_out_vectors()", 
"RBEvaluation");
 
  914   this->
comm().barrier();
 
  916   std::ostringstream file_name;
 
  917   const std::string basis_function_suffix = (write_binary_vectors ? 
".xdr" : 
".dat");
 
  919   file_name << directory_name << 
"/" << data_name << 
"_data" << basis_function_suffix;
 
  920   Xdr bf_data(file_name.str(),
 
  924 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 
  925   version += 
" with infinite elements";
 
  927   bf_data.data(version ,
"# File Format Identifier");
 
  938     std::vector<const NumericVector<Number> *> bf_out;
 
  939     for (
const auto & vec : vectors)
 
  940       bf_out.push_back(vec);
 
  949   bf_data.set_version(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION,
 
  950                                          LIBMESH_MINOR_VERSION,
 
  951                                          LIBMESH_MICRO_VERSION));
 
  959                                            const std::string & directory_name,
 
  960                                            const bool read_binary_basis_functions)
 
  962   LOG_SCOPE(
"read_in_basis_functions()", 
"RBEvaluation");
 
  968                   read_binary_basis_functions);
 
  973                                    const std::string & directory_name,
 
  974                                    const std::string & data_name,
 
  975                                    const bool read_binary_vectors)
 
  977   std::vector<std::vector<std::unique_ptr<NumericVector<Number>>> *> vectors_vec;
 
  978   vectors_vec.push_back(&vectors);
 
  980   std::vector<std::string> directory_name_vec;
 
  981   directory_name_vec.push_back(directory_name);
 
  983   std::vector<std::string> data_name_vec;
 
  984   data_name_vec.push_back(data_name);
 
  990                                       read_binary_vectors);
 
  995                                                        const std::vector<std::string> & multiple_directory_names,
 
  996                                                        const std::vector<std::string> & multiple_data_names,
 
  997                                                        const bool read_binary_vectors)
 
  999   LOG_SCOPE(
"read_in_vectors_from_multiple_files()", 
"RBEvaluation");
 
 1001   std::size_t n_files = multiple_vectors.size();
 
 1002   std::size_t n_directories = multiple_directory_names.size();
 
 1003   libmesh_assert((n_files == n_directories) && (n_files == multiple_data_names.size()));
 
 1009   this->
comm().barrier();
 
 1011   std::ostringstream file_name;
 
 1012   const std::string basis_function_suffix = (read_binary_vectors ? 
".xdr" : 
".dat");
 
 1019   for (std::size_t data_index=0; data_index<n_directories; data_index++)
 
 1021       std::vector<std::unique_ptr<NumericVector<Number>>> & vectors = *multiple_vectors[data_index];
 
 1024       for (
auto & vec : vectors)
 
 1029             libmesh_error_msg(
"Non-nullptr vector passed to read_in_vectors_from_multiple_files");
 
 1040       file_name << multiple_directory_names[data_index]
 
 1041                 << 
"/" << multiple_data_names[data_index]
 
 1042                 << 
"_data" << basis_function_suffix;
 
 1047           struct stat stat_info;
 
 1048           int stat_result = stat(file_name.str().c_str(), &stat_info);
 
 1050           if (stat_result != 0)
 
 1051             libmesh_error_msg(
"File does not exist: " << file_name.str());
 
 1055       Xdr vector_data(file_name.str(),
 
 1060         std::string version;
 
 1061         vector_data.
data(version);
 
 1063         const std::string libMesh_label = 
"libMesh-";
 
 1064         std::string::size_type lm_pos = version.find(libMesh_label);
 
 1065         if (lm_pos==std::string::npos)
 
 1066           libmesh_error_msg(
"version info missing in Xdr header");
 
 1068         std::istringstream iss(version.substr(lm_pos + libMesh_label.size()));
 
 1069         int ver_major = 0, ver_minor = 0, ver_patch = 0;
 
 1071         iss >> ver_major >> dot >> ver_minor >> dot >> ver_patch;
 
 1072         vector_data.set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));
 
 1077         sys.
read_header(vector_data, version, 
false, 
false);
 
 1081       std::vector<NumericVector<Number> *> vec_in;
 
 1082       for (
auto & vec : vectors)
 
 1083         vec_in.push_back(vec.get());
 
  
Manages consistently variables, degrees of freedom, and coefficient vectors.
 
virtual unsigned int get_n_basis_functions() const
Get the current number of basis functions.
 
std::vector< DenseMatrix< Number > > RB_Aq_vector
Dense matrices for the RB computations.
 
unsigned int get_n_output_terms(unsigned int output_index) const
Get the number of affine terms associated with the specified output.
 
unsigned int get_n_F_terms() const
Get Q_f, the number of terms in the affine expansion for the right-hand side.
 
virtual void set_n_basis_functions(unsigned int n_bfs)
Set the number of basis functions.
 
virtual ~RBEvaluation()
Destructor.
 
virtual void write_out_basis_functions(System &sys, const std::string &directory_name="offline_data", const bool write_binary_basis_functions=true)
Write out all the basis functions to file.
 
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.
 
bool is_rb_theta_expansion_initialized() const
 
virtual Real rb_solve(unsigned int N)
Perform online solve with the N RB basis functions, for the set of parameters in current_params,...
 
The libMesh namespace provides an interface to certain functionality in the library.
 
virtual void zero() override
Set every element in the vector to 0.
 
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
 
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrix< T3 > &mat)
Adds factor times mat to this matrix.
 
std::vector< std::vector< Number > > output_dual_innerprods
The vector storing the dual norm inner product terms for each output.
 
MetaPhysicL::DualNumber< T, D > sqrt(const MetaPhysicL::DualNumber< T, D > &in)
 
const Parallel::Communicator & comm() const
 
void write_header(Xdr &io, const std::string &version, const bool write_additional_data) const
Writes the basic data header for this System.
 
virtual void read_in_basis_functions(System &sys, const std::string &directory_name="offline_data", const bool read_binary_basis_functions=true)
Read in all the basis functions from file.
 
This class is part of the rbOOmit framework.
 
virtual void clear() override
Clear this RBEvaluation object.
 
virtual Number eval_output_theta(unsigned int output_index, unsigned int q_l, const RBParameters &mu)
Evaluate theta_q_l at the current parameter.
 
void set_rb_theta_expansion(RBThetaExpansion &rb_theta_expansion_in)
Set the RBThetaExpansion object.
 
void assert_file_exists(const std::string &file_name)
Helper function that checks if file_name exists.
 
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
 
const RBParameters & get_parameters() const
Get the current parameters.
 
void resize(const unsigned int new_m, const unsigned int new_n)
Resize the matrix.
 
RBEvaluation(const Parallel::Communicator &comm)
Constructor.
 
std::vector< Number > RB_outputs
The vectors storing the RB output values and corresponding error bounds.
 
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseVector< T3 > &vec)
Adds factor times vec to this vector.
 
virtual Real residual_scaling_denom(Real alpha_LB)
Specifies the residual scaling on the denominator to be used in the a posteriori error bound.
 
bool evaluate_RB_error_bound
Boolean to indicate whether we evaluate a posteriori error bounds when rb_solve is called.
 
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
 
void read_header(Xdr &io, const std::string &version, const bool read_header=true, const bool read_additional_data=true, const bool read_legacy_format=false)
Reads the basic data header for this System.
 
void data(T &a, const char *comment="")
Inputs or outputs a single value.
 
virtual void legacy_read_offline_data_from_files(const std::string &directory_name="offline_data", bool read_error_bound_data=true, const bool read_binary_data=true)
Read in the saved Offline reduced basis data to initialize the system for Online solves.
 
XdrMODE
Defines an enum for read/write mode in Xdr format.
 
dof_id_type n_local_dofs() const
 
DenseVector< Number > RB_solution
The RB solution vector.
 
std::vector< Number > Fq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online.
 
void read_in_vectors(System &sys, std::vector< std::unique_ptr< NumericVector< Number >>> &vectors, const std::string &directory_name, const std::string &data_name, const bool read_binary_vectors)
Same as read_in_basis_functions, except in this case we pass in the vectors to be written.
 
const MeshBase & get_mesh() const
 
processor_id_type processor_id() const
 
std::vector< std::vector< std::vector< Number > > > Fq_Aq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online.
 
std::string get_io_compatibility_version()
Specifier for I/O file compatibility features.
 
std::size_t read_serialized_vectors(Xdr &io, const std::vector< NumericVector< Number > * > &vectors) const
Read a number of identically distributed vectors.
 
virtual void clear_riesz_representors()
Clear all the Riesz representors that are used to compute the RB residual (and hence error bound).
 
RBThetaExpansion * rb_theta_expansion
A pointer to to the object that stores the theta expansion.
 
CompareTypes< T, T2 >::supertype dot(const DenseVector< T2 > &vec) const
 
void read_in_vectors_from_multiple_files(System &sys, std::vector< std::vector< std::unique_ptr< NumericVector< Number >>> * > multiple_vectors, const std::vector< std::string > &multiple_directory_names, const std::vector< std::string > &multiple_data_names, const bool read_binary_vectors)
Performs read_in_vectors for a list of directory names and data names.
 
virtual Number eval_A_theta(unsigned int q, const RBParameters &mu)
Evaluate theta_q_a at the current parameter.
 
virtual Real get_stability_lower_bound()
Get a lower bound for the stability constant (e.g.
 
virtual Real compute_residual_dual_norm(const unsigned int N)
Compute the dual norm of the residual for the solution saved in RB_solution_vector.
 
virtual void write_out_vectors(System &sys, std::vector< NumericVector< Number > * > &vectors, const std::string &directory_name="offline_data", const std::string &data_name="bf", const bool write_binary_basis_functions=true)
Same as write_out_basis_functions, except in this case we pass in the vectors to be written.
 
virtual void zero() override
Set every element in the matrix to 0.
 
int mkdir(const char *pathname)
Create a directory.
 
std::vector< std::vector< std::vector< Number > > > Aq_Aq_representor_innerprods
 
unsigned int get_n_outputs() const
Get n_outputs, the number output functionals.
 
virtual void legacy_write_offline_data_to_files(const std::string &directory_name="offline_data", const bool write_binary_data=true)
Write out all the data to text files in order to segregate the Offline stage from the Online stage.
 
std::vector< std::vector< DenseVector< Number > > > RB_output_vectors
The vectors storing the RB output vectors.
 
void resize(const unsigned int n)
Resize the vector.
 
Real eval_output_dual_norm(unsigned int n, const RBParameters &mu)
Evaluate the dual norm of output n for the current parameters.
 
const Elem & get(const ElemType type_in)
 
void close()
Closes the file if it is open.
 
virtual void fix_broken_node_and_element_numbering()=0
There is no reason for a user to ever call this function.
 
bool compute_RB_inner_product
Boolean flag to indicate whether we compute the RB_inner_product_matrix.
 
DenseMatrix< Number > RB_inner_product_matrix
The inner product matrix.
 
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.
 
virtual Real get_error_bound_normalization()
 
std::vector< std::vector< std::unique_ptr< NumericVector< Number > > > > Aq_representor
Vector storing the residual representors associated with the left-hand side.
 
std::size_t write_serialized_vectors(Xdr &io, const std::vector< const NumericVector< Number > * > &vectors) const
Serialize & write a number of identically distributed vectors.
 
unsigned int get_n_A_terms() const
Get Q_a, the number of terms in the affine expansion for the bilinear form.
 
virtual Number eval_F_theta(unsigned int q, const RBParameters &mu)
Evaluate theta_q_f at the current parameter.
 
std::vector< Real > RB_output_error_bounds
 
dof_id_type n_dofs() const
 
virtual void resize_data_structures(const unsigned int Nmax, bool resize_error_bound_data=true)
Resize and clear the data vectors corresponding to the value of Nmax.
 
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
Solve the system Ax=b given the input vector b.
 
RBThetaExpansion & get_rb_theta_expansion()
Get a reference to the rb_theta_expansion.
 
std::vector< std::unique_ptr< NumericVector< Number > > > basis_functions
The libMesh vectors storing the finite element coefficients of the RB basis functions.
 
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
 
An object whose state is distributed along a set of processors.
 
std::vector< DenseVector< Number > > RB_Fq_vector
Dense vector for the RHS.
 
This class stores the set of RBTheta functor objects that define the "parameter-dependent expansion" ...
 
NumericVector< Number > & get_basis_function(unsigned int i)
Get a reference to the i^th basis function.
 
std::vector< RBParameters > greedy_param_list
The list of parameters selected by the Greedy algorithm in generating the Reduced Basis associated wi...