libMesh
vector_fe_ex9.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 // <h1>Vector Finite Elements Example 9 - Hybridizable Discontinuous Galerkin Navier Stokes</h1>
19 // \author Alexander Lindsay
20 // \date 2023
21 
22 // Basic utilities.
23 #include "libmesh/string_to_enum.h"
24 
25 // The solver packages supported by libMesh.
26 #include "libmesh/enum_solver_package.h"
27 
28 // The mesh object and mesh generation and modification utilities.
29 #include "libmesh/mesh.h"
30 #include "libmesh/mesh_generation.h"
31 #include "libmesh/mesh_modification.h"
32 
33 // Matrix and vector types.
34 #include "libmesh/dense_matrix.h"
35 #include "libmesh/sparse_matrix.h"
36 #include "libmesh/dense_vector.h"
37 #include "libmesh/numeric_vector.h"
38 
39 // The finite element object and the geometric element type.
40 #include "libmesh/fe.h"
41 #include "libmesh/fe_interface.h"
42 #include "libmesh/elem.h"
43 
44 // Gauss quadrature rules.
45 #include "libmesh/quadrature_gauss.h"
46 #include "libmesh/enum_quadrature_type.h"
47 
48 // The dof map, which handles degree of freedom indexing.
49 #include "libmesh/dof_map.h"
50 
51 // The system of equations.
52 #include "libmesh/equation_systems.h"
53 #include "libmesh/nonlinear_implicit_system.h"
54 #include "libmesh/nonlinear_solver.h"
55 #include "libmesh/static_condensation.h"
56 
57 // The exact solution and error computation.
58 #include "libmesh/exact_solution.h"
59 #include "libmesh/enum_norm_type.h"
60 #include "exact_soln.h"
61 
62 // I/O utilities.
63 #include "libmesh/getpot.h"
64 #include "libmesh/exodusII_io.h"
65 
66 // The HDGProblem application context
67 #include "hdg_problem.h"
68 
69 #if defined(LIBMESH_HAVE_EIGEN_DENSE) && defined(LIBMESH_HAVE_PETSC)
70 
71 using namespace libMesh;
72 
73 int
74 main(int argc, char ** argv)
75 {
76  // Initialize libMesh.
77  LibMeshInit init(argc, argv);
78 
79  // This example requires a linear solver package.
80  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
81  "--enable-petsc, --enable-trilinos, or --enable-eigen");
82 
83  // Parse the input file.
84  GetPot infile("vector_fe_ex9.in");
85 
86  // But allow the command line to override it.
87  infile.parse_command_line(argc, argv);
88 
89  // Read in parameters from the command line and the input file.
90  const unsigned int dimension = 2;
91  const unsigned int grid_size = infile("grid_size", 2);
92  const bool mms = infile("mms", true);
93  const Real nu = infile("nu", 1.);
94  const bool cavity = infile("cavity", false);
95 
96  // Skip higher-dimensional examples on a lower-dimensional libMesh build.
97  libmesh_example_requires(dimension <= LIBMESH_DIM, dimension << "D support");
98 
99  // Create a mesh, with dimension to be overridden later, distributed
100  // across the default MPI communicator.
101  Mesh mesh(init.comm());
102 
103  // Use the MeshTools::Generation mesh generator to create a uniform
104  // grid on the cube [-1,1]^D. To accomodate first order side hierarchics, we must
105  // use TRI6/7 elements
106  const std::string elem_str = infile("element_type", std::string("TRI6"));
107 
108  libmesh_error_msg_if(elem_str != "TRI6" && elem_str != "TRI7",
109  "You selected "
110  << elem_str
111  << " but this example must be run with TRI6, TRI7, QUAD8, or QUAD9 in 2d"
112  << " or with TET14, or HEX27 in 3d.");
113 
114  if (mms && !cavity)
116  mesh, grid_size, grid_size, 0., 2., -1, 1., Utility::string_to_enum<ElemType>(elem_str));
117  else if (cavity)
119  mesh, grid_size, grid_size, -1, 1, -1, 1., Utility::string_to_enum<ElemType>(elem_str));
120  else
122  5 * grid_size,
123  grid_size,
124  0.,
125  10.,
126  -1,
127  1.,
128  Utility::string_to_enum<ElemType>(elem_str));
129  mesh.print_info();
130 
131  // Create an equation systems object.
132  EquationSystems equation_systems(mesh);
133 
134  // Declare the system "Mixed" and its variables.
135  auto & system = equation_systems.add_system<NonlinearImplicitSystem>("HDG");
136 
137  // Adds the velocity variables and their gradients
138  system.add_variable("qu", FIRST, L2_LAGRANGE_VEC);
139  system.add_variable("qv", FIRST, L2_LAGRANGE_VEC);
140  system.add_variable("vel_x", FIRST, L2_LAGRANGE);
141  system.add_variable("vel_y", FIRST, L2_LAGRANGE);
142 
143  // Add our Lagrange multiplier to the implicit system
144  system.add_variable("lm_u", FIRST, SIDE_HIERARCHIC);
145  system.add_variable("lm_v", FIRST, SIDE_HIERARCHIC);
146  const auto p_num = system.add_variable("pressure", FIRST, L2_LAGRANGE);
147  if (cavity)
148  system.add_variable("global_lm", FIRST, SCALAR);
149 
150  const FEType vector_fe_type(FIRST, L2_LAGRANGE_VEC);
151  const FEType scalar_fe_type(FIRST, L2_LAGRANGE);
152  const FEType lm_fe_type(FIRST, SIDE_HIERARCHIC);
153 
154  StaticCondensation * sc = nullptr;
155  if (system.has_static_condensation())
156  {
157  sc = &system.get_static_condensation();
158  sc->dont_condense_vars({p_num});
159  }
160 
161  HDGProblem hdg(nu, cavity);
162  hdg.mesh = &mesh;
163  hdg.system = &system;
164  hdg.dof_map = &system.get_dof_map();
165  hdg.vector_fe = FEVectorBase::build(dimension, vector_fe_type);
166  hdg.scalar_fe = FEBase::build(dimension, scalar_fe_type);
167  hdg.qrule = QBase::build(QGAUSS, dimension, FIFTH);
168  hdg.qface = QBase::build(QGAUSS, dimension - 1, FIFTH);
169  hdg.vector_fe_face = FEVectorBase::build(dimension, vector_fe_type);
170  hdg.scalar_fe_face = FEBase::build(dimension, scalar_fe_type);
171  hdg.lm_fe_face = FEBase::build(dimension, lm_fe_type);
172  hdg.mms = mms;
173  hdg.sc = sc;
174 
175  system.nonlinear_solver->residual_object = &hdg;
176  system.nonlinear_solver->jacobian_object = &hdg;
177 
178  hdg.init();
179 
180  // Initialize the data structures for the equation system.
181  equation_systems.init();
182  equation_systems.print_info();
183 
184  // Solve the implicit system for the Lagrange multiplier
185  system.solve();
186 
187  if (mms)
188  {
189  //
190  // Now we will compute our solution approximation errors
191  //
192 
193  equation_systems.parameters.set<const ExactSoln *>("vel_x_exact_sol") = &hdg.u_true_soln;
194  equation_systems.parameters.set<const ExactSoln *>("vel_y_exact_sol") = &hdg.v_true_soln;
195  equation_systems.parameters.set<const ExactSoln *>("pressure_exact_sol") = &hdg.p_true_soln;
196  ExactSolution exact_sol(equation_systems);
197  exact_sol.attach_exact_value(&compute_error);
198 
199  // Compute the error.
200  exact_sol.compute_error("HDG", "vel_x");
201  exact_sol.compute_error("HDG", "vel_y");
202  exact_sol.compute_error("HDG", "pressure");
203 
204  // Print out the error values.
205  libMesh::out << "L2 error for u is: " << exact_sol.l2_error("HDG", "vel_x") << std::endl;
206  libMesh::out << "L2 error for v is: " << exact_sol.l2_error("HDG", "vel_y") << std::endl;
207  libMesh::out << "L2 error for pressure is: " << exact_sol.l2_error("HDG", "pressure")
208  << std::endl;
209  }
210 
211 #ifdef LIBMESH_HAVE_EXODUS_API
212 
213  // We write the file in the ExodusII format.
214  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
215 
216 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
217 
218  // All done.
219  return 0;
220 }
221 
222 #else
223 
224 int
226 {
227  return 0;
228 }
229 
230 #endif
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
std::unique_ptr< QBase > qface
Definition: hdg_problem.h:56
This is the EquationSystems class.
int main(int argc, char **argv)
Definition: vector_fe_ex9.C:74
std::unique_ptr< FEVectorBase > vector_fe
Definition: hdg_problem.h:50
std::unique_ptr< FEBase > lm_fe_face
Definition: hdg_problem.h:55
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
Number compute_error(const Point &p, const Parameters &params, const std::string &, const std::string &unknown_name)
Definition: exact_soln.h:36
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
const DofMap * dof_map
Definition: hdg_problem.h:46
std::unique_ptr< FEBase > scalar_fe
Definition: hdg_problem.h:51
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 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.
const VSoln v_true_soln
Definition: hdg_problem.h:72
void dont_condense_vars(const std::unordered_set< unsigned int > &vars)
Add vars to the list of variables not to condense.
SolverPackage default_solver_package()
Definition: libmesh.C:1117
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.
Definition: exodusII_io.C:2033
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:1562
std::unique_ptr< QBase > qrule
Definition: hdg_problem.h:52
const PSoln p_true_soln
Definition: hdg_problem.h:73
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
std::unique_ptr< FEBase > scalar_fe_face
Definition: hdg_problem.h:54
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1357
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
StaticCondensation * sc
Definition: hdg_problem.h:47
T & set(const std::string &)
Definition: parameters.h:469
OStreamProxy out
const MeshBase * mesh
Definition: hdg_problem.h:45
Parameters parameters
Data structure holding arbitrary parameters.
static std::unique_ptr< QBase > build(std::string_view name, const unsigned int dim, const Order order=INVALID_ORDER)
Builds a specific quadrature rule based on the name string.
void attach_exact_value(unsigned int sys_num, FunctionBase< Number > *f)
Clone and attach an arbitrary functor which computes the exact value of the system sys_num solution a...
virtual void init()
Initialize all the systems.
const USoln u_true_soln
Definition: hdg_problem.h:71
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
std::unique_ptr< FEVectorBase > vector_fe_face
Definition: hdg_problem.h:53