libMesh
vector_fe_ex3.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 
19 
20 // <h1>Vector Finite Elements Example 3 - Nedelec Elements</h1>
21 // \author Paul Bauman
22 // \date 2012
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 
38 // The systems and solvers we may use
39 #include "curl_curl_system.h"
40 #include "libmesh/diff_solver.h"
41 #include "libmesh/steady_solver.h"
42 #include "solution_function.h"
43 
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_ex3.in");
60 
61  // Read in parameters from the input file
62  const unsigned int grid_size = infile("grid_size", 2);
63 
64  // Skip higher-dimensional examples on a lower-dimensional libMesh build
65  libmesh_example_requires(3 <= LIBMESH_DIM, "2D/3D support");
66 
67  // Create a mesh, with dimension to be overridden later, on the
68  // default MPI communicator.
69  Mesh mesh(init.comm());
70 
71  // Use the MeshTools::Generation mesh generator to create a uniform
72  // grid on the square [-1,1]^D. We must use TRI6 elements for the
73  // Nedelec triangle elements.
74  std::string elem_str =
75  command_line_value(std::string("element_type"),
76  std::string("TRI6"));
77 
78  if (elem_str != "TRI6" && elem_str != "QUAD8" && elem_str != "QUAD9")
79  libmesh_error_msg("You selected: " \
80  << elem_str \
81  << " but this example must be run with TRI6, QUAD8, or QUAD9.");
82 
84  grid_size,
85  grid_size,
86  -1., 1.,
87  -1., 1.,
88  Utility::string_to_enum<ElemType>(elem_str));
89 
90 
91  // Print information about the mesh to the screen.
92  mesh.print_info();
93 
94  // Create an equation systems object.
95  EquationSystems equation_systems (mesh);
96 
97  // Declare the system "Navier-Stokes" and its variables.
98  CurlCurlSystem & system =
99  equation_systems.add_system<CurlCurlSystem> ("CurlCurl");
100 
101  // This example only implements the steady-state problem
102  system.time_solver = libmesh_make_unique<SteadySolver>(system);
103 
104  // Initialize the system
105  equation_systems.init();
106 
107  // And the nonlinear solver options
108  DiffSolver & solver = *(system.time_solver->diff_solver().get());
109  solver.quiet = infile("solver_quiet", true);
110  solver.verbose = !solver.quiet;
111  solver.max_nonlinear_iterations = infile("max_nonlinear_iterations", 15);
112  solver.relative_step_tolerance = infile("relative_step_tolerance", 1.e-3);
113  solver.relative_residual_tolerance = infile("relative_residual_tolerance", 1.0e-13);
114  solver.absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0);
115 
116  // And the linear solver options
117  solver.max_linear_iterations = infile("max_linear_iterations", 50000);
118  solver.initial_linear_tolerance = infile("initial_linear_tolerance", 1.e-10);
119 
120  // Print information about the system to the screen.
121  equation_systems.print_info();
122 
123  system.solve();
124 
125  ExactSolution exact_sol(equation_systems);
126 
127  SolutionFunction soln_func(system.variable_number("u"));
128  SolutionGradient soln_grad(system.variable_number("u"));
129 
130  // Build FunctionBase* containers to attach to the ExactSolution object.
131  std::vector<FunctionBase<Number> *> sols(1, &soln_func);
132  std::vector<FunctionBase<Gradient> *> grads(1, &soln_grad);
133 
134  exact_sol.attach_exact_values(sols);
135  exact_sol.attach_exact_derivs(grads);
136 
137  // Use higher quadrature order for more accurate error results
138  int extra_error_quadrature = infile("extra_error_quadrature", 2);
139  exact_sol.extra_quadrature_order(extra_error_quadrature);
140 
141  // Compute the error.
142  exact_sol.compute_error("CurlCurl", "u");
143 
144  // Print out the error values
145  libMesh::out << "L2-Error is: "
146  << exact_sol.l2_error("CurlCurl", "u")
147  << std::endl;
148  libMesh::out << "HCurl semi-norm error is: "
149  << exact_sol.error_norm("CurlCurl", "u", HCURL_SEMINORM)
150  << std::endl;
151  libMesh::out << "HCurl-Error is: "
152  << exact_sol.hcurl_error("CurlCurl", "u")
153  << std::endl;
154 
155 #ifdef LIBMESH_HAVE_EXODUS_API
156 
157  // We write the file in the ExodusII format.
158  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
159 
160 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
161 
162  // All done.
163  return 0;
164 }
libMesh::ExactSolution
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
Definition: exact_solution.h:73
libMesh::ExactSolution::attach_exact_derivs
void attach_exact_derivs(const std::vector< FunctionBase< Gradient > * > &g)
Clone and attach arbitrary functors which compute the exact gradients of the EquationSystems' solutio...
Definition: exact_solution.C:153
SolutionGradient
Definition: solution_function.h:75
libMesh::FEMSystem::solve
virtual void solve() override
Invokes the solver associated with the system.
Definition: fem_system.C:1046
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
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
CurlCurlSystem
FEMSystem, TimeSolver and NewtonSolver will handle most tasks, but we must specify element residuals.
Definition: curl_curl_system.h:35
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
main
int main(int argc, char **argv)
Definition: vector_fe_ex3.C:49
libMesh::DiffSolver
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
Definition: diff_solver.h:70
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
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::ExactSolution::hcurl_error
Real hcurl_error(const std::string &sys_name, const std::string &unknown_name)
Definition: exact_solution.C:410
libMesh::DifferentiableSystem::time_solver
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
Definition: diff_system.h:233
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::MeshTools::Generation::build_square
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.
Definition: mesh_generation.C:1501
libMesh::ExactSolution::extra_quadrature_order
void extra_quadrature_order(const int extraorder)
Increases or decreases the order of the quadrature rule used for numerical integration.
Definition: exact_solution.h:190
libMesh::DiffSolver::relative_step_tolerance
Real relative_step_tolerance
Definition: diff_solver.h:204
SolutionFunction
Definition: solution_function.h:30
libMesh::DiffSolver::verbose
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:168
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
libMesh::ExactSolution::error_norm
Real error_norm(const std::string &sys_name, const std::string &unknown_name, const FEMNormType &norm)
Definition: exact_solution.C:264
libMesh::command_line_value
T command_line_value(const std::string &, T)
Definition: libmesh.C:931
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::ExactSolution::l2_error
Real l2_error(const std::string &sys_name, const std::string &unknown_name)
Definition: exact_solution.C:333
libMesh::ExactSolution::compute_error
void compute_error(const std::string &sys_name, const std::string &unknown_name)
Computes and stores the error in the solution value e = u-u_h, the gradient grad(e) = grad(u) - grad(...
Definition: exact_solution.C:227
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::ExactSolution::attach_exact_values
void attach_exact_values(const std::vector< FunctionBase< Number > * > &f)
Clone and attach arbitrary functors which compute the exact values of the EquationSystems' solutions ...
Definition: exact_solution.C:112
libMesh::DiffSolver::quiet
bool quiet
The DiffSolver should not print anything to libMesh::out unless quiet is set to false; default is tru...
Definition: diff_solver.h:162
libMesh::DiffSolver::relative_residual_tolerance
Real relative_residual_tolerance
Definition: diff_solver.h:192
libMesh::HCURL_SEMINORM
Definition: enum_norm_type.h:46
libMesh::DiffSolver::absolute_residual_tolerance
Real absolute_residual_tolerance
The DiffSolver should exit after the residual is reduced to either less than absolute_residual_tolera...
Definition: diff_solver.h:191
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::DiffSolver::max_linear_iterations
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
Definition: diff_solver.h:148
libMesh::System::variable_number
unsigned short int variable_number(const std::string &var) const
Definition: system.C:1232
libMesh::DiffSolver::max_nonlinear_iterations
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:156
libMesh::out
OStreamProxy out
libMesh::DiffSolver::initial_linear_tolerance
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:210