libMesh
Loading...
Searching...
No Matches
introduction_ex1.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
37using namespace libMesh;
38
39int 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 libmesh_error_msg_if(argc < 4, "Usage: " << argv[0] << " -d 2 in.mesh [-o out.mesh]");
54
55 // Get the dimensionality of the mesh from the "-d" argument
56 const unsigned int dim =
58 libmesh_error_msg_if(dim > 3, "Usage: " << argv[0] << " -d 2 in.mesh [-o out.mesh]");
59
60 // Skip higher-dimensional examples on a lower-dimensional libMesh build
61 libmesh_example_requires(dim <= LIBMESH_DIM, "2D/3D support");
62
63 // Create a mesh, with dimension to be overridden later, on the
64 // default MPI communicator.
65 Mesh mesh(init.comm());
66
67 // We may need XDR support compiled in to read binary .xdr files
68 const std::string input_filename = argv[3];
69#ifndef LIBMESH_HAVE_XDR
70 libmesh_example_requires(input_filename.rfind(".xdr") >=
71 input_filename.size(), "XDR support");
72#endif
73
74 // Read the input mesh.
75 mesh.read (input_filename);
76
77 // Print information about the mesh to the screen.
79
80 // Write the output mesh if the user specified an
81 // output file name.
83 {
84 // We may need XDR support compiled in to read binary .xdr files
85 const std::string output_filename =
86 libMesh::command_line_next("-o",std::string());
87#ifndef LIBMESH_HAVE_XDR
88 libmesh_example_requires(output_filename.rfind(".xdr") >=
89 output_filename.size(), "XDR support");
90#endif
91
92 mesh.write (output_filename);
93 }
94
95 // All done. libMesh objects are destroyed here. Because the
96 // LibMeshInit object was created first, its destruction occurs
97 // last, and it's destructor finalizes any external libraries and
98 // checks for leaked memory.
99 return 0;
100}
unsigned int dim
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
virtual void write(const std::string &name) const =0
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
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:1755
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
MeshBase & mesh
The libMesh namespace provides an interface to certain functionality in the library.
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition libmesh.h:303
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
Definition libmesh.C:1025
bool on_command_line(std::string arg)
Definition libmesh.C:934
int main()