Dune Core Modules (unstable)

gmsh4reader.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_GRID_IO_FILE_GMSH_GMSH4READER_HH
5#define DUNE_GRID_IO_FILE_GMSH_GMSH4READER_HH
6
7#include <iosfwd>
8#include <map>
9#include <memory>
10#include <vector>
11
13
14#include <dune/grid/io/file/gmsh/filereader.hh>
15#include <dune/grid/io/file/gmsh/gridcreators/continuousgridcreator.hh> // default GridCreator
16
17namespace Dune::Impl::Gmsh
18{
20
33 template <class Grid, class GridCreator = ContinuousGridCreator<Grid>, class SizeType = std::size_t>
34 class Gmsh4Reader
35 : public FileReader<Grid, Gmsh4Reader<Grid, GridCreator> >
36 {
37 // Sections visited during the file parsing
38 enum class Sections {
39 NO_SECTION = 0, MESH_FORMAT, PHYSICAL_NAMES, ENTITIES, PARTITIONED_ENTITIES, NODES, ELEMENTS, PERIODIC,
40 GHOST_ELEMENTS, PARAMETRIZATION, NODE_DATA, ELEMENT_DATA, ELEMENT_NODE_DATA, INTERPOLATION_SCHEME
41 };
42
43 using Entity = typename Grid::template Codim<0>::Entity;
44 using GlobalCoordinate = typename Entity::Geometry::GlobalCoordinate;
45
46 public:
47 using size_type = SizeType;
48
50 template <class ... Args,
51 std::enable_if_t<std::is_constructible<GridCreator, Args...>::value,int> = 0>
52 explicit Gmsh4Reader (Args&&... args)
53 : creator_(std::make_shared<GridCreator>(std::forward<Args>(args)...))
54 {}
55
57 explicit Gmsh4Reader (GridCreator& creator)
58 : creator_(stackobject_to_shared_ptr(creator))
59 {}
60
62 explicit Gmsh4Reader (std::shared_ptr<GridCreator> creator)
63 : creator_(std::move(creator))
64 {}
65
67
72 void read (std::string const& filename, bool fillCreator = true);
73
76
78
83 void readSerialFileFromStream (std::ifstream& input, bool fillCreator = true);
84
86
91 void readParallelFileFromStream (std::ifstream& input, int rank, int size, bool fillCreator = true);
92
94 // NOTE: requires the internal data structures to be filled by an aforgoing call to read
95 void fillGridCreator (bool insertPieces = true);
96
98
100 std::vector<std::string> const& pieces () const
101 {
102 return pieces_;
103 }
104
105#ifndef DOXYGEN
107 static void fillFactoryImpl (GridFactory<Grid>& factory, std::string const& filename)
108 {
109 Gmsh4Reader reader{factory};
110 reader.read(filename);
111 }
112#endif
113
114 private:
115 template<class T>
116 void readValueBinary(std::ifstream& input, T &v);
117 void readMeshFormat (std::ifstream& input,
118 double& version, int& file_type, int& data_size);
119
120 void readPhysicalNames (std::ifstream& input);
121 void readEntitiesAscii (std::ifstream& input);
122 void readEntitiesBinary (std::ifstream& input);
123 void readPartitionedEntitiesAscii (std::ifstream& input);
124 void readPartitionedEntitiesBinary (std::ifstream& input);
125 void readNodesAscii (std::ifstream& input);
126 void readNodesBinary (std::ifstream& input);
127 void readElementsAscii (std::ifstream& input);
128 void readElementsBinary (std::ifstream& input);
129 void readPeriodic (std::ifstream& input);
130 void readGhostElements (std::ifstream& input);
131 void readParametrization (std::ifstream& input);
132 void readNodeData (std::ifstream& input);
133 void readElementData (std::ifstream& input);
134 void readElementNodeData (std::ifstream& input);
135 void readInterpolationScheme (std::ifstream& input);
136
137 // Test whether line belongs to section
138 bool isSection (std::string line,
139 std::string key,
140 Sections current,
141 Sections parent = Sections::NO_SECTION) const
142 {
143 bool result = line.substr(1, key.length()) == key;
144 if (result && current != parent)
145 DUNE_THROW(Dune::Exception , "<" << key << "> in wrong section." );
146 return result;
147 }
148
149 void readString(std::istream& /*stream*/, std::string& name)
150 {
151 DUNE_THROW(Dune::NotImplemented, "Method readString() not yet implemented");
152 name = "";
153 }
154
155 // clear all vectors
156 void clear ()
157 {
158 pieces_.clear();
159
160 physicalNames_.clear();
161
162 points_.clear();
163 curves_.clear();
164 surfaces_.clear();
165 volumes_.clear();
166
167 numPartitions_ = 0;
168 ghostEntities_.clear();
169 partitionedPoints_.clear();
170 partitionedCurves_.clear();
171 partitionedSurfaces_.clear();
172 partitionedVolumes_.clear();
173
174 numNodes_ = 0;
175 minNodeTag_ = 0;
176 maxNodeTag_ = 0;
177 nodes_.clear();
178
179 numElements_ = 0;
180 minElementTag_ = 0;
181 maxElementTag_ = 0;
182 elements_.clear();
183
184 periodic_.clear();
185 ghostElements_.clear();
186 parametrization_.clear();
187 nodeData_.clear();
188 elementData_.clear();
189 elementNodeData_.clear();
190 interpolationScheme_.clear();
191 }
192
193 auto comm () const
194 {
195 return MPIHelper::getCommunication();
196 }
197
198 private: // structures used to store data from file
199
200 struct PhysicalNamesAttributes;
201 struct PointAttributes;
202 struct EntityAttributes;
203
204 template <class Attr>
205 struct PartitionedAttributes : public Attr
206 {
207 int parentDim;
208 int parentTag;
209 std::vector<int> partitions;
210 };
211
212 struct GhostAttributes;
213 struct NodeAttributes;
214 struct ElementAttributes;
215 struct PeriodicAttributes;
216
217 struct GhostElementAttributes {};
218 struct ParametrizationAttributes {};
219 struct NodeDataAttributes {};
220 struct ElementDataAttributes {};
221 struct ElementNodeDataAttributes {};
222 struct InterpolationSchemeAttributes {};
223
224 private:
225 std::shared_ptr<GridCreator> creator_ = nullptr;
226
227 // swap will be true if a binary msh-file has different endianness as the user's system.
228 bool swap = false;
229
230 std::vector<std::string> pieces_;
231
232 // PhysicalNames section
233 std::vector<PhysicalNamesAttributes> physicalNames_;
234
235 // Entities section
236 std::vector<PointAttributes> points_;
237 std::vector<EntityAttributes> curves_;
238 std::vector<EntityAttributes> surfaces_;
239 std::vector<EntityAttributes> volumes_;
240
241 // PartitionedEntities section
242 size_type numPartitions_ = 0;
243 std::vector<GhostAttributes> ghostEntities_;
244 std::vector<PartitionedAttributes<PointAttributes> > partitionedPoints_;
245 std::vector<PartitionedAttributes<EntityAttributes> > partitionedCurves_;
246 std::vector<PartitionedAttributes<EntityAttributes> > partitionedSurfaces_;
247 std::vector<PartitionedAttributes<EntityAttributes> > partitionedVolumes_;
248
249 size_type numNodes_ = 0;
250 size_type minNodeTag_ = 0;
251 size_type maxNodeTag_ = 0;
252 std::vector<NodeAttributes> nodes_;
253
254 size_type numElements_ = 0;
255 size_type minElementTag_ = 0;
256 size_type maxElementTag_ = 0;
257 std::vector<ElementAttributes> elements_;
258 std::vector<PeriodicAttributes> periodic_;
259 std::vector<GhostElementAttributes> ghostElements_;
260 std::vector<ParametrizationAttributes> parametrization_;
261 std::vector<NodeDataAttributes> nodeData_;
262 std::vector<ElementDataAttributes> elementData_;
263 std::vector<ElementNodeDataAttributes> elementNodeData_;
264 std::vector<InterpolationSchemeAttributes> interpolationScheme_;
265
267 static std::map<int, size_type> elementType_;
268
269 // Associate a string with the corresponding Sections enum
270 static std::map<std::string, Sections> sections_;
271 };
272
273 // deduction guides
274 template <class Grid>
275 Gmsh4Reader (GridFactory<Grid>&)
276 -> Gmsh4Reader<Grid, ContinuousGridCreator<Grid> >;
277
278 template <class GridCreator,
279 class = std::void_t<typename GridCreator::Grid> >
280 Gmsh4Reader (GridCreator&)
281 -> Gmsh4Reader<typename GridCreator::Grid, GridCreator>;
282
283 template <class GridCreator,
284 class = std::void_t<typename GridCreator::Grid> >
285 Gmsh4Reader (std::shared_ptr<GridCreator>)
286 -> Gmsh4Reader<typename GridCreator::Grid, GridCreator>;
287
288} // end namespace Dune::Impl::Gmsh
289
290#include "gmsh4reader.impl.hh"
291
292#endif
Base class for Dune-Exceptions.
Definition: exceptions.hh:98
Default exception for dummy implementations.
Definition: exceptions.hh:357
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:498
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:72
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
STL namespace.
This file implements several utilities related to std::shared_ptr.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Nov 2, 23:43, 2025)