libMesh
introduction_ex1.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 1 - Creation of a Mesh Object</h1>
21 // \author Benjamin S. Kirk
22 // \date 2003
23 //
24 // This is the first example program. It simply demonstrates
25 // how to create a mesh object. A mesh is read from file,
26 // information is printed to the screen, and the mesh is then
27 // written.
28 
29 // C++ include files that we need
30 #include <iostream>
31 // Functions to initialize the library.
32 #include "libmesh/libmesh.h"
33 // Basic include files needed for the mesh functionality.
34 #include "libmesh/mesh.h"
35 
36 // Bring in everything from the libMesh namespace
37 using namespace libMesh;
38 
39 int main (int argc, char ** argv)
40 {
41  // Initialize the library. This is necessary because the library
42  // may depend on a number of other libraries (i.e. MPI and PETSc)
43  // that require initialization before use. When the LibMeshInit
44  // object goes out of scope, other libraries and resources are
45  // finalized.
46  LibMeshInit init (argc, argv);
47 
48  // Check for proper usage. The program is designed to be run
49  // as follows:
50  // ./ex1 -d DIM input_mesh_name [-o output_mesh_name]
51  // where [output_mesh_name] is an optional parameter giving
52  // a filename to write the mesh into.
53  if (argc < 4)
54  {
55  // This handy function will print the file name, line number,
56  // specified message, and then throw an exception.
57  libmesh_error_msg("Usage: " << argv[0] << " -d 2 in.mesh [-o out.mesh]");
58  }
59 
60  // Get the dimensionality of the mesh from argv[2]
61  const unsigned int dim = std::atoi(argv[2]);
62 
63  // Skip higher-dimensional examples on a lower-dimensional libMesh build
64  libmesh_example_requires(dim <= LIBMESH_DIM, "2D/3D support");
65 
66  // Create a mesh, with dimension to be overridden later, on the
67  // default MPI communicator.
68  Mesh mesh(init.comm());
69 
70  // We may need XDR support compiled in to read binary .xdr files
71  std::string input_filename = argv[3];
72 #ifndef LIBMESH_HAVE_XDR
73  libmesh_example_requires(input_filename.rfind(".xdr") >=
74  input_filename.size(), "XDR support");
75 #endif
76 
77  // Read the input mesh.
78  mesh.read (argv[3]);
79 
80  // Print information about the mesh to the screen.
81  mesh.print_info();
82 
83  // Write the output mesh if the user specified an
84  // output file name.
85  if (argc >= 6 && std::string("-o") == argv[4])
86  {
87  // We may need XDR support compiled in to read binary .xdr files
88  std::string output_filename = argv[5];
89 #ifndef LIBMESH_HAVE_XDR
90  libmesh_example_requires(output_filename.rfind(".xdr") >=
91  output_filename.size(), "XDR support");
92 #endif
93 
94  mesh.write (argv[5]);
95  }
96 
97  // All done. libMesh objects are destroyed here. Because the
98  // LibMeshInit object was created first, its destruction occurs
99  // last, and it's destructor finalizes any external libraries and
100  // checks for leaked memory.
101  return 0;
102 }
unsigned int dim
MeshBase & mesh
int main(int argc, char **argv)
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.
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
const Parallel::Communicator & comm() const
Definition: libmesh.h:81
virtual void write(const std::string &name)=0
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
virtual void read(const std::string &name, void *mesh_data=libmesh_nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false)=0
Interfaces for reading/writing a mesh to/from a file.
void print_info(std::ostream &os=libMesh::out) const
Prints relevant information about the mesh.
Definition: mesh_base.C:448