GetFEM  5.5
getfem_import.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2000-2026 Julien Pommier
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file getfem_import.h
32  @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
33  @date Januar 17, 2003.
34  @brief Import mesh files from various formats.
35 */
36 
37 #ifndef GETFEM_IMPORT_H__
38 #define GETFEM_IMPORT_H__
39 
40 #include <string>
41 #include <iostream>
42 #include <map>
43 #include <set>
44 #include "bgeot_config.h" /* for bgeot::size_type */
45 
46 namespace getfem {
47  using bgeot::size_type;
48  class mesh;
49 
50  /** imports a mesh file.
51  format can be:
52 
53  - "gid" for meshes generated by GiD http://gid.cimne.upc.es/
54  -- mesh nodes are always 3D
55 
56 
57  - "gmsh" for meshes generated by Gmsh http://www.geuz.org/gmsh/
58  IMPORTANT NOTE: if you do not assign a physical surface/volume
59  to your 3D mesh, the file will also contain the mesh of the
60  boundary (2D elements) and the boundary of the boundary (line
61  elements!).
62 
63  GetFEM makes use of the physical "region" number stored with
64  each element in the gmsh file to fill the corresponding region
65  of the mesh object.
66 
67  For a mesh of dimension N, getfem builds a mesh with the
68  convexes listed in the gmsh file whose dimension are N, the
69  convexes of dim N-1 are used to tag "region" of faces,
70  according to their gmsh "physical region number" (physical
71  region number 'n' is mapped to the getfem region), and
72  the convexes of lower dimension are ignored.
73 
74  All regions must have different number! This means that the
75  parametrization of the mesh in Gmsh .geo file must assign a
76  different number to each region, the problem exists because in
77  Gmsh can coexist, for example, "Physical Surface (200)" and
78  "Physical Line (200)", as they are different "types of regions"
79  in Gmsh, that which does not occur in GetFEM since there is
80  only one "type of region".
81 
82 
83  - "cdb" for meshes generated by ANSYS (in blocked format).
84 
85  Currently, plane and solid elements of types 42,45,73,82,87,89,
86  90,92,95,162,182,183,185,186,187 and 191 are supported.
87  This however does not include any finite element techology linked
88  to these elements but only their geometry.
89 
90  By default GetFEM will define mesh regions corresponding to
91  material ids found in the imported cdb file, if there are more
92  than one material ids available.
93 
94  Using "cdb:N" as format specifier, only elements with material
95  id equal to an integer N will be imported.
96 
97  The recommended ANSYS command for generating a mesh file is:
98 
99  cdwrite,db,filename,cdb
100 
101 
102  - "am_fmt" for 2D meshes from emc2
103  [http://pauillac.inria.fr/cdrom/prog/unix/emc2/eng.htm]
104 
105  */
106  void import_mesh(const std::string& filename, const std::string& format,
107  mesh& m);
108  void import_mesh(std::istream& f, const std::string& format,
109  mesh& m);
110  void import_mesh(const std::string& filename, mesh& m);
111 
112  /** Import a mesh file in format generated by Gmsh.
113 
114  The function works exactly like impot_mesh() functions
115  except that they return additional mapping of physical
116  region names to their numbers.
117 
118  The example below shows how to print region names
119  and their numbers:
120  @code
121  getfem::mesh myMesh;
122  typedef std::map<std::string, size_type> RegMap;
123  typedef RegMap::iterator RegMapIter;
124  RegMap regmap;
125  getfem::import_mesh_gmsh("mesh.msh", myMesh, regmap);
126  std::cout << regmap.size() << "\n";
127  for (RegMapIter i=regmap.begin(); i != regmap.end(); i++) {
128  std::cout << i->first << " " << i->second << "\n";
129  }
130  @endcode
131 
132  Additionally, the optional lower_dim_convex_rg defines a set
133  of face regions that need to be imported explicitly as convexes.
134 
135  add_all_element_type flag, if set to true, will import all the lower
136  dimension elements defined as independent convexes, only if the elements
137  are not face of another convex. Thus, a 3D model can have a mixture of
138  3D solid, 2D plates and 1D rod elements. This feature is still yet to
139  be tested.
140  */
141  void import_mesh_gmsh(const std::string& filename, mesh& m,
142  std::map<std::string, size_type> &region_map,
143  bool remove_last_dimension = true,
144  std::map<size_type, std::set<size_type>> *nodal_map = NULL,
145  bool remove_duplicated_nodes = true);
146 
147  void import_mesh_gmsh(std::istream& f, mesh& m,
148  std::map<std::string, size_type> &region_map,
149  bool remove_last_dimension = true,
150  std::map<size_type, std::set<size_type>> *nodal_map = NULL,
151  bool remove_duplicated_nodes = true);
152 
153  void import_mesh_gmsh(const std::string& filename, mesh& m,
154  bool add_all_element_type = false,
155  std::set<size_type> *lower_dim_convex_rg = NULL,
156  std::map<std::string, size_type> *region_map = NULL,
157  bool remove_last_dimension = true,
158  std::map<size_type, std::set<size_type>> *nodal_map = NULL,
159  bool remove_duplicated_nodes = true);
160 
161  void import_mesh_gmsh(std::istream& f, mesh& m,
162  bool add_all_element_type = false,
163  std::set<size_type> *lower_dim_convex_rg = NULL,
164  std::map<std::string, size_type> *region_map = NULL,
165  bool remove_last_dimension = true,
166  std::map<size_type, std::set<size_type>> *nodal_map = NULL,
167  bool remove_duplicated_nodes = true);
168 
169  /** for gmsh and gid meshes, the mesh nodes are always 3D, so for a 2D mesh
170  the z-component of nodes should be removed */
171  void maybe_remove_last_dimension(mesh &msh);
172 
173  /** for gmsh meshes, create table linking region name to region_id. */
174  std::map<std::string, size_type> read_region_names_from_gmsh_mesh_file(std::istream& f);
175 }
176 #endif /* GETFEM_IMPORT_H__ */
defines and typedefs for namespace bgeot
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
GEneric Tool for Finite Element Methods.
void maybe_remove_last_dimension(mesh &msh)
for gmsh and gid meshes, the mesh nodes are always 3D, so for a 2D mesh the z-component of nodes shou...
void import_mesh(const std::string &filename, const std::string &format, mesh &m)
imports a mesh file.
void import_mesh_gmsh(const std::string &filename, mesh &m, std::map< std::string, size_type > &region_map, bool remove_last_dimension=true, std::map< size_type, std::set< size_type >> *nodal_map=NULL, bool remove_duplicated_nodes=true)
Import a mesh file in format generated by Gmsh.
std::map< std::string, size_type > read_region_names_from_gmsh_mesh_file(std::istream &f)
for gmsh meshes, create table linking region name to region_id.