libMesh
reduced_basis_ex1.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 // rbOOmit: An implementation of the Certified Reduced Basis method.
19 // Copyright (C) 2009, 2010 David J. Knezevic
20 // This file is part of rbOOmit.
21 
22 
23 
24 // <h1>Reduced Basis Example 1 - Certified Reduced Basis Method</h1>
25 // \author David Knezevic
26 // \date 2010
27 //
28 // In this example problem we use the Certified Reduced Basis method
29 // to solve a steady convection-diffusion problem on the unit square.
30 // The reduced basis method relies on an expansion of the PDE in the
31 // form \sum_q=1^Q_a theta_a^q(\mu) * a^q(u,v) = \sum_q=1^Q_f
32 // theta_f^q(\mu) f^q(v) where theta_a, theta_f are parameter
33 // dependent functions and a^q, f^q are parameter independent
34 // operators (\mu denotes a parameter).
35 //
36 // We first attach the parameter dependent functions and parameter
37 // independent operators to the RBSystem. Then in Offline mode, a
38 // reduced basis space is generated and written out to the directory
39 // "offline_data". In Online mode, the reduced basis data in
40 // "offline_data" is read in and used to solve the reduced problem for
41 // the parameters specified in reduced_basis_ex1.in.
42 //
43 // We also attach four outputs to the system which are averages over
44 // certain subregions of the domain. In Online mode, we print out the
45 // values of these outputs as well as rigorous error bounds with
46 // respect to the output associated with the "truth" finite element
47 // discretization.
48 
49 // C++ include files that we need
50 #include <iostream>
51 #include <algorithm>
52 #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
53 #include <cmath>
54 #include <set>
55 
56 // Basic include file needed for the mesh functionality.
57 #include "libmesh/libmesh.h"
58 #include "libmesh/mesh.h"
59 #include "libmesh/mesh_generation.h"
60 #include "libmesh/exodusII_io.h"
61 #include "libmesh/equation_systems.h"
62 #include "libmesh/dof_map.h"
63 #include "libmesh/getpot.h"
64 #include "libmesh/elem.h"
65 #include "libmesh/rb_data_serialization.h"
66 #include "libmesh/rb_data_deserialization.h"
67 #include "libmesh/enum_solver_package.h"
68 
69 // local includes
70 #include "rb_classes.h"
71 #include "assembly.h"
72 
73 // Bring in everything from the libMesh namespace
74 using namespace libMesh;
75 
76 // The main program.
77 int main (int argc, char ** argv)
78 {
79  // Initialize libMesh.
80  LibMeshInit init (argc, argv);
81 
82  // This example requires a linear solver package.
83  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
84  "--enable-petsc, --enable-trilinos, or --enable-eigen");
85 
86 #if !defined(LIBMESH_HAVE_XDR)
87  // We need XDR support to write out reduced bases
88  libmesh_example_requires(false, "--enable-xdr");
89 #elif defined(LIBMESH_DEFAULT_SINGLE_PRECISION)
90  // XDR binary support requires double precision
91  libmesh_example_requires(false, "--disable-singleprecision");
92 #endif
93  // FIXME: This example currently segfaults with Trilinos? It works
94  // with PETSc and Eigen sparse linear solvers though.
95  libmesh_example_requires(libMesh::default_solver_package() != TRILINOS_SOLVERS, "--enable-petsc");
96 
97  // Skip this 2D example if libMesh was compiled as 1D-only.
98  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
99 
100 #ifndef LIBMESH_ENABLE_DIRICHLET
101  libmesh_example_requires(false, "--enable-dirichlet");
102 #else
103 
104  // Parse the input file (reduced_basis_ex1.in) using GetPot
105  std::string parameters_filename = "reduced_basis_ex1.in";
106  GetPot infile(parameters_filename);
107 
108  // But allow the command line to override it
109  infile.parse_command_line(argc, argv);
110 
111  unsigned int n_elem = infile("n_elem", 1); // Determines the number of elements in the "truth" mesh
112  const unsigned int dim = 2; // The number of spatial dimensions
113 
114  bool store_basis_functions = infile("store_basis_functions", true); // Do we write the RB basis functions to disk?
115 
116  // Read the "online_mode" flag from the command line
117  const int online_mode = libMesh::command_line_next("-online_mode", 0);
118 
119  // Build a mesh on the default MPI communicator.
120  Mesh mesh (init.comm(), dim);
122  n_elem, n_elem,
123  0., 1.,
124  0., 1.,
125  QUAD4);
126 
127  // Create an equation systems object.
128  EquationSystems equation_systems (mesh);
129 
130  // We override RBConstruction with SimpleRBConstruction in order to
131  // specialize a few functions for this particular problem.
132  SimpleRBConstruction & rb_con =
133  equation_systems.add_system<SimpleRBConstruction> ("RBConvectionDiffusion");
134 
135  // Initialize the data structures for the equation system.
136  equation_systems.init ();
137 
138  // Print out some information about the "truth" discretization
139  equation_systems.print_info();
140  mesh.print_info();
141 
142  // Build a new RBEvaluation object which will be used to perform
143  // Reduced Basis calculations. This is required in both the
144  // "Offline" and "Online" stages.
145  SimpleRBEvaluation rb_eval(mesh.comm());
146 
147  // We need to give the RBConstruction object a pointer to
148  // our RBEvaluation object
149  rb_con.set_rb_evaluation(rb_eval);
150 
151  if (!online_mode) // Perform the Offline stage of the RB method
152  {
153  // Read in the data that defines this problem from the specified text file
154  rb_con.process_parameters_file(parameters_filename);
155 
156  // Print out info that describes the current setup of rb_con
157  rb_con.print_info();
158 
159  // Prepare rb_con for the Construction stage of the RB method.
160  // This sets up the necessary data structures and performs
161  // initial assembly of the "truth" affine expansion of the PDE.
163 
164  // Compute the reduced basis space by computing "snapshots", i.e.
165  // "truth" solves, at well-chosen parameter values and employing
166  // these snapshots as basis functions.
167  rb_con.train_reduced_basis();
168 
169  // Write out the data that will subsequently be required for the Evaluation stage
170 #if defined(LIBMESH_HAVE_CAPNPROTO)
172  rb_eval_writer.write_to_file("rb_eval.bin");
173 #else
175 #endif
176 
177  // If requested, write out the RB basis functions for visualization purposes
178  if (store_basis_functions)
179  {
180  // Write out the basis functions
182  }
183 
184  // Basis functions should be orthonormal, so
185  // print out the inner products to check this
187  }
188  else // Perform the Online stage of the RB method
189  {
190  // Read in the reduced basis data
191 #if defined(LIBMESH_HAVE_CAPNPROTO)
193  rb_eval_reader.read_from_file("rb_eval.bin", /*read_error_bound_data*/ true);
194 #else
195  rb_eval.legacy_read_offline_data_from_files();
196 #endif
197 
198  // Read in online_N and initialize online parameters
199  unsigned int online_N = infile("online_N", 1);
200  Real online_x_vel = infile("online_x_vel", 0.);
201  Real online_y_vel = infile("online_y_vel", 0.);
202  RBParameters online_mu;
203  online_mu.set_value("x_vel", online_x_vel);
204  online_mu.set_value("y_vel", online_y_vel);
205  rb_eval.set_parameters(online_mu);
206  rb_eval.print_parameters();
207 
208  // Now do the Online solve using the precomputed reduced basis
209  rb_eval.rb_solve(online_N);
210 
211  // Print out outputs as well as the corresponding output error bounds.
212  libMesh::out << "output 1, value = " << rb_eval.RB_outputs[0]
213  << ", bound = " << rb_eval.RB_output_error_bounds[0]
214  << std::endl;
215  libMesh::out << "output 2, value = " << rb_eval.RB_outputs[1]
216  << ", bound = " << rb_eval.RB_output_error_bounds[1]
217  << std::endl;
218  libMesh::out << "output 3, value = " << rb_eval.RB_outputs[2]
219  << ", bound = " << rb_eval.RB_output_error_bounds[2]
220  << std::endl;
221  libMesh::out << "output 4, value = " << rb_eval.RB_outputs[3]
222  << ", bound = " << rb_eval.RB_output_error_bounds[3]
223  << std::endl << std::endl;
224 
225  if (store_basis_functions)
226  {
227  // Read in the basis functions
228  rb_eval.read_in_basis_functions(rb_con);
229 
230  // Plot the solution
231  rb_con.load_rb_solution();
232 #ifdef LIBMESH_HAVE_EXODUS_API
233  ExodusII_IO(mesh).write_equation_systems ("RB_sol.e", equation_systems);
234 #endif
235 
236  // Plot the first basis function that was generated from the train_reduced_basis
237  // call in the Offline stage
238  rb_con.load_basis_function(0);
239 #ifdef LIBMESH_HAVE_EXODUS_API
240  ExodusII_IO(mesh).write_equation_systems ("bf0.e", equation_systems);
241 #endif
242  }
243  }
244 
245 #endif // LIBMESH_ENABLE_DIRICHLET
246 
247  return 0;
248 }
virtual void initialize_rb_construction(bool skip_matrix_assembly=false, bool skip_vector_assembly=false)
Allocate all the data structures necessary for the construction stage of the RB method.
T command_line_next(std::string name, T default_value)
Use GetPot&#39;s search()/next() functions to get following arguments from the command line...
Definition: libmesh.C:1011
This is the EquationSystems class.
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.
virtual Real train_reduced_basis(const bool resize_rb_eval_data=true)
Train the reduced basis.
This class serializes an RBEvaluation object using the Cap&#39;n Proto library.
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: mesh_output.C:31
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Count up the number of elements of a specific type (as defined by an iterator range).
Definition: mesh_tools.C:850
unsigned int dim
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
const Parallel::Communicator & comm() const
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
int main(int argc, char **argv)
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
The libMesh namespace provides an interface to certain functionality in the library.
virtual void print_info() const
Print out info that describes the current setup of this RBConstruction.
void print_basis_function_orthogonality() const
Print out a matrix that shows the orthogonality of the RB basis functions.
SolverPackage default_solver_package()
Definition: libmesh.C:1050
virtual void load_basis_function(unsigned int i)
Load the i^th RB function into the RBConstruction solution vector.
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 print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition: mesh_base.C:1489
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
void read_from_file(const std::string &path, bool read_error_bound_data)
Write the Cap&#39;n&#39;Proto buffer to disk.
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:52
void write_to_file(const std::string &path)
Write the Cap&#39;n&#39;Proto buffer to disk.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
OStreamProxy out
virtual void process_parameters_file(const std::string &parameters_filename)
Read in from the file specified by parameters_filename and set the this system&#39;s member variables acc...
This class de-serializes an RBEvaluation object using the Cap&#39;n Proto library.
RBEvaluation & get_rb_evaluation()
Get a reference to the RBEvaluation object.
virtual void init()
Initialize all the systems.
virtual void load_rb_solution()
Load the RB solution from the most recent solve with rb_eval into this system&#39;s solution vector...
void set_rb_evaluation(RBEvaluation &rb_eval_in)
Set the RBEvaluation object.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50