libMesh
introduction_ex2.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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>Introduction Example 2 - Defining an Equation System</h1>
21 // \author Benjamin S. Kirk
22 // \date 2003
23 //
24 // This is the second example program. It demonstrates how to
25 // create an equation system for two simple scalar systems. This
26 // example will also introduce some of the issues involved with using PETSc
27 // in your application.
28 //
29 // This is the first example program that indirectly
30 // uses the PETSc library. By default equation data is stored
31 // in PETSc vectors, which may span multiple processors. Before
32 // PETSc is used it must be initialized via libMesh::init(). Note that
33 // by passing argc and argv to PETSc you may specify
34 // command line arguments to PETSc. For example, you might
35 // try running this example as:
36 //
37 // ./introduction_ex2 -info
38 //
39 // to see what PETSc is doing behind the scenes or
40 //
41 // ./introduction_ex2 -log_view
42 //
43 // to get a summary of what PETSc did.
44 // Among other things, libMesh::init() initializes the MPI
45 // communications library and PETSc numeric library on your system if
46 // you haven't already done so.
47 
48 // C++ include files that we need
49 #include <iostream>
50 //Basic include file needed for the mesh functionality.
51 #include "libmesh/libmesh.h"
52 #include "libmesh/mesh.h"
53 #include "libmesh/enum_xdr_mode.h"
54 // Include file that defines various mesh generation utilities
55 #include "libmesh/mesh_generation.h"
56 // Include file that defines (possibly multiple) systems of equations.
57 #include "libmesh/equation_systems.h"
58 // Include files that define a simple steady system
59 #include "libmesh/linear_implicit_system.h"
60 #include "libmesh/transient_system.h"
61 #include "libmesh/explicit_system.h"
62 #include "libmesh/enum_solver_package.h"
63 
64 // Bring in everything from the libMesh namespace
65 using namespace libMesh;
66 
67 
68 
69 int main (int argc, char ** argv)
70 {
71  LibMeshInit init (argc, argv);
72 
73  // Skip this 2D example if libMesh was compiled as 1D-only.
74  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
75 
76  // This example requires a linear solver package.
77  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
78  "--enable-petsc, --enable-trilinos, or --enable-eigen");
79 
80  // A brief message to the user to inform her of the
81  // exact name of the program being run, and its command line.
82  libMesh::out << "Running " << argv[0];
83  for (int i=1; i<argc; i++)
84  libMesh::out << " " << argv[i];
85  libMesh::out << std::endl << std::endl;
86 
87  // Create a mesh, with dimension to be overridden later, distributed
88  // across the default MPI communicator.
89  Mesh mesh(init.comm());
90 
91  // Use the MeshTools::Generation mesh generator to create a uniform
92  // 2D grid on the unit square. By default a mesh of QUAD4
93  // elements will be created. We instruct the mesh generator
94  // to build a mesh of 5x5 elements.
96 
97  // Create an equation systems object. This object can
98  // contain multiple systems of different
99  // flavors for solving loosely coupled physics. Each system can
100  // contain multiple variables of different approximation orders.
101  // Here we create two systems, the first with one variable and the second
102  // with three variables.
103  // The EquationSystems object needs a reference to the mesh
104  // object, so the order of construction here is important.
105  EquationSystems equation_systems (mesh);
106 
107  // Add a flag "test" that is visible for all systems. This
108  // helps in inter-system communication.
109  equation_systems.parameters.set<bool> ("test") = true;
110 
111  // Set a simulation-specific parameter visible for all systems.
112  // This helps in inter-system-communication.
113  equation_systems.parameters.set<Real> ("dummy") = 42.;
114 
115  // Set another simulation-specific parameter
116  equation_systems.parameters.set<Real> ("nobody") = 0.;
117 
118  // Now we declare the system and its variables.
119  // We begin by adding a "TransientLinearImplicitSystem" to the
120  // EquationSystems object, and we give it the name
121  // "Simple System".
122  equation_systems.add_system<TransientLinearImplicitSystem> ("Simple System");
123 
124  // Adds the variable "u" to "Simple System". "u"
125  // will be approximated using a first-order approximation.
126  equation_systems.get_system("Simple System").add_variable("u", FIRST);
127 
128  // Next we add an "ExplicitSystem" to the
129  // EquationSystems object, and we give it the name
130  // "Complex System".
131  equation_systems.add_system<ExplicitSystem> ("Complex System");
132 
133  // Give "Complex System" three variables -- each with a different approximation
134  // order. Variables "c" and "T" will use a first-order Lagrange approximation,
135  // while variable "dv" will use a second-order discontinuous
136  // approximation space.
137  equation_systems.get_system("Complex System").add_variable("c", FIRST);
138  equation_systems.get_system("Complex System").add_variable("T", FIRST);
139  equation_systems.get_system("Complex System").add_variable("dv", SECOND, MONOMIAL);
140 
141  // Initialize the data structures for the equation system.
142  equation_systems.init();
143 
144  // Print information about the mesh to the screen.
145  mesh.print_info();
146  // Prints information about the system to the screen.
147  equation_systems.print_info();
148 
149  // Write the equation system if the user specified an
150  // output file name. Note that there are two possible
151  // formats to write to. Specifying WRITE will
152  // create a formatted ASCII file. Optionally, you can specify
153  // ENCODE and get an XDR-encoded binary file.
154  //
155  // We will write the data, clear the object, and read the file
156  // we just wrote. This is simply to demonstrate capability.
157  // Note that you might use this in an application to periodically
158  // dump the state of your simulation. You can then restart from
159  // this data later.
160  if (argc > 1)
161  if (argv[1][0] != '-')
162  {
163  libMesh::out << "<<< Writing system to file " << argv[1]
164  << std::endl;
165 
166  // Write the system.
167  equation_systems.write (argv[1], WRITE);
168 
169  // Clear the equation systems data structure.
170  equation_systems.clear ();
171 
172  libMesh::out << ">>> Reading system from file " << argv[1]
173  << std::endl << std::endl;
174 
175  // Read the file we just wrote. This better
176  // work!
177  equation_systems.read (argv[1], READ);
178 
179  // Print the information again.
180  equation_systems.print_info();
181  }
182 
183  // All done. libMesh objects are destroyed here. Because the
184  // LibMeshInit object was created first, its destruction occurs
185  // last, and it's destructor finalizes any external libraries and
186  // checks for leaked memory.
187  return 0;
188 }
This is the EquationSystems class.
void write(std::string_view name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
Write the systems to disk using the XDR data format.
virtual void clear()
Restores the data structure to a pristine state.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
Manages storage and variables for transient systems.
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.
const T_sys & get_system(std::string_view name) const
SolverPackage default_solver_package()
Definition: libmesh.C:1050
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:1489
int main(int argc, char **argv)
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
void read(std::string_view name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
T & set(const std::string &)
Definition: parameters.h:469
OStreamProxy out
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
Manages consistently variables, degrees of freedom, and coefficient vectors for explicit systems...