From 2343b6c52052173bc4b53f84874bc173cc8a5008 Mon Sep 17 00:00:00 2001
From: Lukas Werner <lks.werner@fau.de>
Date: Sun, 4 Feb 2018 12:34:41 +0100
Subject: [PATCH] Refactored output filename formatting

---
 src/pe/raytracing/Raytracer.cpp | 56 +++++++++++++++++++++------------
 src/pe/raytracing/Raytracer.h   | 33 +++++++++++--------
 2 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/src/pe/raytracing/Raytracer.cpp b/src/pe/raytracing/Raytracer.cpp
index ee09cbd82..94d92b885 100644
--- a/src/pe/raytracing/Raytracer.cpp
+++ b/src/pe/raytracing/Raytracer.cpp
@@ -63,9 +63,10 @@ Raytracer::Raytracer(const shared_ptr<BlockStorage> forest, BlockDataID storageI
    tBufferOutputEnabled_(false),
    imageOutputEnabled_(false),
    localImageOutputEnabled_(false),
-   outputFilenameTimestepZeroPadding_(4)
+   filenameTimestepWidth_(5)
 {
    setupView_();
+   setupFilenameRankWidth_();
 }
 
 /*!\brief Instantiation constructor for the Raytracer class using a config object for view setup.
@@ -108,7 +109,7 @@ Raytracer::Raytracer(const shared_ptr<BlockStorage> forest, BlockDataID storageI
       WALBERLA_ABORT("Cannot enable local image output without image_output_directory parameter being set.");
    }
       
-   outputFilenameTimestepZeroPadding_ = config.getParameter<int8_t>("outputFilenameTimestepZeroPadding", int8_t(4));
+   filenameTimestepWidth_ = config.getParameter<int8_t>("filenameTimestepWidth", int8_t(5));
    
    cameraPosition_ = config.getParameter<Vec3>("cameraPosition");
    lookAtPoint_ = config.getParameter<Vec3>("lookAt");
@@ -119,6 +120,7 @@ Raytracer::Raytracer(const shared_ptr<BlockStorage> forest, BlockDataID storageI
    blockAABBIntersectionPadding_ = config.getParameter<real_t>("blockAABBIntersectionPadding", real_t(0.0));
       
    setupView_();
+   setupFilenameRankWidth_();
 }
 
 /*!\brief Utility function for setting up the view plane and calculating required variables.
@@ -140,21 +142,43 @@ void Raytracer::setupView_() {
    pixelHeight_ = viewingPlaneHeight_ / real_c(pixelsVertical_);
 }
 
-/*!\brief Writes the tBuffer to a file in the tBuffer output directory.
- * \param tBuffer Buffer with t values as generated in rayTrace(...).
+/*!\brief Utility function for initializing the attribute filenameRankWidth.
+ */
+void Raytracer::setupFilenameRankWidth_() {
+   int numProcesses = mpi::MPIManager::instance()->numProcesses();
+   filenameRankWidth_ = int8_c(log10(numProcesses))+1;
+   WALBERLA_LOG_INFO("filenameRankWidth_: " << int_c(filenameRankWidth_));
+}
+
+/*!\brief Generates the filename for output files.
+ * \param base String that precedes the timestap and rank info.
  * \param timestep Timestep this image is from.
  * \param isGlobalImage Whether this image is the fully stitched together one.
  */
-void Raytracer::writeTBufferToFile(const std::vector<real_t>& tBuffer, size_t timestep, bool isGlobalImage) const {
-   uint_t maxTimestep = uint_c(pow(10, outputFilenameTimestepZeroPadding_+1));
-   WALBERLA_CHECK(timestep < maxTimestep, "Raytracer only supports outputting " << (maxTimestep-1) << " timesteps for the configured output filename zero padding.");
+std::string Raytracer::getOutputFilename(const std::string& base, size_t timestep, bool isGlobalImage) const {
+   uint_t maxTimestep = uint_c(pow(10, filenameTimestepWidth_));
+   WALBERLA_CHECK(timestep < maxTimestep, "Raytracer only supports outputting " << (maxTimestep-1) << " timesteps for the configured filename timestep width.");
    mpi::MPIRank rank = mpi::MPIManager::instance()->rank();
    std::stringstream fileNameStream;
-   fileNameStream << "tbuffer_";
-   fileNameStream << std::setfill('0') << std::setw(int_c(outputFilenameTimestepZeroPadding_)) << timestep; // add timestep
-   fileNameStream << "+" << (isGlobalImage ? "global" : std::to_string(rank)); // add rank
+   fileNameStream << base << "_";
+   fileNameStream << std::setfill('0') << std::setw(int_c(filenameTimestepWidth_)) << timestep; // add timestep
+   fileNameStream << "+";
+   if (isGlobalImage) {
+      fileNameStream << "global";
+   } else {
+      fileNameStream << std::setfill('0') << std::setw(int_c(filenameRankWidth_)) << std::to_string(rank); // add rank
+   }
    fileNameStream << ".png"; // add extension
-   writeTBufferToFile(tBuffer, fileNameStream.str());
+   return fileNameStream.str();
+}
+
+/*!\brief Writes the tBuffer to a file in the tBuffer output directory.
+ * \param tBuffer Buffer with t values as generated in rayTrace(...).
+ * \param timestep Timestep this image is from.
+ * \param isGlobalImage Whether this image is the fully stitched together one.
+ */
+void Raytracer::writeTBufferToFile(const std::vector<real_t>& tBuffer, size_t timestep, bool isGlobalImage) const {
+   writeTBufferToFile(tBuffer, getOutputFilename("tbuffer", timestep, isGlobalImage));
 }
 
 /*!\brief Writes the tBuffer to a file in the tBuffer output directory.
@@ -219,15 +243,7 @@ void Raytracer::writeTBufferToFile(const std::vector<real_t>& tBuffer, const std
  * \param isGlobalImage Whether this image is the fully stitched together one.
  */
 void Raytracer::writeImageBufferToFile(const std::vector<Color>& imageBuffer, size_t timestep, bool isGlobalImage) const {
-   uint_t maxTimestep = uint_c(pow(10, outputFilenameTimestepZeroPadding_+1));
-   WALBERLA_CHECK(timestep < maxTimestep, "Raytracer only supports outputting " << (maxTimestep-1) << " timesteps for the configured output filename zero padding.");
-   mpi::MPIRank rank = mpi::MPIManager::instance()->rank();
-   std::stringstream fileNameStream;
-   fileNameStream << "image_";
-   fileNameStream << std::setfill('0') << std::setw(int_c(outputFilenameTimestepZeroPadding_)) << timestep; // add timestep
-   fileNameStream << "+" << (isGlobalImage ? "global" : std::to_string(rank)); // add rank
-   fileNameStream << ".png"; // add extension
-   writeImageBufferToFile(imageBuffer, fileNameStream.str());
+   writeImageBufferToFile(imageBuffer, getOutputFilename("image", timestep, isGlobalImage));
 }
 
 /*!\brief Writes the image buffer to a file in the image output directory.
diff --git a/src/pe/raytracing/Raytracer.h b/src/pe/raytracing/Raytracer.h
index 7701eed0a..52a0a7987 100644
--- a/src/pe/raytracing/Raytracer.h
+++ b/src/pe/raytracing/Raytracer.h
@@ -92,11 +92,14 @@ private:
    bool localImageOutputEnabled_; //!< Enable / disable writing images of the local process to file.
    std::string imageOutputDirectory_; //!< Path to the image output directory.
    
-   int8_t outputFilenameTimestepZeroPadding_; /*!< Zero padding for timesteps of output filenames.
-                                               * Use e.g. 4 for ranges from 1 to 100000: Will result in
-                                               * filenames like image_00001.png up to image_99999.png. */
+   int8_t filenameTimestepWidth_; /*!< Width of the timestep number in output filenames.
+                                  * Use e.g. 5 for ranges from 1 to 99 999: Will result in
+                                  * filenames like image_00001.png up to image_99999.png. */
+   int8_t filenameRankWidth_;  //!< Width of the mpi rank part in a filename.
    //@}
    
+   /*!\name Member variables for raytracing geometry */
+   //@{
    Vec3 n_;                   //!< The normal vector of the viewing plane.
    Vec3 u_;                   //!< The vector spanning the viewing plane in the "right direction".
    Vec3 v_;                   //!< The vector spanning the viewing plane in the "up direction".
@@ -107,6 +110,8 @@ private:
    Vec3 viewingPlaneOrigin_;  //!< The origin of the viewing plane.
    real_t pixelWidth_;        //!< The width of a pixel of the generated image in the viewing plane.
    real_t pixelHeight_;       //!< The height of a pixel of the generated image in the viewing plane.
+   //@}
+   
    WcTimingPool tp_;
 
 public:
@@ -124,7 +129,7 @@ public:
    inline bool getImageOutputEnabled() const;
    inline bool getLocalImageOutputEnabled() const;
    inline const std::string& getImageOutputDirectory() const;
-   inline int8_t getOutputFilenameTimestepZeroPadding() const;
+   inline int8_t getFilenameTimestepWidth() const;
    //@}
 
    /*!\name Set functions */
@@ -135,16 +140,18 @@ public:
    inline void setImageOutputEnabled(const bool enabled);
    inline void setLocalImageOutputEnabled(const bool enabled);
    inline void setImageOutputDirectory(const std::string& path);
-   inline void setOutputFilenameTimestepZeroPadding(int8_t padding);
+   inline void setFilenameTimestepWidth(int8_t width);
    //@}
    
    /*!\name Functions */
    //@{
    void setupView_();
+   void setupFilenameRankWidth_();
    template <typename BodyTypeTuple>
    void rayTrace(const size_t timestep);
    
 private:
+   std::string getOutputFilename(const std::string& base, size_t timestep, bool isGlobalImage) const;
    void writeTBufferToFile(const std::vector<real_t>& tBuffer, size_t timestep, bool isGlobalImage = false) const;
    void writeTBufferToFile(const std::vector<real_t>& tBuffer, const std::string& fileName) const;
    void writeImageBufferToFile(const std::vector<Color>& imageBuffer, size_t timestep, bool isGlobalImage = false) const;
@@ -262,11 +269,11 @@ inline const std::string& Raytracer::getImageOutputDirectory() const {
    return imageOutputDirectory_;
 }
 
-/*!\brief Returns the zero padding for timesteps in output filenames.
- * \return Zero padding amount.
+/*!\brief Returns width of the timestep number in output filenames.
+ * \return Width of the timestep part in filenames.
  */
-inline int8_t Raytracer::getOutputFilenameTimestepZeroPadding() const {
-   return outputFilenameTimestepZeroPadding_;
+inline int8_t Raytracer::getFilenameTimestepWidth() const {
+   return filenameTimestepWidth_;
 }
 
 /*!\brief Set the background color of the scene.
@@ -322,11 +329,11 @@ inline void Raytracer::setImageOutputDirectory(const std::string& path) {
    imageOutputDirectory_ = path;
 }
 
-/*!\brief Set zero padding for timesteps of output filenames.
- * \param padding Set to true / false to enable / disable image output.
+/*!\brief Set width of timestep number in output filenames.
+ * \param width Width of timestep part in a filename.
  */
-inline void Raytracer::setOutputFilenameTimestepZeroPadding(int8_t padding) {
-   outputFilenameTimestepZeroPadding_ = padding;
+inline void Raytracer::setFilenameTimestepWidth(int8_t width) {
+   filenameTimestepWidth_ = width;
 }
    
 /*!\brief Checks if a plane should get rendered.
-- 
GitLab