void ExporterEnsight<MeshType>::writeAsciiValues (const exporterData_Type& dvar, const std::string& suffix) { using std::setw; std::ofstream exportFile; std::string filename; if ( dvar.regime() == exporterData_Type::SteadyRegime ) filename = this->M_postDir + super::M_prefix + "_" + dvar.variableName() + this->M_me + suffix; else filename = this->M_postDir + super::M_prefix + "_" + dvar.variableName() + this->M_postfix + this->M_me + suffix; exportFile.open ( filename.c_str() ); ASSERT (exportFile.is_open(), "There is an error while opening " + filename ); UInt count = 0; const UInt size = dvar.numDOF(); const UInt start = dvar.start(); const UInt vertexNumber = static_cast<UInt> (this->M_ltGNodesMap.size() ); ASSERT (exportFile.good(), "There is an error while writing to " + filename ); if ( suffix.compare (".vct") == 0 ) { exportFile << "Vector per node\n"; } else if ( suffix.compare (".scl") == 0 ) { exportFile << "Scalar per node\n"; } exportFile.setf (std::ios::right | std::ios_base::scientific); exportFile.precision (5); for (UInt i = 0; i < vertexNumber; ++i) for (UInt j = 0; j < dvar.fieldDim(); ++j) { const Int id = this->M_ltGNodesMap[i]; ASSERT (exportFile.good(), "There is an error while writing to " + filename ); exportFile << setw (12) << static_cast<float> (dvar (start + j * size + id) ) ; ++count; if ( count == 6 ) { ASSERT (exportFile.good(), "There is an error while writing to " + filename ); exportFile << "\n"; count = 0; } } ASSERT (exportFile.good(), "There is an error while writing to " + filename ); exportFile << std::endl; exportFile.close(); }
template <typename MeshType> void ExporterEnsight<MeshType>::readAsciiValues (exporterData_Type& dvar, const std::string& suffix) { ASSERT ( this->M_numImportProc, "The number of pieces to be loaded was not specified." ); // this vector lists the global IDs of DOFs listed in each piece std::vector<Real> globalDOF; // Each processor will read all the files, and fill just its own component of the vectors for ( UInt iProc = 0; iProc < this->M_numImportProc; ++iProc ) { // build the postfix for the file corresponding to part iProc std::ostringstream index; index.fill ( '0' ); index << std::setw (1) << "." ; index << std::setw (3) << iProc; // fill globalDOF, the list of DOF IDs on which we are going to operate readGlobalIDs ( this->M_postDir + super::M_prefix + "_globalIDs" + index.str() + ".scl", globalDOF ); // open the file with the vector field to be imported const std::string filename ( this->M_postDir + super::M_prefix + "_" + dvar.variableName() + this->M_postfix + index.str() + suffix ); std::ifstream importFile ( filename.c_str() ); // debugging step if (!this->M_procId) { std::cout << "\tfile " << filename << std::endl; } ASSERT (importFile.is_open(), "There is an error while reading " + filename ); // discard the header of the file std::string line; ASSERT (importFile.good(), "There is an error while reading from " + filename ); getline ( importFile, line ); // the line looks like this: "Vector/Scalar per node" // parameters to access ExporterData structures const UInt size = dvar.numDOF(); const UInt start = dvar.start(); // loop over the DOFs listed in the current piece for (UInt i = 0; i < globalDOF.size(); ++i) { // extract the global ID of each DOF const Int id = globalDOF[i]; // we are working with vectorial fields for (UInt j = 0; j < dvar.fieldDim(); ++j) { // this is the value of the field, to be imported Real value (0); ASSERT (importFile.good(), "There is an error while reading from " + filename ); importFile >> value; // do the actual import only if the global ID belongs to the current process if ( dvar.feSpacePtr()->map().map (Repeated)->MyGID ( id ) ) { dvar (start + j * size + id) = value; } } } ASSERT (!importFile.fail(), "There is an error while reading " + filename ); importFile.close(); } }
void ExporterVTK<MeshType>::readVTUFiles ( exporterData_Type& dvar ) { ASSERT ( this->M_numImportProc, "The number of pieces to be loaded was not specified." ); UInt numPoints, numCells; std::vector<Real> inputValues; std::vector<Real> globalIDs; const UInt start ( dvar.start() ); const UInt numGlobalDOF ( dvar.numDOF() ); // Each processor will read all the files, and fill just its own component of the vectors for ( UInt iProc = 0; iProc < this->M_numImportProc; ++iProc ) { std::string filename ( this->M_postDir + this->M_prefix + "_" + dvar.variableName() + this->M_postfix + "." + iProc + ".vtu" ); std::ifstream inputFile ( filename.c_str() ); if (this->M_procId == 0) { std::cout << "\tfile " << filename << std::endl; } ASSERT (inputFile.is_open(), "There is an error while opening " + filename ); // file parsing: line by line std::string line; size_t found; std::stringstream parseLine; while ( inputFile.good() && getline ( inputFile, line ) ) { // this is essentially a consistency check: the number of DOF is explicitly // written in the VTK files. We will check that the number of values read // from file matches this number found = line.find ( "NumberOfPoints" ); if ( found != std::string::npos ) { // place the get pointer at the first " after NumberOfPoints found = line.find ( "\"", found, 1 ); // after the " we'll find the number: parse the substring parseLine.str ( line.substr (found + 1) ); parseLine >> numPoints; // do the same for NumberOfCells found = line.find ( "NumberOfCells" ); found = line.find ( "\"", found, 1 ); parseLine.str ( line.substr (found + 1) ); parseLine >> numCells; } // load all PointData arrays if ( line.find ( "<PointData" ) != std::string::npos ) { while ( inputFile.good() && getline ( inputFile, line ) ) { if ( line.find ( dvar.variableName() ) != std::string::npos ) { inputValues.resize ( dvar.fieldDim() *numPoints ); if ( line.find ( "binary" ) != std::string::npos ) { UInt numBitsFloat; found = line.find ( "Float" ); parseLine.str ( line.substr (found + 5) ); parseLine >> numBitsFloat; ASSERT (inputFile.good(), "There is an error while opening " + filename ); getline ( inputFile, line ); readBinaryData ( line, inputValues, numBitsFloat ); } else { ASSERT (inputFile.good(), "There is an error while opening " + filename ); getline ( inputFile, line ); readASCIIData ( line, inputValues ); } } if ( line.find ( "GlobalId" ) != std::string::npos ) { globalIDs.resize ( numPoints ); ASSERT (inputFile.good(), "There is an error while opening " + filename ); getline ( inputFile, line ); readASCIIData ( line, globalIDs ); } }
void ExporterVTK<MeshType>::composeDataArrayStream (const exporterData_Type& dvar, const std::map<UInt, UInt>& localToGlobalMap, std::stringstream& dataArraysStringStream) { const UInt start ( dvar.start() ); const UInt numGlobalDOF ( dvar.numDOF() ); const UInt numMyDOF ( localToGlobalMap.size() ); std::stringstream dataToBeEncoded; dataToBeEncoded.str (""); std::string encodedDataString; std::string formatString; std::stringstream nComponents; nComponents << dvar.fieldDim(); int32_type sizeOfFloat = 4; std::string floatTypeString; switch ( M_floatPrecision ) { case SINGLE_PRECISION: ASSERT ( sizeof (float) == 4, "\nThis piece of code assumes sizeof(float) == 4" ) sizeOfFloat = sizeof (float); floatTypeString = "Float32"; break; case DOUBLE_PRECISION: ASSERT ( sizeof (Real) == 8, "\nThis piece of code assumes sizeof(float) == 4" ) sizeOfFloat = sizeof (Real); floatTypeString = "Float64"; break; default: ERROR_MSG ( "WARNING: this float precision cannot be handled in ExporterVTK\n" ) break; } int32_type lengthOfRawData ( dvar.fieldDim() *numMyDOF * sizeOfFloat ); switch ( M_exportMode ) { case ASCII_EXPORT: formatString = "ascii"; break; case BINARY_EXPORT: formatString = "binary"; dataToBeEncoded.write ( reinterpret_cast<char*> ( &lengthOfRawData ), sizeof (int32_type) ); lengthOfRawData += sizeof (int32_type); break; default: ERROR_MSG ( "WARNING: this export mode cannot be handled in ExporterVTK\n" ) break; } dataArraysStringStream << "\t\t\t\t<DataArray type=\"" << floatTypeString << "\" Name=\"" << dvar.variableName() << "\" NumberOfComponents=\"" << nComponents.str() << "\" format=\"" << formatString << "\">\n"; switch ( M_exportMode ) { case ASCII_EXPORT: dataArraysStringStream.setf (std::ios_base::fixed); dataArraysStringStream.precision (5); dataArraysStringStream.width (12); for (UInt iDOF = 0; iDOF < numMyDOF; ++iDOF) { const Int id = localToGlobalMap.find (iDOF)->second; for (UInt iCoor = 0; iCoor < dvar.fieldDim(); ++iCoor) { dataArraysStringStream << dvar ( start + id + iCoor * numGlobalDOF ) << " "; } } break; case BINARY_EXPORT: for (UInt iDOF = 0; iDOF < numMyDOF; ++iDOF) { const Int id = localToGlobalMap.find (iDOF)->second; for (UInt iCoor = 0; iCoor < dvar.fieldDim(); ++iCoor) { if ( M_floatPrecision == SINGLE_PRECISION ) { const float value ( dvar ( start + id + iCoor * numGlobalDOF ) ); dataToBeEncoded.write ( reinterpret_cast<const char*> (&value), sizeof (float) ); } else { const Real value ( dvar ( start + id + iCoor * numGlobalDOF ) ); dataToBeEncoded.write ( reinterpret_cast<const char*> (&value), sizeof (Real) ); } } } encodedDataString = base64_encode (reinterpret_cast<const unsigned char*> ( dataToBeEncoded.str().c_str() ), lengthOfRawData ); dataArraysStringStream << encodedDataString; break; default: ERROR_MSG ( "WARNING: this export mode cannot be handled in ExporterVTK\n" ) break; } dataArraysStringStream << "\n\t\t\t\t</DataArray>\n"; dataArraysStringStream << "\t\t\t\t<DataArray type=\"Int32\" NumberOfComponents=\"1\" " << "Name=\"GlobalId\" format=\"ascii\">\n"; for ( UInt iDOF = 0; iDOF < numMyDOF; ++iDOF ) { dataArraysStringStream << localToGlobalMap.find ( iDOF )->second << " "; } dataArraysStringStream << "\n\t\t\t\t</DataArray>\n"; }