6#ifndef DUNE_GRID_IO_FILE_GMSHREADER_HH
7#define DUNE_GRID_IO_FILE_GMSHREADER_HH
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>
54 insertBoundarySegments = 2,
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();
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) {}
199 typedef GridType Grid;
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
375 static constexpr Opts defaultOpts =
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_);
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_)
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_)
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_)
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_)
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;
529 const bool insertBoundarySegments = options & Opts::insertBoundarySegments;
530 const bool readBoundaryData = options & Opts::readBoundaryData;
531 const bool readElementData = options & Opts::readElementData;
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>{};
545 hasElementData_ = readElementData;
546 hasBoundaryData_ = readBoundaryData;
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;
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:110
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
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
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
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
Options for read operation.
Definition: gmshreader.hh:36
GeometryOrder
Definition: gmshreader.hh:37
@ firstOrder
edges are straight lines.
Definition: gmshreader.hh:39
The GmshReaderParser class has been renamed and moved to the Impl::Gmsh namespace.
Definition: gmshreader.hh:88