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)
75 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
81 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
85 mNodeFile.open(mFileName.c_str());
86 mElementFile.open(mFileName.c_str());
87 mFaceFile.open(mFileName.c_str());
88 if (!mNodeFile.is_open() || !mElementFile.is_open() || !mFaceFile.is_open() )
90 EXCEPTION(
"Could not open data file: " + mFileName);
94 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
102 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
108 std::string this_line;
109 getline(mNodeFile, this_line);
111 assert(this_line ==
"$MeshFormat");
114 getline(mNodeFile, this_line);
115 std::stringstream line(this_line);
117 line >> mVersionNumber >> mFileType >> mDataSize;
119 if (mVersionNumber != 2.2)
121 EXCEPTION(
"Only .msh version 2.2 files are supported.");
123 assert(mFileType == 0);
126 getline(mNodeFile, this_line);
127 assert(this_line ==
"$EndMeshFormat");
133 if (mTotalNumElementsAndFaces != mNumElements + mNumFaces)
135 EXCEPTION(
"Unrecognised element types present in the .msh file: check mesh generation settings in gmsh.");
145 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
149 std::string this_line;
150 std::stringstream line(this_line);
152 while (this_line !=
"$Nodes")
154 getline(mNodeFile, this_line);
156 getline(mNodeFile, this_line);
163 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
167 std::string this_line;
168 std::stringstream line(this_line);
170 getline(mElementFile, this_line);
171 while (this_line !=
"$Elements")
173 getline(mElementFile, this_line);
175 getline(mElementFile, this_line);
178 line >> mTotalNumElementsAndFaces;
179 int ele_start = mElementFile.tellg();
182 getline(mElementFile, this_line);
184 while (this_line !=
"$EndElements")
191 line >> ele_index >> ele_type;
193 if (ELEMENT_DIM == 2 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE))
197 else if (ELEMENT_DIM == 3 && (ele_type == GmshTypes::TETRAHEDRON || ele_type == GmshTypes::QUADRATIC_TETRAHEDRON))
202 getline(mElementFile, this_line);
205 mElementFile.seekg(ele_start);
208 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
212 std::string this_line;
213 std::stringstream line(this_line);
215 getline(mFaceFile, this_line);
216 while (this_line !=
"$Elements")
218 getline(mFaceFile, this_line);
220 getline(mFaceFile, this_line);
221 int face_start = mFaceFile.tellg();
224 getline(mFaceFile, this_line);
226 while (this_line !=
"$EndElements")
233 line >> ele_index >> ele_type;
235 if (ELEMENT_DIM == 2 && (ele_type == GmshTypes::LINE || ele_type == GmshTypes::QUADRATIC_LINE))
239 else if (ELEMENT_DIM == 3 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE))
244 getline(mFaceFile, this_line);
247 mFaceFile.seekg(face_start);
250 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
256 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
262 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
269 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
277 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
280 return mOrderOfElements;
283 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
286 return mOrderOfBoundaryElements;
289 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
292 return mNumElementAttributes;
295 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
298 return mNumFaceAttributes;
302 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
310 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
319 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
322 std::vector<double> ret_coords(SPACE_DIM);
324 std::string this_line;
325 getline(mNodeFile, this_line);
327 std::stringstream line(this_line);
330 line >> node_index >> ret_coords[0] >> ret_coords[1];
334 line >> ret_coords[2];
340 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
343 return std::vector<double>(0);
346 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
353 std::string this_line;
354 std::stringstream line(this_line);
358 unsigned ele_attributes = 0;
359 bool volume_element_found =
false;
361 while (!volume_element_found)
363 getline(mElementFile, this_line);
367 line >> ele_index >> ele_type >> ele_attributes;
369 if ((ELEMENT_DIM == 2 && (ele_type == GmshTypes::TRIANGLE || ele_type == GmshTypes::QUADRATIC_TRIANGLE) ) ||
370 (ELEMENT_DIM == 3 && (ele_type == GmshTypes::TETRAHEDRON || ele_type == GmshTypes::QUADRATIC_TETRAHEDRON)))
372 volume_element_found =
true;
378 if (ele_attributes > 0)
380 mNumElementAttributes = 1u;
383 unsigned unused_attr;
384 for (
unsigned attr_index = 0; attr_index < (ele_attributes-1); ++attr_index)
390 for (
unsigned node_index = 0; node_index < mNodesPerElement; ++node_index)
401 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
408 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
412 face_data.
NodeIndices.resize(mNodesPerBoundaryElement);
415 std::string this_line;
416 std::stringstream line(this_line);
420 unsigned face_attributes=0;
421 bool surface_element_found =
false;
423 while (!surface_element_found)
425 getline(mFaceFile, this_line);
429 line >> face_index >> face_type >> face_attributes;
431 if ((ELEMENT_DIM == 2 && (face_type == GmshTypes::LINE || face_type == GmshTypes::QUADRATIC_LINE) ) ||
432 (ELEMENT_DIM == 3 && (face_type == GmshTypes::TRIANGLE || face_type == GmshTypes::QUADRATIC_TRIANGLE)))
434 surface_element_found =
true;
440 if (face_attributes > 0)
442 mNumFaceAttributes = 1u;
445 unsigned unused_attr;
446 for (
unsigned attr_index = 0; attr_index < (face_attributes-1); ++attr_index)
452 for (
unsigned node_index = 0; node_index < mNodesPerBoundaryElement; ++node_index)
463 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
471 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
479 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()