Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO/VTK: add support for writing time series #432

Merged
merged 20 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4353993
common: reorder functions by their purpose
andrea-iob Oct 25, 2023
7525a0b
common: cosmetic fixes
andrea-iob Oct 25, 2023
21ba9fc
common: add missing function to FileHandler class
andrea-iob Oct 25, 2023
511843a
common: fix Doxygen documentation
andrea-iob Oct 25, 2023
95df59e
common: return an invalid counter if a file is not in a time series
andrea-iob Oct 25, 2023
031f064
common: return strings by constant reference
andrea-iob Oct 25, 2023
96844da
common: declare some functions as constant
andrea-iob Oct 27, 2023
01222b0
IO/config: fix encoding of empty strings
andrea-iob Oct 26, 2023
b1d6b74
IO/VTK: add a function to increment the time series counter
andrea-iob Oct 25, 2023
420961b
IO/VTK: use proper function to get the time series counter
andrea-iob Oct 25, 2023
433cd85
IO/VTK: remove unneeded trailing whitespaces
andrea-iob Oct 25, 2023
a9c2272
IO/VTK: simplify handling of time series counter
andrea-iob Oct 25, 2023
3b05b0a
IO/VTK: declare some functions as protected
andrea-iob Oct 25, 2023
ea066e5
IO/VTK: throw an error if the collection for a rectilinear grid canno…
andrea-iob Nov 21, 2023
45d0a58
IO/VTK: allow to write a collection with a custom name
andrea-iob Oct 27, 2023
06ba757
IO/VTK: check if collection should be written inside writeCollection
andrea-iob Oct 27, 2023
cc221d8
IO/VTK: declare some functions as constant
andrea-iob Oct 27, 2023
de9c3a8
IO/VTK: add a function to get file extensions
andrea-iob Nov 21, 2023
2c8844b
IO/VTK: add a function to create the handler for a collection
andrea-iob Nov 21, 2023
17a46a6
IO/VTK: add support for writing time series
andrea-iob Oct 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 262 additions & 44 deletions src/IO/VTK.cpp

Large diffs are not rendered by default.

