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);
474 #ifdef LIBMESH_ENABLE_DEPRECATED 477 libmesh_deprecated();
482 #endif // LIBMESH_ENABLE_DEPRECATED 485 const std::vector<Number> * evaluated_thetas)
488 Number output_bound_sq = 0.;
499 Real delta = (q_l1==q_l2) ? 1. : 2.;
527 const bool write_binary_data)
529 LOG_SCOPE(
"legacy_write_offline_data_to_files()",
"RBEvaluation");
538 const std::string suffix = write_binary_data ?
".xdr" :
".dat";
552 std::ostringstream file_name;
554 file_name << directory_name <<
"/n_bfs" << suffix;
555 Xdr n_bfs_out(file_name.str(), mode);
562 file_name << directory_name <<
"/parameter_ranges" << suffix;
563 std::string continuous_param_file_name = file_name.str();
567 file_name << directory_name <<
"/discrete_parameter_values" << suffix;
568 std::string discrete_param_file_name = file_name.str();
571 discrete_param_file_name,
576 file_name << directory_name <<
"/Fq_innerprods" << suffix;
577 Xdr RB_Fq_innerprods_out(file_name.str(), mode);
579 for (
unsigned int i=0; i<Q_f_hat; i++)
583 RB_Fq_innerprods_out.close();
589 file_name << directory_name <<
"/output_";
590 file_name << std::setw(3)
591 << std::setprecision(0)
596 file_name <<
"_dual_innerprods" << suffix;
597 Xdr output_dual_innerprods_out(file_name.str(), mode);
600 for (
unsigned int q=0; q<Q_l_hat; q++)
604 output_dual_innerprods_out.close();
614 file_name << directory_name <<
"/output_";
615 file_name << std::setw(3)
616 << std::setprecision(0)
621 file_name << std::setw(3)
622 << std::setprecision(0)
627 Xdr output_n_out(file_name.str(), mode);
629 for (
unsigned int j=0; j<n_bfs; j++)
633 output_n_out.close();
641 file_name << directory_name <<
"/RB_inner_product_matrix" << suffix;
642 Xdr RB_inner_product_matrix_out(file_name.str(), mode);
643 for (
unsigned int i=0; i<n_bfs; i++)
645 for (
unsigned int j=0; j<n_bfs; j++)
650 RB_inner_product_matrix_out.close();
657 file_name << directory_name <<
"/RB_F_";
658 file_name << std::setw(3)
659 << std::setprecision(0)
664 Xdr RB_Fq_f_out(file_name.str(), mode);
666 for (
unsigned int i=0; i<n_bfs; i++)
677 file_name << directory_name <<
"/RB_A_";
678 file_name << std::setw(3)
679 << std::setprecision(0)
684 Xdr RB_Aq_a_out(file_name.str(), mode);
686 for (
unsigned int i=0; i<n_bfs; i++)
688 for (
unsigned int j=0; j<n_bfs; j++)
698 file_name << directory_name <<
"/Fq_Aq_innerprods" << suffix;
699 Xdr RB_Fq_Aq_innerprods_out(file_name.str(), mode);
705 for (
unsigned int i=0; i<n_bfs; i++)
711 RB_Fq_Aq_innerprods_out.close();
715 file_name << directory_name <<
"/Aq_Aq_innerprods" << suffix;
716 Xdr RB_Aq_Aq_innerprods_out(file_name.str(), mode);
719 for (
unsigned int i=0; i<Q_a_hat; i++)
721 for (
unsigned int j=0; j<n_bfs; j++)
723 for (
unsigned int l=0; l<n_bfs; l++)
729 RB_Aq_Aq_innerprods_out.close();
734 file_name << directory_name <<
"/greedy_params" << suffix;
735 Xdr greedy_params_out(file_name.str(), mode);
738 for (
const auto & pr : param)
739 for (
const auto & value_vector : pr.second)
743 libmesh_error_msg_if(value_vector.size() != 1,
744 "Error: multi-value RB parameters are not yet supported here.");
745 Real param_value = value_vector[0];
746 greedy_params_out << param_value;
748 greedy_params_out.close();
755 bool read_error_bound_data,
756 const bool read_binary_data)
758 LOG_SCOPE(
"legacy_read_offline_data_from_files()",
"RBEvaluation");
764 const std::string suffix = read_binary_data ?
".xdr" :
".dat";
767 std::ostringstream file_name;
772 file_name << directory_name <<
"/n_bfs" << suffix;
775 Xdr n_bfs_in(file_name.str(), mode);
783 file_name << directory_name <<
"/parameter_ranges" << suffix;
784 std::string continuous_param_file_name = file_name.str();
788 file_name << directory_name <<
"/discrete_parameter_values" << suffix;
789 std::string discrete_param_file_name = file_name.str();
791 discrete_param_file_name,
800 file_name << directory_name <<
"/output_";
801 file_name << std::setw(3)
802 << std::setprecision(0)
807 file_name << std::setw(3)
808 << std::setprecision(0)
815 Xdr output_n_in(file_name.str(), mode);
817 for (
unsigned int j=0; j<n_bfs; j++)
820 output_n_in >>
value;
831 file_name << directory_name <<
"/RB_inner_product_matrix" << suffix;
834 Xdr RB_inner_product_matrix_in(file_name.str(), mode);
836 for (
unsigned int i=0; i<n_bfs; i++)
838 for (
unsigned int j=0; j<n_bfs; j++)
841 RB_inner_product_matrix_in >>
value;
845 RB_inner_product_matrix_in.close();
852 file_name << directory_name <<
"/RB_F_";
853 file_name << std::setw(3)
854 << std::setprecision(0)
861 Xdr RB_Fq_f_in(file_name.str(), mode);
863 for (
unsigned int i=0; i<n_bfs; i++)
876 file_name << directory_name <<
"/RB_A_";
877 file_name << std::setw(3)
878 << std::setprecision(0)
885 Xdr RB_Aq_a_in(file_name.str(), mode);
887 for (
unsigned int i=0; i<n_bfs; i++)
889 for (
unsigned int j=0; j<n_bfs; j++)
900 if (read_error_bound_data)
904 file_name << directory_name <<
"/Fq_innerprods" << suffix;
907 Xdr RB_Fq_innerprods_in(file_name.str(), mode);
910 for (
unsigned int i=0; i<Q_f_hat; i++)
914 RB_Fq_innerprods_in.close();
920 file_name << directory_name <<
"/output_";
921 file_name << std::setw(3)
922 << std::setprecision(0)
926 file_name <<
"_dual_innerprods" << suffix;
929 Xdr output_dual_innerprods_in(file_name.str(), mode);
932 for (
unsigned int q=0; q<Q_l_hat; q++)
936 output_dual_innerprods_in.close();
942 file_name << directory_name <<
"/Fq_Aq_innerprods" << suffix;
945 Xdr RB_Fq_Aq_innerprods_in(file_name.str(), mode);
951 for (
unsigned int i=0; i<n_bfs; i++)
957 RB_Fq_Aq_innerprods_in.close();
961 file_name << directory_name <<
"/Aq_Aq_innerprods" << suffix;
964 Xdr RB_Aq_Aq_innerprods_in(file_name.str(), mode);
967 for (
unsigned int i=0; i<Q_a_hat; i++)
969 for (
unsigned int j=0; j<n_bfs; j++)
971 for (
unsigned int l=0; l<n_bfs; l++)
977 RB_Aq_Aq_innerprods_in.close();
989 libmesh_error_msg_if(!std::ifstream(file_name.c_str()),
"File missing: " << file_name);
993 const std::string & directory_name,
994 const bool write_binary_basis_functions)
996 LOG_SCOPE(
"write_out_basis_functions()",
"RBEvaluation");
998 std::vector<NumericVector<Number>*> basis_functions_ptrs;
1005 basis_functions_ptrs,
1008 write_binary_basis_functions);
1013 const std::string & directory_name,
1014 const std::string & data_name,
1015 const bool write_binary_vectors)
1017 LOG_SCOPE(
"write_out_vectors()",
"RBEvaluation");
1028 std::ostringstream file_name;
1029 const std::string basis_function_suffix = (write_binary_vectors ?
".xdr" :
".dat");
1031 file_name << directory_name <<
"/" << data_name <<
"_data" << basis_function_suffix;
1032 Xdr bf_data(file_name.str(),
1040 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 1041 version +=
" with infinite elements";
1043 bf_data.data(version ,
"# File Format Identifier");
1055 std::vector<const NumericVector<Number> *> bf_out;
1056 for (
const auto & vec : vectors)
1057 bf_out.push_back(vec);
1066 bf_data.set_version(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION,
1067 LIBMESH_MINOR_VERSION,
1068 LIBMESH_MICRO_VERSION));
1076 const std::string & directory_name,
1077 const bool read_binary_basis_functions)
1079 LOG_SCOPE(
"read_in_basis_functions()",
"RBEvaluation");
1085 read_binary_basis_functions);
1090 const std::string & directory_name,
1091 const std::string & data_name,
1092 const bool read_binary_vectors)
1094 std::vector<std::vector<std::unique_ptr<NumericVector<Number>>> *> vectors_vec;
1095 vectors_vec.push_back(&vectors);
1097 std::vector<std::string> directory_name_vec;
1098 directory_name_vec.push_back(directory_name);
1100 std::vector<std::string> data_name_vec;
1101 data_name_vec.push_back(data_name);
1107 read_binary_vectors);
1112 const std::vector<std::string> & multiple_directory_names,
1113 const std::vector<std::string> & multiple_data_names,
1114 const bool read_binary_vectors)
1116 LOG_SCOPE(
"read_in_vectors_from_multiple_files()",
"RBEvaluation");
1118 std::size_t n_files = multiple_vectors.size();
1119 std::size_t n_directories = multiple_directory_names.size();
1120 libmesh_assert((n_files == n_directories) && (n_files == multiple_data_names.size()));
1128 std::ostringstream file_name;
1129 const std::string basis_function_suffix = (read_binary_vectors ?
".xdr" :
".dat");
1136 for (std::size_t data_index=0; data_index<n_directories; data_index++)
1138 std::vector<std::unique_ptr<NumericVector<Number>>> & vectors = *multiple_vectors[data_index];
1141 for (
auto & vec : vectors)
1145 libmesh_error_msg_if(vec,
"Non-nullptr vector passed to read_in_vectors_from_multiple_files");
1156 file_name << multiple_directory_names[data_index]
1157 <<
"/" << multiple_data_names[data_index]
1158 <<
"_data" << basis_function_suffix;
1163 struct stat stat_info;
1164 int stat_result = stat(file_name.str().c_str(), &stat_info);
1166 libmesh_error_msg_if(stat_result != 0,
"File does not exist: " << file_name.str());
1170 Xdr vector_data(file_name.str(),
1175 std::string version;
1176 vector_data.
data(version);
1178 const std::string libMesh_label =
"libMesh-";
1179 std::string::size_type lm_pos = version.find(libMesh_label);
1180 libmesh_error_msg_if(lm_pos == std::string::npos,
"version info missing in Xdr header");
1182 std::istringstream iss(version.substr(lm_pos + libMesh_label.size()));
1183 int ver_major = 0, ver_minor = 0, ver_patch = 0;
1185 iss >> ver_major >> dot >> ver_minor >> dot >> ver_patch;
1186 vector_data.set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));
1191 sys.
read_header(vector_data, version,
false,
false);
1195 std::vector<NumericVector<Number> *> vec_in;
1196 for (
auto & vec : vectors)
1197 vec_in.push_back(vec.get());
1210 auto actual_size = evaluated_thetas->size();
1211 auto expected_size =
1216 libmesh_error_msg_if(actual_size != expected_size,
1217 "ERROR: Evaluated thetas vector has size " <<
1218 actual_size <<
", but we expected " <<
1222 "for a total of " << expected_size <<
" terms.");
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.
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseVector< T3 > &vec)
Adds factor times vec to this vector.
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.
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrix< T3 > &mat)
Adds factor times mat to this matrix.
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::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.
Real eval_output_dual_norm(unsigned int n, const RBParameters &mu)
Evaluate the dual norm of output n for the current parameters.
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::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.