libMesh
reduced_basis_ex6.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 6 - Heat transfer on a curved domain in 3D</h1>
25 // \author David Knezevic
26 // \date 2012
27 //
28 // In this example we consider heat transfer modeled by a Poisson
29 // equation with Robin boundary condition:
30 //
31 // -kappa \Laplacian u = 1, on \Omega
32 // -kappa du\dn = kappa Bi u, on \partial\Omega_Biot,
33 // u = 0 on \partial\Omega_Dirichlet,
34 //
35 // We consider a reference domain \Omega_hat =
36 // [-0.2,0.2]x[-0.2,0.2]x[0,3], and the physical domain is then obtain
37 // via the parametrized mapping:
38 //
39 // x = -1/mu + (1/mu+x_hat)*cos(mu*z_hat)
40 // y = y_hat
41 // z = (1/mu+x_hat)*sin(mu*z_hat)
42 //
43 // for (x_hat,y_hat,z_hat) \in \Omega_hat. (Here "hats" denotes
44 // reference domain.) Also, the "reference Dirichlet boundaries" are
45 // [-0.2,0.2]x[-0.2,0.2]x{0} and [-0.2,0.2]x[-0.2,0.2]x{3}, and the
46 // remaining boundaries are the "Biot" boundaries.
47 //
48 // Then, after putting the PDE into weak form and mapping it to the
49 // reference domain, we obtain:
50 // \kappa \int_\Omega_hat
51 // [ (1+mu*x_hat) v_x w_x + (1+mu*x_hat) v_y w_y + 1/(1+mu*x_hat) v_z w_z ]
52 // + \kappa Bi \int_\partial\Omega_hat_Biot1 (1-0.2mu) u v
53 // + \kappa Bi \int_\partial\Omega_hat_Biot2 (1+mu x_hat) u v
54 // + \kappa Bi \int_\partial\Omega_hat_Biot3 (1+0.2mu) u v
55 // = \int_\Omega_hat (1+mu x_hat) v
56 // where
57 // \partial\Omega_hat_Biot1 = [-0.2] x [-0.2,0.2] x [0,3]
58 // \partial\Omega_hat_Biot2 = [-0.2,0.2] x {-0.2} x [0,3] \UNION [-0.2,0.2] x {0.2} x [0,3]
59 // \partial\Omega_hat_Biot3 = [0.2] x [-0.2,0.2] x [0,3]
60 //
61 // The term
62 // \kappa \int_\Omega_hat 1/(1+mu*x_hat) v_z w_z
63 // is "non-affine" (in the Reduced Basis sense), since we can't
64 // express it in the form \sum theta_q(kappa,mu) a(v,w). As a result,
65 // (as in reduced_basis_ex4) we must employ the Empirical
66 // Interpolation Method (EIM) in order to apply the Reduced Basis
67 // method here.
68 //
69 // The approach we use is to construct an EIM approximation, G_EIM, to
70 // the vector-valued function G(x_hat,y_hat;mu) = (1 + mu*x_hat, 1 +
71 // mu*x_hat, 1/(1+mu*x_hat)) and then we express the "volumetric
72 // integral part" of the left-hand side operator as a(v,w;mu) =
73 // \int_\hat\Omega G_EIM(x_hat,y_hat;mu) \dot (v_x w_x, v_y w_y, v_z
74 // w_z). (We actually only need EIM for the third component of
75 // G_EIM, but it's helpful to demonstrate "vector-valued" EIM here.)
76 
77 // C++ include files that we need
78 #include <iostream>
79 #include <algorithm>
80 #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
81 #include <cmath>
82 #include <set>
83 
84 // Basic include file needed for the mesh functionality.
85 #include "libmesh/libmesh.h"
86 #include "libmesh/mesh.h"
87 #include "libmesh/mesh_generation.h"
88 #include "libmesh/exodusII_io.h"
89 #include "libmesh/equation_systems.h"
90 #include "libmesh/dof_map.h"
91 #include "libmesh/getpot.h"
92 #include "libmesh/elem.h"
93 #include "libmesh/rb_data_serialization.h"
94 #include "libmesh/rb_data_deserialization.h"
95 #include "libmesh/enum_solver_package.h"
96 
97 // local includes
98 #include "rb_classes.h"
99 #include "eim_classes.h"
100 #include "assembly.h"
101 
102 // Bring in everything from the libMesh namespace
103 using namespace libMesh;
104 
105 // Define a function to scale the mesh according to the parameter.
107  Real curvature,
108  const std::string & filename);
109 
110 // The main program.
111 int main (int argc, char ** argv)
112 {
113  // Initialize libMesh.
114  LibMeshInit init (argc, argv);
115 
116  // This example requires a linear solver package.
117  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
118  "--enable-petsc, --enable-trilinos, or --enable-eigen");
119 
120 #if !defined(LIBMESH_HAVE_XDR)
121  // We need XDR support to write out reduced bases
122  libmesh_example_requires(false, "--enable-xdr");
123 #elif defined(LIBMESH_DEFAULT_SINGLE_PRECISION)
124  // XDR binary support requires double precision
125  libmesh_example_requires(false, "--disable-singleprecision");
126 #elif defined(LIBMESH_DEFAULT_TRIPLE_PRECISION)
127  // I have no idea why long double isn't working here... [RHS]
128  libmesh_example_requires(false, "double precision");
129 #elif defined(LIBMESH_ENABLE_BLOCKED_STORAGE)
130  // This example dies with "New nonzero at (0,2) caused a malloc"
131  // when blocked storage is enabled.
132  libmesh_example_requires(false, "--disable-blocked-storage");
133 #endif
134 
135  // This is a 3D example
136  libmesh_example_requires(3 == LIBMESH_DIM, "3D support");
137 
138 #ifndef LIBMESH_ENABLE_DIRICHLET
139  libmesh_example_requires(false, "--enable-dirichlet");
140 #else
141 
142  // Parse the input file using GetPot
143  std::string eim_parameters = "eim.in";
144  std::string rb_parameters = "rb.in";
145  std::string main_parameters = "reduced_basis_ex6.in";
146  GetPot infile(main_parameters);
147 
148  unsigned int n_elem_xy = infile("n_elem_xy", 1);
149  unsigned int n_elem_z = infile("n_elem_z", 1);
150 
151  // Do we write the RB basis functions to disk?
152  bool store_basis_functions = infile("store_basis_functions", true);
153 
154  // Read the "online_mode" flag from the command line
155  GetPot command_line (argc, argv);
156  int online_mode = 0;
157  if (command_line.search(1, "-online_mode"))
158  online_mode = command_line.next(online_mode);
159 
160  // Create a mesh, with dimension to be overridden by build_cube, on
161  // the default MPI communicator. We currently have to create a
162  // ReplicatedMesh here due to a reduced_basis regression with
163  // DistributedMesh
164  ReplicatedMesh mesh(init.comm());
165 
167  n_elem_xy, n_elem_xy, n_elem_z,
168  -0.2, 0.2,
169  -0.2, 0.2,
170  0., 3.,
171  HEX8);
172 
173  // Create an equation systems object.
174  EquationSystems equation_systems (mesh);
175 
176  SimpleEIMConstruction & eim_construction =
177  equation_systems.add_system<SimpleEIMConstruction> ("EIM");
178  SimpleRBConstruction & rb_construction =
179  equation_systems.add_system<SimpleRBConstruction> ("RB");
180 
181  // Initialize the data structures for the equation system.
182  equation_systems.init ();
183 
184  // Print out some information about the "truth" discretization
185  equation_systems.print_info();
186  mesh.print_info();
187 
188  // Initialize the standard RBEvaluation object
189  SimpleRBEvaluation rb_eval(mesh.comm());
190 
191  // Initialize the EIM RBEvaluation object
192  SimpleEIMEvaluation eim_rb_eval(mesh.comm());
193 
194  // Set the rb_eval objects for the RBConstructions
195  eim_construction.set_rb_evaluation(eim_rb_eval);
196  rb_construction.set_rb_evaluation(rb_eval);
197 
198  if (!online_mode) // Perform the Offline stage of the RB method
199  {
200  // Read data from input file and print state
201  eim_construction.process_parameters_file(eim_parameters);
202  eim_construction.print_info();
203 
204  // Perform the EIM Greedy and write out the data
205  eim_construction.initialize_rb_construction();
206 
207  eim_construction.train_reduced_basis();
208 
209 #if defined(LIBMESH_HAVE_CAPNPROTO)
210  RBDataSerialization::RBEIMEvaluationSerialization rb_eim_eval_writer(eim_rb_eval);
211  rb_eim_eval_writer.write_to_file("rb_eim_eval.bin");
212 #else
213  eim_construction.get_rb_evaluation().legacy_write_offline_data_to_files("eim_data");
214 #endif
215 
216  // Read data from input file and print state
217  rb_construction.process_parameters_file(rb_parameters);
218 
219  // attach the EIM theta objects to the RBEvaluation
220  eim_rb_eval.initialize_eim_theta_objects();
221  rb_eval.get_rb_theta_expansion().attach_multiple_A_theta(eim_rb_eval.get_eim_theta_objects());
222 
223  // attach the EIM assembly objects to the RBConstruction
224  eim_construction.initialize_eim_assembly_objects();
225  rb_construction.get_rb_assembly_expansion().attach_multiple_A_assembly(eim_construction.get_eim_assembly_objects());
226 
227  // Print out the state of rb_construction now that the EIM objects have been attached
228  rb_construction.print_info();
229 
230  // Need to initialize _after_ EIM greedy so that
231  // the system knows how many affine terms there are
232  rb_construction.initialize_rb_construction();
233  rb_construction.train_reduced_basis();
234 
235 #if defined(LIBMESH_HAVE_CAPNPROTO)
236  RBDataSerialization::RBEvaluationSerialization rb_eval_writer(rb_construction.get_rb_evaluation());
237  rb_eval_writer.write_to_file("rb_eval.bin");
238 #else
239  rb_construction.get_rb_evaluation().legacy_write_offline_data_to_files("rb_data");
240 #endif
241 
242  // Write out the basis functions, if requested
243  if (store_basis_functions)
244  {
245  // Write out the basis functions
246  eim_construction.get_rb_evaluation().write_out_basis_functions(eim_construction.get_explicit_system(),
247  "eim_data");
248  rb_construction.get_rb_evaluation().write_out_basis_functions(rb_construction,
249  "rb_data");
250  }
251  }
252  else // Perform the Online stage of the RB method
253  {
254 #if defined(LIBMESH_HAVE_CAPNPROTO)
255  RBDataDeserialization::RBEIMEvaluationDeserialization rb_eim_eval_reader(eim_rb_eval);
256  rb_eim_eval_reader.read_from_file("rb_eim_eval.bin");
257 #else
258  eim_rb_eval.legacy_read_offline_data_from_files("eim_data");
259 #endif
260 
261  // attach the EIM theta objects to rb_eval objects
262  eim_rb_eval.initialize_eim_theta_objects();
263  rb_eval.get_rb_theta_expansion().attach_multiple_A_theta(eim_rb_eval.get_eim_theta_objects());
264 
265  // Read in the offline data for rb_eval
266 #if defined(LIBMESH_HAVE_CAPNPROTO)
268  rb_eval_reader.read_from_file("rb_eval.bin", /*read_error_bound_data*/ true);
269 #else
270  rb_eval.legacy_read_offline_data_from_files("rb_data");
271 #endif
272 
273  // Get the parameters at which we will do a reduced basis solve
274  Real online_curvature = infile("online_curvature", 0.);
275  Real online_Bi = infile("online_Bi", 0.);
276  Real online_kappa = infile("online_kappa", 0.);
277  RBParameters online_mu;
278  online_mu.set_value("curvature", online_curvature);
279  online_mu.set_value("Bi", online_Bi);
280  online_mu.set_value("kappa", online_kappa);
281  rb_eval.set_parameters(online_mu);
282  rb_eval.print_parameters();
283  rb_eval.rb_solve(rb_eval.get_n_basis_functions());
284 
285  // plot the solution, if requested
286  if (store_basis_functions)
287  {
288  // read in the data from files
289  eim_rb_eval.read_in_basis_functions(
290  eim_construction.get_explicit_system(), "eim_data");
291  rb_eval.read_in_basis_functions(rb_construction, "rb_data");
292 
293  eim_construction.load_rb_solution();
294  rb_construction.load_rb_solution();
295 
296  transform_mesh_and_plot(equation_systems, online_curvature, "RB_sol.e");
297  }
298  }
299 
300 #endif // LIBMESH_ENABLE_DIRICHLET
301 
302  return 0;
303 }
304 
305 #ifdef LIBMESH_ENABLE_DIRICHLET
306 
308  Real curvature,
309  const std::string & filename)
310 {
311  // Loop over the mesh nodes and move them!
312  MeshBase & mesh = es.get_mesh();
313 
314  for (auto & node : mesh.node_ptr_range())
315  {
316  Real x = (*node)(0);
317  Real z = (*node)(2);
318 
319  (*node)(0) = -1./curvature + (1./curvature + x)*cos(curvature*z);
320  (*node)(2) = (1./curvature + x)*sin(curvature*z);
321  }
322 
323 #ifdef LIBMESH_HAVE_EXODUS_API
324  ExodusII_IO(mesh).write_equation_systems(filename, es);
325 #endif
326 }
327 
328 #endif // LIBMESH_ENABLE_DIRICHLET
SimpleRBConstruction
Definition: rb_classes.h:73
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh::EquationSystems::get_mesh
const MeshBase & get_mesh() const
Definition: equation_systems.h:637
libMesh::HEX8
Definition: enum_elem_type.h:47
SimpleEIMConstruction
Definition: eim_classes.h:40
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MeshTools::Generation::build_cube
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
Definition: mesh_generation.C:298
SimpleEIMEvaluation
Definition: eim_classes.h:23
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::RBParameters
This class is part of the rbOOmit framework.
Definition: rb_parameters.h:42
libMesh::RBDataSerialization::RBEvaluationSerialization
This class serializes an RBEvaluation object using the Cap'n Proto library.
Definition: rb_data_serialization.h:58
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
main
int main(int argc, char **argv)
Definition: reduced_basis_ex6.C:111
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::ExodusII_IO
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:51
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::RBDataSerialization::RBEIMEvaluationSerialization::write_to_file
void write_to_file(const std::string &path)
Write the Cap'n'Proto buffer to disk.
Definition: rb_data_serialization.C:178
libMesh::RBDataSerialization::RBEvaluationSerialization::write_to_file
void write_to_file(const std::string &path)
Write the Cap'n'Proto buffer to disk.
Definition: rb_data_serialization.C:82
libMesh::MeshBase::node_ptr_range
virtual SimpleRange< node_iterator > node_ptr_range()=0
libMesh::INVALID_SOLVER_PACKAGE
Definition: enum_solver_package.h:43
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
transform_mesh_and_plot
void transform_mesh_and_plot(EquationSystems &es, Real curvature, const std::string &filename)
Definition: reduced_basis_ex6.C:307
libMesh::RBParameters::set_value
void set_value(const std::string &param_name, Real value)
Set the value of the specified parameter.
Definition: rb_parameters.C:47
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::RBDataSerialization::RBEIMEvaluationSerialization
This class serializes an RBEIMEvaluation object using the Cap'n Proto library.
Definition: rb_data_serialization.h:124
libMesh::EquationSystems::print_info
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
Definition: equation_systems.C:1314
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::MeshOutput::write_equation_systems
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
libMesh::RBDataDeserialization::RBEIMEvaluationDeserialization::read_from_file
void read_from_file(const std::string &path)
Write the Cap'n'Proto buffer to disk.
Definition: rb_data_deserialization.C:187
SimpleRBEvaluation
Definition: rb_classes.h:45
libMesh::MeshBase::print_info
void print_info(std::ostream &os=libMesh::out) const
Prints relevant information about the mesh.
Definition: mesh_base.C:585
libMesh::RBDataDeserialization::RBEvaluationDeserialization
This class de-serializes an RBEvaluation object using the Cap'n Proto library.
Definition: rb_data_deserialization.h:60
libMesh::RBDataDeserialization::RBEvaluationDeserialization::read_from_file
void read_from_file(const std::string &path, bool read_error_bound_data)
Write the Cap'n'Proto buffer to disk.
Definition: rb_data_deserialization.C:78
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::RBDataDeserialization::RBEIMEvaluationDeserialization
This class de-serializes a RBEIMEvaluation object using the Cap'n Proto library.
Definition: rb_data_deserialization.h:126