/*! * Calculates the compnents of a field * @param[in] field field * @return size of the field */ uint8_t VTKUnstructuredGrid::calcFieldComponents( const VTKField &field ){ uint8_t comp ; std::string name( field.getName() ) ; if( name == "Points" ){ comp = static_cast<int>(VTKFieldType::VECTOR) ; } else if( name == "offsets" ){ comp = 1 ; } else if( name == "types" ){ comp = 1 ; } else if( name == "connectivity" ){ if( m_homogeneousType != VTKElementType::UNDEFINED){ comp = vtk::getNNodeInElement( m_homogeneousType ) ; } else { comp = 1; } } else{ VTKFieldType fieldType( field.getFieldType() ) ; assert( fieldType != VTKFieldType::UNDEFINED) ; comp = static_cast<uint8_t>(fieldType) ; } return comp ; };
/*! * Converts a string into a Field information. * @param[in] line_ string to be converted * @param[out] data_ Field information * \return true if successful */ bool vtk::convertStringToDataArray( const std::string &line_, VTKField &data_ ){ std::string type_, name_, code_, comp_, offs_ ; int components_(1), offset_ ; VTKFormat codex ; VTKDataType type ; VTKFieldType comp(VTKFieldType::SCALAR) ; bool success(true) ; if( bitpit::utils::keywordInString( line_, "<DataArray ") ){ success = success && bitpit::utils::getAfterKeyword( line_, "type=", '\"', type_) ; success = success && bitpit::utils::getAfterKeyword( line_, "Name=", '\"', name_) ; success = success && bitpit::utils::getAfterKeyword( line_, "format=", '\"', code_) ; if( bitpit::utils::getAfterKeyword( line_, "NumberOfComponents=", '\"', comp_) ){ bitpit::utils::convertString( comp_, components_ ) ; }; if(components_==3) comp=VTKFieldType::VECTOR ; vtk::convertStringToEnum( type_, type) ; vtk::convertStringToEnum( code_, codex) ; data_.setDataType(type) ; data_.setName(name_) ; data_.setCodification(codex) ; if(name_ != "connectivity") data_.setFieldType(comp) ; if(code_=="appended") { if( bitpit::utils::getAfterKeyword( line_, "offset=", '\"', offs_) ){ bitpit::utils::convertString( offs_, offset_ ) ; data_.setOffset(offset_) ; } else{ success = false ; }; } return success ; } else{ return false ; }; };
/*! * Converts a string into a Field information. * @param[in] line string to be converted * @param[out] field Field information * \return true if successful */ bool vtk::convertStringToDataArray( const std::string &line, VTKField &field ){ std::string typ, name, code, com, offs ; int components(1), offset ; VTKFormat codex ; VTKDataType type ; VTKFieldType comp(VTKFieldType::SCALAR) ; bool success(true) ; if( bitpit::utils::keywordInString( line, "<DataArray ") ){ success = success && bitpit::utils::getAfterKeyword( line, "type=", '\"', typ) ; success = success && bitpit::utils::getAfterKeyword( line, "Name=", '\"', name) ; success = success && bitpit::utils::getAfterKeyword( line, "format=", '\"', code) ; if( bitpit::utils::getAfterKeyword( line, "NumberOfComponents=", '\"', com) ){ bitpit::utils::convertString( com, components ) ; }; if(components==3) comp=VTKFieldType::VECTOR ; vtk::convertStringToEnum( typ, type) ; vtk::convertStringToEnum( code, codex) ; field.setDataType(type) ; field.setName(name) ; field.setCodification(codex) ; if(name != "connectivity") field.setFieldType(comp) ; if(code=="appended") { if( bitpit::utils::getAfterKeyword( line, "offset=", '\"', offs) ){ bitpit::utils::convertString( offs, offset ) ; field.setOffset(offset) ; } else{ success = false ; }; } return success ; } else{ return false ; }; };
/*! * Converts a parallel field information to string as requested by VTK format. * @param[in] field_ Field information * @return string in VTK format */ std::string vtk::convertPDataArrayToString( const VTKField &field_ ){ std::stringstream os("") ; os << " <PDataArray " << "type=\"" << vtk::convertEnumToString(field_.getType()) << "\" " << "Name=\"" << field_.getName() << "\" " << "NumberOfComponents=\""<< unsigned(field_.getComponents()) << "\" " << ">" ; return( os.str() ) ; };
/*! * Read Field data from stream * @param[in] str input stream * @param[in] field field to be read */ void VTKUnstructuredGrid::readFieldData( std::fstream &str, VTKField &field ){ if( field.usesInterface() ){ absorbData( str, field.getCodification(), field.getName() ) ; } else if( field.autoWrite() ){ field.absorbData( str ) ; } else { //dont do nothing } return; }
/*! * Converts a parallel field information to string as requested by VTK format. * @param[in] field_ Field information * @return string in VTK format */ std::string vtk::convertPDataArrayToString( const VTKField &field_ ){ std::stringstream os("") ; unsigned comp = static_cast<unsigned>(field_.getFieldType()) ; if( field_.getFieldType() != VTKFieldType::SCALAR && field_.getFieldType() != VTKFieldType::VECTOR ) comp = 1 ; os << " <PDataArray " << "type=\"" << vtk::convertEnumToString(field_.getDataType()) << "\" " << "Name=\"" << field_.getName() << "\" " << "NumberOfComponents=\""<< comp << "\" " << ">" ; return( os.str() ) ; };
/*! * Calculates the size (in bytes) of a field * @param[in] field field * @return size of the field */ uint64_t VTKUnstructuredGrid::calcFieldSize( const VTKField &field ){ uint64_t bytes = calcFieldEntries(field) ; bytes *= VTKTypes::sizeOfType( field.getDataType() ) ; return bytes ; };
/*! * Write Field data to stream * @param[in] str output stream * @param[in] field field to be written */ void VTKUnstructuredGrid::writeFieldData( std::fstream &str, VTKField &field ){ if( field.usesInterface() ){ flushData( str, field.getCodification(), field.getName() ) ; } else if( field.autoWrite() ) { field.flushData( str ) ; } else{ if(field.getName() == "types" && homogeneousType != VTKElementType::UNDEFINED){ uint8_t type = (uint8_t) homogeneousType ; for( unsigned int i=0; i<nr_cells; ++i) genericIO::flushBINARY(str, type ); } else if(field.getName() == "offsets" && homogeneousType != VTKElementType::UNDEFINED){ uint8_t n = vtk::getNNodeInElement(homogeneousType) ; uint64_t offset(0) ; for( unsigned int i=0; i<nr_cells; ++i){ offset += n ; genericIO::flushBINARY(str, offset ); } } } };
/*! * Calculates the compnents of a field * @param[in] field field * @return size of the field */ uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ){ uint8_t comp ; std::string name( field.getName() ) ; if( name == "x_Coord" || name == "y_Cooord" || name == "z_Coord" ){ comp = 1 ; } else{ VTKFieldType fieldType( field.getFieldType() ) ; assert( fieldType != VTKFieldType::UNDEFINED) ; comp = static_cast<uint8_t>(fieldType) ; } return comp ; };
/*! * Calculates the number of entries of a field * @param[in] field field * @return size of the field */ uint64_t VTKUnstructuredGrid::calcFieldEntries( const VTKField &field ){ uint64_t entries(0) ; std::string name( field.getName() ) ; if( name == "Points" ){ entries = m_points *static_cast<int>(VTKFieldType::VECTOR) ; } else if( name == "offsets" ){ entries = m_cells ; } else if( name == "types" ){ entries = m_cells ; } else if( name == "connectivity"){ entries = m_connectivity ; } else{ VTKLocation location( field.getLocation() ) ; assert( location != VTKLocation::UNDEFINED) ; if( location == VTKLocation::CELL ){ entries = m_cells ; } else if( location == VTKLocation::POINT ){ entries = m_points ; } VTKFieldType fieldType( field.getFieldType() ) ; assert( fieldType != VTKFieldType::UNDEFINED) ; entries *= static_cast<uint64_t>(fieldType) ; } return entries ; };
/*! * Reads data array from stream and stores in field information * @param[in] str output stream * @param[out] field_ field information */ bool VTK::readDataArray( std::fstream &str, VTKField &field_ ){ std::string line_ ; while( getline(str, line_) ){ if( bitpit::utils::keywordInString( line_, field_.getName() ) ){ if( vtk::convertStringToDataArray( line_, field_ ) ){ if( field_.getCodification() == VTKFormat::ASCII) { field_.setPosition( str.tellg() ) ; }; return true ; }; }; }; return false ; };
/*! * Calculates the number of entries of a field * @param[in] field field * @return size of the field */ uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ){ uint64_t entries(0) ; std::string name( field.getName() ) ; if( name == "x_Coord" ){ entries = m_localIndex[0][1] -m_localIndex[0][0] +1 ; } else if( name == "y_Coord" ){ entries = m_localIndex[1][1] -m_localIndex[1][0] +1 ; } else if( name == "z_Coord" ){ entries = m_localIndex[2][1] -m_localIndex[2][0] +1 ; } else{ VTKLocation location( field.getLocation() ) ; assert( location != VTKLocation::UNDEFINED) ; if( location == VTKLocation::CELL ){ entries = m_cells ; } else if( location == VTKLocation::POINT ){ entries = m_points ; } VTKFieldType fieldType( field.getFieldType() ) ; assert( fieldType != VTKFieldType::UNDEFINED) ; entries *= static_cast<uint64_t>(fieldType) ; } return entries ; };
/*! * Converts a Field information to string as requested by VTK format. * @param[in] field_ Field information * @return string in VTK format */ std::string vtk::convertDataArrayToString( const VTKField &field_ ){ std::stringstream os("") ; unsigned comp = static_cast<unsigned>(field_.getFieldType()) ; if( field_.getFieldType() != VTKFieldType::SCALAR && field_.getFieldType() != VTKFieldType::VECTOR ) comp = 1 ; os << " <DataArray " << "type=\"" << vtk::convertEnumToString( field_.getDataType() ) << "\" " << "Name=\"" << field_.getName() << "\" " << "NumberOfComponents=\""<< comp << "\" " << "format=\"" << vtk::convertEnumToString(field_.getCodification()) << "\" "; if( field_.getCodification() == VTKFormat::APPENDED ){ os << "offset=\"" << field_.getOffset() << "\" " ; }; os << ">" ; return( os.str() ) ; };
/*! * Converts a Field information to string as requested by VTK format. * @param[in] field_ Field information * @return string in VTK format */ std::string vtk::convertDataArrayToString( const VTKField &field_ ){ std::stringstream os("") ; os << " <DataArray " << "type=\"" << vtk::convertEnumToString( field_.getType() ) << "\" " << "Name=\"" << field_.getName() << "\" " << "NumberOfComponents=\""<< unsigned(field_.getComponents()) << "\" " << "format=\"" << vtk::convertEnumToString(field_.getCodification()) << "\" "; if( field_.getCodification() == VTKFormat::APPENDED ){ os << "offset=\"" << field_.getOffset() << "\" " ; }; os << ">" ; return( os.str() ) ; };
/*! * Reads data headers from strean. * All field information available in file are stored. * @param[in] str output stream */ void VTK::readDataHeader( std::fstream &str ){ std::fstream::pos_type pos_ ; VTKLocation location; std::string locationString ; std::string line, loc_; std::stringstream ss; bool read ; VTKField temp ; VTKField** ptemp ; for( int i=0; i<2; i++){ ss.str("") ; if( i== 0) { location = VTKLocation::POINT; locationString = "Point" ; } else if( i== 1) { location = VTKLocation::CELL; locationString = "Cell" ; } temp.setLocation( location ) ; ss << "</" << locationString << "Data>" ; loc_ = ss.str(); read= true ; if( ! getline( str, line) ) read = false ; if( bitpit::utils::keywordInString( line, loc_) ) read=false ; while( read ){ if( vtk::convertStringToDataArray( line, temp ) ) { if( temp.getCodification() == VTKFormat::ASCII) { pos_ = str.tellg() ; } else{ pos_ = 0 ; }; temp.setPosition( pos_ ) ; if( ! getFieldByName( temp.getName(), ptemp )) { data.push_back( new VTKField( temp ) ) ; } else{ (*ptemp)->setOffset( temp.getOffset() ) ; (*ptemp)->setLocation( temp.getLocation() ) ; (*ptemp)->setDataType( temp.getDataType() ) ; (*ptemp)->setFieldType( temp.getFieldType() ) ; (*ptemp)->setCodification( temp.getCodification() ) ; }; }; if( ! getline( str, line) ) read = false ; if( bitpit::utils::keywordInString( line, loc_) ) read=false ; }; }; return ; };
/*! * Writes data only in VTK file */ void VTK::writeData( ){ std::fstream str ; std::fstream::pos_type position_insert, position_eof ; int length; char* buffer ; str.open( fh.getPath( ), std::ios::in | std::ios::out ) ; { // Write Ascii position_insert = str.tellg(); VTKField temp ; //Writing first point data then cell data for( auto &field : data ){ if( field->getCodification() == VTKFormat::ASCII && field->getLocation() == VTKLocation::POINT ) { str.seekg( position_insert); readDataArray( str, *field ) ; str.seekg( field->getPosition() ) ; genericIO::copyUntilEOFInString( str, buffer, length ); writeFieldData( str, *field) ; position_insert = str.tellg(); str << std::endl ; genericIO::flushBINARY( str, buffer, length) ; delete [] buffer ; }; }; for( auto &field : data ){ if( field->getCodification() == VTKFormat::ASCII && field->getLocation() == VTKLocation::CELL ) { str.seekg( position_insert); readDataArray( str, *field ) ; str.seekg( field->getPosition() ) ; genericIO::copyUntilEOFInString( str, buffer, length ); writeFieldData( str, *field) ; position_insert = str.tellg(); str << std::endl ; genericIO::flushBINARY( str, buffer, length) ; delete [] buffer ; }; }; for( auto &field : geometry ){ if( field->getCodification() == VTKFormat::ASCII ) { str.seekg( position_insert); readDataArray( str, *field ) ; str.seekg( field->getPosition() ) ; genericIO::copyUntilEOFInString( str, buffer, length ); writeFieldData( str, *field) ; //flushData( str, VTKFormat::ASCII, field.getName() ) ; position_insert = str.tellg(); str << std::endl ; genericIO::flushBINARY( str, buffer, length) ; delete [] buffer ; }; }; str.seekg( temp.getPosition() ) ; } { // Write Appended char c_; std::string line ; std::fstream::pos_type position_appended ; //Go to the initial position of the appended section while( getline(str, line) && (! bitpit::utils::keywordInString( line, "<AppendedData")) ){} ; str >> c_; while( c_ != '_') str >> c_; position_insert = str.tellg(); genericIO::copyUntilEOFInString( str, buffer, length ); str.close(); str.clear(); //Reopening in binary mode str.open( fh.getPath( ), std::ios::out | std::ios::in | std::ios::binary); str.seekg( position_insert) ; //str.open( "data.dat", std::ios::out | std::ios::binary); //Writing first point data then cell data for( auto &field : data ){ if( field->getCodification() == VTKFormat::APPENDED && field->getLocation() == VTKLocation::POINT ) { if( getHeaderType() == "UInt32"){ uint32_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; } else{ uint64_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; }; writeFieldData( str, *field) ; }; } for( auto &field : data ){ if( field->getCodification() == VTKFormat::APPENDED && field->getLocation() == VTKLocation::CELL ) { if( getHeaderType() == "UInt32"){ uint32_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; } else{ uint64_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; }; writeFieldData( str, *field) ; }; } //Writing Geometry Data for( auto &field : geometry ){ if( field->getCodification() == VTKFormat::APPENDED ) { if( getHeaderType() == "UInt32"){ uint32_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; } else{ uint64_t nbytes = field->getNbytes() ; genericIO::flushBINARY(str, nbytes) ; }; writeFieldData( str, *field) ; }; }; //{ //std::fstream str2 ; //str2.open( "test2.dat", std::ios::out | std::ios::binary); //genericIO::flushBINARY( str2, buffer, length) ; //str2.close(); //} genericIO::flushBINARY( str, buffer, length) ; delete [] buffer ; }; // Closing Appended Secyion str.close(); return ; };