6#ifndef DUNE_GRID_IO_FILE_GMSHREADER_HH
7#define DUNE_GRID_IO_FILE_GMSHREADER_HH
15#include <dune/common/exceptions.hh>
63 static_cast<int>(a) |
static_cast<int>(b)
70 return static_cast<int>(a) &
static_cast<int>(b);
85 template <
typename Gr
idType>
86 struct [[deprecated(
"Public interface of the GmshReaderParser has been deprecated since dune 2.11.")]]
89 using Impl::Gmsh::Gmsh2Parser<GridType>::Gmsh2Parser;
108 template<
typename Gr
idType>
132 const std::string &fileName,
133 std::vector<int>& boundarySegmentToPhysicalEntity,
134 std::vector<int>& elementToPhysicalEntity,
135 bool verbose,
bool insertBoundarySegments)
139 Impl::Gmsh::GmshReaderQuadraticBoundarySegment< Grid::dimension, Grid::dimensionworld >::registerFactory();
143 factory.
comm().barrier();
147 if (factory.
comm().rank() == 0)
149 Impl::Gmsh::Gmsh2Parser<Grid> parser(factory,verbose,insertBoundarySegments);
150 parser.read(fileName);
152 boundarySegmentToPhysicalEntity = std::move(parser.boundaryIdMap());
153 elementToPhysicalEntity = std::move(parser.elementIndexMap());
157 boundarySegmentToPhysicalEntity = {};
158 elementToPhysicalEntity = {};
183 static T &discarded(T &&value) {
return static_cast<T&
>(value); }
186 std::vector<int> *data_ =
nullptr;
187 DataArg(std::vector<int> &data) : data_(&data) {}
188 DataArg(
const decltype(std::ignore)&) {}
192 struct DataFlagArg : DataArg {
194 using DataArg::DataArg;
195 DataFlagArg(
bool flag) : flag_(flag) {}
203 static std::unique_ptr<Grid>
read (
const std::string& fileName,
bool verbose =
true,
bool insertBoundarySegments=
true)
208 read(factory, fileName, verbose, insertBoundarySegments);
236 static std::unique_ptr<Grid>
read (
const std::string& fileName,
237 std::vector<int>& boundarySegmentToPhysicalEntity,
238 std::vector<int>& elementToPhysicalEntity,
239 bool verbose =
true,
bool insertBoundarySegments=
true)
244 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
246 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
251 factory, fileName, boundarySegmentToPhysicalEntity,
252 elementToPhysicalEntity, verbose, insertBoundarySegments
264 bool verbose =
true,
bool insertBoundarySegments=
true)
266 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
268 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
273 factory, fileName, discarded(std::vector<int>{}),
274 discarded(std::vector<int>{}), verbose, insertBoundarySegments
306 const std::string &fileName,
307 DataFlagArg boundarySegmentData,
311 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
313 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
319 boundarySegmentData.data_
320 ? *boundarySegmentData.data_ : discarded(std::vector<int>{}),
322 ? *
elementData.data_ : discarded(std::vector<int>{}),
324 boundarySegmentData.flag_ || boundarySegmentData.data_
353 const std::string& fileName,
354 std::vector<int>& boundarySegmentToPhysicalEntity,
355 std::vector<int>& elementToPhysicalEntity,
356 bool verbose,
bool insertBoundarySegments)
358 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
360 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
365 factory, fileName, boundarySegmentToPhysicalEntity,
366 elementToPhysicalEntity, verbose, insertBoundarySegments
376 Opts::verbose | Opts::insertBoundarySegments | Opts::readElementData | Opts::readBoundaryData;
407 gridFactory_ = std::make_unique<Dune::GridFactory<Grid>>();
408 readGridFile(fileName, *gridFactory_, options);
422 readGridFile(fileName, factory, options);
429 return elementIndexToGmshPhysicalEntity_;
436 return boundarySegmentIndexToGmshPhysicalEntity_;
444 {
return hasElementData_ && !extractedElementData_; }
451 {
return hasBoundaryData_ && !extractedBoundaryData_; }
457 extractedElementData_ =
true;
458 return std::move(elementIndexToGmshPhysicalEntity_);
465 extractedBoundaryData_ =
true;
466 return std::move(boundarySegmentIndexToGmshPhysicalEntity_);
473 DUNE_THROW(Dune::InvalidStateException,
474 "This GmshReader has been constructed with a Dune::GridFactory. "
475 <<
"This grid factory has been filled with all information to create a grid. "
476 <<
"Please use this factory to create the grid by calling factory.createGrid(). "
477 <<
"Alternatively use the constructor without passing the factory in combination with this member function."
480 return gridFactory_->createGrid();
486 void checkElementData ()
const
488 if (!hasElementData_)
489 DUNE_THROW(Dune::InvalidStateException,
490 "This GmshReader has been constructed without the option 'readElementData'. "
491 <<
"Please enable reading element data by passing the option 'Gmsh::ReaderOpts::readElementData' "
492 <<
"to the constructor of this class."
495 if (extractedElementData_)
496 DUNE_THROW(Dune::InvalidStateException,
497 "The element data has already been extracted from this GmshReader "
498 <<
"via a function call to reader.extractElementData(). Use the extracted data or "
499 <<
"read the grid data from file again by constructing a new reader."
503 void checkBoundaryData ()
const
505 if (!hasBoundaryData_)
506 DUNE_THROW(Dune::InvalidStateException,
507 "This GmshReader has been constructed without the option 'readBoundaryData'. "
508 <<
"Please enable reading boundary data by passing the option 'Gmsh::ReaderOpts::readBoundaryData' "
509 <<
"to the constructor of this class."
512 if (extractedBoundaryData_)
513 DUNE_THROW(Dune::InvalidStateException,
514 "The boundary data has already been extracted from this GmshReader "
515 <<
"via a function call to reader.extractBoundaryData(). Use the extracted data or "
516 <<
"read the grid data from file again by constructing a new reader."
520 void readGridFile (
const std::string& fileName, GridFactory<Grid>& factory,
Gmsh::ReaderOptions options)
522 if (Impl::Gmsh::fileVersion(fileName)[0]==4)
524 Impl::Gmsh::Gmsh4Reader<Grid>::fillFactory(factory, fileName);
528 const bool verbose = options & Opts::verbose;
534 factory, fileName, boundarySegmentIndexToGmshPhysicalEntity_,
535 elementIndexToGmshPhysicalEntity_, verbose,
536 readBoundaryData || insertBoundarySegments
540 if (!readBoundaryData)
541 boundarySegmentIndexToGmshPhysicalEntity_ = std::vector<int>{};
542 if (!readElementData)
543 elementIndexToGmshPhysicalEntity_ = std::vector<int>{};
549 std::unique_ptr<Dune::GridFactory<Grid>> gridFactory_;
551 std::vector<int> elementIndexToGmshPhysicalEntity_;
552 std::vector<int> boundarySegmentIndexToGmshPhysicalEntity_;
554 bool hasElementData_ =
false;
555 bool hasBoundaryData_ =
false;
558 bool extractedElementData_ =
false;
559 bool extractedBoundaryData_ =
false;
ReaderOptions
Options for the Gmsh mesh file reader.
Definition gmshreader.hh:52
@ verbose
Definition gmshreader.hh:53
@ readBoundaryData
Definition gmshreader.hh:56
@ readElementData
Definition gmshreader.hh:55
@ insertBoundarySegments
Definition gmshreader.hh:54
Include standard header files.
Definition agrid.hh:60
Definition gmshreader.hh:45
constexpr bool operator&(ReaderOptions a, ReaderOptions b)
query operator for reader options (is b set in a)
Definition gmshreader.hh:68
constexpr ReaderOptions operator|(ReaderOptions a, ReaderOptions b)
composition operator for reader options
Definition gmshreader.hh:60
Communication comm() const
Return the Communication used by the grid factory.
Definition common/gridfactory.hh:258
Provide a generic factory class for unstructured grids.
Definition common/gridfactory.hh:275
virtual std::unique_ptr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition common/gridfactory.hh:333
Options for read operation.
Definition gmshreader.hh:36
GeometryOrder
Definition gmshreader.hh:37
@ firstOrder
edges are straight lines.
Definition gmshreader.hh:39
@ secondOrder
quadratic boundary approximation.
Definition gmshreader.hh:41
The GmshReaderParser class has been renamed and moved to the Impl::Gmsh namespace.
Definition gmshreader.hh:88
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:236
const std::vector< int > & elementData() const
Access element data (maps element index to Gmsh physical entity).
Definition gmshreader.hh:426
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:305
static std::unique_ptr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition gmshreader.hh:203
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:263
Gmsh::ReaderOptions Opts
Dynamic Gmsh reader interface.
Definition gmshreader.hh:373
GridType Grid
Definition gmshreader.hh:199
std::unique_ptr< Grid > createGrid()
Create the grid.
Definition gmshreader.hh:470
std::vector< int > extractBoundaryData()
Erase boundary data from reader and return the data.
Definition gmshreader.hh:462
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:352
bool hasElementData() const
If element data is available.
Definition gmshreader.hh:443
bool hasBoundaryData() const
If boundary data is available.
Definition gmshreader.hh:450
static constexpr Opts defaultOpts
Definition gmshreader.hh:375
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:419
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:404
std::vector< int > extractElementData()
Erase element data from reader and return the data.
Definition gmshreader.hh:454
const std::vector< int > & boundaryData() const
Access boundary data (maps boundary segment index to Gmsh physical entity).
Definition gmshreader.hh:433
Provide a generic factory class for unstructured grids.