5#ifndef DUNE_GRID_IO_FILE_GMSHWRITER_HH
6#define DUNE_GRID_IO_FILE_GMSHWRITER_HH
16#include <dune/geometry/referenceelements.hh>
35 template <
class Gr
idView>
44 static_assert( (dimWorld <= 3),
"GmshWriter requires dimWorld <= 3." );
47 template<
typename Entity>
48 std::size_t nodeIndexFromEntity(
const Entity& entity,
int i)
const {
49 return gv.
indexSet().subIndex(entity, i, dim)+1;
55 static std::size_t translateDuneToGmshType(
const GeometryType& type) {
56 std::size_t element_type;
94 void outputElements(std::ofstream& file,
const std::vector<int>& physicalEntities,
const std::vector<int>& physicalBoundaries)
const {
96 std::size_t counter(1);
97 for (
const auto& entity : elements(gv)) {
100 std::size_t element_type = translateDuneToGmshType(entity.
type());
102 file << counter <<
" " << element_type;
104 if (!physicalEntities.empty())
105 file <<
" " << 1 <<
" " << physicalEntities[elementMapper.
index(entity)];
111 if (3 == element_type)
113 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
114 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2);
115 else if (5 == element_type)
117 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
118 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2) <<
" "
119 << nodeIndexFromEntity(entity, 4) <<
" " << nodeIndexFromEntity(entity, 5) <<
" "
120 << nodeIndexFromEntity(entity, 7) <<
" " << nodeIndexFromEntity(entity, 6);
121 else if (7 == element_type)
123 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
124 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2) <<
" "
125 << nodeIndexFromEntity(entity, 4);
127 for (
int k = 0; k < entity.
geometry().corners(); ++k)
128 file <<
" " << nodeIndexFromEntity(entity, k);
135 if (!physicalBoundaries.empty()) {
136 auto refElement = referenceElement<typename GridView::ctype,dim>(entity.
type());
137 for(
const auto& intersection : intersections(gv, entity)) {
138 if(intersection.boundary()) {
139 const auto faceLocalIndex(intersection.indexInInside());
140 file << counter <<
" " << translateDuneToGmshType(intersection.type())
141 <<
" " << 1 <<
" " << physicalBoundaries[intersection.boundarySegmentIndex()];
142 for (
int k = 0; k < intersection.geometry().corners(); ++k)
144 const auto vtxLocalIndex(refElement.subEntity(faceLocalIndex, 1, k, dim));
145 file <<
" " << nodeIndexFromEntity(entity, vtxLocalIndex);
167 void outputNodes(std::ofstream& file)
const {
168 for (
const auto&
vertex : vertices(gv)) {
169 const auto globalCoord =
vertex.geometry().center();
173 file << nodeIndex <<
" " << globalCoord[0] <<
" " << 0 <<
" " << 0 << std::endl;
174 else if (2 == dimWorld)
175 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << 0 << std::endl;
177 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << globalCoord[2] << std::endl;
194 precision = numDigits;
218 void write(
const std::string& fileName,
219 const std::vector<int>& physicalEntities=std::vector<int>(),
220 const std::vector<int>& physicalBoundaries=std::vector<int>())
const {
222 std::ofstream file(fileName.c_str());
227 file << std::setprecision( precision );
230 file <<
"$MeshFormat" << std::endl
231 <<
"2.0 0 " <<
sizeof(double) << std::endl
232 <<
"$EndMeshFormat" << std::endl;
235 file <<
"$Nodes" << std::endl
236 << gv.
size(dim) << std::endl;
240 file <<
"$EndNodes" << std::endl;
243 int boundariesSize(0);
244 if(!physicalBoundaries.empty())
245 for(
const auto& entity : elements(gv))
246 for(
const auto& intersection : intersections(gv, entity))
247 if(intersection.boundary())
250 file <<
"$Elements" << std::endl
251 << gv.
size(0) + boundariesSize<< std::endl;
253 outputElements(file, physicalEntities, physicalBoundaries);
255 file <<
"$EndElements" << std::endl;
Wrapper class for entities.
Definition: entity.hh:66
Geometry geometry() const
obtain geometric realization of the entity
Definition: entity.hh:141
GeometryType type() const
Return the name of the reference element. The type can be used to access the Dune::ReferenceElement.
Definition: entity.hh:146
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:304
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:299
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:309
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:279
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:289
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:284
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:294
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:314
Write Gmsh mesh file.
Definition: gmshwriter.hh:37
GmshWriter(const GridView &gridView, int numDigits=6)
Constructor expecting GridView of Grid to be written.
Definition: gmshwriter.hh:187
void setPrecision(int numDigits)
Set the number of digits to be used when writing the vertices. By default is 6.
Definition: gmshwriter.hh:193
void write(const std::string &fileName, const std::vector< int > &physicalEntities=std::vector< int >(), const std::vector< int > &physicalBoundaries=std::vector< int >()) const
Write given grid in Gmsh 2.0 compatible ASCII file.
Definition: gmshwriter.hh:218
Grid view abstract base class.
Definition: gridview.hh:66
Default exception class for I/O errors.
Definition: exceptions.hh:231
Index index(const EntityType &e) const
Map entity to starting index in array for dof block.
Definition: mcmgmapper.hh:171
Different resources needed by all grid implementations.
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:177
int size(int codim) const
obtain number of entities in a given codimension
Definition: gridview.hh:183
static constexpr int dimension
The dimension of the grid.
Definition: gridview.hh:134
static constexpr int dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:137
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
MCMGLayout mcmgElementLayout()
layout for elements (codim-0 entities)
Definition: mcmgmapper.hh:97
Mapper for multiple codim and multiple geometry types.
Dune namespace.
Definition: alignedallocator.hh:13
A unique label for each type of element that can occur in a grid.