libMesh
Loading...
Searching...
No Matches
reduced_basis_ex3.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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 3 - Transient Reduced Basis Problem</h1>
25// \author David Knezevic
26// \date 2011
27//
28// In this example problem we use the Certified Reduced Basis method
29// to solve a transient convection-diffusion problem on the unit square.
30// The PDE is similar to reduced_basis_ex1, except there is a time-derivative
31// in this case.
32
33// Basic include file needed for the mesh functionality.
34#include "libmesh/libmesh.h"
35#include "libmesh/mesh.h"
36#include "libmesh/mesh_generation.h"
37#include "libmesh/exodusII_io.h"
38#include "libmesh/equation_systems.h"
39#include "libmesh/dof_map.h"
40#include "libmesh/getpot.h"
41#include "libmesh/elem.h"
42#include "libmesh/rb_data_serialization.h"
43#include "libmesh/rb_data_deserialization.h"
44#include "libmesh/enum_solver_package.h"
45
46// local includes
47#include "rb_classes.h"
48#include "assembly.h"
49
50// Bring in everything from the libMesh namespace
51using namespace libMesh;
52
53// The main program.
54int main (int argc, char** argv)
55{
56 // Initialize libMesh.
57 LibMeshInit init (argc, argv);
58
59 // This example requires SLEPc
60#if !defined(LIBMESH_HAVE_SLEPC)
61 libmesh_example_requires(false, "--enable-slepc");
62#else
63
64#if !defined(LIBMESH_HAVE_XDR)
65 // We need XDR support to write out reduced bases
66 libmesh_example_requires(false, "--enable-xdr");
67#elif defined(LIBMESH_DEFAULT_SINGLE_PRECISION)
68 // XDR binary support requires double precision
69 libmesh_example_requires(false, "--disable-singleprecision");
70#endif
71 // FIXME: This example currently segfaults with Trilinos?
72 libmesh_example_requires(libMesh::default_solver_package() == PETSC_SOLVERS, "--enable-petsc");
73
74 // Skip this 2D example if libMesh was compiled as 1D-only.
75 libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
76
77#ifndef LIBMESH_ENABLE_DIRICHLET
78 libmesh_example_requires(false, "--enable-dirichlet");
79#else
80
81 // Parse the input file (reduced_basis_ex3.in) using GetPot
82 std::string parameters_filename = "reduced_basis_ex3.in";
83 GetPot infile(parameters_filename);
84
85 // But allow the command line to override it.
86 infile.parse_command_line(argc, argv);
87
88 unsigned int n_elem = infile("n_elem", 1); // Determines the number of elements in the "truth" mesh
89 const unsigned int dim = 2; // The number of spatial dimensions
90
91 bool store_basis_functions = infile("store_basis_functions", true); // Do we write the RB basis functions to disk?
92
93 // Read the "online_mode" flag from the command line
94 const int online_mode = libMesh::command_line_next("-online_mode", 0);
95
96 // Build a mesh on the default MPI communicator.
97 Mesh mesh (init.comm(), dim);
99 n_elem, n_elem,
100 0., 1.,
101 0., 1.,
102 QUAD4);
103
104 // Create an equation systems object.
105 EquationSystems equation_systems (mesh);
106
107 // We override RBConstruction with SimpleRBConstruction in order to
108 // specialize a few functions for this particular problem.
109 SimpleRBConstruction & rb_con =
110 equation_systems.add_system<SimpleRBConstruction> ("RBConvectionDiffusion");
111
112
113 // Initialize the data structures for the equation system.
114 equation_systems.init ();
115
116 // Print out some information about the "truth" discretization
117 equation_systems.print_info();
119
120 // Build a new RBEvaluation object which will be used to perform
121 // Reduced Basis calculations. This is required in both the
122 // "Offline" and "Online" stages.
123 SimpleRBEvaluation rb_eval(mesh.comm());
124
125 // Finally, we need to give the RBConstruction object a pointer to
126 // our RBEvaluation object
127 rb_con.set_rb_evaluation(rb_eval);
128
129 if (!online_mode) // Perform the Offline stage of the RB method
130 {
131 // Read in the data that defines this problem from the specified text file
132 rb_con.process_parameters_file(parameters_filename);
133
134 // Print out info that describes the current setup of rb_con
135 rb_con.print_info();
136
137 // Prepare rb_con for the Construction stage of the RB method.
138 // This sets up the necessary data structures and performs
139 // initial assembly of the "truth" affine expansion of the PDE.
141
142 // Compute the reduced basis space by computing "snapshots", i.e.
143 // "truth" solves, at well-chosen parameter values and employing
144 // these snapshots as basis functions.
145 rb_con.train_reduced_basis();
146
147 // Write out the data that will subsequently be required for the Evaluation stage
148#if defined(LIBMESH_HAVE_CAPNPROTO)
150 rb_eval_writer.write_to_file("trans_rb_eval.bin");
151#else
153#endif
154
155 // If requested, write out the RB basis functions for visualization purposes
156 if (store_basis_functions)
157 {
158 // Write out the basis functions
160 }
161 }
162 else // Perform the Online stage of the RB method
163 {
164 // Read in the reduced basis data
165#if defined(LIBMESH_HAVE_CAPNPROTO)
167 rb_eval_reader.read_from_file("trans_rb_eval.bin", /*read_error_bound_data*/ true);
168#else
170#endif
171
172 // Read in online_N and initialize online parameters
173 unsigned int online_N = infile("online_N", 1);
174 Real online_x_vel = infile("online_x_vel", 0.);
175 Real online_y_vel = infile("online_y_vel", 0.);
176 RBParameters online_mu;
177 online_mu.set_value("x_vel", online_x_vel);
178 online_mu.set_value("y_vel", online_y_vel);
179 rb_eval.set_parameters(online_mu);
180 rb_eval.print_parameters();
181
182 // Now do the Online solve using the precomputed reduced basis
183 Real error_bound_final_time = rb_eval.rb_solve(online_N);
184
185 libMesh::out << "Error bound (absolute) at the final time is "
186 << error_bound_final_time << std::endl << std::endl;
187
188 if (store_basis_functions)
189 {
190 // Read in the basis functions
191 rb_eval.read_in_basis_functions(rb_con);
192
193 // Plot the solution at the final time level
194 rb_con.pull_temporal_discretization_data(rb_eval);
195 rb_con.set_time_step(rb_con.get_n_time_steps());
196 rb_con.load_rb_solution();
197#ifdef LIBMESH_HAVE_EXODUS_API
198 ExodusII_IO(mesh).write_equation_systems ("RB_sol.e", equation_systems);
199#endif
200
201 // Plot the first basis function that was generated from the train_reduced_basis
202 // call in the Offline stage
203 rb_con.load_basis_function(0);
204#ifdef LIBMESH_HAVE_EXODUS_API
205 ExodusII_IO(mesh).write_equation_systems ("bf0.e", equation_systems);
206#endif
207 }
208 }
209
210#endif // LIBMESH_ENABLE_DIRICHLET
211
212 return 0;
213
214#endif // LIBMESH_HAVE_SLEPC
215}
unsigned int dim
This is the EquationSystems class.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
virtual void init()
Initialize all the systems.
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 ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition exodusII_io.h:53
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
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:1755
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
const Parallel::Communicator & comm() const
void set_rb_evaluation(RBEvaluation &rb_eval_in)
Set the RBEvaluation object.
virtual void print_info() const
Print out info that describes the current setup of this RBConstruction.
RBEvaluation & get_rb_evaluation()
Get a reference to the RBEvaluation object.
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.
virtual Real train_reduced_basis(const bool resize_rb_eval_data=true)
Train the reduced basis.
virtual void load_basis_function(unsigned int i)
Load the i^th RB function into the RBConstruction solution vector.
virtual void load_rb_solution()
Load the RB solution from the most recent solve with rb_eval into this system's solution vector.
virtual void process_parameters_file(const std::string &parameters_filename)
Read in from the file specified by parameters_filename and set the this system's member variables acc...
This class de-serializes a TransientRBEvaluation object using the Cap'n Proto library.
void read_from_file(const std::string &path, bool read_error_bound_data, bool use_packing=false)
Read the Cap'n'Proto buffer from disk.
This class serializes a TransientRBEvaluation object using the Cap'n Proto library.
void write_to_file(const std::string &path, bool use_packing=false)
Write the Cap'n'Proto buffer to disk.
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.
virtual Real rb_solve(unsigned int N)
Perform online solve with the N RB basis functions, for the set of parameters in current_params,...
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 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 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.
This class is part of the rbOOmit framework.
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
bool set_parameters(const RBParameters &params)
Set the current parameters to params The parameters are checked for validity; an error is thrown if t...
void print_parameters() const
Print the current parameters.
MeshBase & mesh
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.
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage default_solver_package()
Definition libmesh.C:1064
OStreamProxy out
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
Definition libmesh.C:1025
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
int main()