3#ifndef DUNE_GRID_IO_FILE_GMSHWRITER_HH
4#define DUNE_GRID_IO_FILE_GMSHWRITER_HH
36 template <
class Gr
idView>
44 dune_static_assert( (dimWorld <= 3),
"GmshWriter requires dimWorld <= 3." );
46 typedef typename GridView::template Codim<dim>::Iterator VertexIterator;
47 typedef typename GridView::template Codim<0>::Iterator ElementIterator;
51 size_t nodeIndexFromIterator(
const ElementIterator& eIt,
int i)
const {
52 return gv.
indexSet().subIndex(*eIt, i, dim)+1;
58 static size_t translateDuneToGmshType(
const GeometryType& type) {
64 else if (type.isTriangle())
66 else if (type.isQuadrilateral())
68 else if (type.isTetrahedron())
70 else if (type.isHexahedron())
72 else if (type.isPrism())
74 else if (type.isPyramid())
76 else if (type.isVertex())
92 void outputElements(std::ofstream& file)
const {
93 ElementIterator eIt = gv.template begin<0>();
94 ElementIterator eEndIt = gv.template end<0>();
96 for (
size_t i = 1; eIt != eEndIt; ++eIt, ++i) {
99 size_t element_type = translateDuneToGmshType(eIt->type());
101 file << i <<
" " << element_type <<
" " << 0;
105 if (3 == element_type)
107 << nodeIndexFromIterator(eIt, 0) <<
" " << nodeIndexFromIterator(eIt, 1) <<
" "
108 << nodeIndexFromIterator(eIt, 3) <<
" " << nodeIndexFromIterator(eIt, 2);
109 else if (5 == element_type)
111 << nodeIndexFromIterator(eIt, 0) <<
" " << nodeIndexFromIterator(eIt, 1) <<
" "
112 << nodeIndexFromIterator(eIt, 3) <<
" " << nodeIndexFromIterator(eIt, 2) <<
" "
113 << nodeIndexFromIterator(eIt, 4) <<
" " << nodeIndexFromIterator(eIt, 5) <<
" "
114 << nodeIndexFromIterator(eIt, 7) <<
" " << nodeIndexFromIterator(eIt, 6);
115 else if (7 == element_type)
117 << nodeIndexFromIterator(eIt, 0) <<
" " << nodeIndexFromIterator(eIt, 1) <<
" "
118 << nodeIndexFromIterator(eIt, 3) <<
" " << nodeIndexFromIterator(eIt, 2) <<
" "
119 << nodeIndexFromIterator(eIt, 4);
121 for (
int k = 0; k < eIt->geometry().corners(); ++k)
122 file <<
" " << nodeIndexFromIterator(eIt, k);
141 void outputNodes(std::ofstream& file)
const {
142 VertexIterator vIt = gv.template begin<dim>();
143 VertexIterator vEndIt = gv.template end<dim>();
145 for (; vIt != vEndIt; ++vIt) {
146 typename VertexIterator::Entity::Geometry::GlobalCoordinate globalCoord = vIt->geometry().center();
147 int nodeIndex = gv.
indexSet().index(*vIt)+1;
150 file << nodeIndex <<
" " << globalCoord[0] <<
" " << 0 <<
" " << 0 << std::endl;
151 else if (2 == dimWorld)
152 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << 0 << std::endl;
154 file << nodeIndex <<
" " << globalCoord[0] <<
" " << globalCoord[1] <<
" " << globalCoord[2] << std::endl;
176 void write(
const std::string& fileName)
const {
177 std::ofstream file(fileName.c_str());
183 file <<
"$MeshFormat" << std::endl
184 <<
"2.0 0 " <<
sizeof(double) << std::endl
185 <<
"$EndMeshFormat" << std::endl;
189 const size_t number_of_nodes = gv.
size(dim);
190 file <<
"$Nodes" << std::endl
191 << number_of_nodes << std::endl;
195 file <<
"$EndNodes" << std::endl;
199 const size_t number_of_elements = gv.
size(0);
200 file <<
"$Elements" << std::endl
201 << number_of_elements << std::endl;
203 outputElements(file);
205 file <<
"$EndElements" << std::endl;
Base class for Dune-Exceptions.
Definition: exceptions.hh:92
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:25
Write Gmsh mesh file.
Definition: gmshwriter.hh:38
void write(const std::string &fileName) const
Write given grid in Gmsh 2.0 compatible ASCII file.
Definition: gmshwriter.hh:176
GmshWriter(const GridView &gridView)
Constructor expecting GridView of Grid to be written.
Definition: gmshwriter.hh:163
Grid view abstract base class.
Definition: gridview.hh:57
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:158
int size(int codim) const
obtain number of entities in a given codimension
Definition: gridview.hh:164
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:124
@ dimension
The dimension of the grid.
Definition: gridview.hh:120
Default exception class for I/O errors.
Definition: exceptions.hh:257
Different resources needed by all grid implementations.
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Dune namespace.
Definition: alignment.hh:14
A unique label for each type of element that can occur in a grid.