libMesh
vector_fe_ex5.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 5 - Solving an uncoupled Poisson Problem using DG</h1>
19 // \author Alexander Lindsay
20 // \date 2019
21 //
22 // This is the fifth vector FE example program. It solves the same PDE as the first vector example,
23 // but uses a vector monomial basis. The approximate solution of both ex1 and this example will
24 // converge to the same true solution, but at different rates. Moreover, although the PDE is linear,
25 // we demonstrate use of a non-linear solver in this example while ex1 uses an appropriate linear
26 // solver
27 
28 #include "libmesh/libmesh.h"
29 #include "libmesh/mesh.h"
30 #include "libmesh/equation_systems.h"
31 #include "libmesh/fpe_disabler.h"
32 #include "libmesh/nonlinear_implicit_system.h"
33 #include "libmesh/nonlinear_solver.h"
34 #include "libmesh/parameters.h"
35 #include "libmesh/getpot.h"
36 #include "libmesh/enum_solver_package.h"
37 #include "libmesh/exodusII_io.h"
38 #include "libmesh/mesh_generation.h"
39 #include "libmesh/enum_elem_type.h"
40 
41 namespace libMesh
42 {
43 template <typename>
45 template <typename>
47 }
48 
49 // Bring in everything from the libMesh namespace
50 using namespace libMesh;
51 
52 // residual assembly function
56 
57 // jacobian assembly function
61 
62 int
63 main(int argc, char ** argv)
64 {
65  // Initialize libraries.
66  LibMeshInit init(argc, argv);
67 
68  // This example requires a non-linear solver package.
69  libmesh_example_requires(libMesh::default_solver_package() == PETSC_SOLVERS ||
71  "--enable-petsc or --enable-trilinos");
72 
73  // Parse input file
74  GetPot input_file("vector_fe_ex5.in");
75 
76  // But allow the command line to override it.
77  input_file.parse_command_line(argc, argv);
78 
79  // Read DG parameters
80  const Real epsilon = input_file("epsilon", -1);
81  const Real sigma = input_file("sigma", 6);
82 
83  // Read mesh size
84  const std::size_t nx = input_file("nx", 15);
85  const std::size_t ny = input_file("ny", 15);
86 
87  // Read approximation order - default to FIRST
88  const unsigned int approx_order = input_file("approx_order", 1);
89 
90  // Brief message to the user regarding the program name
91  // and command line arguments.
92  libMesh::out << "Running " << argv[0];
93 
94  for (int i = 1; i < argc; i++)
95  libMesh::out << " " << argv[i];
96 
97  libMesh::out << std::endl << std::endl;
98 
99  // Skip this 2D example if libMesh was compiled as 1D-only.
100  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
101 
102  // Create a mesh, with dimension to be overridden later, on the
103  // default MPI communicator.
104  Mesh mesh(init.comm());
105 
106  // Use the MeshTools::Generation mesh generator to create a uniform
107  // 2D grid on the square [-1,1]^2. We instruct the mesh generator
108  // to build a mesh of 15x15 QUAD4 elements. Note that at the end of this
109  // function call, the mesh will be prepared and remote elements will be deleted
110  MeshTools::Generation::build_square(mesh, nx, ny, -1., 1., -1., 1., QUAD4);
111 
112  // Print information about the mesh to the screen.
113  mesh.print_info();
114 
115  // Create an equation systems object.
116  EquationSystems equation_systems(mesh);
117  equation_systems.parameters.set<Real>("epsilon") = epsilon;
118  equation_systems.parameters.set<Real>("sigma") = sigma;
119 
120  // Declare the Poisson system and its variables.
121  // The Poisson system is another example of a steady system.
122  auto & nl_system = equation_systems.add_system<NonlinearImplicitSystem>("Poisson");
123 
124  // Adds the variable "u" to "Poisson". Variable "u" will be approximated using
125  // first-order (can be overridden with command line) vector monomial elements.
126  // Since the mesh is 2-D, "u" will have two components.
127  nl_system.add_variable("u", static_cast<Order>(approx_order), MONOMIAL_VEC);
128 
129  // Set the residual and Jacobian evaluation functions
130  auto & nl_solver = *nl_system.nonlinear_solver;
131  nl_solver.residual = compute_residual;
132  nl_solver.jacobian = compute_jacobian;
133 
134  // Initialize the data structures for the equation system.
135  equation_systems.init();
136 
137  // Prints information about the system to the screen.
138  equation_systems.print_info();
139 
140  // A p=0 "solve" here is a hack to test ExodusII output; the
141  // solve() is stymied by division by zero.
142  std::unique_ptr<FPEDisabler> maybe_disable_fpe;
143  if (! approx_order) /* CONSTANT, MONOMIAL_VEC */
144  maybe_disable_fpe = std::make_unique<FPEDisabler>();
145 
146  nl_system.solve();
147 
148 #ifdef LIBMESH_HAVE_EXODUS_API
149  if (! approx_order) /* CONSTANT, MONOMIAL_VEC */
150  {
151  // Warn about failed solve for CONSTANT approximations (Poisson needs at least p=1)
152  libMesh::out << "\nWARNING: The elemental vector order is CONSTANT. The solution" << std::endl
153  << "written out to 'out_constant.e' will be trivial." << std::endl;
154 
155  // Need to get string of variable names (vector components) for 'set_output_variables()'
156  std::vector<std::string> varnames;
157  equation_systems.build_variable_names(varnames);
158 
159  // Create ExodusII object with an unambiguous filename (indicating this solution is special)
160  ExodusII_IO exo_ptr(mesh);
161  exo_ptr.write("out_constant.e");
162  exo_ptr.set_output_variables(varnames);
163  exo_ptr.write_element_data(equation_systems);
164  }
165  else
166  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
167 #endif
168 
169  // All done.
170  return 0;
171 }
This is the EquationSystems class.
void build_variable_names(std::vector< std::string > &var_names, const FEType *type=nullptr, const std::set< std::string > *system_names=nullptr) const
Fill the input vector var_names with the names of the variables for each system.
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
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 compute_jacobian(const NumericVector< Number > &X, SparseMatrix< Number > &J, NonlinearImplicitSystem &S)
Definition: assembly.C:315
void compute_residual(const NumericVector< Number > &X, NumericVector< Number > &R, NonlinearImplicitSystem &S)
Definition: assembly.C:27
Generic sparse matrix.
Definition: vector_fe_ex5.C:46
SolverPackage default_solver_package()
Definition: libmesh.C:1117
void set_output_variables(const std::vector< std::string > &output_variables, bool allow_empty=true)
Sets the list of variable names to be included in the output.
Definition: exodusII_io.C:184
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
int main(int argc, char **argv)
Definition: vector_fe_ex5.C:63
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
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
virtual void write(const std::string &fname) override
This method implements writing a mesh to a specified file.
Definition: exodusII_io.C:2180
T & set(const std::string &)
Definition: parameters.h:469
OStreamProxy out
void write_element_data(const EquationSystems &es)
Write out element solution.
Definition: exodusII_io.C:1359
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