libMesh
vector_fe_ex5.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 // <h1>Vector Finite Element Example 1 - 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/nonlinear_implicit_system.h"
32 #include "libmesh/nonlinear_solver.h"
33 #include "libmesh/parameters.h"
34 #include "libmesh/getpot.h"
35 #include "libmesh/enum_solver_package.h"
36 #include "libmesh/exodusII_io.h"
37 #include "libmesh/mesh_generation.h"
38 #include "libmesh/enum_elem_type.h"
39 
40 namespace libMesh
41 {
42 template <typename>
44 template <typename>
46 }
47 
48 // Bring in everything from the libMesh namespace
49 using namespace libMesh;
50 
51 // residual assembly function
55 
56 // jacobian assembly function
60 
61 int
62 main(int argc, char ** argv)
63 {
64  // Initialize libraries.
65  LibMeshInit init(argc, argv);
66 
67  // This example requires a non-linear solver package.
68  libmesh_example_requires(libMesh::default_solver_package() == PETSC_SOLVERS ||
70  "--enable-petsc or --enable-trilinos");
71 
72  // Parse input file
73  GetPot input_file("vector_fe_ex5.in");
74 
75  // Read DG parameters
76  const Real epsilon = input_file("epsilon", -1);
77  const Real sigma = input_file("sigma", 6);
78 
79  // Read mesh size
80  const std::size_t nx = input_file("nx", 15);
81  const std::size_t ny = input_file("ny", 15);
82 
83  // Brief message to the user regarding the program name
84  // and command line arguments.
85  libMesh::out << "Running " << argv[0];
86 
87  for (int i = 1; i < argc; i++)
88  libMesh::out << " " << argv[i];
89 
90  libMesh::out << std::endl << std::endl;
91 
92  // Skip this 2D example if libMesh was compiled as 1D-only.
93  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
94 
95  // Create a mesh, with dimension to be overridden later, on the
96  // default MPI communicator.
97  Mesh mesh(init.comm());
98 
99  // Use the MeshTools::Generation mesh generator to create a uniform
100  // 2D grid on the square [-1,1]^2. We instruct the mesh generator
101  // to build a mesh of 15x15 QUAD4 elements. Note that at the end of this
102  // fuction call, the mesh will be prepared and remote elements will be deleted
103  MeshTools::Generation::build_square(mesh, nx, ny, -1., 1., -1., 1., QUAD4);
104 
105  // Print information about the mesh to the screen.
106  mesh.print_info();
107 
108  // Create an equation systems object.
109  EquationSystems equation_systems(mesh);
110  equation_systems.parameters.set<Real>("epsilon") = epsilon;
111  equation_systems.parameters.set<Real>("sigma") = sigma;
112 
113  // Declare the Poisson system and its variables.
114  // The Poisson system is another example of a steady system.
115  auto & nl_system = equation_systems.add_system<NonlinearImplicitSystem>("Poisson");
116 
117  // Adds the variable "u" to "Poisson". "u"
118  // will be approximated using first-order vector monomial elements.
119  // Since the mesh is 2-D, "u" will have two components.
120  nl_system.add_variable("u", FIRST, MONOMIAL_VEC);
121 
122  // Set the residual and Jacobian evaluation functions
123  auto & nl_solver = *nl_system.nonlinear_solver;
124  nl_solver.residual = compute_residual;
125  nl_solver.jacobian = compute_jacobian;
126 
127  // Initialize the data structures for the equation system.
128  equation_systems.init();
129 
130  // Prints information about the system to the screen.
131  equation_systems.print_info();
132 
133  nl_system.solve();
134 
135 #ifdef LIBMESH_HAVE_EXODUS_API
136  ExodusII_IO(mesh).write_equation_systems("out.e", equation_systems);
137 #endif
138 
139  // All done.
140  return 0;
141 }
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
libMesh::PETSC_SOLVERS
Definition: enum_solver_package.h:36
compute_jacobian
void compute_jacobian(const NumericVector< Number > &X, SparseMatrix< Number > &J, NonlinearImplicitSystem &S)
Definition: assembly.C:311
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MONOMIAL_VEC
Definition: enum_fe_family.h:62
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::NonlinearImplicitSystem
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
Definition: nonlinear_implicit_system.h:54
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
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
compute_residual
void compute_residual(const NumericVector< Number > &X, NumericVector< Number > &R, NonlinearImplicitSystem &S)
Definition: assembly.C:26
libMesh::System::add_variable
unsigned int add_variable(const std::string &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:1069
libMesh::QUAD4
Definition: enum_elem_type.h:41
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
main
int main(int argc, char **argv)
Definition: vector_fe_ex5.C:62
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
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::Parameters::set
T & set(const std::string &)
Definition: parameters.h:460
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::TRILINOS_SOLVERS
Definition: enum_solver_package.h:37
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::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::out
OStreamProxy out
libMesh::FIRST
Definition: enum_order.h:42
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557