3#ifndef DUNE_GRID_IO_FILE_GMSHWRITER_HH
4#define DUNE_GRID_IO_FILE_GMSHWRITER_HH
14#include <dune/geometry/referenceelements.hh>
33 template <
class Gr
idView>
42 static_assert( (dimWorld <= 3),
"GmshWriter requires dimWorld <= 3." );
45 template<
typename Entity>
46 std::size_t nodeIndexFromEntity(
const Entity& entity,
int i)
const {
47 return gv.
indexSet().subIndex(entity, i, dim)+1;
53 static std::size_t translateDuneToGmshType(
const GeometryType& type) {
54 std::size_t element_type;
92 void outputElements(std::ofstream& file,
const std::vector<int>& physicalEntities,
const std::vector<int>& physicalBoundaries)
const {
94 std::size_t counter(1);
95 for (
const auto& entity : elements(gv)) {
98 std::size_t element_type = translateDuneToGmshType(entity.
type());
100 file << counter <<
" " << element_type;
102 if (!physicalEntities.empty())
103 file <<
" " << 1 <<
" " << physicalEntities[elementMapper.
index(entity)];
109 if (3 == element_type)
111 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
112 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2);
113 else if (5 == element_type)
115 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
116 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2) <<
" "
117 << nodeIndexFromEntity(entity, 4) <<
" " << nodeIndexFromEntity(entity, 5) <<
" "
118 << nodeIndexFromEntity(entity, 7) <<
" " << nodeIndexFromEntity(entity, 6);
119 else if (7 == element_type)
121 << nodeIndexFromEntity(entity, 0) <<
" " << nodeIndexFromEntity(entity, 1) <<
" "
122 << nodeIndexFromEntity(entity, 3) <<
" " << nodeIndexFromEntity(entity, 2) <<
" "
123 << nodeIndexFromEntity(entity, 4);
125 for (
int k = 0; k < entity.
geometry().corners(); ++k)
126 file <<
" " << nodeIndexFromEntity(entity, k);
133 if (!physicalBoundaries.empty()) {
135 for(
const auto& intersection : intersections(gv, entity)) {
136 if(intersection.boundary()) {
137 const auto faceLocalIndex(intersection.indexInInside());
138 file << counter <<
" " << translateDuneToGmshType(intersection.type())
139 <<
" " << 1 <<
" " << physicalBoundaries[intersection.boundarySegmentIndex()];
140 for (
int k = 0; k < intersection.geometry().corners(); ++k)
142 const auto vtxLocalIndex(refElement.subEntity(faceLocalIndex, 1, k, dim));
143 file <<
" " << nodeIndexFromEntity(entity, vtxLocalIndex);
165 void outputNodes(std::ofstream& file)
const {
166 for (
const auto& vertex : vertices(gv)) {
167 const auto globalCoord = vertex.geometry().center();
168 const auto nodeIndex = gv.
indexSet().index(vertex)+1;
171 file << nodeIndex <<
" " << globalCoord[0] <<
" " << 0 <<
" " << 0 << std::endl;
172 else if (2 == dimWorld)
173 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << 0 << std::endl;
175 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << globalCoord[2] << std::endl;
192 precision = numDigits;
216 void write(
const std::string& fileName,
217 const std::vector<int>& physicalEntities=std::vector<int>(),
218 const std::vector<int>& physicalBoundaries=std::vector<int>())
const {
220 std::ofstream file(fileName.c_str());
225 file << std::setprecision( precision );
228 file <<
"$MeshFormat" << std::endl
229 <<
"2.0 0 " <<
sizeof(double) << std::endl
230 <<
"$EndMeshFormat" << std::endl;
233 file <<
"$Nodes" << std::endl
234 << gv.
size(dim) << std::endl;
238 file <<
"$EndNodes" << std::endl;
241 int boundariesSize(0);
242 if(!physicalBoundaries.empty())
243 for(
const auto& entity : elements(gv))
244 for(
const auto& intersection : intersections(gv, entity))
245 if(intersection.boundary())
248 file <<
"$Elements" << std::endl
249 << gv.
size(0) + boundariesSize<< std::endl;
251 outputElements(file, physicalEntities, physicalBoundaries);
253 file <<
"$EndElements" << std::endl;
Wrapper class for entities.
Definition: entity.hh:65
Geometry geometry() const
obtain geometric realization of the entity
Definition: entity.hh:145
GeometryType type() const
Return the name of the reference element. The type can be used to access the Dune::ReferenceElement.
Definition: entity.hh:150
Base class for Dune-Exceptions.
Definition: exceptions.hh:94
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:268
bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:540
bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:515
bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:535
bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:530
bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:510
bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:545
bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:525
bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:520
Write Gmsh mesh file.
Definition: gmshwriter.hh:35
GmshWriter(const GridView &gridView, int numDigits=6)
Constructor expecting GridView of Grid to be written.
Definition: gmshwriter.hh:185
void setPrecision(int numDigits)
Set the number of digits to be used when writing the vertices. By default is 6.
Definition: gmshwriter.hh:191
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:216
Grid view abstract base class.
Definition: gridview.hh:60
Default exception class for I/O errors.
Definition: exceptions.hh:229
Implementation class for a multiple codim and multiple geometry type mapper.
Definition: mcmgmapper.hh:103
Index index(const EntityType &e) const
Map entity to array index.
Definition: mcmgmapper.hh:146
Different resources needed by all grid implementations.
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:173
int size(int codim) const
obtain number of entities in a given codimension
Definition: gridview.hh:179
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:132
@ dimension
The dimension of the grid.
Definition: gridview.hh:128
Mapper for multiple codim and multiple geometry types.
Dune namespace.
Definition: alignment.hh:11
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:753
A unique label for each type of element that can occur in a grid.