libMesh
gmsh_io.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 #ifndef LIBMESH_GMSH_IO_H
21 #define LIBMESH_GMSH_IO_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/mesh_input.h"
26 #include "libmesh/mesh_output.h"
27 
28 // C++ includes
29 #include <cstddef>
30 
31 namespace libMesh
32 {
33 
34 // Forward declarations
35 class MeshBase;
36 
37 
38 
51 class GmshIO : public MeshInput<MeshBase>,
52  public MeshOutput<MeshBase>
53 {
54 public:
55 
60  explicit
61  GmshIO (MeshBase & mesh);
62 
67  explicit
68  GmshIO (const MeshBase & mesh);
69 
83  virtual void read (const std::string & name) override;
84 
89  virtual void write (const std::string & name) override;
90 
96 
101  virtual void write_nodal_data (const std::string &,
102  const std::vector<Number> &,
103  const std::vector<std::string> &) override;
104 
115  bool & binary ();
116 
122 
123 private:
129  void read_mesh (std::istream & in);
130 
135  void write_mesh (std::ostream & out);
136 
142  void write_post (const std::string &,
143  const std::vector<Number> * = nullptr,
144  const std::vector<std::string> * = nullptr);
145 
149  bool _binary;
150 
156 
161  {
163  unsigned gmsh_type_in,
164  unsigned dim_in,
165  unsigned nnodes_in) :
166  type(type_in),
167  gmsh_type(gmsh_type_in),
168  dim(dim_in),
169  nnodes(nnodes_in)
170  {}
171 
173  unsigned int gmsh_type;
174  unsigned int dim;
175  unsigned int nnodes;
176  std::vector<unsigned int> nodes;
177  };
178 
183  struct ElementMaps
184  {
185  // Helper function to add a (key, value) pair to both maps
186  void add_def(const ElementDefinition & eledef)
187  {
188  out.emplace(eledef.type, eledef);
189  in.emplace(eledef.gmsh_type, eledef);
190  }
191 
192  std::map<ElemType, ElementDefinition> out;
193  std::map<unsigned int, ElementDefinition> in;
194  };
195 
201 
207 };
208 
209 
210 } // namespace libMesh
211 
212 #endif // LIBMESH_GMSH_IO_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
ElemType
Defines an enum for geometric element types.
Reading and writing meshes in the Gmsh format.
Definition: gmsh_io.h:51
bool _binary
Flag to write binary data.
Definition: gmsh_io.h:149
virtual void write(const std::string &name) override
This method implements writing a mesh to a specified file in the Gmsh *.msh format.
Definition: gmsh_io.C:909
bool & write_lower_dimensional_elements()
Access to the flag which controls whether boundary elements are written to the Mesh file...
Definition: gmsh_io.C:142
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: gmsh_io.C:926
This class defines an abstract interface for Mesh output.
Definition: mesh_output.h:53
bool & binary()
Flag indicating whether or not to write a binary file.
Definition: gmsh_io.C:135
The libMesh namespace provides an interface to certain functionality in the library.
std::map< unsigned int, ElementDefinition > in
Definition: gmsh_io.h:193
This is the MeshBase class.
Definition: mesh_base.h:75
std::map< ElemType, ElementDefinition > out
Definition: gmsh_io.h:192
This class defines an abstract interface for Mesh input.
Definition: mesh_base.h:57
ElementDefinition(ElemType type_in, unsigned gmsh_type_in, unsigned dim_in, unsigned nnodes_in)
Definition: gmsh_io.h:162
void write_mesh(std::ostream &out)
This method implements writing a mesh to a specified file.
Definition: gmsh_io.C:938
bool _write_lower_dimensional_elements
If true, lower-dimensional elements based on the boundary conditions get written to the output file...
Definition: gmsh_io.h:155
Defines mapping from libMesh element types to Gmsh element types or vice-versa.
Definition: gmsh_io.h:160
static ElementMaps build_element_maps()
A static function used to construct the _element_maps struct, statically.
Definition: gmsh_io.C:47
OStreamProxy out
static ElementMaps _element_maps
A static ElementMaps object that is built statically and used by all instances of this class...
Definition: gmsh_io.h:200
GmshIO(MeshBase &mesh)
Constructor.
Definition: gmsh_io.C:125
void add_def(const ElementDefinition &eledef)
Definition: gmsh_io.h:186
virtual void read(const std::string &name) override
Reads in a mesh in the Gmsh *.msh format from the ASCII file given by name.
Definition: gmsh_io.C:149
void write_post(const std::string &, const std::vector< Number > *=nullptr, const std::vector< std::string > *=nullptr)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: gmsh_io.C:1084
struct which holds a map from Gmsh to libMesh element numberings and vice-versa.
Definition: gmsh_io.h:183
void read_mesh(std::istream &in)
Implementation of the read() function.
Definition: gmsh_io.C:157
std::vector< unsigned int > nodes
Definition: gmsh_io.h:176