55 unsigned orderOfElements,
56 unsigned orderOfBoundaryElements,
57 bool readContainingElementForBoundaryElements)
58 : mFilesBaseName(pathBaseName),
68 mCableElementsRead(0),
70 mBoundaryFacesRead(0),
72 mNumNodeAttributes(0),
73 mNumElementAttributes(0),
74 mNumFaceAttributes(0),
75 mNumCableElementAttributes(0),
76 mOrderOfElements(orderOfElements),
77 mOrderOfBoundaryElements(orderOfBoundaryElements),
79 mReadContainingElementOfBoundaryElement(readContainingElementForBoundaryElements),
80 mFilesAreBinary(false),
81 mMeshIsHexahedral(false),
82 mNodeFileReadBuffer(nullptr),
83 mElementFileReadBuffer(nullptr),
84 mFaceFileReadBuffer(nullptr),
85 mNodePermutationDefined(false)
88 assert(orderOfElements==1 || orderOfElements==2);
91 EXCEPTION(
"Boundary element file should not have containing element info if it is quadratic");
99 assert(SPACE_DIM==ELEMENT_DIM);
109 assert(SPACE_DIM==ELEMENT_DIM);
205 std::vector<double> element_attributes;
206 GetNextItemFromStream(mElementsFile, mElementsRead, element_data.
NodeIndices, mNumElementAttributes, element_attributes);
208 if (mNumElementAttributes > 0)
217 if (mNodePermutationDefined)
219 for (std::vector<unsigned>::iterator node_it = element_data.
NodeIndices.begin();
223 assert(*node_it < mPermutationVector.size());
224 *node_it = mPermutationVector[*node_it];
268 std::vector<unsigned> ret_indices;
271 if (ELEMENT_DIM == 1)
273 ret_indices.push_back( mOneDimBoundary[mBoundaryFacesRead] );
277 ret_indices.resize(mNodesPerBoundaryElement);
279 assert(ELEMENT_DIM != 0);
284 std::vector<double> face_attributes;
285 if (mReadContainingElementOfBoundaryElement)
287 assert(mNumFaceAttributes == 0);
288 GetNextItemFromStream(mFacesFile, mFacesRead, ret_indices, 1, face_attributes);
290 if (face_attributes.size() > 0)
298 GetNextItemFromStream(mFacesFile, mFacesRead, ret_indices, mNumFaceAttributes,
301 if (mNumFaceAttributes > 0)
307 EnsureIndexingFromZero(ret_indices);
314 mBoundaryFacesRead++;
316 if (mNodePermutationDefined)
318 for (std::vector<unsigned>::iterator node_it = ret_indices.begin();
319 node_it != ret_indices.end();
322 assert(*node_it < mPermutationVector.size());
323 *node_it = mPermutationVector[*node_it];
432 if (!mFilesAreBinary)
434 EXCEPTION(
"NCL file functionality is only implemented in mesh readers for binary mesh files.");
437 if (!mNclFileAvailable)
439 EXCEPTION(
"No NCL file available for this mesh.");
441 if (index >= mNumNodes)
443 EXCEPTION(
"Connectivity list does not exist - not enough nodes.");
446 if (mNodePermutationDefined)
448 assert(index < mInversePermutationVector.size());
449 index = mInversePermutationVector[index];
453 if (index > mNclItemsRead)
457 mNclFile.seekg( mNclItemWidth*(index-mNclItemsRead), std::ios_base::cur);
459 else if ( mNclItemsRead != index )
461 mNclFile.seekg(mNclFileDataStart + mNclItemWidth*index, std::ios_base::beg);
465 std::vector<unsigned> containing_element_indices;
466 containing_element_indices.resize(mMaxContainingElements);
468 std::vector<double> dummy;
469 GetNextItemFromStream(mNclFile, index, containing_element_indices, 0, dummy);
470 mNclItemsRead = index + 1;
472 EnsureIndexingFromZero(containing_element_indices);
474 unsigned num_containing_elements = mMaxContainingElements;
475 while ( containing_element_indices[num_containing_elements-1] == UINT_MAX )
477 num_containing_elements--;
480 containing_element_indices.resize(num_containing_elements);
482 return containing_element_indices;
599 GetNextLineFromStream(mNodesFile, buffer);
600 std::stringstream node_header_line(buffer);
602 node_header_line >> mNumNodes >> dimension >> mNumNodeAttributes >> mMaxNodeBdyMarker;
603 if (SPACE_DIM != dimension)
605 EXCEPTION(
"SPACE_DIM != dimension read from file ");
610 node_header_line >> extras;
613 mFilesAreBinary =
true;
614 mNodeFileDataStart = mNodesFile.tellg();
615 mNodeItemWidth = SPACE_DIM *
sizeof(
double);
618 mIndexFromZero =
true;
623 assert(!mNclFileAvailable);
626 GetNextLineFromStream(mNodesFile, buffer);
627 std::stringstream node_first_line(buffer);
628 unsigned first_index;
629 node_first_line >> first_index;
630 assert(first_index == 0 || first_index == 1);
631 mIndexFromZero = (first_index == 0);
636 GetNextLineFromStream(mNodesFile, buffer);
642 GetNextLineFromStream(mElementsFile, buffer);
643 std::stringstream element_header_line(buffer);
645 unsigned extra_attributes = 0;
647 if (ELEMENT_DIM == SPACE_DIM)
649 element_header_line >> mNumElements >> mNumElementNodes >> mNumElementAttributes;
651 extra_attributes = mNumElementAttributes;
654 std::string element_extras;
655 element_header_line >> element_extras;
656 if (element_extras ==
"BIN")
659 assert (mFilesAreBinary);
661 else if (element_extras ==
"HEX")
663 mMeshIsHexahedral =
true;
664 if (ELEMENT_DIM == 2)
666 mNodesPerElement = 4;
667 mNodesPerBoundaryElement = 2;
669 if (ELEMENT_DIM == 3)
671 mNodesPerElement = 8;
672 mNodesPerBoundaryElement = 4;
677 assert (element_extras ==
"");
681 if (mNumElements != 0)
683 if (mNumElementNodes != mNodesPerElement)
685 EXCEPTION(
"Number of nodes per elem, " << mNumElementNodes <<
", does not match "
686 <<
"expected number, " << mNodesPerElement <<
" (which is calculated given "
687 <<
"the order of elements chosen, " << mOrderOfElements <<
" (1=linear, 2=quadratics)");
694 element_header_line >> mNumElements >> mNumFaceAttributes;
696 extra_attributes = mNumFaceAttributes;
698 if (ELEMENT_DIM == 1 || ELEMENT_DIM == 2)
700 mNumElementAttributes = mNumFaceAttributes;
704 std::string element_extras;
705 element_header_line >> element_extras;
706 if (element_extras ==
"BIN")
709 assert (mFilesAreBinary);
712 mNodesPerElement = ELEMENT_DIM+1;
717 mElementFileDataStart = mElementsFile.tellg();
718 mElementItemWidth = mNodesPerElement*
sizeof(
unsigned) + extra_attributes*
sizeof(
double);
725 if (mNumElements != 0)
727 if (ELEMENT_DIM == 1)
730 mNumFaces = mOneDimBoundary.size();
734 GetNextLineFromStream(mFacesFile, buffer);
735 std::stringstream face_header_line(buffer);
737 face_header_line >> mNumFaces >> mNumFaceAttributes;
738 assert(mNumFaceAttributes==0 || mNumFaceAttributes==1);
745 std::string face_extras;
746 face_header_line >> face_extras;
747 assert (mFilesAreBinary == (face_extras ==
"BIN"));
748 if (mNumFaceAttributes==1)
750 unsigned num_boundary_faces = 0;
751 bool end_of_file=
false;
757 num_boundary_faces++;
771 mNumFaces = num_boundary_faces;
784 GetNextLineFromStream(mFacesFile, buffer);
786 mBoundaryFacesRead = 0;
793 mFaceFileDataStart = mFacesFile.tellg();
794 mFaceItemWidth = ELEMENT_DIM*
sizeof(
unsigned) + mNumFaceAttributes*
sizeof(
double);
800 if (mNclFileAvailable)
802 GetNextLineFromStream(mNclFile, buffer);
803 std::stringstream ncl_header_line(buffer);
804 unsigned num_nodes_in_file;
805 ncl_header_line >> num_nodes_in_file >> mMaxContainingElements;
807 if (mNumNodes != num_nodes_in_file)
809 EXCEPTION(
"NCL file does not contain the correct number of nodes for mesh");
812 mNclFileDataStart = mNclFile.tellg();
813 mNclItemWidth = mMaxContainingElements *
sizeof(
unsigned);
819 if (mCableElementsFile.is_open())
821 GetNextLineFromStream(mCableElementsFile, buffer);
822 std::stringstream cable_header_line(buffer);
823 unsigned num_nodes_per_cable_element;
824 cable_header_line >> mNumCableElements >> num_nodes_per_cable_element >> mNumCableElementAttributes;
825 assert(num_nodes_per_cable_element == 2u);
826 mCableElementsRead = 0u;
865 std::vector<T_DATA>& rDataPacket,
const unsigned& rNumAttributes, std::vector<double>& rAttributes)
869 if (!rDataPacket.empty())
871 rFileStream.read((
char*)&rDataPacket[0], rDataPacket.size()*
sizeof(T_DATA));
873 if (rNumAttributes > 0)
875 for (
unsigned i = 0; i < rNumAttributes; i++)
878 rFileStream.read((
char*) &attribute,
sizeof(
double));
879 rAttributes.push_back(attribute);
886 GetNextLineFromStream(rFileStream, buffer);
887 std::stringstream buffer_stream(buffer);
890 buffer_stream >> item_index;
893 expectedItemNumber += mIndexFromZero ? 0 : 1;
895 if (item_index != expectedItemNumber)
900 expectedItemNumber--;
902 EXCEPTION(
"Data for item " << expectedItemNumber <<
" missing");
905 for (
unsigned i=0; i<rDataPacket.size(); i++)
907 buffer_stream >> rDataPacket[i];
910 if (rNumAttributes > 0)
912 for (
unsigned i = 0; i < rNumAttributes; i++)
915 buffer_stream >> attribute;
916 if (buffer_stream.fail())
918 EXCEPTION(
"Error in reading attribute index " << i <<
" (out of " << rNumAttributes <<
") in one of the files in " << mFilesBaseName);
920 rAttributes.push_back(attribute);
935 assert(ELEMENT_DIM == 1);
936 mNumFaceAttributes = 0;
937 if (!mOneDimBoundary.empty())
942 std::vector<unsigned> node_indices(2);
943 std::vector<double> dummy_attribute;
946 std::vector<unsigned> node_count(mNumNodes);
947 for (
unsigned element_index=0; element_index<mNumElements;element_index++)
949 GetNextItemFromStream(mElementsFile, element_index, node_indices, mNumElementAttributes, dummy_attribute);
956 node_count[node_indices[0]]++;
957 node_count[node_indices[1]]++;
961 for (
unsigned node_index=0; node_index<mNumNodes;node_index++)
963 if (node_count[node_index] == 1u)
965 mOneDimBoundary.push_back(node_index);
970 mElementsFile.close();
971 mElementsFile.clear();
974 GetNextLineFromStream(mElementsFile, buffer);