From 4353993df71611d6979dad897953d084790175d8 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:27:30 +0200 Subject: [PATCH 01/20] common: reorder functions by their purpose --- src/common/fileHandler.cpp | 163 +++++++++++++++++++------------------ src/common/fileHandler.hpp | 26 +++--- 2 files changed, 97 insertions(+), 92 deletions(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index efc1ee8fa9..16bdd2de60 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -85,32 +85,85 @@ FileHandler& FileHandler::operator=(const FileHandler& other){ return *this ; }; +/*! + * Checks if file exists. + * @return [true/false] if file exists + */ +bool FileHandler::exists() { + std::ifstream f( getPath() ); + return f.good() ; +}; + +/*! + * Composes the filename. + * @return complete filename + */ +std::string FileHandler::getPath(){ + std::stringstream filename ; + + if (!directory.empty()) filename << directory << "/"; + filename << name; + if(parallel) filename <<".b"<< bitpit::utils::string::zeroPadNumber(4, block) ; + if(series) filename <<"." << bitpit::utils::string::zeroPadNumber(4, counter) ; + filename <<"."<< appendix ; + + return filename.str() ; + +}; + +/*! + * Get the directory where the file will be saved + * @return The directory where the file will be saved. + */ +std::string FileHandler::getDirectory() const { + + return directory; +}; + +/*! + * Get the name of the file + * @return The name of the file. + */ +std::string FileHandler::getName() const { + + return name; +}; + +/*! + * Get the appendix of the file + * @return The appendix of the file. + */ +std::string FileHandler::getAppendix() const { + + return appendix; +}; + /*! * sets the directory * @param[in] d_ name of directory */ void FileHandler::setDirectory(const std::string &d_){ - directory=d_; + directory=d_; return; -} ; +} /*! * sets the name * @param[in] n_ name of file */ void FileHandler::setName(const std::string &n_){ - name=n_; + name=n_; return; -} ; +} /*! * sets the appendix * @param[in] a_ appendix of file */ void FileHandler::setAppendix(const std::string &a_){ - appendix=a_; + appendix=a_; return; -} ; +} /*! * sets if file belongs to a time series @@ -119,7 +172,17 @@ void FileHandler::setAppendix(const std::string &a_){ void FileHandler::setSeries( bool s_){ series=s_; return; -} ; +} + +/*! + * Get the time index of the following file. + * If the file doen't belong to a time series, a negative number will + * be returned. + * @return counter + */ +int FileHandler::getCounter() const { + return counter; +} /*! * sets the counter of the series @@ -130,24 +193,6 @@ void FileHandler::setCounter(int c_){ return; }; -/*! - * sets if file belongs to a parallel output - * @param[in] p_ [true/false] if parallel - */ -void FileHandler::setParallel( bool p_){ - parallel=p_; - return; -} ; - -/*! - * sets the counter of the series - * @param[in] b_ index of block of parallel set - */ -void FileHandler::setBlock( int b_){ - block=b_; - return; -} ; - /*! * Increments the counter used for series. */ @@ -157,65 +202,21 @@ void FileHandler::incrementCounter(){ }; /*! - * Composes the filename. - * @return complete filename - */ -std::string FileHandler::getPath(){ - std::stringstream filename ; - - if (!directory.empty()) filename << directory << "/"; - filename << name; - if(parallel) filename <<".b"<< bitpit::utils::string::zeroPadNumber(4, block) ; - if(series) filename <<"." << bitpit::utils::string::zeroPadNumber(4, counter) ; - filename <<"."<< appendix ; - - return filename.str() ; - -}; - -/*! - * Get the name of the file - * @return The name of the file. - */ -std::string FileHandler::getName() const { - - return name; -}; - -/*! - * Get the directory where the file will be saved - * @return The directory where the file will be saved. - */ -std::string FileHandler::getDirectory() const { - - return directory; -}; - -/*! - * Get the appendix of the file - * @return The appendix of the file. - */ -std::string FileHandler::getAppendix() const { - - return appendix; -}; - -/*! - * Get the time index of the following file - * @return counter + * sets if file belongs to a parallel output + * @param[in] p_ [true/false] if parallel */ -int FileHandler::getCounter() const { - - return counter; -}; +void FileHandler::setParallel( bool p_){ + parallel=p_; + return; +} /*! - * Checks if file exists. - * @return [true/false] if file exists + * sets the counter of the series + * @param[in] b_ index of block of parallel set */ -bool FileHandler::exists() { - std::ifstream f( getPath() ); - return f.good() ; -}; +void FileHandler::setBlock( int b_){ + block=b_; + return; +} } diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index 205e1a18a8..17999d20af 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -48,11 +48,12 @@ class FileHandler{ std::string directory; /**< name od directory where file resides */ std::string name; /**< name of file; */ std::string appendix ; /**< appendix; */ + bool series ; /**< is time series? */ - bool parallel ; /**< is part of distributed data? */ int counter ; /**< counter for time series ; */ - int block ; /**< index of block if parallel data */ + bool parallel ; /**< is part of distributed data? */ + int block ; /**< index of block if parallel data */ public: FileHandler() ; @@ -61,22 +62,25 @@ class FileHandler{ FileHandler& operator=( const FileHandler& other) ; + bool exists() ; + + std::string getPath() ; + std::string getDirectory() const ; + std::string getName() const ; + std::string getAppendix() const ; void setDirectory( const std::string & d_) ; void setName( const std::string & n_) ; void setAppendix( const std::string & a_) ; + void setSeries( bool s_) ; - void setParallel( bool p_) ; - void setCounter(int c_); - void setBlock( int b_) ; - std::string getPath() ; - std::string getName() const ; - std::string getDirectory() const ; - std::string getAppendix() const ; int getCounter() const ; - + void setCounter(int c_); void incrementCounter(); - bool exists() ; + + void setParallel( bool p_) ; + + void setBlock( int b_) ; }; From 7525a0ba79e8906433cc57845456d23ecc8a2904 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 11:28:21 +0200 Subject: [PATCH 02/20] common: cosmetic fixes --- src/common/fileHandler.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index 16bdd2de60..9951d251f5 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -42,7 +42,7 @@ FileHandler::FileHandler() : directory("./"), name("file"), appendix("dat"), series(false), parallel(false), counter(0), block(0) { -} ; +} /*! * Constructor. @@ -56,7 +56,7 @@ FileHandler::FileHandler(const std::string &dir_, const std::string &name_, cons : directory(dir_), name(name_), appendix(app_), series(false), parallel(false), counter(0), block(0) { -}; +} /*! * Copy Constructor. @@ -66,7 +66,7 @@ FileHandler::FileHandler(const FileHandler& other){ (*this) = other ; -}; +} /*! * Assignment operator. @@ -83,7 +83,7 @@ FileHandler& FileHandler::operator=(const FileHandler& other){ return *this ; -}; +} /*! * Checks if file exists. @@ -92,7 +92,7 @@ FileHandler& FileHandler::operator=(const FileHandler& other){ bool FileHandler::exists() { std::ifstream f( getPath() ); return f.good() ; -}; +} /*! * Composes the filename. @@ -109,7 +109,7 @@ std::string FileHandler::getPath(){ return filename.str() ; -}; +} /*! * Get the directory where the file will be saved @@ -118,7 +118,7 @@ std::string FileHandler::getPath(){ std::string FileHandler::getDirectory() const { return directory; -}; +} /*! * Get the name of the file @@ -127,7 +127,7 @@ std::string FileHandler::getDirectory() const { std::string FileHandler::getName() const { return name; -}; +} /*! * Get the appendix of the file @@ -136,7 +136,7 @@ std::string FileHandler::getName() const { std::string FileHandler::getAppendix() const { return appendix; -}; +} /*! * sets the directory @@ -191,7 +191,7 @@ int FileHandler::getCounter() const { void FileHandler::setCounter(int c_){ counter=c_; return; -}; +} /*! * Increments the counter used for series. @@ -199,7 +199,7 @@ void FileHandler::setCounter(int c_){ void FileHandler::incrementCounter(){ counter++; return; -}; +} /*! * sets if file belongs to a parallel output From 21ba9fc3f23f4387134952cdded70e05582ab8f6 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:42:24 +0200 Subject: [PATCH 03/20] common: add missing function to FileHandler class --- src/common/fileHandler.cpp | 24 ++++++++++++++++++++++++ src/common/fileHandler.hpp | 3 +++ 2 files changed, 27 insertions(+) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index 9951d251f5..fcc34f112f 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -165,6 +165,14 @@ void FileHandler::setAppendix(const std::string &a_){ return; } +/*! + * Checks if file belongs to a time series + * @return Returns true if the file belongs to a time series, false otherwise + */ +bool FileHandler::isSeries() const { + return series; +} + /*! * sets if file belongs to a time series * @param[in] s_ [true/false] if series @@ -201,6 +209,14 @@ void FileHandler::incrementCounter(){ return; } +/*! + * Checks if file belongs to a parallel output + * @return Returns true if the file belongs to a parallel output, false otherwise + */ +bool FileHandler::isParallel() const { + return parallel; +} + /*! * sets if file belongs to a parallel output * @param[in] p_ [true/false] if parallel @@ -210,6 +226,14 @@ void FileHandler::setParallel( bool p_){ return; } +/*! + * gets the index of the parallel block + * @return the index of the parallel block + */ +int FileHandler::getBlock( ) const { + return block; +} + /*! * sets the counter of the series * @param[in] b_ index of block of parallel set diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index 17999d20af..d050631787 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -72,14 +72,17 @@ class FileHandler{ void setName( const std::string & n_) ; void setAppendix( const std::string & a_) ; + bool isSeries( ) const ; void setSeries( bool s_) ; int getCounter() const ; void setCounter(int c_); void incrementCounter(); + bool isParallel( ) const ; void setParallel( bool p_) ; + int getBlock( ) const ; void setBlock( int b_) ; }; From 511843a353405e82aa59f66a30b4d14d3fc2e68b Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:42:50 +0200 Subject: [PATCH 04/20] common: fix Doxygen documentation --- src/common/fileHandler.cpp | 4 ++-- src/common/fileHandler.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index fcc34f112f..89a83808c5 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -235,8 +235,8 @@ int FileHandler::getBlock( ) const { } /*! - * sets the counter of the series - * @param[in] b_ index of block of parallel set + * sets the index of the parallel block + * @param[in] b_ index of the parallel block */ void FileHandler::setBlock( int b_){ block=b_; diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index d050631787..32bf0f529f 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -53,7 +53,7 @@ class FileHandler{ int counter ; /**< counter for time series ; */ bool parallel ; /**< is part of distributed data? */ - int block ; /**< index of block if parallel data */ + int block ; /**< index of the parallel block for distributed data */ public: FileHandler() ; From 95df59e5c8dcc2e681dccdd41f7530dfaadf5ce7 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 11:28:16 +0200 Subject: [PATCH 05/20] common: return an invalid counter if a file is not in a time series --- src/common/fileHandler.cpp | 6 +++++- src/common/fileHandler.hpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index 89a83808c5..c284d6a4cc 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -189,7 +189,11 @@ void FileHandler::setSeries( bool s_){ * @return counter */ int FileHandler::getCounter() const { - return counter; + if (series) { + return counter; + } else { + return INVALID_COUNTER; + } } /*! diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index 32bf0f529f..279cc01d9d 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -56,6 +56,8 @@ class FileHandler{ int block ; /**< index of the parallel block for distributed data */ public: + static const int INVALID_COUNTER = -1; /**< dummy counter associate with files that doesn't belong to a time series*/ + FileHandler() ; FileHandler( const std::string & dir_, const std::string & name_, const std::string & app_) ; FileHandler( const FileHandler& other ) ; From 031f0647544632fdb92cb49fb6a843538b2213b0 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 15:23:27 +0200 Subject: [PATCH 06/20] common: return strings by constant reference --- src/common/fileHandler.cpp | 6 +++--- src/common/fileHandler.hpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index c284d6a4cc..786bc2b04b 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -115,7 +115,7 @@ std::string FileHandler::getPath(){ * Get the directory where the file will be saved * @return The directory where the file will be saved. */ -std::string FileHandler::getDirectory() const { +const std::string & FileHandler::getDirectory() const { return directory; } @@ -124,7 +124,7 @@ std::string FileHandler::getDirectory() const { * Get the name of the file * @return The name of the file. */ -std::string FileHandler::getName() const { +const std::string & FileHandler::getName() const { return name; } @@ -133,7 +133,7 @@ std::string FileHandler::getName() const { * Get the appendix of the file * @return The appendix of the file. */ -std::string FileHandler::getAppendix() const { +const std::string & FileHandler::getAppendix() const { return appendix; } diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index 279cc01d9d..ff9bfdfb28 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -67,9 +67,9 @@ class FileHandler{ bool exists() ; std::string getPath() ; - std::string getDirectory() const ; - std::string getName() const ; - std::string getAppendix() const ; + const std::string & getDirectory() const ; + const std::string & getName() const ; + const std::string & getAppendix() const ; void setDirectory( const std::string & d_) ; void setName( const std::string & n_) ; void setAppendix( const std::string & a_) ; From 96844da4e3e9d9169a5a1a425c0e35f423c963e3 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 27 Oct 2023 15:43:35 +0200 Subject: [PATCH 07/20] common: declare some functions as constant --- src/common/fileHandler.cpp | 4 ++-- src/common/fileHandler.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/fileHandler.cpp b/src/common/fileHandler.cpp index 786bc2b04b..4edf8946c5 100644 --- a/src/common/fileHandler.cpp +++ b/src/common/fileHandler.cpp @@ -89,7 +89,7 @@ FileHandler& FileHandler::operator=(const FileHandler& other){ * Checks if file exists. * @return [true/false] if file exists */ -bool FileHandler::exists() { +bool FileHandler::exists() const { std::ifstream f( getPath() ); return f.good() ; } @@ -98,7 +98,7 @@ bool FileHandler::exists() { * Composes the filename. * @return complete filename */ -std::string FileHandler::getPath(){ +std::string FileHandler::getPath() const { std::stringstream filename ; if (!directory.empty()) filename << directory << "/"; diff --git a/src/common/fileHandler.hpp b/src/common/fileHandler.hpp index ff9bfdfb28..3f5419f3d2 100644 --- a/src/common/fileHandler.hpp +++ b/src/common/fileHandler.hpp @@ -64,9 +64,9 @@ class FileHandler{ FileHandler& operator=( const FileHandler& other) ; - bool exists() ; + bool exists() const ; - std::string getPath() ; + std::string getPath() const ; const std::string & getDirectory() const ; const std::string & getName() const ; const std::string & getAppendix() const ; From 01222b06bbcb82317225ac81350317c16459b9bf Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Thu, 26 Oct 2023 15:44:02 +0200 Subject: [PATCH 08/20] IO/config: fix encoding of empty strings --- src/IO/configuration_XML.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/IO/configuration_XML.cpp b/src/IO/configuration_XML.cpp index a709292e69..fe8a7bd8bb 100644 --- a/src/IO/configuration_XML.cpp +++ b/src/IO/configuration_XML.cpp @@ -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; From b1d6b7481eee16f0f5b9afe3d320a9340afa28ff Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:45:34 +0200 Subject: [PATCH 09/20] IO/VTK: add a function to increment the time series counter --- src/IO/VTK.cpp | 14 ++++++++++++++ src/IO/VTK.hpp | 1 + 2 files changed, 15 insertions(+) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index f07e4b7d82..4d1c0827e8 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -159,6 +159,20 @@ void VTK::setCounter( int counter){ } +/*! + * Increment the time series' counter. If the time series is not active, it will + * be activated and the counter will be reset. + */ +void VTK::incrementCounter( ){ + + if (!m_fh.isSeries()) { + setCounter(0); + } else { + m_fh.incrementCounter() ; + } + +} + /*! * De-activates output for time series. * @return last value of counter diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index 9335f44d8e..63d84c2c34 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -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 ) ; From 420961b45af3fafc4670b6d2195693b3369b2592 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:46:12 +0200 Subject: [PATCH 10/20] IO/VTK: use proper function to get the time series counter --- src/IO/VTK.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 4d1c0827e8..15f7684936 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -179,7 +179,7 @@ void VTK::incrementCounter( ){ */ int VTK::unsetCounter( ){ - int counter = m_fh.getCounter() ; + int counter = getCounter() ; m_fh.setSeries(false) ; return counter; From 433cd85bcc248e89f60fce2755857402706d1e45 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:47:04 +0200 Subject: [PATCH 11/20] IO/VTK: remove unneeded trailing whitespaces --- src/IO/VTK.cpp | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 15f7684936..bb9424a172 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -33,14 +33,14 @@ namespace bitpit{ /*! * @ingroup VisualizationToolKit * @interface VTK - * @brief A base class for VTK input output. + * @brief A base class for VTK input output. * * VTK provides all basic methods for reading and writing VTK files. * ASCII and APPENDED mode are supported. * */ -/*! +/*! * Default constructor referes to a serial VTK file with appended binary data. */ VTK::VTK(){ @@ -49,7 +49,7 @@ VTK::VTK(){ m_rank = 0; m_headerType = "UInt32" ; - + m_fh.setDirectory( "." ) ; m_fh.setSeries( false ) ; m_fh.setParallel( false ) ; @@ -62,7 +62,7 @@ VTK::VTK(){ } -/*! +/*! * Constructor referes to a serial VTK file with appended binary data. * @param[in] dir directory of file with final "/" * @param[in] name file name without suffix @@ -74,7 +74,7 @@ VTK::VTK(const std::string &dir, const std::string &name ): } -/*! +/*! * set header type for appended binary output * @param[in] st header type ["UInt32"/"UInt64"] */ @@ -90,7 +90,7 @@ void VTK::setHeaderType(const std::string &st){ } -/*! +/*! * Get header type for appended binary output * @return header type ["UInt32"/"UInt64"] */ @@ -98,7 +98,7 @@ const std::string & VTK::getHeaderType( ) const{ return m_headerType ; } -/*! +/*! * set directory and name for VTK file * @param[in] dir directory of file with final "/" * @param[in] name file name without suffix @@ -152,7 +152,7 @@ std::string VTK::getDirectory() const { * Activates output for time series. sets series to true first output index to input * @param[in] counter first output index */ -void VTK::setCounter( int counter){ +void VTK::setCounter( int counter){ m_fh.setSeries(true) ; m_fh.setCounter(counter) ; @@ -174,22 +174,22 @@ void VTK::incrementCounter( ){ } /*! - * De-activates output for time series. + * De-activates output for time series. * @return last value of counter */ -int VTK::unsetCounter( ){ +int VTK::unsetCounter( ){ int counter = getCounter() ; m_fh.setSeries(false) ; - return counter; + return counter; } /*! * Returns the time index of the following file - * @return counter + * @return counter */ -int VTK::getCounter( ) const{ +int VTK::getCounter( ) const{ return m_fh.getCounter( ) ; @@ -200,13 +200,13 @@ int VTK::getCounter( ) const{ * @param[in] procs number of processes * @param[in] rank my rank */ -void VTK::setParallel( uint16_t procs, uint16_t rank){ +void VTK::setParallel( uint16_t procs, uint16_t rank){ if( procs < 1 ) log::cout() << " Numer of processes must be greater than 0" << std::endl ; if( rank >= procs) log::cout() << " m_rankess is not in valid range " << std::endl ; - m_procs = procs; - m_rank = rank; + m_procs = procs; + m_rank = rank; if(m_procs == 0) { m_fh.setParallel(false) ; @@ -296,7 +296,7 @@ VTKField& VTK::addData( VTKField &&field ){ } /*! - * Add user data for input or output. + * Add user data for input or output. * Codification will be set according to default value [appended] or to value * set by VTK::setDataCodex( VTKFormat ) or VTK::setCodex( VTKFormat ) * @param[in] name name of field @@ -337,7 +337,7 @@ void VTK::removeData( const std::string &name ){ } else { ++fieldItr; } - } + } log::cout() << "did not find field for removing in VTK: " << name << std::endl; @@ -886,7 +886,7 @@ void VTK::writeData( ){ assert(false); } } - } + } for( auto &field : m_data ){ if( field.isEnabled() && field.getCodification() == VTKFormat::APPENDED && field.getLocation() == VTKLocation::CELL ) { @@ -909,7 +909,7 @@ void VTK::writeData( ){ } } - } + } //Writing Geometry Data for( auto &field : m_geometry ){ @@ -996,7 +996,7 @@ void VTK::writeDataHeader( std::fstream &str, bool parallel ){ //Writing DataArray for( auto &field : m_data ){ if( field.isEnabled() && field.getLocation() == location && !parallel) writeDataArray( str, field ) ; - if( field.isEnabled() && field.getLocation() == location && parallel) writePDataArray( str, field ); + if( field.isEnabled() && field.getLocation() == location && parallel) writePDataArray( str, field ); } str << " " ; loc = ss.str(); - read= true ; + read= true ; if( ! getline( str, line) ) read = false ; if( bitpit::utils::string::keywordInString( line, loc) ) read=false ; @@ -1225,7 +1225,7 @@ void VTK::readDataHeader( std::fstream &str ){ } else{ - pos_ = 0 ; + pos_ = 0 ; } temp.setPosition( pos_ ) ; @@ -1288,7 +1288,7 @@ bool VTK::readDataArray( std::fstream &str, VTKField &field ){ } } - return false ; + return false ; } From a9c22723f7b2e4ca6a2388e112874cd04a377315 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 10:57:16 +0200 Subject: [PATCH 12/20] IO/VTK: simplify handling of time series counter --- src/IO/VTK.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index bb9424a172..3099e61f4f 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -700,30 +700,30 @@ void VTK::checkAllFields(){ */ void VTK::write( VTKWriteMode writeMode ){ - int counter(0); - + // Initialize counter + int counter = getCounter(); if( writeMode == VTKWriteMode::NO_SERIES ){ - counter = unsetCounter() ; - } - - if( writeMode == VTKWriteMode::NO_INCREMENT ){ - counter = getCounter() -1 ; - setCounter( counter) ; - } + unsetCounter() ; + } else if( writeMode == VTKWriteMode::NO_INCREMENT ){ + setCounter(counter - 1) ; + } + // Write data checkAllFields() ; calcAppendedOffsets() ; writeMetaInformation() ; writeData() ; - if( m_procs > 1 && m_rank == 0) writeCollection() ; - - if( writeMode == VTKWriteMode::DEFAULT || writeMode == VTKWriteMode::NO_INCREMENT ){ - m_fh.incrementCounter() ; + // Write collection + if( m_procs > 1 && m_rank == 0){ + writeCollection() ; } - if( writeMode == VTKWriteMode::NO_SERIES ){ + // Update counter + if( writeMode == VTKWriteMode::DEFAULT || writeMode == VTKWriteMode::NO_INCREMENT ){ + incrementCounter() ; + } else if( writeMode == VTKWriteMode::NO_SERIES ){ setCounter(counter) ; } From 3b05b0af3a31c44349a51b2486b8e32117073e74 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 25 Oct 2023 11:18:03 +0200 Subject: [PATCH 13/20] IO/VTK: declare some functions as protected --- src/IO/VTK.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index 63d84c2c34..cb7adfede1 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -377,12 +377,13 @@ class VTK{ void write( VTKWriteMode writeMode=VTKWriteMode::DEFAULT ) ; void write( const std::string &, VTKWriteMode writeMode=VTKWriteMode::NO_INCREMENT ) ; - virtual void writeMetaInformation() = 0 ; - void writeData() ; virtual void writeCollection() = 0 ; protected: //For Writing + virtual void writeMetaInformation() = 0 ; + void writeData() ; + void writeDataHeader( std::fstream &, bool parallel=false ) ; void writeDataArray( std::fstream &, VTKField &) ; void writePDataArray( std::fstream &, VTKField &) ; From ea066e5a7cecbbbac0c3b6c4e7990743c507f4bd Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 21 Nov 2023 15:06:14 +0100 Subject: [PATCH 14/20] IO/VTK: throw an error if the collection for a rectilinear grid cannot be created --- src/IO/VTKRectilinear.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index f62ead5600..f278988905 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -295,6 +295,9 @@ void VTKRectilinearGrid::writeCollection( ){ fho.setDirectory(".") ; 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 << "" << std::endl; From 45d0a582c5dd1e004b87e6e3c05631c64fe2c6fe Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 27 Oct 2023 15:13:21 +0200 Subject: [PATCH 15/20] IO/VTK: allow to write a collection with a custom name --- src/IO/VTK.cpp | 18 ++++++++++++++++++ src/IO/VTK.hpp | 13 +++++++------ src/IO/VTKRectilinear.cpp | 19 +++++++++++-------- src/IO/VTKUnstructured.cpp | 19 +++++++++++-------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 3099e61f4f..5dc62b3be9 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -746,6 +746,24 @@ void VTK::write( const std::string &name, VTKWriteMode writeMode ){ } +/*! + * Writes collection file for parallel output. + * Is called by rank 0 in VTK::Write() + */ +void VTK::writeCollection( ) { + writeCollection(getName(), getName()) ; +} + +/*! + * Writes collection file for parallel output. + * Is called by rank 0 in VTK::Write() + * + * \param outputName filename to be set for this output only + */ +void VTK::writeCollection( const std::string &outputName ) { + writeCollection(outputName, outputName) ; +} + /*! * Writes data only in VTK file */ diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index cb7adfede1..f110850c39 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -377,7 +377,9 @@ class VTK{ void write( VTKWriteMode writeMode=VTKWriteMode::DEFAULT ) ; void write( const std::string &, VTKWriteMode writeMode=VTKWriteMode::NO_INCREMENT ) ; - virtual void writeCollection() = 0 ; + void writeCollection( ) ; + void writeCollection( const std::string &outputName ) ; + virtual void writeCollection( const std::string &outputName, const std::string &collectionName ) = 0; protected: //For Writing @@ -444,8 +446,6 @@ 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( ) ; @@ -457,6 +457,8 @@ class VTKUnstructuredGrid : public VTK { void readMetaInformation() override ; void writeMetaInformation() override ; + void writeCollection( const std::string &outputName, const std::string &collectionName ) override ; + void setDimensions( uint64_t , uint64_t , uint64_t nconn = 0 , uint64_t nfacestream = 0 ) ; template @@ -495,15 +497,14 @@ class VTKRectilinearGrid : public VTK{ VTKRectilinearGrid( const std::string & , const std::string & , VTKFormat, int, int, int, int ); VTKRectilinearGrid( const std::string & , const std::string & , VTKFormat, int, int ); - protected: - void writeCollection() override ; - public: using VTK::setGeomData; void readMetaInformation() override ; void writeMetaInformation() override ; + void writeCollection( const std::string &outputName, const std::string &collectionName ) override ; + void setDimensions( int, int, int, int, int, int ) ; void setDimensions( int, int, int ) ; void setDimensions( int, int, int, int ) ; diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index f278988905..5dc7b11bdd 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -279,21 +279,20 @@ 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( ){ +void VTKRectilinearGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) { std::fstream str ; - FileHandler fhp, fho ; - - fhp = m_fh ; - fho = m_fh ; - + FileHandler fhp(m_fh) ; + fhp.setSeries(false) ; fhp.setParallel(false) ; + fhp.setName(collectionName) ; fhp.setAppendix("pvtr") ; - fho.setDirectory(".") ; - 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() + "\""); @@ -323,6 +322,10 @@ void VTKRectilinearGrid::writeCollection( ){ str << " " << std::endl; + FileHandler fho(m_fh) ; + fho.setSeries(false) ; + fho.setDirectory(".") ; + fho.setName(outputName) ; for( int i=0; i" << std::endl; + FileHandler fho(m_fh) ; + fho.setSeries(false) ; + fho.setDirectory(".") ; + fho.setName(outputName) ; for( int i=0; i" << std::endl; From 06ba757e34eebf628dd9e95476eed1fc4bcf05d7 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 27 Oct 2023 15:21:04 +0200 Subject: [PATCH 16/20] IO/VTK: check if collection should be written inside writeCollection --- src/IO/VTK.cpp | 4 +--- src/IO/VTKRectilinear.cpp | 7 ++++++- src/IO/VTKUnstructured.cpp | 7 ++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 5dc62b3be9..6bf576a893 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -716,9 +716,7 @@ void VTK::write( VTKWriteMode writeMode ){ writeData() ; // Write collection - if( m_procs > 1 && m_rank == 0){ - writeCollection() ; - } + writeCollection() ; // Update counter if( writeMode == VTKWriteMode::DEFAULT || writeMode == VTKWriteMode::NO_INCREMENT ){ diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index 5dc7b11bdd..3e0b4fd9d9 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -278,13 +278,18 @@ 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( const std::string &outputName, const std::string &collectionName ) { + // Only one process in a parallel output should write the collection + if (m_procs <= 1 || m_rank != 0) { + return; + } + + // Initialize file handler std::fstream str ; FileHandler fhp(m_fh) ; diff --git a/src/IO/VTKUnstructured.cpp b/src/IO/VTKUnstructured.cpp index 7936dd3e11..6aa37d06d2 100644 --- a/src/IO/VTKUnstructured.cpp +++ b/src/IO/VTKUnstructured.cpp @@ -494,13 +494,18 @@ 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( const std::string &outputName, const std::string &collectionName ) { + // Only one process in a parallel output should write the collection + if (m_procs <= 1 || m_rank != 0) { + return; + } + + // Initialize file handler std::fstream str ; FileHandler fhp(m_fh) ; From cc221d8fa442de580989cacc13832e6df96a0db2 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 27 Oct 2023 16:11:00 +0200 Subject: [PATCH 17/20] IO/VTK: declare some functions as constant --- src/IO/VTK.cpp | 12 +++++----- src/IO/VTK.hpp | 48 +++++++++++++++++++------------------- src/IO/VTKRectilinear.cpp | 12 +++++----- src/IO/VTKUnstructured.cpp | 14 +++++------ 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 6bf576a893..cd271ac1a0 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -748,7 +748,7 @@ void VTK::write( const std::string &name, VTKWriteMode writeMode ){ * Writes collection file for parallel output. * Is called by rank 0 in VTK::Write() */ -void VTK::writeCollection( ) { +void VTK::writeCollection( ) const { writeCollection(getName(), getName()) ; } @@ -758,7 +758,7 @@ void VTK::writeCollection( ) { * * \param outputName filename to be set for this output only */ -void VTK::writeCollection( const std::string &outputName ) { +void VTK::writeCollection( const std::string &outputName ) const { writeCollection(outputName, outputName) ; } @@ -964,7 +964,7 @@ void VTK::writeData( ){ * @param[in] str output stream * @param[in] parallel flag for parallel data headers for collection files [true/false] */ -void VTK::writeDataHeader( std::fstream &str, bool parallel ){ +void VTK::writeDataHeader( std::fstream &str, bool parallel ) const { VTKLocation location ; std::stringstream scalars, vectors ; @@ -1030,7 +1030,7 @@ void VTK::writeDataHeader( std::fstream &str, bool parallel ){ * @param[in] str output stream * @param[in] field field to be written */ -void VTK::writeDataArray( std::fstream &str, VTKField &field ){ +void VTK::writeDataArray( std::fstream &str, const VTKField &field ) const { str << vtk::convertDataArrayToString( field ) << std::endl ; str << " " << std::endl ; @@ -1042,7 +1042,7 @@ void VTK::writeDataArray( std::fstream &str, VTKField &field ){ * @param[in] str output stream * @param[in] field field to be written */ -void VTK::writePDataArray( std::fstream &str, VTKField &field ){ +void VTK::writePDataArray( std::fstream &str, const VTKField &field ) const { str << vtk::convertPDataArrayToString( field ) << std::endl ; str << " " << std::endl ; @@ -1286,7 +1286,7 @@ void VTK::readDataHeader( std::fstream &str ){ * @param[in] str output stream * @param[out] field field information */ -bool VTK::readDataArray( std::fstream &str, VTKField &field ){ +bool VTK::readDataArray( std::fstream &str, VTKField &field ) const { std::string line ; diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index f110850c39..92f5cc7ead 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -377,22 +377,22 @@ class VTK{ void write( VTKWriteMode writeMode=VTKWriteMode::DEFAULT ) ; void write( const std::string &, VTKWriteMode writeMode=VTKWriteMode::NO_INCREMENT ) ; - void writeCollection( ) ; - void writeCollection( const std::string &outputName ) ; - virtual void writeCollection( const std::string &outputName, const std::string &collectionName ) = 0; + void writeCollection( ) const ; + void writeCollection( const std::string &outputName ) const ; + virtual void writeCollection( const std::string &outputName, const std::string &collectionName ) const = 0; protected: //For Writing - virtual void writeMetaInformation() = 0 ; + virtual void writeMetaInformation() const = 0 ; void writeData() ; - void writeDataHeader( std::fstream &, bool parallel=false ) ; - void writeDataArray( std::fstream &, VTKField &) ; - void writePDataArray( std::fstream &, VTKField &) ; + void writeDataHeader( std::fstream &, bool parallel=false ) const ; + void writeDataArray( std::fstream &, const VTKField &) const ; + void writePDataArray( std::fstream &, const VTKField &) const ; //For Reading void readDataHeader( std::fstream &) ; - bool readDataArray( std::fstream &, VTKField &); + bool readDataArray( std::fstream &, VTKField &) const ; //General Purpose std::vector getFieldNames( const std::vector &fields ) const; @@ -411,10 +411,10 @@ 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(); }; @@ -455,9 +455,9 @@ class VTKUnstructuredGrid : public VTK { using VTK::setGeomData; void readMetaInformation() override ; - void writeMetaInformation() override ; + void writeMetaInformation() const override ; - void writeCollection( const std::string &outputName, const std::string &collectionName ) 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 ) ; @@ -468,13 +468,13 @@ class VTKUnstructuredGrid : public VTK { template 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 ; }; @@ -501,9 +501,9 @@ class VTKRectilinearGrid : public VTK{ using VTK::setGeomData; void readMetaInformation() override ; - void writeMetaInformation() override ; + void writeMetaInformation() const override ; - void writeCollection( const std::string &outputName, const std::string &collectionName ) 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 ) ; @@ -523,9 +523,9 @@ class VTKRectilinearGrid : public VTK{ void setGlobalIndex( const std::vector & ) ; void setGlobalIndex( const std::vector & ) ; - 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 ; }; /*! diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index 3e0b4fd9d9..729db078be 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -221,7 +221,7 @@ void VTKRectilinearGrid::readMetaInformation( ){ /*! * Writes entire VTR but the data. */ -void VTKRectilinearGrid::writeMetaInformation( ){ +void VTKRectilinearGrid::writeMetaInformation( ) const { std::fstream str; @@ -282,7 +282,7 @@ void VTKRectilinearGrid::writeMetaInformation( ){ * \param outputName filename to be set for this output only * \param collectionName collection filename to be set for this output only */ -void VTKRectilinearGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) { +void VTKRectilinearGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) const { // Only one process in a parallel output should write the collection if (m_procs <= 1 || m_rank != 0) { @@ -333,7 +333,7 @@ void VTKRectilinearGrid::writeCollection( const std::string &outputName, const s fho.setName(outputName) ; for( int i=0; i &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() ) ; @@ -519,7 +519,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() ; @@ -559,7 +559,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() ; diff --git a/src/IO/VTKUnstructured.cpp b/src/IO/VTKUnstructured.cpp index 6aa37d06d2..98abd90fee 100644 --- a/src/IO/VTKUnstructured.cpp +++ b/src/IO/VTKUnstructured.cpp @@ -438,7 +438,7 @@ uint64_t VTKUnstructuredGrid::readFaceStreamEntries( ){ /*! * Writes entire VTU but the data. */ -void VTKUnstructuredGrid::writeMetaInformation( ){ +void VTKUnstructuredGrid::writeMetaInformation( ) const { std::fstream str ; @@ -498,7 +498,7 @@ void VTKUnstructuredGrid::writeMetaInformation( ){ * \param outputName filename to be set for this output only * \param collectionName collection filename to be set for this output only */ -void VTKUnstructuredGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) { +void VTKUnstructuredGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) const { // Only one process in a parallel output should write the collection if (m_procs <= 1 || m_rank != 0) { @@ -633,7 +633,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)] ) ; } @@ -643,7 +643,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() ) ; @@ -657,7 +657,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() ; @@ -706,7 +706,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() ; @@ -749,7 +749,7 @@ 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::type>(field) ; } From de9c3a8f1946651497c9b2761dfce84d879a46dd Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 21 Nov 2023 15:07:45 +0100 Subject: [PATCH 18/20] IO/VTK: add a function to get file extensions --- src/IO/VTK.cpp | 9 +++++++++ src/IO/VTK.hpp | 8 ++++++++ src/IO/VTKRectilinear.cpp | 13 +++++++++++-- src/IO/VTKUnstructured.cpp | 13 +++++++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index cd271ac1a0..5c7818dbe8 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -1308,4 +1308,13 @@ bool VTK::readDataArray( std::fstream &str, VTKField &field ) const { } +/*! + * Gets the extension of the collection file for parallel output. + * + * \result The extension of the collection file for parallel output. + */ +std::string VTK::getCollectionExtension() const { + return "p" + getExtension(); +} + } diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index 92f5cc7ead..d0b6a9791b 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -416,6 +416,9 @@ class VTK{ virtual uint8_t calcFieldComponents( const VTKField &) const = 0; void checkAllFields(); + virtual std::string getExtension() const = 0 ; + virtual std::string getCollectionExtension() const; + }; class VTKUnstructuredGrid : public VTK { @@ -451,6 +454,8 @@ class VTKUnstructuredGrid : public VTK { void setElementType( VTKElementType ) ; + std::string getExtension() const override; + public: using VTK::setGeomData; @@ -497,6 +502,9 @@ class VTKRectilinearGrid : public VTK{ VTKRectilinearGrid( const std::string & , const std::string & , VTKFormat, int, int, int, int ); VTKRectilinearGrid( const std::string & , const std::string & , VTKFormat, int, int ); + protected: + std::string getExtension() const override; + public: using VTK::setGeomData; diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index 729db078be..09f192e64a 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -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") ) ; @@ -296,7 +296,7 @@ void VTKRectilinearGrid::writeCollection( const std::string &outputName, const s fhp.setSeries(false) ; fhp.setParallel(false) ; fhp.setName(collectionName) ; - fhp.setAppendix("pvtr") ; + fhp.setAppendix(getCollectionExtension()) ; str.open( fhp.getPath( ), std::ios::out ) ; if (!str.is_open()) { @@ -576,4 +576,13 @@ uint8_t VTKRectilinearGrid::calcFieldComponents( const VTKField &field ) const { } +/*! + * Gets the extension of the VTK file. + * + * \result The extension of the VTK file. + */ +std::string VTKRectilinearGrid::getExtension() const { + return "vtr"; +} + } diff --git a/src/IO/VTKUnstructured.cpp b/src/IO/VTKUnstructured.cpp index 98abd90fee..8be20ce949 100644 --- a/src/IO/VTKUnstructured.cpp +++ b/src/IO/VTKUnstructured.cpp @@ -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; @@ -512,7 +512,7 @@ void VTKUnstructuredGrid::writeCollection( const std::string &outputName, const fhp.setSeries(false) ; fhp.setParallel(false) ; fhp.setName(collectionName) ; - fhp.setAppendix("pvtu") ; + fhp.setAppendix(getCollectionExtension()) ; str.open( fhp.getPath( ), std::ios::out ) ; if (!str.is_open()) { @@ -754,4 +754,13 @@ int VTKUnstructuredGrid::getFieldGeomId( VTKUnstructuredField field ) const { return static_cast::type>(field) ; } +/*! + * Gets the extension of the VTK file. + * + * \result The extension of the VTK file. + */ +std::string VTKUnstructuredGrid::getExtension() const { + return "vtu"; +} + } From 2c8844bad4b0e51d9eb41d9c80ad220df71ddb41 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 21 Nov 2023 15:10:56 +0100 Subject: [PATCH 19/20] IO/VTK: add a function to create the handler for a collection --- src/IO/VTK.cpp | 15 +++++++++++++++ src/IO/VTK.hpp | 2 ++ src/IO/VTKRectilinear.cpp | 12 ++++-------- src/IO/VTKUnstructured.cpp | 12 ++++-------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 5c7818dbe8..4b83f218ca 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -762,6 +762,21 @@ void VTK::writeCollection( const std::string &outputName ) const { writeCollection(outputName, outputName) ; } +/*! + * Creates a handler for the collection. + * + * \param collectionName collection filename to be set for this output only + */ +FileHandler VTK::createCollectionHandler( const std::string &collectionName ) const { + FileHandler handler(m_fh) ; + handler.setSeries(false) ; + handler.setParallel(false) ; + handler.setName(collectionName) ; + handler.setAppendix(getCollectionExtension()) ; + + return handler; +} + /*! * Writes data only in VTK file */ diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index d0b6a9791b..9993c4f939 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -390,6 +390,8 @@ class VTK{ 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 &) const ; diff --git a/src/IO/VTKRectilinear.cpp b/src/IO/VTKRectilinear.cpp index 09f192e64a..a47e0ebf71 100644 --- a/src/IO/VTKRectilinear.cpp +++ b/src/IO/VTKRectilinear.cpp @@ -289,15 +289,11 @@ void VTKRectilinearGrid::writeCollection( const std::string &outputName, const s return; } - // Initialize file handler - std::fstream str ; - - FileHandler fhp(m_fh) ; - fhp.setSeries(false) ; - fhp.setParallel(false) ; - fhp.setName(collectionName) ; - fhp.setAppendix(getCollectionExtension()) ; + // 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() + "\""); diff --git a/src/IO/VTKUnstructured.cpp b/src/IO/VTKUnstructured.cpp index 8be20ce949..8fde0ccbe5 100644 --- a/src/IO/VTKUnstructured.cpp +++ b/src/IO/VTKUnstructured.cpp @@ -505,15 +505,11 @@ void VTKUnstructuredGrid::writeCollection( const std::string &outputName, const return; } - // Initialize file handler - std::fstream str ; - - FileHandler fhp(m_fh) ; - fhp.setSeries(false) ; - fhp.setParallel(false) ; - fhp.setName(collectionName) ; - fhp.setAppendix(getCollectionExtension()) ; + // 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() + "\""); From 17a46a6870afbdc3230ca76e852507f54a9ebcf8 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 27 Oct 2023 15:01:03 +0200 Subject: [PATCH 20/20] IO/VTK: add support for writing time series --- src/IO/VTK.cpp | 164 +++++++++++++++++++++++++++++++ src/IO/VTK.hpp | 6 ++ src/patchkernel/patch_kernel.cpp | 54 +++++++++- src/patchkernel/patch_kernel.hpp | 5 + 4 files changed, 226 insertions(+), 3 deletions(-) diff --git a/src/IO/VTK.cpp b/src/IO/VTK.cpp index 4b83f218ca..ede4ed74b2 100644 --- a/src/IO/VTK.cpp +++ b/src/IO/VTK.cpp @@ -23,6 +23,8 @@ \*---------------------------------------------------------------------------*/ #include +#include +#include #include #include "VTK.hpp" @@ -692,6 +694,25 @@ void VTK::checkAllFields(){ } +/*! + * Writes entire VTK file (headers and data). + * @param[in] writeMode if writeMode == VTKWriteMode::DEFAULT the default write setting will be used according to setCounter(); + * if writeMode == VTKWriteMode::NO_SERIES no time stamp will be added and the counter will not be increased; + * if writeMode == VTKWriteMode::NO_INCREMENT the output file will have the same time stamp like the previous one ; + * @param[in] time is the time associated with the file + */ +void VTK::write( VTKWriteMode writeMode, double time ){ + + // Write VTK file + write(writeMode); + + // Write time series file + if( writeMode == VTKWriteMode::DEFAULT || writeMode == VTKWriteMode::NO_INCREMENT ){ + writeTimeSeries(time); + } + +} + /*! * Writes entire VTK file (headers and data). * @param[in] writeMode if writeMode == VTKWriteMode::DEFAULT the default write setting will be used according to setCounter(); @@ -741,6 +762,23 @@ void VTK::write( const std::string &name, VTKWriteMode writeMode ){ setName(name) ; write(writeMode) ; setName(oldName) ; +} + +/*! + * Writes entire VTK file (headers and data). + * @param[in] name filename to be set for this output only + * @param[in] writeMode if writeMode == VTKWriteMode::DEFAULT the default write setting will be used according to setCounter(); + * if writeMode == VTKWriteMode::NO_SERIES no time stamp will be added and the counter will not be increased; + * if writeMode == VTKWriteMode::NO_INCREMENT the output file will have the same time stamp like the previous one ; + * @param[in] time is the time associated with the file + */ +void VTK::write( const std::string &name, VTKWriteMode writeMode, double time ){ + + std::string oldName = getName() ; + + setName(name) ; + write(writeMode, time) ; + setName(oldName) ; } @@ -762,6 +800,132 @@ void VTK::writeCollection( const std::string &outputName ) const { writeCollection(outputName, outputName) ; } +/*! + * Writes time series file. + * + * If the file doesn't exists or if this is the first data set of the series + * (i.e., the counter is equal to zero), the time series will be written from + * scratch and will contain only the current data set. Otherwise, the existing + * time series will be inspected and the current data set will be either added + * at the end of the series or will it will replace the entry with the same + * file name it such an entry exists. + * + * \param time is the time associated with the data set + */ +void VTK::writeTimeSeries( double time ) const { + writeTimeSeries(getName(), getName(), time) ; +} + +/*! + * Writes time series file. + * + * If the file doesn't exists or if this is the first data set of the series + * (i.e., the counter is equal to zero), the time series will be written from + * scratch and will contain only the current data set. Otherwise, the existing + * time series will be inspected and the current data set will be either added + * at the end of the series or will it will replace the entry with the same + * file name it such an entry exists. + * + * \param outputName filename to be set for this output only + * \param time is the time associated with the data set + */ +void VTK::writeTimeSeries( const std::string &outputName, double time ) const { + writeTimeSeries(outputName, outputName, time) ; +} + +/*! + * Writes time series file. + * + * If the file doesn't exists or if this is the first data set of the series + * (i.e., the counter is equal to zero), the time series will be written from + * scratch and will contain only the current data set. Otherwise, the existing + * time series will be inspected and the current data set will be either added + * at the end of the series or will it will replace the entry with the same + * file name it such an entry exists. + * + * \param outputName filename to be set for this output only + * \param seriesName series filename to be set for this output only + * \param time is the time associated with the data set + */ +void VTK::writeTimeSeries( const std::string &outputName, const std::string &seriesName, double time ) const { + + // Only one process should write the time series + if (m_rank != 0) { + return; + } + + // Initialize series file handle + FileHandler seriesFileHandler = m_fh ; + seriesFileHandler.setSeries(false) ; + seriesFileHandler.setParallel(false) ; + seriesFileHandler.setName(seriesName) ; + seriesFileHandler.setAppendix("pvd") ; + + // Initialize series + if (!seriesFileHandler.exists() || getCounter() == 0) { + std::fstream emptySeriesFileStream; + emptySeriesFileStream.open( seriesFileHandler.getPath(), std::ios::out | std::ios::trunc ) ; + if (!emptySeriesFileStream.is_open()) { + throw std::runtime_error("Cannot create file \"" + seriesFileHandler.getName() + "\"" + " inside the directory \"" + seriesFileHandler.getDirectory() + "\""); + } + + emptySeriesFileStream << "" << std::endl; + emptySeriesFileStream << "" << std::endl; + emptySeriesFileStream << " " << std::endl; + emptySeriesFileStream << " " << std::endl; + emptySeriesFileStream << "" << std::endl; + emptySeriesFileStream.close(); + } + + // Create updated series + FileHandler dataSetFileHandler; + if (m_procs <= 1) { + dataSetFileHandler = m_fh ; + dataSetFileHandler.setSeries(false) ; + dataSetFileHandler.setParallel(false) ; + dataSetFileHandler.setName(outputName); + } else { + dataSetFileHandler = createCollectionHandler(outputName) ; + } + dataSetFileHandler.setDirectory("."); + + std::string line; + std::stringstream series; + + std::fstream seriesFileStream; + seriesFileStream.open( seriesFileHandler.getPath( ), std::ios::in ) ; + if (!seriesFileStream.is_open()) { + throw std::runtime_error("Cannot read file \"" + seriesFileHandler.getName() + "\"" + " inside the directory \"" + seriesFileHandler.getDirectory() + "\""); + } + + while (std::getline(seriesFileStream, line)) { + bool duplicateEntry = (line.find("file=\"" + dataSetFileHandler.getPath() + "\"") != std::string::npos); + bool collectionEnd = (line.find("") != std::string::npos); + + if (collectionEnd || duplicateEntry) { + series << " " << std::endl; + } + + if (!duplicateEntry) { + series << line << std::endl; + } + } + seriesFileStream.close(); + + // Write updated series + std::ofstream updatedSeriesFileStream; + updatedSeriesFileStream.open( seriesFileHandler.getPath( )) ; + if (!updatedSeriesFileStream.is_open()) { + throw std::runtime_error("Cannot create file \"" + seriesFileHandler.getName() + "\"" + " inside the directory \"" + seriesFileHandler.getDirectory() + "\""); + } + + updatedSeriesFileStream << series.rdbuf(); + updatedSeriesFileStream.close(); +} + /*! * Creates a handler for the collection. * diff --git a/src/IO/VTK.hpp b/src/IO/VTK.hpp index 9993c4f939..de8a0e9473 100644 --- a/src/IO/VTK.hpp +++ b/src/IO/VTK.hpp @@ -374,13 +374,19 @@ 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 ) ; 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 virtual void writeMetaInformation() const = 0 ; diff --git a/src/patchkernel/patch_kernel.cpp b/src/patchkernel/patch_kernel.cpp index ea04de3b11..c7b637ee0a 100644 --- a/src/patchkernel/patch_kernel.cpp +++ b/src/patchkernel/patch_kernel.cpp @@ -1173,11 +1173,54 @@ void PatchKernel::write(const std::string &filename, VTKWriteMode mode) } /*! - Writes the patch a filename with the same name of the patch. + Writes the patch to filename specified in input. + + \param filename the filename where the patch will be written to + \param mode is the VTK file mode that will be used for writing the patch + \param time is the current time +*/ +void PatchKernel::write(const std::string &filename, VTKWriteMode mode, double time) +{ + std::string oldFilename = m_vtk.getName(); + + m_vtk.setName(filename); + write(mode, time); + m_vtk.setName(oldFilename); +} + +/*! + Writes the patch to filename specified in input. \param mode is the VTK file mode that will be used for writing the patch */ void PatchKernel::write(VTKWriteMode mode) +{ + _writePrepare(); + + m_vtk.write(mode); + + _writeFinalize(); +} + +/*! + Writes the patch a filename with the same name of the patch. + + \param mode is the VTK file mode that will be used for writing the patch + \param time is the current time +*/ +void PatchKernel::write(VTKWriteMode mode, double time) +{ + _writePrepare(); + + m_vtk.write(mode, time); + + _writeFinalize(); +} + +/*! + Internal function to be called before writing the patch. +*/ +void PatchKernel::_writePrepare() { // Get VTK cell count long vtkCellCount = 0; @@ -1246,9 +1289,14 @@ void PatchKernel::write(VTKWriteMode mode) } m_vtk.setDimensions(vtkCellCount, vtkVertexCount, vtkConnectSize, vtkFaceStreamSize); +} - // Write the mesh - m_vtk.write(mode); +/*! + Internal function to be called after writing the patch. +*/ +void PatchKernel::_writeFinalize() +{ + // Nothing to do } /*! diff --git a/src/patchkernel/patch_kernel.hpp b/src/patchkernel/patch_kernel.hpp index c121c02469..a0ddfa4ad2 100644 --- a/src/patchkernel/patch_kernel.hpp +++ b/src/patchkernel/patch_kernel.hpp @@ -682,7 +682,9 @@ friend class PatchManager; void setVTKWriteTarget(WriteTarget targetCells); const CellConstRange getVTKCellWriteRange() const; void write(VTKWriteMode mode = VTKWriteMode::DEFAULT); + void write(VTKWriteMode mode, double time); void write(const std::string &name, VTKWriteMode mode = VTKWriteMode::DEFAULT); + void write(const std::string &name, VTKWriteMode mode, double time); void flushData(std::fstream &stream, const std::string &name, VTKFormat format) override; @@ -903,6 +905,9 @@ friend class PatchManager; virtual void _findCellEdgeNeighs(long id, int edge, const std::vector *blackList, std::vector *neighs) const; virtual void _findCellVertexNeighs(long id, int vertex, const std::vector *blackList, std::vector *neighs) const; + virtual void _writePrepare(); + virtual void _writeFinalize(); + void setExpert(bool expert); void extractEnvelope(PatchKernel &envelope) const;