21 #include "libmesh/rb_evaluation.h" 22 #include "libmesh/rb_theta_expansion.h" 25 #include "libmesh/libmesh_common.h" 26 #include "libmesh/libmesh_version.h" 27 #include "libmesh/system.h" 28 #include "libmesh/numeric_vector.h" 29 #include "libmesh/libmesh_logging.h" 30 #include "libmesh/xdr_cxx.h" 31 #include "libmesh/mesh_tools.h" 32 #include "libmesh/utility.h" 38 #include <sys/types.h> 51 evaluate_RB_error_bound(true),
52 compute_RB_inner_product(false),
53 rb_theta_expansion(nullptr)
61 LOG_SCOPE(
"clear()",
"RBEvaluation");
88 "Error: rb_theta_expansion hasn't been initialized yet");
96 "Error: rb_theta_expansion hasn't been initialized yet");
114 bool resize_error_bound_data)
116 LOG_SCOPE(
"resize_data_structures()",
"RBEvaluation");
119 "Error: Cannot set Nmax to be less than the current number of basis functions.");
158 if (resize_error_bound_data)
177 for (
unsigned int i=0; i<Q_a_hat; i++)
180 for (
unsigned int j=0; j<Nmax; j++)
227 const std::vector<Number> * evaluated_thetas)
229 LOG_SCOPE(
"rb_solve()",
"RBEvaluation");
232 "ERROR: N cannot be larger than the number of basis functions in rb_solve");
246 RB_system_matrix.
zero();
253 if (evaluated_thetas)
254 RB_system_matrix.
add((*evaluated_thetas)[q_a], RB_Aq_a);
268 if (evaluated_thetas)
284 unsigned int output_counter = 0;
302 auto coeff = evaluated_thetas ?
322 libmesh_assert_greater ( alpha_LB, 0. );
331 return abs_error_bound;
348 return normalization;
357 const std::vector<Number> * evaluated_thetas)
359 LOG_SCOPE(
"compute_residual_dual_norm()",
"RBEvaluation");
372 Number residual_norm_sq = 0.;
377 auto eval_F = [&](
unsigned int index)
381 auto eval_A = [&](
unsigned int index)
387 for (
unsigned int q_f1=0; q_f1<n_F_terms; q_f1++)
389 const Number val_q_f1 = eval_F(q_f1);
391 for (
unsigned int q_f2=q_f1; q_f2<n_F_terms; q_f2++)
393 const Number val_q_f2 = eval_F(q_f2);
395 Real delta = (q_f1==q_f2) ? 1. : 2.;
402 for (
unsigned int q_f=0; q_f<n_F_terms; q_f++)
404 const Number val_q_f = eval_F(q_f);
406 for (
unsigned int q_a=0; q_a<n_A_terms; q_a++)
408 const Number val_q_a = eval_A(q_a);
410 for (
unsigned int i=0; i<N; i++)
421 for (
unsigned int q_a1=0; q_a1<n_A_terms; q_a1++)
423 const Number val_q_a1 = eval_A(q_a1);
425 for (
unsigned int q_a2=q_a1; q_a2<n_A_terms; q_a2++)
427 const Number val_q_a2 = eval_A(q_a2);
429 Real delta = (q_a1==q_a2) ? 1. : 2.;
431 for (
unsigned int i=0; i<N; i++)
433 for (
unsigned int j=0; j<N; j++)
453 residual_norm_sq = std::abs(residual_norm_sq);
475 const std::vector<Number> * evaluated_thetas)
478 Number output_bound_sq = 0.;
489 Real delta = (q_l1==q_l2) ? 1. : 2.;
517 const bool write_binary_data)
519 LOG_SCOPE(
"legacy_write_offline_data_to_files()",
"RBEvaluation");
528 const std::string suffix = write_binary_data ?
".xdr" :
".dat";
542 std::ostringstream file_name;
544 file_name << directory_name <<
"/n_bfs" << suffix;
545 Xdr n_bfs_out(file_name.str(), mode);
552 file_name << directory_name <<
"/parameter_ranges" << suffix;
553 std::string continuous_param_file_name = file_name.str();
557 file_name << directory_name <<
"/discrete_parameter_values" << suffix;
558 std::string discrete_param_file_name = file_name.str();
561 discrete_param_file_name,
566 file_name << directory_name <<
"/Fq_innerprods" << suffix;
567 Xdr RB_Fq_innerprods_out(file_name.str(), mode);
569 for (
unsigned int i=0; i<Q_f_hat; i++)
573 RB_Fq_innerprods_out.close();
579 file_name << directory_name <<
"/output_";
580 file_name << std::setw(3)
581 << std::setprecision(0)
586 file_name <<
"_dual_innerprods" << suffix;
587 Xdr output_dual_innerprods_out(file_name.str(), mode);
590 for (
unsigned int q=0; q<Q_l_hat; q++)
594 output_dual_innerprods_out.close();
604 file_name << directory_name <<
"/output_";
605 file_name << std::setw(3)
606 << std::setprecision(0)
611 file_name << std::setw(3)
612 << std::setprecision(0)
617 Xdr output_n_out(file_name.str(), mode);
619 for (
unsigned int j=0; j<n_bfs; j++)
623 output_n_out.close();
631 file_name << directory_name <<
"/RB_inner_product_matrix" << suffix;
632 Xdr RB_inner_product_matrix_out(file_name.str(), mode);
633 for (
unsigned int i=0; i<n_bfs; i++)
635 for (
unsigned int j=0; j<n_bfs; j++)
640 RB_inner_product_matrix_out.close();
647 file_name << directory_name <<
"/RB_F_";
648 file_name << std::setw(3)
649 << std::setprecision(0)
654 Xdr RB_Fq_f_out(file_name.str(), mode);
656 for (
unsigned int i=0; i<n_bfs; i++)
667 file_name << directory_name <<
"/RB_A_";
668 file_name << std::setw(3)
669 << std::setprecision(0)
674 Xdr RB_Aq_a_out(file_name.str(), mode);
676 for (
unsigned int i=0; i<n_bfs; i++)
678 for (
unsigned int j=0; j<n_bfs; j++)
688 file_name << directory_name <<
"/Fq_Aq_innerprods" << suffix;
689 Xdr RB_Fq_Aq_innerprods_out(file_name.str(), mode);
695 for (
unsigned int i=0; i<n_bfs; i++)
701 RB_Fq_Aq_innerprods_out.close();
705 file_name << directory_name <<
"/Aq_Aq_innerprods" << suffix;
706 Xdr RB_Aq_Aq_innerprods_out(file_name.str(), mode);
709 for (
unsigned int i=0; i<Q_a_hat; i++)
711 for (
unsigned int j=0; j<n_bfs; j++)
713 for (
unsigned int l=0; l<n_bfs; l++)
719 RB_Aq_Aq_innerprods_out.close();
724 file_name << directory_name <<
"/greedy_params" << suffix;
725 Xdr greedy_params_out(file_name.str(), mode);
728 for (
const auto & pr : param)
729 for (
const auto & value_vector : pr.second)
733 libmesh_error_msg_if(value_vector.size() != 1,
734 "Error: multi-value RB parameters are not yet supported here.");
735 Real param_value = value_vector[0];
736 greedy_params_out << param_value;
738 greedy_params_out.close();
745 bool read_error_bound_data,
746 const bool read_binary_data)
748 LOG_SCOPE(
"legacy_read_offline_data_from_files()",
"RBEvaluation");
754 const std::string suffix = read_binary_data ?
".xdr" :
".dat";
757 std::ostringstream file_name;
762 file_name << directory_name <<
"/n_bfs" << suffix;
765 Xdr n_bfs_in(file_name.str(), mode);
773 file_name << directory_name <<
"/parameter_ranges" << suffix;
774 std::string continuous_param_file_name = file_name.str();
778 file_name << directory_name <<
"/discrete_parameter_values" << suffix;
779 std::string discrete_param_file_name = file_name.str();
781 discrete_param_file_name,
790 file_name << directory_name <<
"/output_";
791 file_name << std::setw(3)
792 << std::setprecision(0)
797 file_name << std::setw(3)
798 << std::setprecision(0)
805 Xdr output_n_in(file_name.str(), mode);
807 for (
unsigned int j=0; j<n_bfs; j++)
810 output_n_in >>
value;
821 file_name << directory_name <<
"/RB_inner_product_matrix" << suffix;
824 Xdr RB_inner_product_matrix_in(file_name.str(), mode);
826 for (
unsigned int i=0; i<n_bfs; i++)
828 for (
unsigned int j=0; j<n_bfs; j++)
831 RB_inner_product_matrix_in >>
value;
835 RB_inner_product_matrix_in.close();
842 file_name << directory_name <<
"/RB_F_";
843 file_name << std::setw(3)
844 << std::setprecision(0)
851 Xdr RB_Fq_f_in(file_name.str(), mode);
853 for (
unsigned int i=0; i<n_bfs; i++)
866 file_name << directory_name <<
"/RB_A_";
867 file_name << std::setw(3)
868 << std::setprecision(0)
875 Xdr RB_Aq_a_in(file_name.str(), mode);
877 for (
unsigned int i=0; i<n_bfs; i++)
879 for (
unsigned int j=0; j<n_bfs; j++)
890 if (read_error_bound_data)
894 file_name << directory_name <<
"/Fq_innerprods" << suffix;
897 Xdr RB_Fq_innerprods_in(file_name.str(), mode);
900 for (
unsigned int i=0; i<Q_f_hat; i++)
904 RB_Fq_innerprods_in.close();
910 file_name << directory_name <<
"/output_";
911 file_name << std::setw(3)
912 << std::setprecision(0)
916 file_name <<
"_dual_innerprods" << suffix;
919 Xdr output_dual_innerprods_in(file_name.str(), mode);
922 for (
unsigned int q=0; q<Q_l_hat; q++)
926 output_dual_innerprods_in.close();
932 file_name << directory_name <<
"/Fq_Aq_innerprods" << suffix;
935 Xdr RB_Fq_Aq_innerprods_in(file_name.str(), mode);
941 for (
unsigned int i=0; i<n_bfs; i++)
947 RB_Fq_Aq_innerprods_in.close();
951 file_name << directory_name <<
"/Aq_Aq_innerprods" << suffix;
954 Xdr RB_Aq_Aq_innerprods_in(file_name.str(), mode);
957 for (
unsigned int i=0; i<Q_a_hat; i++)
959 for (
unsigned int j=0; j<n_bfs; j++)
961 for (
unsigned int l=0; l<n_bfs; l++)
967 RB_Aq_Aq_innerprods_in.close();
979 libmesh_error_msg_if(!std::ifstream(file_name.c_str()),
"File missing: " << file_name);
983 const std::string & directory_name,
984 const bool write_binary_basis_functions)
986 LOG_SCOPE(
"write_out_basis_functions()",
"RBEvaluation");
988 std::vector<NumericVector<Number>*> basis_functions_ptrs;
995 basis_functions_ptrs,
998 write_binary_basis_functions);
1003 const std::string & directory_name,
1004 const std::string & data_name,
1005 const bool write_binary_vectors)
1007 LOG_SCOPE(
"write_out_vectors()",
"RBEvaluation");
1018 std::ostringstream file_name;
1019 const std::string basis_function_suffix = (write_binary_vectors ?
".xdr" :
".dat");
1021 file_name << directory_name <<
"/" << data_name <<
"_data" << basis_function_suffix;
1022 Xdr bf_data(file_name.str(),
1030 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 1031 version +=
" with infinite elements";
1033 bf_data.data(version ,
"# File Format Identifier");
1045 std::vector<const NumericVector<Number> *> bf_out;
1046 for (
const auto & vec : vectors)
1047 bf_out.push_back(vec);
1056 bf_data.set_version(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION,
1057 LIBMESH_MINOR_VERSION,
1058 LIBMESH_MICRO_VERSION));
1066 const std::string & directory_name,
1067 const bool read_binary_basis_functions)
1069 LOG_SCOPE(
"read_in_basis_functions()",
"RBEvaluation");
1075 read_binary_basis_functions);
1080 const std::string & directory_name,
1081 const std::string & data_name,
1082 const bool read_binary_vectors)
1084 std::vector<std::vector<std::unique_ptr<NumericVector<Number>>> *> vectors_vec;
1085 vectors_vec.push_back(&vectors);
1087 std::vector<std::string> directory_name_vec;
1088 directory_name_vec.push_back(directory_name);
1090 std::vector<std::string> data_name_vec;
1091 data_name_vec.push_back(data_name);
1097 read_binary_vectors);
1102 const std::vector<std::string> & multiple_directory_names,
1103 const std::vector<std::string> & multiple_data_names,
1104 const bool read_binary_vectors)
1106 LOG_SCOPE(
"read_in_vectors_from_multiple_files()",
"RBEvaluation");
1108 std::size_t n_files = multiple_vectors.size();
1109 std::size_t n_directories = multiple_directory_names.size();
1110 libmesh_assert((n_files == n_directories) && (n_files == multiple_data_names.size()));
1118 std::ostringstream file_name;
1119 const std::string basis_function_suffix = (read_binary_vectors ?
".xdr" :
".dat");
1126 for (std::size_t data_index=0; data_index<n_directories; data_index++)
1128 std::vector<std::unique_ptr<NumericVector<Number>>> & vectors = *multiple_vectors[data_index];
1131 for (
auto & vec : vectors)
1135 libmesh_error_msg_if(vec,
"Non-nullptr vector passed to read_in_vectors_from_multiple_files");
1146 file_name << multiple_directory_names[data_index]
1147 <<
"/" << multiple_data_names[data_index]
1148 <<
"_data" << basis_function_suffix;
1153 struct stat stat_info;
1154 int stat_result = stat(file_name.str().c_str(), &stat_info);
1156 libmesh_error_msg_if(stat_result != 0,
"File does not exist: " << file_name.str());
1160 Xdr vector_data(file_name.str(),
1165 std::string version;
1166 vector_data.
data(version);
1168 const std::string libMesh_label =
"libMesh-";
1169 std::string::size_type lm_pos = version.find(libMesh_label);
1170 libmesh_error_msg_if(lm_pos == std::string::npos,
"version info missing in Xdr header");
1172 std::istringstream iss(version.substr(lm_pos + libMesh_label.size()));
1173 int ver_major = 0, ver_minor = 0, ver_patch = 0;
1175 iss >> ver_major >> dot >> ver_minor >> dot >> ver_patch;
1176 vector_data.set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));
1181 sys.
read_header(vector_data, version,
false,
false);
1185 std::vector<NumericVector<Number> *> vec_in;
1186 for (
auto & vec : vectors)
1187 vec_in.push_back(vec.get());
1200 auto actual_size = evaluated_thetas->size();
1201 auto expected_size =
1206 libmesh_error_msg_if(actual_size != expected_size,
1207 "ERROR: Evaluated thetas vector has size " <<
1208 actual_size <<
", but we expected " <<
1212 "for a total of " << expected_size <<
" terms.");
Real eval_output_dual_norm(unsigned int n, const std::vector< Number > *evaluated_thetas)
Evaluate the dual norm of output n for the current parameters, or using the pre-evaluted theta values...
std::vector< std::vector< std::unique_ptr< NumericVector< Number > > > > Aq_representor
Vector storing the residual representors associated with the left-hand side.
bool compute_RB_inner_product
Boolean flag to indicate whether we compute the RB_inner_product_matrix.
virtual Number eval_output_theta(unsigned int output_index, unsigned int q_l, const RBParameters &mu) const
Evaluate theta_q_l at the current parameter.
virtual Number eval_A_theta(unsigned int q, const RBParameters &mu) const
Evaluate theta_q_a at the current parameter.
bool evaluate_RB_error_bound
Boolean to indicate whether we evaluate a posteriori error bounds when rb_solve is called...
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.
std::vector< Number > Fq_representor_innerprods
Vectors storing the residual representor inner products to be used in computing the residuals online...
virtual void zero() override final
Set every element in the vector to 0.
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 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.
DenseVector< Number > RB_solution
The RB solution vector.
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 write_header(Xdr &io, std::string_view version, const bool write_additional_data) const
Writes the basic data header for this System.
virtual void zero() override final
Sets all elements of the matrix to 0 and resets any decomposition flag which may have been previously...
DenseMatrix< Number > RB_inner_product_matrix
The inner product matrix.
void resize(const unsigned int n)
Resize the vector.
void close()
Closes the file if it is open.
std::vector< std::unique_ptr< NumericVector< Number > > > basis_functions
The libMesh vectors storing the finite element coefficients of the RB basis functions.
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.
processor_id_type rank() const
unsigned int get_n_A_terms() const
Get Q_a, the number of terms in the affine expansion for the bilinear form.
const Parallel::Communicator & comm() const
virtual void fix_broken_node_and_element_numbering()=0
There is no reason for a user to ever call this function.
int mkdir(const char *pathname)
Create a directory.
std::vector< std::vector< Number > > output_dual_innerprods
The vector storing the dual norm inner product terms for each output.
virtual Real get_error_bound_normalization()
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.
static 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.
The libMesh namespace provides an interface to certain functionality in the library.
dof_id_type n_local_dofs() const
std::vector< RBParameters > greedy_param_list
The list of parameters selected by the Greedy algorithm in generating the Reduced Basis associated wi...
static 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...
unsigned int output_index_1D(unsigned int n, unsigned int q_l) const
Computes the one-dimensional index for output n, term q_l implied by a "row-major" ordering of the ou...
unsigned int get_n_outputs() const
Get n_outputs, the number output functionals.
const MeshBase & get_mesh() const
This class stores the set of RBTheta functor objects that define the "parameter-dependent expansion" ...
dof_id_type n_dofs() const
RBEvaluation(const Parallel::Communicator &comm)
Constructor.
XdrMODE
Defines an enum for read/write mode in Xdr format.
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
std::vector< DenseVector< Number > > RB_Fq_vector
Dense vector for the RHS.
static 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...
std::string get_io_compatibility_version()
Specifier for I/O file compatibility features.
virtual Real get_stability_lower_bound()
Get a lower bound for the stability constant (e.g.
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...
void read_header(Xdr &io, std::string_view 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.
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.
Manages consistently variables, degrees of freedom, and coefficient vectors.
NumericVector< Number > & get_basis_function(unsigned int i)
Get a reference to the i^th basis function.
virtual Number eval_F_theta(unsigned int q, const RBParameters &mu) const
Evaluate theta_q_f at the current parameter.
std::vector< Real > RB_output_error_bounds
const RBParameters & get_parameters() const
Get the current parameters.
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.
virtual void set_n_basis_functions(unsigned int n_bfs)
Set the number of basis functions.
This class is part of the rbOOmit framework.
std::size_t read_serialized_vectors(Xdr &io, const std::vector< NumericVector< Number > *> &vectors) const
Read a number of identically distributed vectors.
An object whose state is distributed along a set of processors.
virtual Real residual_scaling_denom(Real alpha_LB)
Specifies the residual scaling on the denominator to be used in the a posteriori error bound...
void set_rb_theta_expansion(RBThetaExpansion &rb_theta_expansion_in)
Set the RBThetaExpansion object.
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
std::enable_if< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseVector< T3 > &vec)
Adds factor times vec to this vector.
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::size_t write_serialized_vectors(Xdr &io, const std::vector< const NumericVector< Number > *> &vectors) const
Serialize & write a number of identically distributed vectors.
virtual Real rb_solve(unsigned int N)
Perform online solve with the N RB basis functions, for the set of parameters in current_params, where 0 <= N <= RB_size.
virtual void clear_riesz_representors()
Clear all the Riesz representors that are used to compute the RB residual (and hence error bound)...
CompareTypes< T, T2 >::supertype dot(const DenseVector< T2 > &vec) const
std::vector< DenseMatrix< Number > > RB_Aq_vector
Dense matrices for the RB computations.
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
Solve the system Ax=b given the input vector b.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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_total_n_output_terms() const
Returns the total number of affine terms associated with all outputs.
static void assert_file_exists(const std::string &file_name)
Helper function that checks if file_name exists.
virtual unsigned int get_n_basis_functions() const
Get the current number of basis functions.
virtual void clear() override
Clear this RBEvaluation object.
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
void check_evaluated_thetas_size(const std::vector< Number > *evaluated_thetas) const
For interfaces like rb_solve() and compute_residual_dual_norm() that optinally take a vector of "pre-...
std::vector< Number > RB_outputs
The vectors storing the RB output values and corresponding error bounds.
processor_id_type processor_id() const
RBThetaExpansion * rb_theta_expansion
A pointer to to the object that stores the theta expansion.
RBThetaExpansion & get_rb_theta_expansion()
Get a reference to the rb_theta_expansion.
std::enable_if< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrix< T3 > &mat)
Adds factor times mat to this matrix.
std::vector< std::vector< std::vector< Number > > > Aq_Aq_representor_innerprods
bool is_rb_theta_expansion_initialized() const
std::vector< std::vector< DenseVector< Number > > > RB_output_vectors
The vectors storing the RB output vectors.