libMesh
introduction_ex2.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 a Simple 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 a simple scalar system. 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 -log_info
38 //
39 // to see what PETSc is doing behind the scenes or
40 //
41 // ./introduction_ex2 -log_summary
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 file that defines various mesh generation utilities
54 #include "libmesh/mesh_generation.h"
55 // Include file that defines (possibly multiple) systems of equations.
56 #include "libmesh/equation_systems.h"
57 // Include files that define a simple steady system
58 #include "libmesh/linear_implicit_system.h"
59 #include "libmesh/transient_system.h"
60 #include "libmesh/explicit_system.h"
61 
62 // Bring in everything from the libMesh namespace
63 using namespace libMesh;
64 
65 
66 
67 int main (int argc, char ** argv)
68 {
69  LibMeshInit init (argc, argv);
70 
71  // Skip this 2D example if libMesh was compiled as 1D-only.
72  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
73 
74  // This example requires a linear solver package.
75  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
76  "--enable-petsc, --enable-trilinos, or --enable-eigen");
77 
78  // A brief message to the user to inform her of the
79  // exact name of the program being run, and its command line.
80  libMesh::out << "Running " << argv[0];
81  for (int i=1; i<argc; i++)
82  libMesh::out << " " << argv[i];
83  libMesh::out << std::endl << std::endl;
84 
85  // Create a mesh, with dimension to be overridden later, distributed
86  // across the default MPI communicator.
87  Mesh mesh(init.comm());
88 
89  // Use the MeshTools::Generation mesh generator to create a uniform
90  // 2D grid on the unit square. By default a mesh of QUAD4
91  // elements will be created. We instruct the mesh generator
92  // to build a mesh of 5x5 elements.
94 
95  // Create an equation systems object. This object can
96  // contain multiple systems of different
97  // flavors for solving loosely coupled physics. Each system can
98  // contain multiple variables of different approximation orders.
99  // Here we will simply create a single system with one variable.
100  // Later on, other flavors of systems will be introduced. For the
101  // moment, we use the general system.
102  // The EquationSystems object needs a reference to the mesh
103  // object, so the order of construction here is important.
104  EquationSystems equation_systems (mesh);
105 
106  // Add a flag "test" that is visible for all systems. This
107  // helps in inter-system communication.
108  equation_systems.parameters.set<bool> ("test") = true;
109 
110  // Set a simulation-specific parameter visible for all systems.
111  // This helps in inter-system-communication.
112  equation_systems.parameters.set<Real> ("dummy") = 42.;
113 
114  // Set another simulation-specific parameter
115  equation_systems.parameters.set<Real> ("nobody") = 0.;
116 
117  // Now we declare the system and its variables.
118  // We begin by adding a "TransientLinearImplicitSystem" to the
119  // EquationSystems object, and we give it the name
120  // "Simple System".
121  equation_systems.add_system<TransientLinearImplicitSystem> ("Simple System");
122 
123  // Adds the variable "u" to "Simple System". "u"
124  // will be approximated using first-order approximation.
125  equation_systems.get_system("Simple System").add_variable("u", FIRST);
126 
127  // Next we'll by add an "ExplicitSystem" to the
128  // EquationSystems object, and we give it the name
129  // "Complex System".
130  equation_systems.add_system<ExplicitSystem> ("Complex System");
131 
132  // Give "Complex System" three variables -- each with a different approximation
133  // order. Variables "c" and "T" will use first-order Lagrange approximation,
134  // while variable "dv" will use a second-order discontinuous
135  // approximation space.
136  equation_systems.get_system("Complex System").add_variable("c", FIRST);
137  equation_systems.get_system("Complex System").add_variable("T", FIRST);
138  equation_systems.get_system("Complex System").add_variable("dv", SECOND, MONOMIAL);
139 
140  // Initialize the data structures for the equation system.
141  equation_systems.init();
142 
143  // Print information about the mesh to the screen.
144  mesh.print_info();
145  // Prints information about the system to the screen.
146  equation_systems.print_info();
147 
148  // Write the equation system if the user specified an
149  // output file name. Note that there are two possible
150  // formats to write to. Specifying WRITE will
151  // create a formatted ASCII file. Optionally, you can specify
152  // ENCODE and get an XDR-encoded binary file.
153  //
154  // We will write the data, clear the object, and read the file
155  // we just wrote. This is simply to demonstrate capability.
156  // Note that you might use this in an application to periodically
157  // dump the state of your simulation. You can then restart from
158  // this data later.
159  if (argc > 1)
160  if (argv[1][0] != '-')
161  {
162  libMesh::out << "<<< Writing system to file " << argv[1]
163  << std::endl;
164 
165  // Write the system.
166  equation_systems.write (argv[1], WRITE);
167 
168  // Clear the equation systems data structure.
169  equation_systems.clear ();
170 
171  libMesh::out << ">>> Reading system from file " << argv[1]
172  << std::endl << std::endl;
173 
174  // Read the file we just wrote. This better
175  // work!
176  equation_systems.read (argv[1], READ);
177 
178  // Print the information again.
179  equation_systems.print_info();
180  }
181 
182  // All done. libMesh objects are destroyed here. Because the
183  // LibMeshInit object was created first, its destruction occurs
184  // last, and it's destructor finalizes any external libraries and
185  // checks for leaked memory.
186  return 0;
187 }
This is the EquationSystems class.
void write(const std::string &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.
MeshBase & mesh
This class provides a specific system class.
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.
void read(const std::string &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.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:62
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage default_solver_package()
Definition: libmesh.C:995
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
int main(int argc, char **argv)
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
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.
const Parallel::Communicator & comm() const
Definition: libmesh.h:81
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.
const T_sys & get_system(const std::string &name) const
virtual void init()
Initialize all the systems.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
void print_info(std::ostream &os=libMesh::out) const
Prints relevant information about the mesh.
Definition: mesh_base.C:448
The ExplicitSystem provides only "right hand side" storage, which should be sufficient for solving mo...