libMesh
Functions
vector_fe_ex4.C File Reference

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 49 of file vector_fe_ex4.C.

References libMesh::DiffSolver::absolute_residual_tolerance, libMesh::EquationSystems::add_system(), libMesh::ExactSolution::attach_exact_derivs(), libMesh::ExactSolution::attach_exact_values(), libMesh::MeshTools::Generation::build_cube(), libMesh::command_line_value(), libMesh::ExactSolution::compute_error(), libMesh::default_solver_package(), libMesh::ExactSolution::error_norm(), libMesh::ExactSolution::extra_quadrature_order(), libMesh::ExactSolution::hcurl_error(), libMesh::HCURL_SEMINORM, libMesh::TriangleWrapper::init(), libMesh::EquationSystems::init(), libMesh::DiffSolver::initial_linear_tolerance, libMesh::INVALID_SOLVER_PACKAGE, libMesh::ExactSolution::l2_error(), libMesh::DiffSolver::max_linear_iterations, libMesh::DiffSolver::max_nonlinear_iterations, mesh, libMesh::out, libMesh::EquationSystems::print_info(), libMesh::MeshBase::print_info(), libMesh::DiffSolver::quiet, libMesh::DiffSolver::relative_residual_tolerance, libMesh::DiffSolver::relative_step_tolerance, libMesh::FEMSystem::solve(), libMesh::DifferentiableSystem::time_solver, libMesh::System::variable_number(), libMesh::DiffSolver::verbose, and libMesh::ExodusII_IO::write_equation_systems().

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.
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
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
MeshBase & mesh
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:91
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 init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
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
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