From 732df1dd523e26d5d5f9f0c238daf162e14e4726 Mon Sep 17 00:00:00 2001
From: Sebastian Eibl <sebastian.eibl@fau.de>
Date: Tue, 18 Jul 2017 17:18:51 +0200
Subject: [PATCH] [BUGFIX] incorrect header size fixed

---
 src/blockforest/BlockForest.cpp      |  8 ++-
 src/blockforest/BlockForestFile.h    | 95 ++++++++++++++++++++++++++++
 src/blockforest/SetupBlockForest.cpp | 57 ++---------------
 3 files changed, 105 insertions(+), 55 deletions(-)
 create mode 100644 src/blockforest/BlockForestFile.h

diff --git a/src/blockforest/BlockForest.cpp b/src/blockforest/BlockForest.cpp
index 9a95eaf21..5aa3323e8 100644
--- a/src/blockforest/BlockForest.cpp
+++ b/src/blockforest/BlockForest.cpp
@@ -20,6 +20,7 @@
 //======================================================================================================================
 
 #include "BlockForest.h"
+#include "BlockForestFile.h"
 #include "BlockNeighborhoodSection.h"
 #include "SetupBlockForest.h"
 #include "core/Abort.h"
@@ -2752,8 +2753,9 @@ void BlockForest::update( PhantomBlockForest & phantomForest )
 }
 
 
