Dune Core Modules (2.11.0)

gmsh4reader.hh
1#pragma once
2
3#include <iosfwd>
4#include <map>
5#include <memory>
6#include <vector>
7
9
10#include <dune/gmsh4/filereader.hh>
11#include <dune/gmsh4/gridcreators/continuousgridcreator.hh> // default GridCreator
12
13namespace Dune
14{
16
29 template <class Grid, class GridCreator = Gmsh4::ContinuousGridCreator<Grid>, class SizeType = std::size_t>
31 : public Gmsh4::FileReader<Grid, Gmsh4Reader<Grid, GridCreator>>
32 {
33 // Sections visited during the file parsing
34 enum class Sections {
35 NO_SECTION = 0, MESH_FORMAT, PHYSICAL_NAMES, ENTITIES, PARTITIONED_ENTITIES, NODES, ELEMENTS, PERIODIC,
36 GHOST_ELEMENTS, PARAMETRIZATION, NODE_DATA, ELEMENT_DATA, ELEMENT_NODE_DATA, INTERPOLATION_SCHEME
37 };
38
39 using Entity = typename Grid::template Codim<0>::Entity;
40 using GlobalCoordinate = typename Entity::Geometry::GlobalCoordinate;
41
42 public:
43 using size_type = SizeType;
44
46 template <class... Args,
47 std::enable_if_t<std::is_constructible<GridCreator, Args...>::value,int> = 0>
48 explicit Gmsh4Reader (Args&&... args)
49 : creator_(std::make_shared<GridCreator>(std::forward<Args>(args)...))
50 {}
51
53 explicit Gmsh4Reader (GridCreator& creator)
54 : creator_(stackobject_to_shared_ptr(creator))
55 {}
56
58 explicit Gmsh4Reader (std::shared_ptr<GridCreator> creator)
59 : creator_(std::move(creator))
60 {}
61
63
68 void read (std::string const& filename, bool fillCreator = true);
69
72
74
79 void readSerialFileFromStream (std::ifstream& input, bool fillCreator = true);
80
82
87 void readParallelFileFromStream (std::ifstream& input, int rank, int size, bool fillCreator = true);
88
90 // NOTE: requires the internal data structures to be filled by an aforgoing call to read
91 void fillGridCreator (bool insertPieces = true);
92
94
96 std::vector<std::string> const& pieces () const
97 {
98 return pieces_;
99 }
100
101#ifndef DOXYGEN
103 static void fillFactoryImpl (GridFactory<Grid>& factory, std::string const& filename)
104 {
105 Gmsh4Reader reader{factory};
106 reader.read(filename);
107 }
108#endif
109
110 private:
111 template<class T>
112 void readValueBinary(std::ifstream& input, T &v);
113 void readMeshFormat (std::ifstream& input,
114 double& version, int& file_type, int& data_size);
115
116 void readPhysicalNames (std::ifstream& input);
117 void readEntitiesAscii (std::ifstream& input);
118 void readEntitiesBinary (std::ifstream& input);
119 void readPartitionedEntitiesAscii (std::ifstream& input);
120 void readPartitionedEntitiesBinary (std::ifstream& input);
121 void readNodesAscii (std::ifstream& input);
122 void readNodesBinary (std::ifstream& input);
123 void readElementsAscii (std::ifstream& input);
124 void readElementsBinary (std::ifstream& input);
125 void readPeriodic (std::ifstream& input);
126 void readGhostElements (std::ifstream& input);
127 void readParametrization (std::ifstream& input);
128 void readNodeData (std::ifstream& input);
129 void readElementData (std::ifstream& input);
130 void readElementNodeData (std::ifstream& input);
131 void readInterpolationScheme (std::ifstream& input);
132
133 // Test whether line belongs to section
134 bool isSection (std::string line,
135 std::string key,
136 Sections current,
137 Sections parent = Sections::NO_SECTION) const
138 {
139 bool result = line.substr(1, key.length()) == key;
140 if (result && current != parent)
141 DUNE_THROW(Dune::Exception , "<" << key << "> in wrong section." );
142 return result;
143 }
144
145 void readString(std::istream& /*stream*/, std::string& name)
146 {
147 DUNE_THROW(Dune::NotImplemented, "Method readString() not yet implemented");
148 name = "";
149 }
150
151 // clear all vectors
152 void clear ()
153 {
154 pieces_.clear();
155
156 physicalNames_.clear();
157
158 points_.clear();
159 curves_.clear();
160 surfaces_.clear();
161 volumes_.clear();
162
163 numPartitions_ = 0;
164 ghostEntities_.clear();
165 partitionedPoints_.clear();
166 partitionedCurves_.clear();
167 partitionedSurfaces_.clear();
168 partitionedVolumes_.clear();
169
170 numNodes_ = 0;
171 minNodeTag_ = 0;
172 maxNodeTag_ = 0;
173 nodes_.clear();
174
175 numElements_ = 0;
176 minElementTag_ = 0;
177 maxElementTag_ = 0;
178 elements_.clear();
179
180 periodic_.clear();
181 ghostElements_.clear();
182 parametrization_.clear();
183 nodeData_.clear();
184 elementData_.clear();
185 elementNodeData_.clear();
186 interpolationScheme_.clear();
187 }
188
189 auto comm () const
190 {
191 return MPIHelper::getCommunication();
192 }
193
194 private: // structures used to store data from file
195
196 struct PhysicalNamesAttributes;
197 struct PointAttributes;
198 struct EntityAttributes;
199
200 template <class Attr>
201 struct PartitionedAttributes : public Attr
202 {
203 int parentDim;
204 int parentTag;
205 std::vector<int> partitions;
206 };
207
208 struct GhostAttributes;
209 struct NodeAttributes;
210 struct ElementAttributes;
211 struct PeriodicAttributes;
212
213 struct GhostElementAttributes {};
214 struct ParametrizationAttributes {};
215 struct NodeDataAttributes {};
216 struct ElementDataAttributes {};
217 struct ElementNodeDataAttributes {};
218 struct InterpolationSchemeAttributes {};
219
220 private:
221 std::shared_ptr<GridCreator> creator_ = nullptr;
222
223 // swap will be true if a binary msh-file has different endianness as the user's system.
224 bool swap = false;
225
226 std::vector<std::string> pieces_;
227
228 // PhysicalNames section
229 std::vector<PhysicalNamesAttributes> physicalNames_;
230
231 // Entities section
232 std::vector<PointAttributes> points_;
233 std::vector<EntityAttributes> curves_;
234 std::vector<EntityAttributes> surfaces_;
235 std::vector<EntityAttributes> volumes_;
236
237 // PartitionedEntities section
238 size_type numPartitions_ = 0;
239 std::vector<GhostAttributes> ghostEntities_;
240 std::vector<PartitionedAttributes<PointAttributes>> partitionedPoints_;
241 std::vector<PartitionedAttributes<EntityAttributes>> partitionedCurves_;
242 std::vector<PartitionedAttributes<EntityAttributes>> partitionedSurfaces_;
243 std::vector<PartitionedAttributes<EntityAttributes>> partitionedVolumes_;
244
245 size_type numNodes_ = 0;
246 size_type minNodeTag_ = 0;
247 size_type maxNodeTag_ = 0;
248 std::vector<NodeAttributes> nodes_;
249
250 size_type numElements_ = 0;
251 size_type minElementTag_ = 0;
252 size_type maxElementTag_ = 0;
253 std::vector<ElementAttributes> elements_;
254 std::vector<PeriodicAttributes> periodic_;
255 std::vector<GhostElementAttributes> ghostElements_;
256 std::vector<ParametrizationAttributes> parametrization_;
257 std::vector<NodeDataAttributes> nodeData_;
258 std::vector<ElementDataAttributes> elementData_;
259 std::vector<ElementNodeDataAttributes> elementNodeData_;
260 std::vector<InterpolationSchemeAttributes> interpolationScheme_;
261
263 static std::map<int, size_type> elementType_;
264
265 // Associate a string with the corresponding Sections enum
266 static std::map<std::string, Sections> sections_;
267 };
268
269 // deduction guides
270 template <class Grid>
271 Gmsh4Reader (GridFactory<Grid>&)
272 -> Gmsh4Reader<Grid, Gmsh4::ContinuousGridCreator<Grid>>;
273
274 template <class GridCreator,
275 class = std::void_t<typename GridCreator::Grid>>
276 Gmsh4Reader (GridCreator&)
277 -> Gmsh4Reader<typename GridCreator::Grid, GridCreator>;
278
279 template <class GridCreator,
280 class = std::void_t<typename GridCreator::Grid>>
281 Gmsh4Reader (std::shared_ptr<GridCreator>)
282 -> Gmsh4Reader<typename GridCreator::Grid, GridCreator>;
283
284} // end namespace Dune
285
286#include "gmsh4reader.impl.hh"
Base class for Dune-Exceptions.
Definition: exceptions.hh:98
File-Reader for GMsh unstructured .msh files.
Definition: gmsh4reader.hh:32
std::vector< std::string > const & pieces() const
Return the filenames of parallel pieces.
Definition: gmsh4reader.hh:96
Gmsh4Reader(Args &&... args)
Constructor. Creates a new GridCreator with the passed factory.
Definition: gmsh4reader.hh:48
void readSerialFileFromStream(std::ifstream &input, bool fillCreator=true)
Read the grid from an input stream, referring to a .msh file, into the GridCreator.
Definition: gmsh4reader.impl.hh:340
Gmsh4Reader(std::shared_ptr< GridCreator > creator)
Constructor. Stores the shared_ptr.
Definition: gmsh4reader.hh:58
void fillGridCreator(bool insertPieces=true)
Construct a grid using the GridCreator.
Definition: gmsh4reader.impl.hh:1261
void read(std::string const &filename, bool fillCreator=true)
Read the grid from file with filename into the GridFactory stored in the GridCreator.
Definition: gmsh4reader.impl.hh:271
Gmsh4Reader(GridCreator &creator)
Constructor. Stores the references in a non-destroying shared_ptr.
Definition: gmsh4reader.hh:53
void readParallelFileFromStream(std::ifstream &input, int rank, int size, bool fillCreator=true)
Read the grid from and input stream, referring to a .pro file, into the GridCreator.
Definition: gmsh4reader.impl.hh:413
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
Dune namespace
Definition: alignedallocator.hh:13
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.
Static tag representing a codimension.
Definition: dimension.hh:24
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Feb 14, 23:39, 2026)