libMesh
vector_fe_ex6.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 
19 // <h1>Vector Finite Elements Example 6 - Raviart-Thomas elements (div-grad)</h1>
20 // \author Nuno Nobre
21 // \date 2023
22 //
23 // This example uses Raviart-Thomas elements to solve a model div-grad problem
24 // in H(div) in both 2d and 3d. The problem is simply a mixed div-grad
25 // formulation, \vec{u} = -\nabla p, and -\nabla \cdot \vec{u} = -f, of the
26 // Poisson problem in Introduction Example 3, \nabla^2 p = -f. In particular,
27 // unlike in Introduction Example 3, where we solve solely for the scalar field
28 // p, here we solve for both the vector field \vec{u} and the scalar field p.
29 
30 // Basic utilities.
31 #include "libmesh/string_to_enum.h"
32 
33 // The solver packages supported by libMesh.
34 #include "libmesh/enum_solver_package.h"
35 
36 // The mesh object and mesh generation and modification utilities.
37 #include "libmesh/mesh.h"
38 #include "libmesh/mesh_generation.h"
39 #include "libmesh/mesh_modification.h"
40 
41 // Matrix and vector types.
42 #include "libmesh/dense_matrix.h"
43 #include "libmesh/sparse_matrix.h"
44 #include "libmesh/dense_vector.h"
45 #include "libmesh/numeric_vector.h"
46 
47 // The finite element object and the geometric element type.
48 #include "libmesh/fe.h"
49 #include "libmesh/elem.h"
50 
51 // Gauss quadrature rules.
52 #include "libmesh/quadrature_gauss.h"
53 
54 // The dof map, which handles degree of freedom indexing.
55 #include "libmesh/dof_map.h"
56 
57 // The system of equations.
58 #include "libmesh/equation_systems.h"
59 #include "libmesh/linear_implicit_system.h"
60 
61 // The exact solution and error computation.
62 #include "libmesh/exact_solution.h"
63 #include "libmesh/enum_norm_type.h"
64 #include "solution_function.h"
65 
66 // I/O utilities.
67 #include "libmesh/getpot.h"
68 #include "libmesh/exodusII_io.h"
69 
70 
71 // Bring in everything from the libMesh namespace.
72 using namespace libMesh;
73 
74 // Function prototype. This is the function that will assemble
75 // the linear system for our div-grad problem. Note that the
76 // function will take the EquationSystems object and the
77 // name of the system we are assembling as input. From the
78 // EquationSystems object we have access to the Mesh and
79 // other objects we might need.
81  const std::string & system_name);
82 
83 int main (int argc, char ** argv)
84 {
85  // Initialize libMesh.
86  LibMeshInit init (argc, argv);
87 
88  // This example requires a linear solver package.
89  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
90  "--enable-petsc, --enable-trilinos, or --enable-eigen");
91 
92  // Parse the input file.
93  GetPot infile("vector_fe_ex6.in");
94 
95  // But allow the command line to override it.
96  infile.parse_command_line(argc, argv);
97 
98  // Read in parameters from the command line and the input file.
99  const unsigned int dimension = infile("dim", 2);
100  const unsigned int grid_size = infile("grid_size", 15);
101 
102  // Skip higher-dimensional examples on a lower-dimensional libMesh build.
103  libmesh_example_requires(dimension <= LIBMESH_DIM, dimension << "D support");
104 
105  // Create a mesh, with dimension to be overridden later, distributed
106  // across the default MPI communicator.
107  Mesh mesh(init.comm());
108 
109  // Use the MeshTools::Generation mesh generator to create a uniform
110  // grid on the cube [-1,1]^D. To accomodate Raviart-Thomas elements, we must
111  // use TRI6/7 or QUAD8/9 elements in 2d, or TET14 or HEX27 in 3d.
112  const std::string elem_str = infile("element_type", std::string("TRI6"));
113 
114  libmesh_error_msg_if((dimension == 2 && elem_str != "TRI6" && elem_str != "TRI7" && elem_str != "QUAD8" && elem_str != "QUAD9") ||
115  (dimension == 3 && elem_str != "TET14" && elem_str != "HEX27"),
116  "You selected " << elem_str <<
117  " but this example must be run with TRI6, TRI7, QUAD8, or QUAD9 in 2d" <<
118  " or with TET14, or HEX27 in 3d.");
119 
120  const std::string bc_str = infile("boundary_condition", std::string("neumann"));
121  libmesh_error_msg_if(
122  (bc_str != "neumann") && (bc_str != "dirichlet"),
123  "You selected '" << bc_str << "', however, the valid options are 'dirichlet' or 'neumann'");
124  const bool neumann = (bc_str == "neumann");
125 
126  if (dimension == 2)
128  grid_size,
129  grid_size,
130  -1., 1.,
131  -1., 1.,
132  Utility::string_to_enum<ElemType>(elem_str));
133  else if (dimension == 3)
135  grid_size,
136  grid_size,
137  grid_size,
138  -1., 1.,
139  -1., 1.,
140  -1., 1.,
141  Utility::string_to_enum<ElemType>(elem_str));
142 
143  // Make sure the code is robust against nodal reorderings.
145 
146  // Print information about the mesh to the screen.
147  mesh.print_info();
148 
149  // Create an equation systems object.
150  EquationSystems equation_systems (mesh);
151  equation_systems.parameters.set<bool>("neumann") = neumann;
152 
153  // Declare the system "DivGrad" and its variables.
154  LinearImplicitSystem & system = equation_systems.add_system<LinearImplicitSystem>("DivGrad");
155 
156  // Set the FE approximation order for the vector and scalar field variables.
157  const Order vector_order = static_cast<Order>(infile("order", 1u));
158  const Order scalar_order = static_cast<Order>(vector_order - 1u);
159 
160  libmesh_error_msg_if(vector_order < FIRST || vector_order > ((dimension == 3) ? FIRST : FIFTH),
161  "You selected: " << vector_order <<
162  " but this example must be run with either 1 <= order <= 5 in 2d"
163  " or with order 1 in 3d.");
164 
165  // Adds the variables "u" and "p" to "DivGrad". "u" will be our vector field
166  // whereas "p" will be the scalar field.
167  system.add_variable("u", vector_order, RAVIART_THOMAS);
168  system.add_variable("p", scalar_order, scalar_order == CONSTANT ? MONOMIAL : L2_HIERARCHIC);
169 
170  // Add a scalar Lagrange multiplier to remove the nullspace if imposing the Neumann condition.
171  if (neumann)
172  system.add_variable("l", FIRST, SCALAR);
173 
174  // Give the system a pointer to the matrix assembly
175  // function. This will be called when needed by the library.
176  system.attach_assemble_function(assemble_divgrad);
177 
178  // Initialize the data structures for the equation system.
179  equation_systems.init();
180 
181  // Prints information about the system to the screen.
182  equation_systems.print_info();
183 
184  // Solve the system "DivGrad". Note that calling this
185  // member will assemble the linear system and invoke
186  // the default numerical solver.
187  system.solve();
188 
189  ExactSolution exact_sol(equation_systems);
190 
191  if (dimension == 2)
192  {
193  SolutionFunction<2> soln_func;
194  SolutionGradient<2> soln_grad;
195 
196  // Build FunctionBase* containers to attach to the ExactSolution object.
197  std::vector<FunctionBase<Number> *> sols(1, &soln_func);
198  std::vector<FunctionBase<Gradient> *> grads(1, &soln_grad);
199 
200  exact_sol.attach_exact_values(sols);
201  exact_sol.attach_exact_derivs(grads);
202  }
203  else if (dimension == 3)
204  {
205  SolutionFunction<3> soln_func;
206  SolutionGradient<3> soln_grad;
207 
208  // Build FunctionBase* containers to attach to the ExactSolution object.
209  std::vector<FunctionBase<Number> *> sols(1, &soln_func);
210  std::vector<FunctionBase<Gradient> *> grads(1, &soln_grad);
211 
212  exact_sol.attach_exact_values(sols);
213  exact_sol.attach_exact_derivs(grads);
214  }
215 
216  // Use higher quadrature order for more accurate error results.
217  int extra_error_quadrature = infile("extra_error_quadrature", 2);
218  exact_sol.extra_quadrature_order(extra_error_quadrature);
219 
220  // Compute the error.
221  exact_sol.compute_error("DivGrad", "u");
222  exact_sol.compute_error("DivGrad", "p");
223 
224  // Print out the error values.
225  libMesh::out << "~~ Vector field (u) ~~"
226  << std::endl;
227  libMesh::out << "L2 error is: "
228  << exact_sol.l2_error("DivGrad", "u")
229  << std::endl;
230  libMesh::out << "HDiv semi-norm error is: "
231  << exact_sol.error_norm("DivGrad", "u", HDIV_SEMINORM)
232  << std::endl;
233  libMesh::out << "HDiv error is: "
234  << exact_sol.hdiv_error("DivGrad", "u")
235  << std::endl;
236  libMesh::out << "~~ Scalar field (p) ~~"
237  << std::endl;
238  libMesh::out << "L2 error is: "
239  << exact_sol.l2_error("DivGrad", "p")
240  << std::endl;
241 
242 #ifdef LIBMESH_HAVE_EXODUS_API
243 
244  // We write the file in the ExodusII format.
245  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
246 
247 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
248 
249  // All done.
250  return 0;
251 }
252 
253 
254 
255 // We now define the matrix assembly function for the
256 // div-grad system. We need to first compute element
257 // matrices and right-hand sides, and then take into
258 // account the boundary conditions, which will be handled
259 // via a penalty method.
261  const std::string & libmesh_dbg_var(system_name))
262 {
263 
264  // It is a good idea to make sure we are assembling
265  // the proper system.
266  libmesh_assert_equal_to (system_name, "DivGrad");
267 
268  // Retrieve our boundary condition type. If not Neumann, then it is Dirichlet.
269  const bool neumann = es.parameters.get<bool>("neumann");
270 
271  // Get a constant reference to the mesh object.
272  const MeshBase & mesh = es.get_mesh();
273 
274  // The dimension that we are running.
275  const unsigned int dim = mesh.mesh_dimension();
276 
277  // Get a reference to the LinearImplicitSystem we are solving.
278  LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>("DivGrad");
279 
280  // A reference to the DofMap object for this system. The DofMap
281  // object handles the index translation from node and element numbers
282  // to degree of freedom numbers.
283  const DofMap & dof_map = system.get_dof_map();
284 
285  // Get a constant reference to the Finite Element type
286  // for the two variables in the system.
287  FEType vector_fe_type = dof_map.variable_type(system.variable_number("u"));
288  FEType scalar_fe_type = dof_map.variable_type(system.variable_number("p"));
289 
290  // Build two Finite Element objects, one of each specified type. Since the
291  // FEBase::build() member dynamically creates memory we will
292  // store the object as a std::unique_ptr<FEBase>. This can be thought
293  // of as a pointer that will clean up after itself. Introduction Example 4
294  // describes some advantages of std::unique_ptr's in the context of
295  // quadrature rules.
296  std::unique_ptr<FEVectorBase> vector_fe (FEVectorBase::build(dim, vector_fe_type));
297  std::unique_ptr<FEBase> scalar_fe (FEBase::build(dim, scalar_fe_type));
298 
299  // A just-high-enough Gauss quadrature rule for numerical integration.
300  QGauss qrule (dim, vector_fe_type.default_quadrature_order());
301 
302  // Tell the finite element objects to use our quadrature rule.
303  vector_fe->attach_quadrature_rule (&qrule);
304  scalar_fe->attach_quadrature_rule (&qrule);
305 
306  // Declare a special finite element object for boundary integration.
307  std::unique_ptr<FEVectorBase> vector_fe_face (FEVectorBase::build(dim, vector_fe_type));
308 
309  // Boundary integration requires one quadrature rule with dimensionality one
310  // less than the dimensionality of the element.
311  QGauss qface(dim-1, vector_fe_type.default_quadrature_order());
312 
313  // Tell the finite element object to use our quadrature rule.
314  vector_fe_face->attach_quadrature_rule (&qface);
315 
316  // Here we define some references to cell-specific data that
317  // will be used to assemble the linear system.
318  //
319  // The element Jacobian * quadrature weight at each integration point.
320  const std::vector<Real> & JxW = vector_fe->get_JxW();
321 
322  // The physical XY locations of the quadrature points on the element.
323  // These might be useful for evaluating spatially varying material
324  // properties at the quadrature points.
325  const std::vector<Point> & q_point = vector_fe->get_xyz();
326 
327  // The element shape functions evaluated at the quadrature points.
328  const std::vector<std::vector<RealGradient>> & vector_phi = vector_fe->get_phi();
329  const std::vector<std::vector<Real>> & scalar_phi = scalar_fe->get_phi();
330 
331  // The divergence of the element vector shape functions evaluated at the
332  // quadrature points.
333  const std::vector<std::vector<Real>> & div_vector_phi = vector_fe->get_div_phi();
334 
335  // Define data structures to contain the element matrix
336  // and right-hand-side vector contribution. Following
337  // basic finite element terminology we will denote these
338  // "Ke" and "Fe". These datatypes are templated on
339  // Number, which allows the same code to work for real
340  // or complex numbers.
343 
344  // These vectors will hold the degree of freedom indices for
345  // the element. These define where in the global system
346  // the element degrees of freedom get mapped.
347  std::vector<dof_id_type> dof_indices;
348  std::vector<dof_id_type> vector_dof_indices;
349  std::vector<dof_id_type> scalar_dof_indices;
350  std::vector<dof_id_type> lambda_dof_indices;
351 
352  // The global system matrix
353  SparseMatrix<Number> & matrix = system.get_system_matrix();
354 
355  // Now we will loop over all the elements in the mesh.
356  // We will compute the element matrix and right-hand-side
357  // contribution.
358  //
359  // Element ranges are a nice way to iterate through all the
360  // elements, or all the elements that have some property. The
361  // range will iterate from the first to the last element on
362  // the local processor.
363  // It is smart to make this one const so that we don't accidentally
364  // mess it up! In case users later modify this program to include
365  // refinement, we will be safe and will only consider the active
366  // elements; hence we use a variant of the
367  // active_local_element_ptr_range.
368  for (const auto & elem : mesh.active_local_element_ptr_range())
369  {
370  // Get the degree of freedom indices for the
371  // current element. These define where in the global
372  // matrix and right-hand-side this element will
373  // contribute to.
374  dof_map.dof_indices (elem, dof_indices);
375  dof_map.dof_indices (elem, vector_dof_indices, system.variable_number("u"));
376  dof_map.dof_indices (elem, scalar_dof_indices, system.variable_number("p"));
377  if (neumann)
378  dof_map.dof_indices (elem, lambda_dof_indices, system.variable_number("l"));
379 
380  // Cache the number of degrees of freedom, in total and for each
381  // variable, on this element, for use as array and loop bounds later.
382  // We use cast_int to explicitly convert from size() (which may be
383  // 64-bit) to unsigned int (which may be 32-bit but which is definitely
384  // enough to count *local* degrees of freedom.
385  const unsigned int n_dofs =
386  cast_int<unsigned int>(dof_indices.size());
387  const unsigned int vector_n_dofs =
388  cast_int<unsigned int>(vector_dof_indices.size());
389  const unsigned int scalar_n_dofs =
390  cast_int<unsigned int>(scalar_dof_indices.size());
391  const unsigned int lambda_n_dofs =
392  cast_int<unsigned int>(lambda_dof_indices.size());
393 
394  // Compute the element-specific data for the current
395  // element. This involves computing the location of the
396  // quadrature points (q_point) and the shape functions
397  // and their divergences for the current element.
398  vector_fe->reinit (elem);
399  scalar_fe->reinit (elem);
400 
401  // The total number of degrees of freedom is just the sum of the number
402  // of degrees of freedom per variable. We should also have the same
403  // number of degrees of freedom as shape functions for each variable.
404  libmesh_assert_equal_to (n_dofs, vector_n_dofs + scalar_n_dofs + lambda_n_dofs);
405  libmesh_assert_equal_to (vector_n_dofs, vector_phi.size());
406  libmesh_assert_equal_to (scalar_n_dofs, scalar_phi.size());
407 
408  // Zero the element matrix and right-hand side before
409  // summing them. We use the resize member here because
410  // the number of degrees of freedom might have changed from
411  // the last element. Note that this will be the case if the
412  // element type is different (i.e. the last element was a
413  // triangle, now we are on a quadrilateral).
414 
415  // The DenseMatrix::resize() and the DenseVector::resize()
416  // members will automatically zero out the matrix and vector.
417  Ke.resize (n_dofs, n_dofs);
418  Fe.resize (n_dofs);
419 
420  // Now loop over the quadrature points. This handles
421  // the numeric integration.
422  for (unsigned int qp=0; qp<qrule.n_points(); qp++)
423  {
424 
425  // Now we will build the element matrix.
426  // The upper-left block involves a double loop to integrate the
427  // vector test functions (i) against the vector trial functions (j).
428  for (unsigned int i = 0; i != vector_n_dofs; i++)
429  for (unsigned int j = 0; j != vector_n_dofs; j++)
430  {
431  Ke(i, j) += JxW[qp]*(vector_phi[i][qp]*vector_phi[j][qp]);
432  }
433 
434  // The upper-right block involves a double loop to integrate the
435  // divergence of the vector test functions (i) against the scalar
436  // trial functions (l).
437  for (unsigned int i = 0; i != vector_n_dofs; i++)
438  for (unsigned int l = 0; l != scalar_n_dofs; l++)
439  {
440  Ke(i, l + vector_n_dofs) -= JxW[qp]*(div_vector_phi[i][qp]*scalar_phi[l][qp]);
441  }
442 
443  // The lower-left block involves a double loop to integrate the
444  // scalar test functions (k) against the divergence of the vector
445  // trial functions (j).
446  for (unsigned int k = 0; k != scalar_n_dofs; k++)
447  for (unsigned int j = 0; j != vector_n_dofs; j++)
448  {
449  Ke(k + vector_n_dofs, j) -= JxW[qp]*(div_vector_phi[j][qp]*scalar_phi[k][qp]);
450  }
451 
452  // This is the end of the matrix summation loop
453  // Now we build the element right-hand-side contribution.
454  // This involves a single loop in which we integrate the "forcing
455  // function" in the PDE against the scalar test functions (k).
456  {
457  // The location of the current quadrature point.
458  const Real x = q_point[qp](0);
459  const Real y = q_point[qp](1);
460  const Real z = q_point[qp](2);
461 
462  // "f" is the forcing function for the Poisson equation, which is
463  // just the divergence of the exact solution for the vector field.
464  // This is the well-known "method of manufactured solutions".
465  Real f = 0;
466  if (dim == 2)
467  f = DivGradExactSolution().forcing(x, y);
468  else if (dim == 3)
469  f = DivGradExactSolution().forcing(x, y, z);
470 
471  // Loop to integrate the scalar test functions (k) against the
472  // forcing function.
473  for (unsigned int k = 0; k != scalar_n_dofs; k++)
474  {
475  Fe(k + vector_n_dofs) -= JxW[qp]*f*scalar_phi[k][qp];
476  }
477  }
478 
479  // We have now reached the end of the RHS summation. In addition,
480  // however, since the scalar variable is defined only up
481  // to an additive constant with purely Neumann boundary conditions, we
482  // constrain the integral of the scalar variable to the integral
483  // of the exact solution we seek.
484  {
485  // The location of the current quadrature point.
486  const Real x = q_point[qp](0);
487  const Real y = q_point[qp](1);
488  const Real z = q_point[qp](2);
489 
490  // The value of the scalar variable.
491  Real scalar_value = 0;
492  if (dim == 2)
493  scalar_value = DivGradExactSolution().scalar(x, y);
494  else if (dim == 3)
495  scalar_value = DivGradExactSolution().scalar(x, y, z);
496 
497  // A double loop to integrate the
498  // scalar test functions (k) against the Lagrange dof (n).
499  for (unsigned int k = 0; k != scalar_n_dofs; k++)
500  for (unsigned int n = 0; n != lambda_n_dofs; n++)
501  {
502  Ke(k + vector_n_dofs, n + vector_n_dofs + scalar_n_dofs) += JxW[qp]*scalar_phi[k][qp];
503  }
504 
505  // A double loop to integrate the Lagrange dof (m) against the
506  // scalar trial functions (l).
507  for (unsigned int m = 0; m != lambda_n_dofs; m++)
508  for (unsigned int l = 0; l != scalar_n_dofs; l++)
509  {
510  Ke(m + vector_n_dofs + scalar_n_dofs, l + vector_n_dofs) += JxW[qp]*scalar_phi[l][qp];
511  }
512 
513  // Loop to integrate the exact solution for the scalar variable.
514  for (unsigned int m = 0; m != lambda_n_dofs; m++)
515  {
516  Fe(m + vector_n_dofs + scalar_n_dofs) += JxW[qp]*scalar_value;
517  }
518  }
519  }
520 
521  // We have now reached the end of the quadrature point loop, so
522  // the interior element integration has
523  // been completed. However, we have not yet addressed
524  // boundary conditions. For this Poisson example, we consider either
525  // Dirichlet or Neumann for the scalar solution field p. Note that in
526  // the mixed formulation a Neumann condition for p corresponds to a
527  // Dirichlet condition for u.
528  {
529 
530  // The following loop is over the sides of the element.
531  // If the element has no neighbor on a side then that
532  // side MUST live on a boundary of the domain.
533  for (auto side : elem->side_index_range())
534  if (elem->neighbor_ptr(side) == nullptr)
535  {
536  // The value of the shape functions at the quadrature points.
537  const std::vector<std::vector<RealGradient>> & vector_phi_face = vector_fe_face->get_phi();
538 
539  // The Jacobian * Quadrature Weight at the quadrature
540  // points on the face.
541  const std::vector<Real> & JxW_face = vector_fe_face->get_JxW();
542 
543  // The XYZ locations (in physical space) of, and the normals at,
544  // the quadrature points on the face. This is where
545  // we will interpolate the boundary value function.
546  const std::vector<Point> & qface_point = vector_fe_face->get_xyz();
547  const std::vector<Point> & normals = vector_fe_face->get_normals();
548 
549  // Compute the vector shape function values on the element face.
550  vector_fe_face->reinit(elem, side);
551 
552  // Some shape functions will be 0 on the face, but for ease of
553  // indexing and generality of code we loop over them anyway.
554  libmesh_assert_equal_to (vector_n_dofs, vector_phi_face.size());
555 
556  // Loop over the face quadrature points for integration.
557  for (unsigned int qp=0; qp<qface.n_points(); qp++)
558  {
559  // The location on the boundary of the current
560  // face quadrature point.
561  const Real xf = qface_point[qp](0);
562  const Real yf = qface_point[qp](1);
563  const Real zf = qface_point[qp](2);
564 
565  if (neumann)
566  {
567  // The boundary value for the vector variable.
568  RealGradient vector_value;
569  if (dim == 2)
570  vector_value = DivGradExactSolution()(xf, yf);
571  else if (dim == 3)
572  vector_value = DivGradExactSolution()(xf, yf, zf);
573 
574  // We use the penalty method to set the flux of the vector
575  // variable at the boundary, i.e. the RT vector boundary dof.
576  const Real penalty = 1.e10;
577 
578  // A double loop to integrate the normal component of the
579  // vector test functions (i) against the normal component of
580  // the vector trial functions (j).
581  for (unsigned int i = 0; i != vector_n_dofs; i++)
582  for (unsigned int j = 0; j != vector_n_dofs; j++)
583  {
584  Ke(i, j) += JxW_face[qp]*penalty*vector_phi_face[i][qp]*
585  normals[qp]*vector_phi_face[j][qp]*normals[qp];
586  }
587 
588  // Loop to integrate the normal component of the vector test
589  // functions (i) against the normal component of the
590  // exact solution for the vector variable.
591  for (unsigned int i = 0; i != vector_n_dofs; i++)
592  {
593  Fe(i) += JxW_face[qp]*penalty*vector_phi_face[i][qp]*normals[qp]*
594  vector_value*normals[qp];
595  }
596  }
597  else
598  {
599  // The boundary value for scalar field.
600  Real scalar_value = 0;
601  if (dim == 2)
602  scalar_value = DivGradExactSolution().scalar(xf, yf);
603  else if (dim == 3)
604  scalar_value = DivGradExactSolution().scalar(xf, yf, zf);
605 
606  // Loop to integrate the normal component of the vector test
607  // functions (i) against the exact solution for the scalar variable.
608  for (unsigned int i = 0; i != vector_n_dofs; i++)
609  {
610  Fe(i) += -JxW_face[qp]*vector_phi_face[i][qp]*normals[qp]*scalar_value;
611  }
612  }
613  }
614  }
615  }
616 
617  // We have now finished the quadrature point loop,
618  // and have therefore applied all the boundary conditions.
619 
620  // If this assembly program were to be used on an adaptive mesh,
621  // we would have to apply any hanging node constraint equations.
622  dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
623 
624  // The element matrix and right-hand-side are now built
625  // for this element. Add them to the global matrix and
626  // right-hand-side vector. The SparseMatrix::add_matrix()
627  // and NumericVector::add_vector() members do this for us.
628  matrix.add_matrix (Ke, dof_indices);
629  system.rhs->add_vector (Fe, dof_indices);
630  }
631 
632  // All done!
633 }
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...
void assemble_divgrad(EquationSystems &es, const std::string &system_name)
This is the EquationSystems class.
Order
defines an enum for polynomial orders.
Definition: enum_order.h:40
Real hdiv_error(std::string_view sys_name, std::string_view unknown_name)
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 attach_exact_values(const std::vector< FunctionBase< Number > *> &f)
Clone and attach arbitrary functors which compute the exact values of the EquationSystems&#39; solutions ...
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
void resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:396
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
void extra_quadrature_order(const int extraorder)
Increases or decreases the order of the quadrature rule used for numerical integration.
NumericVector< Number > * rhs
The system matrix.
Order default_quadrature_order() const
Definition: fe_type.h:371
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.
void attach_exact_derivs(const std::vector< FunctionBase< Gradient > *> &g)
Clone and attach arbitrary functors which compute the exact gradients of the EquationSystems&#39; solutio...
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:75
unsigned int variable_number(std::string_view var) const
Definition: system.C:1609
void permute_elements(MeshBase &mesh)
Randomly permute the nodal ordering of each element (without twisting the element mapping)...
SolverPackage default_solver_package()
Definition: libmesh.C:1117
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
const T & get(std::string_view) const
Definition: parameters.h:426
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
void compute_error(std::string_view sys_name, std::string_view unknown_name)
Computes and stores the error in the solution value e = u-u_h, the gradient grad(e) = grad(u) - grad(...
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.
int main(int argc, char **argv)
Definition: vector_fe_ex6.C:83
Real l2_error(std::string_view sys_name, std::string_view unknown_name)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
T & set(const std::string &)
Definition: parameters.h:469
Real forcing(Real x, Real y)
OStreamProxy out
const MeshBase & get_mesh() const
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
Definition: dense_matrix.h:895
This class implements specific orders of Gauss quadrature.
unsigned int mesh_dimension() const
Definition: mesh_base.C:372
Parameters parameters
Data structure holding arbitrary parameters.
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 Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
const DofMap & get_dof_map() const
Definition: system.h:2374
const SparseMatrix< Number > & get_system_matrix() const
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.
Real scalar(Real x, Real y)
Real error_norm(std::string_view sys_name, std::string_view unknown_name, const FEMNormType &norm)