67 changes: 43 additions & 24 deletions src/IO/VTK.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class VTK{
void setName( const std::string & ) ;
void setDirectory( const std::string & ) ;
void setCounter( int c_=0 ) ;
void incrementCounter( ) ;
int unsetCounter( ) ;
void setParallel( uint16_t , uint16_t ) ;

Expand Down Expand Up @@ -373,22 +374,33 @@ class VTK{
virtual void readMetaInformation() = 0 ;
void readData() ;

void write( VTKWriteMode writeMode, double time ) ;
void write( VTKWriteMode writeMode=VTKWriteMode::DEFAULT ) ;
void write( const std::string &, VTKWriteMode writeMode, double time ) ;
void write( const std::string &, VTKWriteMode writeMode=VTKWriteMode::NO_INCREMENT ) ;

virtual void writeMetaInformation() = 0 ;
void writeData() ;
virtual void writeCollection() = 0 ;
void writeCollection( ) const ;
void writeCollection( const std::string &outputName ) const ;
virtual void writeCollection( const std::string &outputName, const std::string &collectionName ) const = 0;

void writeTimeSeries( double time ) const;
void writeTimeSeries( const std::string &outputName, double time ) const ;
void writeTimeSeries( const std::string &outputName, const std::string &seriesName, double time ) const ;

protected:
//For Writing
void writeDataHeader( std::fstream &, bool parallel=false ) ;
void writeDataArray( std::fstream &, VTKField &) ;
void writePDataArray( std::fstream &, VTKField &) ;
virtual void writeMetaInformation() const = 0 ;
void writeData() ;

void writeDataHeader( std::fstream &, bool parallel=false ) const ;
void writeDataArray( std::fstream &, const VTKField &) const ;
void writePDataArray( std::fstream &, const VTKField &) const ;

FileHandler createCollectionHandler( const std::string &collectionName) const ;

//For Reading
void readDataHeader( std::fstream &) ;
bool readDataArray( std::fstream &, VTKField &);
bool readDataArray( std::fstream &, VTKField &) const ;

//General Purpose
std::vector<std::string> getFieldNames( const std::vector<VTKField> &fields ) const;
Expand All @@ -407,10 +419,13 @@ class VTK{
bool isASCIIActive() const;

void calcAppendedOffsets() ;
virtual uint64_t calcFieldSize( const VTKField &) =0;
virtual uint64_t calcFieldEntries( const VTKField &) =0;
virtual uint8_t calcFieldComponents( const VTKField &) =0;
void checkAllFields() ;
virtual uint64_t calcFieldSize( const VTKField &) const = 0;
virtual uint64_t calcFieldEntries( const VTKField &) const = 0;
virtual uint8_t calcFieldComponents( const VTKField &) const = 0;
void checkAllFields();

virtual std::string getExtension() const = 0 ;
virtual std::string getCollectionExtension() const;

};

Expand Down Expand Up @@ -442,18 +457,20 @@ class VTKUnstructuredGrid : public VTK {
VTKUnstructuredGrid( const std::string &, const std::string &, VTKElementType elementType = VTKElementType::UNDEFINED ) ;

protected:
void writeCollection() override ;

uint64_t readConnectivityEntries( ) ;
uint64_t readFaceStreamEntries( ) ;

void setElementType( VTKElementType ) ;

std::string getExtension() const override;

public:
using VTK::setGeomData;

void readMetaInformation() override ;
void writeMetaInformation() override ;
void writeMetaInformation() const override ;

void writeCollection( const std::string &outputName, const std::string &collectionName ) const override ;

void setDimensions( uint64_t , uint64_t , uint64_t nconn = 0 , uint64_t nfacestream = 0 ) ;

Expand All @@ -464,13 +481,13 @@ class VTKUnstructuredGrid : public VTK {
template<class T>
void setGeomData( VTKUnstructuredField, VTKBaseStreamer* = nullptr );

uint64_t calcConnectivityEntries( ) ;
uint64_t calcFieldSize( const VTKField &) override ;
uint64_t calcFieldEntries( const VTKField &) override ;
uint8_t calcFieldComponents( const VTKField &) override ;
uint64_t calcConnectivityEntries( ) const ;
uint64_t calcFieldSize( const VTKField &) const override ;
uint64_t calcFieldEntries( const VTKField &) const override ;
uint8_t calcFieldComponents( const VTKField &) const override ;

private:
int getFieldGeomId( VTKUnstructuredField field ) ;
int getFieldGeomId( VTKUnstructuredField field ) const ;

};

Expand All @@ -494,13 +511,15 @@ class VTKRectilinearGrid : public VTK{
VTKRectilinearGrid( const std::string & , const std::string & , VTKFormat, int, int );

protected:
void writeCollection() override ;
std::string getExtension() const override;

public:
using VTK::setGeomData;

void readMetaInformation() override ;
void writeMetaInformation() override ;
void writeMetaInformation() const override ;

void writeCollection( const std::string &outputName, const std::string &collectionName ) const override ;

void setDimensions( int, int, int, int, int, int ) ;
void setDimensions( int, int, int ) ;
Expand All @@ -520,9 +539,9 @@ class VTKRectilinearGrid : public VTK{
void setGlobalIndex( const std::vector<extension3D_t> & ) ;
void setGlobalIndex( const std::vector<extension2D_t> & ) ;

uint64_t calcFieldSize( const VTKField &) override ;
uint64_t calcFieldEntries( const VTKField &) override ;
uint8_t calcFieldComponents( const VTKField &) override ;
uint64_t calcFieldSize( const VTKField &) const override ;
uint64_t calcFieldEntries( const VTKField &) const override ;
uint8_t calcFieldComponents( const VTKField &) const override ;
};

/*!
Expand Down
52 changes: 34 additions & 18 deletions src/IO/VTKRectilinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace bitpit{
*/
VTKRectilinearGrid::VTKRectilinearGrid( ) :VTK() {

m_fh.setAppendix( "vtr" );
m_fh.setAppendix( getExtension() );

m_geometry.push_back( VTKField("x_Coord") ) ;
m_geometry.push_back( VTKField("y_Coord") ) ;
Expand Down Expand Up @@ -221,7 +221,7 @@ void VTKRectilinearGrid::readMetaInformation( ){
/*!
* Writes entire VTR but the data.
*/
void VTKRectilinearGrid::writeMetaInformation( ){
void VTKRectilinearGrid::writeMetaInformation( ) const {

std::fstream str;

Expand Down Expand Up @@ -278,23 +278,26 @@ void VTKRectilinearGrid::writeMetaInformation( ){

/*!
* Writes collection file for parallel output.
* Is called by rank 0 in VTK::Write()
*
* \param outputName filename to be set for this output only
* \param collectionName collection filename to be set for this output only
*/
void VTKRectilinearGrid::writeCollection( ){

std::fstream str ;

FileHandler fhp, fho ;
void VTKRectilinearGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) const {

fhp = m_fh ;
fho = m_fh ;

fhp.setParallel(false) ;
fhp.setAppendix("pvtr") ;
// Only one process in a parallel output should write the collection
if (m_procs <= 1 || m_rank != 0) {
return;
}

fho.setDirectory(".") ;
// Create file handler
FileHandler fhp = createCollectionHandler(collectionName) ;

// Initialize outout stream
std::fstream str ;
str.open( fhp.getPath( ), std::ios::out ) ;
if (!str.is_open()) {
throw std::runtime_error("Cannot create file \"" + fhp.getName() + "\"" + " inside the directory \"" + fhp.getDirectory() + "\"");
}

//Writing XML header
str << "<?xml version=\"1.0\"?>" << std::endl;
Expand All @@ -320,9 +323,13 @@ void VTKRectilinearGrid::writeCollection( ){
str << " </PCoordinates>" << std::endl;


FileHandler fho(m_fh) ;
fho.setSeries(false) ;
fho.setDirectory(".") ;
fho.setName(outputName) ;
for( int i=0; i<m_procs; i++){
fho.setBlock(i) ;
extension3D_t &index = m_procIndex[i] ;
const extension3D_t &index = m_procIndex[i] ;

str << " <Piece Extent= \" "
<< index[0][0] << " " << index[0][1] << " "
Expand Down Expand Up @@ -494,7 +501,7 @@ void VTKRectilinearGrid::setGlobalIndex( const std::vector<extension2D_t> &loc )
* @param[in] field field
* @return size of the field
*/
uint64_t VTKRectilinearGrid::calcFieldSize( const VTKField &field ){
uint64_t VTKRectilinearGrid::calcFieldSize( const VTKField &field ) const {

uint64_t bytes = calcFieldEntries(field) ;
bytes *= VTKTypes::sizeOfType( field.getDataType() ) ;
Expand All @@ -508,7 +515,7 @@ uint64_t VTKRectilinearGrid::calcFieldSize( const VTKField &field ){
* @param[in] field field
* @return size of the field
*/
uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ){
uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ) const {

uint64_t entries(0) ;
const std::string &name = field.getName() ;
Expand Down Expand Up @@ -548,7 +555,7 @@ uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ){
* @param[in] field field
* @return size of the field
*/
uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ){
uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ) const {

uint8_t comp ;
const std::string &name = field.getName() ;
Expand All @@ -565,4 +572,13 @@ uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ){

}

/*!
* Gets the extension of the VTK file.
*
* \result The extension of the VTK file.
*/
std::string VTKRectilinearGrid::getExtension() const {
return "vtr";
}

}
51 changes: 32 additions & 19 deletions src/IO/VTKUnstructured.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void VTKUnstructuredGrid::HomogeneousInfoStreamer::flushData( std::fstream &str,
*/
VTKUnstructuredGrid::VTKUnstructuredGrid( VTKElementType elementType ) :VTK() {

m_fh.setAppendix("vtu");
m_fh.setAppendix(getExtension());

int nGeomFields = 6;

Expand Down Expand Up @@ -438,7 +438,7 @@ uint64_t VTKUnstructuredGrid::readFaceStreamEntries( ){
/*!
* Writes entire VTU but the data.
*/
void VTKUnstructuredGrid::writeMetaInformation( ){
void VTKUnstructuredGrid::writeMetaInformation( ) const {

std::fstream str ;

Expand Down Expand Up @@ -494,22 +494,22 @@ void VTKUnstructuredGrid::writeMetaInformation( ){

/*!
* Writes collection file for parallel output.
* Is called by rank 0 in VTK::Write()
*
* \param outputName filename to be set for this output only
* \param collectionName collection filename to be set for this output only
*/
void VTKUnstructuredGrid::writeCollection( ){

std::fstream str ;

FileHandler fhp, fho ;
void VTKUnstructuredGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) const {

fhp = m_fh ;
fho = m_fh ;

fhp.setParallel(false) ;
fhp.setAppendix("pvtu") ;
// Only one process in a parallel output should write the collection
if (m_procs <= 1 || m_rank != 0) {
return;
}

fho.setDirectory(".") ;
// Create file handler
FileHandler fhp = createCollectionHandler(collectionName) ;

// Initialize outout stream
std::fstream str ;
str.open( fhp.getPath( ), std::ios::out ) ;
if (!str.is_open()) {
throw std::runtime_error("Cannot create file \"" + fhp.getName() + "\"" + " inside the directory \"" + fhp.getDirectory() + "\"");
Expand Down Expand Up @@ -540,6 +540,10 @@ void VTKUnstructuredGrid::writeCollection( ){
}
str << " </PCells>" << std::endl;

FileHandler fho(m_fh) ;
fho.setSeries(false) ;
fho.setDirectory(".") ;
fho.setName(outputName) ;
for( int i=0; i<m_procs; i++){
fho.setBlock(i) ;
str << " <Piece Source=\"" << fho.getPath() << "\"/>" << std::endl;
Expand Down Expand Up @@ -625,7 +629,7 @@ void VTKUnstructuredGrid::readMetaInformation( ){
* Returns the size of the connectivity information
* @return size of connectivity
*/
uint64_t VTKUnstructuredGrid::calcConnectivityEntries( ){
uint64_t VTKUnstructuredGrid::calcConnectivityEntries( ) const {

return calcFieldEntries( m_geometry[getFieldGeomId(VTKUnstructuredField::CONNECTIVITY)] ) ;
}
Expand All @@ -635,7 +639,7 @@ uint64_t VTKUnstructuredGrid::calcConnectivityEntries( ){
* @param[in] field field
* @return size of the field
*/
uint64_t VTKUnstructuredGrid::calcFieldSize( const VTKField &field ){
uint64_t VTKUnstructuredGrid::calcFieldSize( const VTKField &field ) const {

uint64_t bytes = calcFieldEntries(field) ;
bytes *= VTKTypes::sizeOfType( field.getDataType() ) ;
Expand All @@ -649,7 +653,7 @@ uint64_t VTKUnstructuredGrid::calcFieldSize( const VTKField &field ){
* @param[in] field field
* @return size of the field
*/
uint64_t VTKUnstructuredGrid::calcFieldEntries( const VTKField &field ){
uint64_t VTKUnstructuredGrid::calcFieldEntries( const VTKField &field ) const {

uint64_t entries(0) ;
const std::string &name = field.getName() ;
Expand Down Expand Up @@ -698,7 +702,7 @@ uint64_t VTKUnstructuredGrid::calcFieldEntries( const VTKField &field ){
* @param[in] field field
* @return size of the field
*/
uint8_t VTKUnstructuredGrid::calcFieldComponents( const VTKField &field ){
uint8_t VTKUnstructuredGrid::calcFieldComponents( const VTKField &field ) const {

uint8_t comp ;
const std::string &name = field.getName() ;
Expand Down Expand Up @@ -741,9 +745,18 @@ uint8_t VTKUnstructuredGrid::calcFieldComponents( const VTKField &field ){
* @param[in] field field
* @return geometry index associated to the field
*/
int VTKUnstructuredGrid::getFieldGeomId( VTKUnstructuredField field ){
int VTKUnstructuredGrid::getFieldGeomId( VTKUnstructuredField field ) const {

return static_cast<std::underlying_type<VTKElementType>::type>(field) ;
}

/*!
* Gets the extension of the VTK file.
*
* \result The extension of the VTK file.
*/
std::string VTKUnstructuredGrid::getExtension() const {
return "vtu";
}

}
4 changes: 0 additions & 4 deletions src/IO/configuration_XML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string
*/
xmlChar * encodeString(const std::string &in, const std::string &encoding)
{
if (in.empty()) {
return nullptr;
}

xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(encoding.c_str());
if (!handler) {
return nullptr;
Expand Down
Loading