DUNE PDELab (unstable)

gmshreader.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_GRID_IO_FILE_GMSHREADER_HH
7#define DUNE_GRID_IO_FILE_GMSHREADER_HH
8
9#include <iostream>
10#include <memory>
11#include <string>
12#include <vector>
13#include <utility>
14
16
18#include <dune/grid/io/file/gmsh/gmsh2parser.hh>
19#include <dune/grid/io/file/gmsh/gmsh4reader.hh>
20#include <dune/grid/io/file/gmsh/utility/version.hh>
21
22namespace Dune
23{
24
35 struct [[deprecated]] GmshReaderOptions
36 {
41 secondOrder
42 };
43 };
44
45 namespace Gmsh {
51 enum class ReaderOptions
52 {
53 verbose = 1,
54 insertBoundarySegments = 2,
55 readElementData = 4,
56 readBoundaryData = 8
57 };
58
60 constexpr ReaderOptions operator | (ReaderOptions a, ReaderOptions b)
61 {
62 return static_cast<ReaderOptions>(
63 static_cast<int>(a) | static_cast<int>(b)
64 );
65 }
66
68 constexpr bool operator & (ReaderOptions a, ReaderOptions b)
69 {
70 return static_cast<int>(a) & static_cast<int>(b);
71 }
72
73 } // end namespace Gmsh
74
85 template <typename GridType>
86 using GmshReaderParser [[deprecated("Use Impl::Gmsh::Gmsh2Parser directly")]] = Impl::Gmsh::Gmsh2Parser<GridType>;
87
104 template<typename GridType>
106 {
108
127 static void doRead(Dune::GridFactory<GridType> &factory,
128 const std::string &fileName,
129 std::vector<int>& boundarySegmentToPhysicalEntity,
130 std::vector<int>& elementToPhysicalEntity,
131 bool verbose, bool insertBoundarySegments)
132 {
133 // register boundary segment to boundary segment factory for possible load balancing
134 // this needs to be done on all cores since the type might not be known otherwise
135 Impl::Gmsh::GmshReaderQuadraticBoundarySegment< Grid::dimension, Grid::dimensionworld >::registerFactory();
136
137#ifndef NDEBUG
138 // check that this method is called on all cores
139 factory.comm().barrier();
140#endif
141
142 // create parse object and read grid on process 0
143 if (factory.comm().rank() == 0)
144 {
145 Impl::Gmsh::Gmsh2Parser<Grid> parser(factory,verbose,insertBoundarySegments);
146 parser.read(fileName);
147
148 boundarySegmentToPhysicalEntity = std::move(parser.boundaryIdMap());
149 elementToPhysicalEntity = std::move(parser.elementIndexMap());
150 }
151 else
152 {
153 boundarySegmentToPhysicalEntity = {};
154 elementToPhysicalEntity = {};
155 }
156 }
157
159
178 template<class T>
179 static T &discarded(T &&value) { return static_cast<T&>(value); }
180
181 struct DataArg {
182 std::vector<int> *data_ = nullptr;
183 DataArg(std::vector<int> &data) : data_(&data) {}
184 DataArg(const decltype(std::ignore)&) {}
185 DataArg() = default;
186 };
187
188 struct DataFlagArg : DataArg {
189 bool flag_ = false;
190 using DataArg::DataArg;
191 DataFlagArg(bool flag) : flag_(flag) {}
192 };
193
194 public:
195 typedef GridType Grid;
196
199 static std::unique_ptr<Grid> read (const std::string& fileName, bool verbose = true, bool insertBoundarySegments=true)
200 {
201 // make a grid factory
203
204 read(factory, fileName, verbose, insertBoundarySegments);
205
206 return factory.createGrid();
207 }
208
232 static std::unique_ptr<Grid> read (const std::string& fileName,
233 std::vector<int>& boundarySegmentToPhysicalEntity,
234 std::vector<int>& elementToPhysicalEntity,
235 bool verbose = true, bool insertBoundarySegments=true)
236 {
237 // make a grid factory
239
240 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
241 {
242 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
243 return factory.createGrid();
244 }
245
246 doRead(
247 factory, fileName, boundarySegmentToPhysicalEntity,
248 elementToPhysicalEntity, verbose, insertBoundarySegments
249 );
250
251 return factory.createGrid();
252 }
253
259 static void read (Dune::GridFactory<Grid>& factory, const std::string& fileName,
260 bool verbose = true, bool insertBoundarySegments=true)
261 {
262 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
263 {
264 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
265 return;
266 }
267
268 doRead(
269 factory, fileName, discarded(std::vector<int>{}),
270 discarded(std::vector<int>{}), verbose, insertBoundarySegments
271 );
272 }
273
275
301 static void read (Dune::GridFactory<Grid> &factory,
302 const std::string &fileName,
303 DataFlagArg boundarySegmentData,
304 DataArg elementData,
305 bool verbose=true)
306 {
307 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
308 {
309 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
310 return;
311 }
312
313 doRead(
314 factory, fileName,
315 boundarySegmentData.data_
316 ? *boundarySegmentData.data_ : discarded(std::vector<int>{}),
317 elementData.data_
318 ? *elementData.data_ : discarded(std::vector<int>{}),
319 verbose,
320 boundarySegmentData.flag_ || boundarySegmentData.data_
321 );
322 }
323
348 static void read (Dune::GridFactory<Grid>& factory,
349 const std::string& fileName,
350 std::vector<int>& boundarySegmentToPhysicalEntity,
351 std::vector<int>& elementToPhysicalEntity,
352 bool verbose, bool insertBoundarySegments)
353 {
354 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
355 {
356 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
357 return;
358 }
359
360 doRead(
361 factory, fileName, boundarySegmentToPhysicalEntity,
362 elementToPhysicalEntity, verbose, insertBoundarySegments
363 );
364 }
365
367 //\{
368
370
371 static constexpr Opts defaultOpts =
372 Opts::verbose | Opts::insertBoundarySegments | Opts::readElementData | Opts::readBoundaryData;
373
375
400 GmshReader(const std::string& fileName,
401 Gmsh::ReaderOptions options = defaultOpts)
402 {
403 gridFactory_ = std::make_unique<Dune::GridFactory<Grid>>();
404 readGridFile(fileName, *gridFactory_, options);
405 }
406
415 GmshReader(const std::string& fileName, GridFactory<Grid>& factory,
416 Gmsh::ReaderOptions options = defaultOpts)
417 {
418 readGridFile(fileName, factory, options);
419 }
420
422 const std::vector<int>& elementData () const
423 {
424 checkElementData();
425 return elementIndexToGmshPhysicalEntity_;
426 }
427
429 const std::vector<int>& boundaryData () const
430 {
431 checkBoundaryData();
432 return boundarySegmentIndexToGmshPhysicalEntity_;
433 }
434
439 bool hasElementData () const
440 { return hasElementData_ && !extractedElementData_; }
441
446 bool hasBoundaryData () const
447 { return hasBoundaryData_ && !extractedBoundaryData_; }
448
450 std::vector<int> extractElementData ()
451 {
452 checkElementData();
453 extractedElementData_ = true;
454 return std::move(elementIndexToGmshPhysicalEntity_);
455 }
456
458 std::vector<int> extractBoundaryData ()
459 {
460 checkBoundaryData();
461 extractedBoundaryData_ = true;
462 return std::move(boundarySegmentIndexToGmshPhysicalEntity_);
463 }
464
466 std::unique_ptr<Grid> createGrid ()
467 {
468 if (!gridFactory_)
470 "This GmshReader has been constructed with a Dune::GridFactory. "
471 << "This grid factory has been filled with all information to create a grid. "
472 << "Please use this factory to create the grid by calling factory.createGrid(). "
473 << "Alternatively use the constructor without passing the factory in combination with this member function."
474 );
475
476 return gridFactory_->createGrid();
477 }
478
479 //\}
480
481 private:
482 void checkElementData () const
483 {
484 if (!hasElementData_)
486 "This GmshReader has been constructed without the option 'readElementData'. "
487 << "Please enable reading element data by passing the option 'Gmsh::ReaderOpts::readElementData' "
488 << "to the constructor of this class."
489 );
490
491 if (extractedElementData_)
493 "The element data has already been extracted from this GmshReader "
494 << "via a function call to reader.extractElementData(). Use the extracted data or "
495 << "read the grid data from file again by constructing a new reader."
496 );
497 }
498
499 void checkBoundaryData () const
500 {
501 if (!hasBoundaryData_)
503 "This GmshReader has been constructed without the option 'readBoundaryData'. "
504 << "Please enable reading boundary data by passing the option 'Gmsh::ReaderOpts::readBoundaryData' "
505 << "to the constructor of this class."
506 );
507
508 if (extractedBoundaryData_)
510 "The boundary data has already been extracted from this GmshReader "
511 << "via a function call to reader.extractBoundaryData(). Use the extracted data or "
512 << "read the grid data from file again by constructing a new reader."
513 );
514 }
515
516 void readGridFile (const std::string& fileName, GridFactory<Grid>& factory, Gmsh::ReaderOptions options)
517 {
518 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
519 {
520 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
521 return;
522 }
523
524 const bool verbose = options & Opts::verbose;
525 const bool insertBoundarySegments = options & Opts::insertBoundarySegments;
526 const bool readBoundaryData = options & Opts::readBoundaryData;
527 const bool readElementData = options & Opts::readElementData;
528
529 doRead(
530 factory, fileName, boundarySegmentIndexToGmshPhysicalEntity_,
531 elementIndexToGmshPhysicalEntity_, verbose,
532 readBoundaryData || insertBoundarySegments
533 );
534
535 // clear unwanted data
536 if (!readBoundaryData)
537 boundarySegmentIndexToGmshPhysicalEntity_ = std::vector<int>{};
538 if (!readElementData)
539 elementIndexToGmshPhysicalEntity_ = std::vector<int>{};
540
541 hasElementData_ = readElementData;
542 hasBoundaryData_ = readBoundaryData;
543 }
544
545 std::unique_ptr<Dune::GridFactory<Grid>> gridFactory_;
546
547 std::vector<int> elementIndexToGmshPhysicalEntity_;
548 std::vector<int> boundarySegmentIndexToGmshPhysicalEntity_;
549
550 bool hasElementData_ = false;
551 bool hasBoundaryData_ = false;
552
553 // for better error messages, we keep track of these separately
554 bool extractedElementData_ = false;
555 bool extractedBoundaryData_ = false;
556 };
557
560} // namespace Dune
561
562#endif
int rank() const
Return rank, is between 0 and size()-1.
Definition: communication.hh:114
int barrier() const
Wait until all processes have arrived at this point in the program.
Definition: communication.hh:267
Read Gmsh mesh file.
Definition: gmshreader.hh:106
static std::unique_ptr< Grid > read(const std::string &fileName, std::vector< int > &boundarySegmentToPhysicalEntity, std::vector< int > &elementToPhysicalEntity, bool verbose=true, bool insertBoundarySegments=true)
Read Gmsh file, possibly with data.
Definition: gmshreader.hh:232
const std::vector< int > & elementData() const
Access element data (maps element index to Gmsh physical entity)
Definition: gmshreader.hh:422
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, DataFlagArg boundarySegmentData, DataArg elementData, bool verbose=true)
read Gmsh file, possibly with data
Definition: gmshreader.hh:301
static std::unique_ptr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:199
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Read Gmsh grid file into a GridFactory object.
Definition: gmshreader.hh:259
std::unique_ptr< Grid > createGrid()
Create the grid.
Definition: gmshreader.hh:466
std::vector< int > extractBoundaryData()
Erase boundary data from reader and return the data.
Definition: gmshreader.hh:458
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, std::vector< int > &boundarySegmentToPhysicalEntity, std::vector< int > &elementToPhysicalEntity, bool verbose, bool insertBoundarySegments)
Read Gmsh file, possibly with data.
Definition: gmshreader.hh:348
bool hasElementData() const
If element data is available.
Definition: gmshreader.hh:439
bool hasBoundaryData() const
If boundary data is available.
Definition: gmshreader.hh:446
GmshReader(const std::string &fileName, GridFactory< Grid > &factory, Gmsh::ReaderOptions options=defaultOpts)
Construct a Gmsh reader object from a file name and a grid factory.
Definition: gmshreader.hh:415
GmshReader(const std::string &fileName, Gmsh::ReaderOptions options=defaultOpts)
Construct a Gmsh reader object (alternatively use one of the static member functions)
Definition: gmshreader.hh:400
std::vector< int > extractElementData()
Erase element data from reader and return the data.
Definition: gmshreader.hh:450
const std::vector< int > & boundaryData() const
Access boundary data (maps boundary segment index to Gmsh physical entity)
Definition: gmshreader.hh:429
Communication comm() const
Return the Communication used by the grid factory.
Definition: gridfactory.hh:258
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:275
virtual std::unique_ptr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition: gridfactory.hh:333
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:375
Provide a generic factory class for unstructured grids.
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
ReaderOptions
Options for the Gmsh mesh file reader.
Definition: gmshreader.hh:52
Dune namespace.
Definition: alignedallocator.hh:13
Impl::Gmsh::Gmsh2Parser< GridType > GmshReaderParser
The GmshReaderParser class has been renamed and moved to the Impl::Gmsh namespace.
Definition: gmshreader.hh:86
Options for read operation.
Definition: gmshreader.hh:36
GeometryOrder
Definition: gmshreader.hh:37
@ firstOrder
edges are straight lines.
Definition: gmshreader.hh:39
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Nov 2, 23:43, 2025)