libMesh
vector_fe_ex4.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 
19 
20 // <h1>Vector Finite Elements Example 4 - Nedelec Elements</h1>
21 // \author Paul Bauman
22 // \date 2013
23 //
24 // This example shows an example of using the Nedelec elements of the
25 // first type to solve a model problem in H(curl).
26 
27 // Basic include files
28 #include "libmesh/equation_systems.h"
29 #include "libmesh/getpot.h"
30 #include "libmesh/exodusII_io.h"
31 #include "libmesh/mesh.h"
32 #include "libmesh/mesh_generation.h"
33 #include "libmesh/exact_solution.h"
34 #include "libmesh/string_to_enum.h"
35 #include "libmesh/enum_solver_package.h"
36 #include "libmesh/enum_norm_type.h"
37 #include "libmesh/petsc_macro.h"
38 
39 // The systems and solvers we may use
40 #include "curl_curl_system.h"
41 #include "libmesh/diff_solver.h"
42 #include "libmesh/steady_solver.h"
43 #include "solution_function.h"
44 
45 // Bring in everything from the libMesh namespace
46 using namespace libMesh;
47 
48 // The main program.
49 int main (int argc, char ** argv)
50 {
51  // Initialize libMesh.
52  LibMeshInit init (argc, argv);
53 
54  // This example requires a linear solver package.
55  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
56  "--enable-petsc, --enable-trilinos, or --enable-eigen");
57 
58  // Parse the input file
59  GetPot infile("vector_fe_ex4.in");
60 
61  // But allow the command line to override it.
62  infile.parse_command_line(argc, argv);
63 
64  // hypre AMS requires PETSc 3.12.2 or above with hypre support enabled
65 #if PETSC_VERSION_LESS_THAN(3, 12, 2) || !defined(LIBMESH_HAVE_PETSC_HYPRE)
66  libmesh_example_requires(!infile.search("ams"),
67  "PETSc 3.12.2 or above with hypre support enabled");
68 #endif
69 
70  // Read in parameters from the input file
71  const unsigned int grid_size = infile("grid_size", 2);
72 
73  // Skip higher-dimensional examples on a lower-dimensional libMesh build
74  libmesh_example_requires(3 <= LIBMESH_DIM, "3D support");
75 
76  // Create a mesh, with dimension to be overridden later, on the
77  // default MPI communicator.
78  Mesh mesh(init.comm());
79 
80  // Use the MeshTools::Generation mesh generator to create a uniform
81  // grid on the square [-1,1]^D. We must use at least TET10 or HEX20 elements
82  // for the Nedelec tetrahedral or hexahedral elements, respectively.
83  std::string elem_str =
84  command_line_value(std::string("element_type"),
85  std::string("HEX27"));
86 
87  // In general we expect O(h) convergence in *both* the L2 and
88  // H(curl) norms when using Nedelec elements. For more information
89  // on this topic, see the relevant results in other software [1-3]
90  // and the descriptions in [4,5]. In this particular example, we
91  // have observed O(h^2) convergence in L2 for HEX20/HEX27s, since the
92  // particular solution we seek is, just like the basis functions, constant
93  // along each of the cartesian axes. The basis functions are linear in the
94  // other two dimensions, improving the convergence speed.
95  //
96  // [1]: deal.ii, https://www.dealii.org/reports/nedelec/nedelec.pdf
97  // [2]: FEMPAR, https://www.sciencedirect.com/science/article/pii/S096599781831113X
98  // [3]: FEniCS, https://fenicsproject.org/pub/book/book/fenics-book-2011-06-24.pdf
99  // [4]: Monk, https://icerm.brown.edu/materials/Slides/tw-18-7/Finite_Element_Methods_for_Maxwells_Equations_%5D_Peter_Monk,_University_of_Delaware.pdf
100  // [5]: Hiptmair at al., https://www.sam.math.ethz.ch/sam_reports/reports_final/reports2009/2009-04_fp.pdf
101  libmesh_error_msg_if(elem_str != "TET10" && elem_str != "TET14" && elem_str != "HEX20" && elem_str != "HEX27",
102  "You entered: " << elem_str <<
103  " but this example must be run with TET10, TET14, HEX20 or HEX27.");
104 
106  grid_size,
107  grid_size,
108  grid_size,
109  -1., 1.,
110  -1., 1.,
111  -1., 1.,
112  Utility::string_to_enum<ElemType>(elem_str));
113 
114 
115  // Print information about the mesh to the screen.
116  mesh.print_info();
117 
118  // Create an equation systems object.
119  EquationSystems equation_systems (mesh);
120 
121  // Declare the system "CurlCurl" and its variables.
122  CurlCurlSystem & system =
123  equation_systems.add_system<CurlCurlSystem> ("CurlCurl");
124 
125  // This example only implements the steady-state problem
126  system.time_solver = std::make_unique<SteadySolver>(system);
127 
128  // Initialize the system
129  equation_systems.init();
130 
131  // And the nonlinear solver options
132  DiffSolver & solver = *(system.time_solver->diff_solver().get());
133  solver.quiet = infile("solver_quiet", true);
134  solver.verbose = !solver.quiet;
135  solver.max_nonlinear_iterations = infile("max_nonlinear_iterations", 15);
136  solver.relative_step_tolerance = infile("relative_step_tolerance", 1.e-3);
137  solver.relative_residual_tolerance = infile("relative_residual_tolerance", 1.0e-13);
138  solver.absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0);
139 
140  // And the linear solver options
141  solver.max_linear_iterations = infile("max_linear_iterations", 50000);
142  solver.initial_linear_tolerance = infile("initial_linear_tolerance", 1.e-10);
143 
144  // Print information about the system to the screen.
145  equation_systems.print_info();
146 
147  // Solve the system.
148  system.solve();
149 
150  ExactSolution exact_sol(equation_systems);
151 
152  SolutionFunction soln_func(system.variable_number("u"));
153  SolutionGradient soln_grad(system.variable_number("u"));
154 
155  // Build FunctionBase* containers to attach to the ExactSolution object.
156  std::vector<FunctionBase<Number> *> sols(1, &soln_func);
157  std::vector<FunctionBase<Gradient> *> grads(1, &soln_grad);
158 
159  exact_sol.attach_exact_values(sols);
160  exact_sol.attach_exact_derivs(grads);
161 
162  // Use higher quadrature order for more accurate error results
163  int extra_error_quadrature = infile("extra_error_quadrature", 2);
164  exact_sol.extra_quadrature_order(extra_error_quadrature);
165 
166  // Compute the error.
167  exact_sol.compute_error("CurlCurl", "u");
168 
169  // Print out the error values
170  libMesh::out << "L2-Error is: "
171  << exact_sol.l2_error("CurlCurl", "u")
172  << std::endl;
173  libMesh::out << "HCurl semi-norm error is: "
174  << exact_sol.error_norm("CurlCurl", "u", HCURL_SEMINORM)
175  << std::endl;
176  libMesh::out << "HCurl-Error is: "
177  << exact_sol.hcurl_error("CurlCurl", "u")
178  << std::endl;
179 
180 #ifdef LIBMESH_HAVE_EXODUS_API
181 
182  // We write the file in the ExodusII format.
183  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
184 
185 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
186 
187  // All done.
188  return 0;
189 }
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
This is the EquationSystems class.
int main(int argc, char **argv)
Definition: vector_fe_ex4.C:49
bool quiet
The DiffSolver should not print anything to libMesh::out unless quiet is set to false; default is tru...
Definition: diff_solver.h:160
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:50
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 ...
unsigned int max_nonlinear_iterations
The DiffSolver should exit in failure if max_nonlinear_iterations is exceeded and continue_after_max_...
Definition: diff_solver.h:154
Real absolute_residual_tolerance
The DiffSolver should exit after the residual is reduced to either less than absolute_residual_tolera...
Definition: diff_solver.h:189
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we&#39;re going to use.
Definition: diff_system.h:245
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
Real hcurl_error(std::string_view sys_name, std::string_view unknown_name)
void extra_quadrature_order(const int extraorder)
Increases or decreases the order of the quadrature rule used for numerical integration.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:91
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...
unsigned int variable_number(std::string_view var) const
Definition: system.C:1398
SolverPackage default_solver_package()
Definition: libmesh.C:1051
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
Definition: diff_solver.h:68
T command_line_value(const std::string &, T)
Definition: libmesh.C:958
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
Definition: diff_solver.h:146
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:2050
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:1698
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.
Real l2_error(std::string_view sys_name, std::string_view unknown_name)
FEMSystem, TimeSolver and NewtonSolver will handle most tasks, but we must specify element residuals...
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false...
Definition: diff_solver.h:166
OStreamProxy out
virtual void solve() override
Invokes the solver associated with the system.
Definition: fem_system.C:1076
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
double initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the DiffSolver may tighten the ...
Definition: diff_solver.h:208
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 relative_residual_tolerance
Definition: diff_solver.h:190
Real error_norm(std::string_view sys_name, std::string_view unknown_name, const FEMNormType &norm)