39 #include "GmshMeshReader.hpp"
42 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
44 unsigned orderOfElements,
45 unsigned orderOfBoundaryElements) :
46 mFileName(pathBaseName),
47 mOrderOfElements(orderOfElements),
48 mOrderOfBoundaryElements(orderOfBoundaryElements)
50 assert(SPACE_DIM==ELEMENT_DIM);
77 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
83 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
87 mNodeFile.open(mFileName.c_str());
88 mElementFile.open(mFileName.c_str());
89 mFaceFile.open(mFileName.c_str());
90 if (!mNodeFile.is_open() || !mElementFile.is_open() || !mFaceFile.is_open() )
92 EXCEPTION(
"Could not open data file: " + mFileName);
96 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
100 mElementFile.close();
105 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
111 std::string this_line;
112 getline(mNodeFile, this_line);
114 assert(this_line ==
"$MeshFormat");
117 getline(mNodeFile, this_line);
118 std::stringstream line(this_line);
120 line >> mVersionNumber >> mFileType >> mDataSize;
122 if(mVersionNumber != 2.2)
124 EXCEPTION(
"Only .msh version 2.2 files are supported.");
126 assert(mFileType == 0);
129 getline(mNodeFile, this_line);
130 assert(this_line ==
"$EndMeshFormat");
136 if (mTotalNumElementsAndFaces != mNumElements + mNumFaces)
138 EXCEPTION(
"Unrecognised element types present in the .msh file: check mesh generation settings in gmsh.");
149 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
153 std::string this_line;
154 std::stringstream line(this_line);
156 while(this_line !=
"$Nodes")
158 getline(mNodeFile, this_line);
160 getline(mNodeFile, this_line);
167 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
171 std::string this_line;
172 std::stringstream line(this_line);
174 getline(mElementFile, this_line);
175 while(this_line !=
"$Elements")
177 getline(mElementFile, this_line);
179 getline(mElementFile, this_line);
182 line >> mTotalNumElementsAndFaces;
183 int ele_start = mElementFile.tellg();
186 getline(mElementFile, this_line);
188 while(this_line !=
"$EndElements")
195 line >> ele_index >> ele_type;
197 if(ELEMENT_DIM == 2 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE))
201 else if(ELEMENT_DIM == 3 && (ele_type == GmshTypes::TETRAHEDRON || ele_type == GmshTypes::QUADRATIC_TETRAHEDRON))
206 getline(mElementFile, this_line);
209 mElementFile.seekg(ele_start);
212 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
216 std::string this_line;
217 std::stringstream line(this_line);
219 getline(mFaceFile, this_line);
220 while(this_line !=
"$Elements")
222 getline(mFaceFile, this_line);
224 getline(mFaceFile, this_line);
225 int face_start = mFaceFile.tellg();
228 getline(mFaceFile, this_line);
230 while(this_line !=
"$EndElements")
237 line >> ele_index >> ele_type;
239 if(ELEMENT_DIM == 2 && (ele_type == GmshTypes::LINE || ele_type == GmshTypes::QUADRATIC_LINE))
243 else if(ELEMENT_DIM == 3 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE))
248 getline(mFaceFile, this_line);
251 mFaceFile.seekg(face_start);
254 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
260 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
266 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
272 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
279 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
282 return mOrderOfElements;
285 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
288 return mOrderOfBoundaryElements;
291 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
294 return mNumElementAttributes;
297 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
300 return mNumFaceAttributes;
303 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
311 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
320 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
323 std::vector<double> ret_coords(SPACE_DIM);
325 std::string this_line;
326 getline(mNodeFile, this_line);
328 std::stringstream line(this_line);
331 line >> node_index >> ret_coords[0] >> ret_coords[1];
335 line >> ret_coords[2];
341 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
344 return std::vector<double>(0);
347 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
354 std::string this_line;
355 std::stringstream line(this_line);
359 unsigned ele_attributes=0;
360 bool volume_element_found =
false;
362 while(!volume_element_found)
364 getline(mElementFile, this_line);
368 line >> ele_index >> ele_type >> ele_attributes;
370 if((ELEMENT_DIM == 2 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE) ) ||
371 (ELEMENT_DIM == 3 && (ele_type == GmshTypes::TETRAHEDRON || ele_type == GmshTypes::QUADRATIC_TETRAHEDRON)))
373 volume_element_found =
true;
379 if(ele_attributes > 0)
381 mNumElementAttributes = 1u;
384 unsigned unused_attr;
385 for(
unsigned attr_index = 0; attr_index < (ele_attributes-1); ++attr_index)
391 for(
unsigned node_index = 0; node_index < mNodesPerElement; ++node_index)
401 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
407 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
411 face_data.
NodeIndices.resize(mNodesPerBoundaryElement);
414 std::string this_line;
415 std::stringstream line(this_line);
419 unsigned face_attributes=0;
420 bool surface_element_found =
false;
422 while(!surface_element_found)
424 getline(mFaceFile, this_line);
428 line >> face_index >> face_type >> face_attributes;
430 if((ELEMENT_DIM == 2 && (face_type == GmshTypes::LINE || face_type == GmshTypes::QUADRATIC_LINE) ) ||
431 (ELEMENT_DIM == 3 && (face_type == GmshTypes::TRIANGLE || face_type == GmshTypes::QUADRATIC_TRIANGLE)))
433 surface_element_found =
true;
439 if(face_attributes > 0)
441 mNumFaceAttributes = 1u;
444 unsigned unused_attr;
445 for(
unsigned attr_index = 0; attr_index < (face_attributes-1); ++attr_index)
451 for(
unsigned node_index = 0; node_index < mNodesPerBoundaryElement; ++node_index)
461 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
467 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
473 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
unsigned GetNumElements() const
unsigned GetOrderOfBoundaryElements()
unsigned GetNumFaceAttributes() const
unsigned mOrderOfBoundaryElements
ElementData GetFaceData(unsigned index)
ElementData GetNextCableElementData()
unsigned mNodesPerBoundaryElement
unsigned GetOrderOfElements()
ElementData GetNextFaceData()
#define EXCEPTION(message)
ElementData GetNextElementData()
std::vector< unsigned > NodeIndices
unsigned GetNumFaces() const
std::vector< double > GetNodeAttributes()
ElementData GetElementData(unsigned index)
GmshMeshReader(std::string pathBaseName, unsigned orderOfElements=1, unsigned orderOfBoundaryElements=1)
std::vector< double > GetNode(unsigned index)
unsigned GetNumCableElements() const
unsigned GetNumElementAttributes() const
unsigned mOrderOfElements
unsigned GetNumCableElementAttributes() const
unsigned GetNumNodes() const
unsigned mNodesPerElement
std::vector< double > GetNextNode()