-
-/// ATTENTION: 'suidMap' and 'suidBytes' must be identical for every process!
+/// For a description of the file format see BlockForestFile.h
+/// \attention 'suidMap' and 'suidBytes' must be identical for every process!
+/// \see BlockForestFile.h
 void BlockForest::saveToFile( const std::string & filename, FileIOMode fileIOMode,
                               const std::map< SUID, boost::dynamic_bitset<uint8_t> > & suidMap, const uint_t suidBytes ) const
 {
@@ -2764,7 +2766,7 @@ void BlockForest::saveToFile( const std::string & filename, FileIOMode fileIOMod
    uint_t dataSize = uint_t(2) + blocks_.size() * ( blockIdBytes + suidBytes ) + uint_t(2) + neighborhood_.size() * processIdBytes_;
    if( MPIManager::instance()->rank() == 0 )
    {
-      dataSize += uint_c(89); // header
+      dataSize += internal::FILE_HEADER_SIZE; // header
       ++dataSize; // number of SUIDs
       for( auto suid = suidMap.begin(); suid != suidMap.end(); ++suid )
          dataSize += uint_t(1) + uint_c( suid->first.getIdentifier().length() );
diff --git a/src/blockforest/BlockForestFile.h b/src/blockforest/BlockForestFile.h
new file mode 100644
index 000000000..af8619b71
--- /dev/null
+++ b/src/blockforest/BlockForestFile.h
@@ -0,0 +1,95 @@
+//======================================================================================================================
+//
+//  This file is part of waLBerla. waLBerla is free software: you can
+//  redistribute it and/or modify it under the terms of the GNU General Public
+//  License as published by the Free Software Foundation, either version 3 of
+//  the License, or (at your option) any later version.
+//
+//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
+//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+//  for more details.
+//
+//  You should have received a copy of the GNU General Public License along
+//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file BlockForestFile.h
+//! \author Florian Schornbaum <florian.schornbaum@fau.de>
+//
+//======================================================================================================================
+
+#pragma once
+
+#include "core/DataTypes.h"
+
+namespace walberla {
+namespace blockforest {
+namespace internal {
+
+//**********************************************************************************************************************
+/*!
+ *   \file BlockForestFile.h
+ *   \brief Description of the BlockForest save file format.
+ *
+ *   \section FileFormat File Format
+ *
+ *   \subsection HEADER HEADER
+ *
+ *   BYTES                      | DESCRIPTION
+ *   ---------------------------|-----------------
+ *   6 x ( 3 + sizeof(real_t) ) | domain AABB
+ *   3 x 4                      | number of coarse/root blocks in each direction ( max 2^32 = 4 294 967 296 )
+ *   3 x 1                      | domain periodicity
+ *   1                          | block forest depth (= number of levels - 1)
+ *   1                          | treeIdDigits (= number of bits used for storing the tree ID [tree ID marker + tree index])
+ *   1                          | processIdBytes (= number of bytes required for storing process IDs)
+ *   1                          | insertBuffersIntoProcessNetwork? ( 0=no, 1=yes )
+ *   4                          | number of processes ( max 2^32 = 4 294 967 296 )
+ *
+ *   --> 23 + 6 x ( 3 + sizeof(real_t) ) BYTES
+ *
+ *   \subsection SUID SUID MAPPING:
+ *
+ *   1 | number of SUIDs (= #SUIDs)
+ *
+ *   \code{.unparsed}
+ *   for each SUID:
+ *      1                | length of the UID identifier string
+ *      length-of-string | UID identifier string
+ *   \endcode
+ *
+ *   --> 1 + #SUIDs + number-of-characters-of-all-identifiers-combined BYTES
+ *
+ *   How the mapping works:\n
+ *   SUID #1 is assigned bit #1 ( -> [...]0 0000 0001 )\n
+ *   SUID #2 is assigned bit #2 ( -> [...]0 0000 0010 )\n
+ *   SUID #3 is assigned bit #3 ( -> [...]0 0000 0100 )\n
+ *   ...\n
+ *   For every block a bit mask containing information about all SUIDs (i.e., is the corresponding SUID set at this block?) is saved.
+ *   -> The number of available SUIDs determines the size that is needed to store this bit mask (= SUID-mask-bytes).
+ *   One byte is enough to hold 8 SUIDs, two bytes are enough to hold 16 SUIDs, ...
+ *
+ *   \subsection BLOCKDATA BLOCK DATA
+ *
+ *   \code{.unparsed}
+ *   for each process:
+ *      2 | number of blocks (can be '0' -> buffer process! - 2^16 = 65 536 )
+ *      if( number-of-blocks > 0 ):
+ *         for each block:
+ *            block-ID-bytes  | ID of the block (the number of bytes required for storing the block ID largely depends on the size
+ *                              of the simulation, the total number of blocks, and the number of refinement levels)
+ *            SUID-mask-bytes | state of the block = bit mask containing information about all SUIDs (see "How the mapping works" for SUIDs,
+ *                              SUID-mask-bytes can be equal to 0 bytes if no SUIDs exist!)
+ *      2 | number of neighbor processes
+ *      for each neighbor process:
+ *         process-ID-bytes | process ID / rank of the neighbor process (one byte if there are less than 257 processes,
+ *                                                                          two bytes if there are less than 65 537 processes, ...)
+ *   \endcode
+ */
+//**********************************************************************************************************************
+
+static const uint_t FILE_HEADER_SIZE = 6 * sizeof( real_t ) + 6 + 12 + 3 * 4 + 3 + 1 + 1 + 1 + 1 + 4;
+
+}
+}
+}
diff --git a/src/blockforest/SetupBlockForest.cpp b/src/blockforest/SetupBlockForest.cpp
index c2d003d9a..9ae11ce9c 100644
--- a/src/blockforest/SetupBlockForest.cpp
+++ b/src/blockforest/SetupBlockForest.cpp
@@ -19,6 +19,7 @@
 //
 //======================================================================================================================
 
+#include "BlockForestFile.h"
 #include "BlockNeighborhoodConstruction.h"
 #include "BlockNeighborhoodSection.h"
 #include "HilbertCurveConstruction.h"
@@ -1615,57 +1616,9 @@ void SetupBlockForest::calculateProcessDistributionFinalization( const bool reor
 
 
 //**********************************************************************************************************************
-/*!
-*   File Format:
-*
-*   HEADER:
-*   -------
-*     BYTES            |     DESCRIPTION
-*   6 x ( 3 + real_t ) | domain AABB
-*   3 x 4              | number of coarse/root blocks in each direction ( max 2^32 = 4 294 967 296 )
-*   3 x 1              | domain periodicity
-*   1                  | block forest depth (= number of levels - 1)
-*   1                  | treeIdDigits (= number of bits used for storing the tree ID [tree ID marker + tree index])
-*   1                  | processIdBytes (= number of bytes required for storing process IDs)
-*   1                  | insertBuffersIntoProcessNetwork? ( 0=no, 1=yes )
-*   4                  | number of processes ( max 2^32 = 4 294 967 296 )
-*
-*   --> 23 + 6 x ( 3 + real_t ) BYTES
-*
-*   SUID MAPPING:
-*   -------------
-*   1 | number of SUIDs (= #SUIDs)
-*
-*   for each SUID:
-*      1                | length of the UID identifier string
-*      length-of-string | UID identifier string
-*
-*   --> 1 + #SUIDs + number-of-characters-of-all-identifiers-combined BYTES
-*
-*   How the mapping works:
-*   SUID #1 is assigned bit #1 ( -> [...]0 0000 0001 )
-*   SUID #2 is assigned bit #2 ( -> [...]0 0000 0010 )
-*   SUID #3 is assigned bit #3 ( -> [...]0 0000 0100 )
-*   ...
-*   For every block a bit mask containing information about all SUIDs (i.e., is the corresponding SUID set at this block?) is saved.
-*   -> The number of available SUIDs determines the size that is needed to store this bit mask (= SUID-mask-bytes).
-*   One byte is enough to hold 8 SUIDs, two bytes are enough to hold 16 SUIDs, ...
-*
-*   BLOCK DATA:
-*   -----------
-*   for each process:
-*      2 | number of blocks (can be '0' -> buffer process! - 2^16 = 65 536 )
-*      if( number-of-blocks > 0 ):
-*         for each block:
-*            block-ID-bytes  | ID of the block (the number of bytes required for storing the block ID largely depends on the size
-*                              of the simulation, the total number of blocks, and the number of refinement levels)
-*            SUID-mask-bytes | state of the block = bit mask containing information about all SUIDs (see "How the mapping works" for SUIDs,
-*                              SUID-mask-bytes can be equal to 0 bytes if no SUIDs exist!)
-*      2 | number of neighbor processes
-*      for each neighbor process:
-*         process-ID-bytes | process ID / rank of the neighbor process (one byte if there are less than 257 processes,
-*                                                                          two bytes if there are less than 65 537 processes, ...)
-*/
+/// \brief
+///
+/// For a description of the file format see BlockForestFile.h \see BlockForestFile.h
 //**********************************************************************************************************************
 
 void SetupBlockForest::saveToFile( const char* const filename ) const {
@@ -1675,7 +1628,7 @@ void SetupBlockForest::saveToFile( const char* const filename ) const {
    // HEADER
 
    uint_t offset = 0;
-   std::vector< uint8_t > buffer( 6 * sizeof( real_t ) + 6 + 12 + 3 * 4 + 3 + 1 + 1 + 1 + 1 + 4 );
+   std::vector< uint8_t > buffer( internal::FILE_HEADER_SIZE );
 
    // domain AABB
 
-- 
GitLab