Skip to content
Snippets Groups Projects
Initialization.cpp 41.3 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
//======================================================================================================================
//
//  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 Initialization.cpp
//! \ingroup blockforest
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#include "BlockNeighborhoodSection.h"
#include "Initialization.h"
#include "SetupBlockForest.h"
#include "loadbalancing/Cartesian.h"

#include "core/Abort.h"
#include "core/cell/CellInterval.h"
#include "core/math/IntegerFactorization.h"
#include "core/mpi/MPIManager.h"

#include "stencil/D3Q19.h"

namespace walberla {
namespace blockforest {


//**********************************************************************************************************************
/*!
* Parses config block called 'DomainSetup' and creates a StructuredBlockForest
*
* For more information see function below.
*/
//**********************************************************************************************************************
shared_ptr< StructuredBlockForest > createUniformBlockGridFromConfig( const shared_ptr< Config > & config,
                                                                      CellInterval * requestedDomainSize,
                                                                      const bool keepGlobalBlockInformation )
{
   if( !!config )
   {
      auto block = config->getGlobalBlock();
      if( block ) {
         auto subBlock = block.getBlock( "DomainSetup" );
         if ( ! subBlock ) {
            WALBERLA_ABORT_NO_DEBUG_INFO( "Unable to create uniform block grid from configuration file."
                                          "\nDid you forget to specify a \"DomainSetup\" block in the configuration file?" );
         }
         return createUniformBlockGridFromConfig( subBlock, requestedDomainSize, keepGlobalBlockInformation );
      }
   }
   WALBERLA_ABORT_NO_DEBUG_INFO( "No Configuration specified" );
   return shared_ptr<StructuredBlockForest>();
}



//**********************************************************************************************************************
/*!
* \brief Parses config block and creates a StructuredBlockForest
*
* Two possibilities:
*    1)  Using the cells per block and number of blocks for each direction
\verbatim
          {
             cellsPerBlock < 10, 20, 30 > ;  // required
             blocks        < 1,   2,  3 > ;  // required
             periodic      < 0, 0, 1 >;      // not required, defaults to no periodicity

             dx  0.01;  // defaults to 1.0
          }
\endverbatim
*    2) Using the number of global cells, #blocks = #processes, if this does not fit, extend the domain
\verbatim
          {
             cells <    10,40,90>;    // required
             periodic   < 0, 0, 1 >;  // not required, defaults to no periodicity
             dx  0.01;                // defaults to 1.0
          }
\endverbatim
*          Example:  cells < 31,31,31> started using 8 processors <BR>
*                    calculated processor distribution <2,2,2>    <BR>
*                    real domain is then extended to <32,32,32> and every processor gets a block of <16,16,16>
*
*          When this setup is used and requestedDomainSize is not the null pointer, it is set to <BR>
*          the requested domain size ( in the example <31,31,31> )
*
*/
//**********************************************************************************************************************
shared_ptr< StructuredBlockForest > createUniformBlockGridFromConfig( const Config::BlockHandle & configBlock,
                                                                      CellInterval * requestedDomainSize,
                                                                      const bool keepGlobalBlockInformation )
{
   const Vector3<bool> periodic = configBlock.getParameter<Vector3<bool> >( "periodic",  Vector3<bool> (false) );
   const real_t        dx       = configBlock.getParameter<real_t        >( "dx",        real_t(1)             );

   Vector3<uint_t> cellsPerBlock;
   Vector3<uint_t> blocks;

   if ( configBlock.isDefined("cells") )
   {
      if ( configBlock.isDefined("cellsPerBlock") ||  configBlock.isDefined("blocks")  )
         WALBERLA_ABORT_NO_DEBUG_INFO("Config Error:  Use either ('cellsPerBlock' and 'blocks') or 'cells', not both!");

      Vector3<uint_t> cells = configBlock.getParameter<Vector3<uint_t>  >( "cells" );

      if ( requestedDomainSize )
         *requestedDomainSize = CellInterval( 0,0,0,
                                              cell_idx_c(cells[0])-1,
                                              cell_idx_c(cells[1])-1,
                                              cell_idx_c(cells[2])-1 );

      uint_t nrOfProcesses = uint_c( MPIManager::instance()->numProcesses() );

      calculateCellDistribution( cells, nrOfProcesses, blocks, cellsPerBlock );
   }
   else
   {
      cellsPerBlock = configBlock.getParameter<Vector3<uint_t>  >( "cellsPerBlock" );
      blocks        = configBlock.getParameter<Vector3<uint_t>  >( "blocks"        );

      if ( requestedDomainSize )
         *requestedDomainSize = CellInterval( 0, 0, 0, cell_idx_c( cellsPerBlock[0] * blocks[0] ),
                                                       cell_idx_c( cellsPerBlock[1] * blocks[1] ),
                                                       cell_idx_c( cellsPerBlock[2] * blocks[2] ) );
   }

   const bool oneBlockPerProcess = configBlock.getParameter<bool> ( "oneBlockPerProcess", true );

   return createUniformBlockGrid(
            blocks[0],        blocks[1],        blocks[2],         // blocks/processes in x/y/z direction
            cellsPerBlock[0], cellsPerBlock[1], cellsPerBlock[2],  // cells per block in x/y/z direction
            dx,                                                    // cell size
            oneBlockPerProcess,                                    // one block per process
            periodic[0], periodic[1], periodic[2],                 // periodicity
            keepGlobalBlockInformation                             // keep global block information
            );
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks, each block has the same size.
*   The distribution of blocks to processes also follows a Cartesian decomposition.
*
*   \param domainAABB                 An axis-aligned bounding box that spans the entire simulation space/domain
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXProcesses         Number of processes the blocks are distributed to in x direction
*   \param numberOfYProcesses         Number of processes the blocks are distributed to in y direction
*   \param numberOfZProcesses         Number of processes the blocks are distributed to in z direction
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< BlockForest >
createBlockForest(      const AABB& domainAABB,
                        const uint_t numberOfXBlocks,         const uint_t numberOfYBlocks,         const uint_t numberOfZBlocks,
                        const uint_t numberOfXProcesses,      const uint_t numberOfYProcesses,      const uint_t numberOfZProcesses,
                        const bool   xPeriodic /* = false */, const bool   yPeriodic /* = false */, const bool   zPeriodic /* = false */,
                        const bool keepGlobalBlockInformation /* = false */ ) {

   const uint_t numberOfProcesses = numberOfXProcesses * numberOfYProcesses * numberOfZProcesses;

   if( numeric_cast< int >( numberOfProcesses ) != MPIManager::instance()->numProcesses() )
      WALBERLA_ABORT( "The number of requested processes (" << numberOfProcesses << ") doesn't match the number "
                                                                                    "of active MPI processes (" << MPIManager::instance()->numProcesses() << ")!" );

   // initialize SetupBlockForest = determine domain decomposition

   SetupBlockForest sforest;

   sforest.addWorkloadMemorySUIDAssignmentFunction( uniformWorkloadAndMemoryAssignment );

   sforest.init( domainAABB, numberOfXBlocks, numberOfYBlocks, numberOfZBlocks, xPeriodic, yPeriodic, zPeriodic );

   // if possible, create Cartesian MPI communicator

   std::vector< uint_t >* processIdMap = NULL;

   WALBERLA_MPI_SECTION()
   {
      auto mpiManager = MPIManager::instance();
      //create cartesian communicator only if not yet a cartesian communicator (or other communicator was created)
      if ( ! mpiManager->rankValid() )
      {
         mpiManager->createCartesianComm( numberOfXProcesses, numberOfYProcesses, numberOfZProcesses, xPeriodic, yPeriodic, zPeriodic );

         processIdMap = new std::vector< uint_t >( numberOfProcesses );

         for( uint_t z = 0; z != numberOfZProcesses; ++z ) {
            for( uint_t y = 0; y != numberOfYProcesses; ++y ) {
               for( uint_t x = 0; x != numberOfXProcesses; ++x ) {

                  (*processIdMap)[ z * numberOfXProcesses * numberOfYProcesses + y * numberOfXProcesses + x ] =
                        uint_c( MPIManager::instance()->cartesianRank(x,y,z) );
               }
            }
         }
      }
   }

   // calculate process distribution

   sforest.balanceLoad( blockforest::CartesianDistribution( numberOfXProcesses, numberOfYProcesses, numberOfZProcesses, processIdMap ),
                        numberOfXProcesses * numberOfYProcesses * numberOfZProcesses );

   if( processIdMap != NULL ) delete processIdMap;

   // create StructuredBlockForest (encapsulates a newly created BlockForest)

   return shared_ptr< BlockForest >( new BlockForest( uint_c( MPIManager::instance()->rank() ), sforest, keepGlobalBlockInformation ) );
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   The distribution of blocks to processes also follows a Cartesian decomposition.
*
*   \param domainAABB                 An axis-aligned bounding box that spans the entire simulation space/domain
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param numberOfXProcesses         Number of processes the blocks are distributed to in x direction
*   \param numberOfYProcesses         Number of processes the blocks are distributed to in y direction
*   \param numberOfZProcesses         Number of processes the blocks are distributed to in z direction
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const AABB& domainAABB,
                        const uint_t numberOfXBlocks,         const uint_t numberOfYBlocks,         const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,  const uint_t numberOfYCellsPerBlock,  const uint_t numberOfZCellsPerBlock,
                        const uint_t numberOfXProcesses,      const uint_t numberOfYProcesses,      const uint_t numberOfZProcesses,
                        const bool   xPeriodic /* = false */, const bool   yPeriodic /* = false */, const bool   zPeriodic /* = false */,
                        const bool keepGlobalBlockInformation /* = false */ )
{
   auto bf = createBlockForest(
            domainAABB,
            numberOfXBlocks,
            numberOfYBlocks,
            numberOfZBlocks,
            numberOfXProcesses,
            numberOfYProcesses,
            numberOfZProcesses,
            xPeriodic,
            yPeriodic,
            zPeriodic,
            keepGlobalBlockInformation);

   auto sbf = shared_ptr< StructuredBlockForest >( new StructuredBlockForest( bf, numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock ) );
   sbf->createCellBoundingBoxes();

   return sbf;
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   The distribution of blocks to processes also follows a Cartesian decomposition.
*
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param dx                         Edge length of each cell (cells are assumed to be cubes)
*   \param numberOfXProcesses         Number of processes the blocks are distributed to in x direction
*   \param numberOfYProcesses         Number of processes the blocks are distributed to in y direction
*   \param numberOfZProcesses         Number of processes the blocks are distributed to in z direction
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const uint_t numberOfXBlocks,         const uint_t numberOfYBlocks,         const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,  const uint_t numberOfYCellsPerBlock,  const uint_t numberOfZCellsPerBlock,
                        const real_t dx,
                        const uint_t numberOfXProcesses,      const uint_t numberOfYProcesses,      const uint_t numberOfZProcesses,
                        const bool   xPeriodic /* = false */, const bool   yPeriodic /* = false */, const bool   zPeriodic /* = false */,
                        const bool keepGlobalBlockInformation /* = false */ ) {

   return createUniformBlockGrid( AABB( real_c(0), real_c(0), real_c(0), dx * real_c( numberOfXBlocks * numberOfXCellsPerBlock ),
                                                                         dx * real_c( numberOfYBlocks * numberOfYCellsPerBlock ),
                                                                         dx * real_c( numberOfZBlocks * numberOfZCellsPerBlock ) ),
                                  numberOfXBlocks, numberOfYBlocks, numberOfZBlocks,
                                  numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock,
                                  numberOfXProcesses, numberOfYProcesses, numberOfZProcesses,
                                  xPeriodic, yPeriodic, zPeriodic, keepGlobalBlockInformation );
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   Either all blocks are assigned to the same process (useful for non-parallel simulations) or each blocks is assigned
*   to a different process (useful if only one block shall be assigned to each process).
*
*   \param domainAABB                 An axis-aligned bounding box that spans the entire simulation space/domain
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param oneBlockPerProcess         If true, each block is assigned to a different process. If false, all blocks are
*                                     assigned to the same process (process 0).
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const AABB& domainAABB,
                        const uint_t numberOfXBlocks,         const uint_t numberOfYBlocks,         const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,  const uint_t numberOfYCellsPerBlock,  const uint_t numberOfZCellsPerBlock,
                        const bool oneBlockPerProcess,
                        const bool   xPeriodic /* = false */, const bool   yPeriodic /* = false */, const bool   zPeriodic /* = false */,
                        const bool keepGlobalBlockInformation /* = false */ ) {

   if( oneBlockPerProcess )
      return createUniformBlockGrid( domainAABB, numberOfXBlocks, numberOfYBlocks, numberOfZBlocks,
                                     numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock,
                                     numberOfXBlocks, numberOfYBlocks, numberOfZBlocks, xPeriodic, yPeriodic, zPeriodic, keepGlobalBlockInformation );

   // all blocks on the same process
   return createUniformBlockGrid( domainAABB, numberOfXBlocks, numberOfYBlocks, numberOfZBlocks,
                                  numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock, uint_c(1), uint_c(1), uint_c(1),
                                  xPeriodic, yPeriodic, zPeriodic, keepGlobalBlockInformation );
}

//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   Either all blocks are assigned to the same process (useful for non-parallel simulations) or each blocks is assigned
*   to a different process (useful if only one block shall be assigned to each process).
*
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param dx                         Edge length of each cell (cells are assumed to be cubes)
*   \param oneBlockPerProcess         If true, each block is assigned to a different process. If false, all blocks are
*                                     assigned to the same process (process 0).
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const uint_t numberOfXBlocks,         const uint_t numberOfYBlocks,         const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,  const uint_t numberOfYCellsPerBlock,  const uint_t numberOfZCellsPerBlock,
                        const real_t dx,
                        const bool oneBlockPerProcess,
                        const bool   xPeriodic /* = false */, const bool   yPeriodic /* = false */, const bool   zPeriodic /* = false */,
                        const bool keepGlobalBlockInformation /* = false */ ) {

   return createUniformBlockGrid( AABB( real_c(0), real_c(0), real_c(0), dx * real_c( numberOfXBlocks * numberOfXCellsPerBlock ),
                                                                         dx * real_c( numberOfYBlocks * numberOfYCellsPerBlock ),
                                                                         dx * real_c( numberOfZBlocks * numberOfZCellsPerBlock ) ),
                                  numberOfXBlocks, numberOfYBlocks, numberOfZBlocks,
                                  numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock,
                                  oneBlockPerProcess, xPeriodic, yPeriodic, zPeriodic, keepGlobalBlockInformation );
}

//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   The number of active MPI processes is used in order to determine the process distribution = in order to perform the
*   initial, static load balancing. Each block is assumed to generate the same amount of work and to require the same
*   amount of memory.
*
*   \param domainAABB                 An axis-aligned bounding box that spans the entire simulation space/domain
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param maxBlocksPerProcess        Maximum number of blocks that are allowed to be assigned to one process. If a
*                                     value of '0' is provided, any number of blocks are allowed to be located on one
*                                     process - meaning static load balancing doesn't try to obey any memory limit. ['0' by default]
*   \param includeMetis               If true (and if available!), METIS is also used during load balancing. [true by default]
*   \param forceMetis                 If true, METIS is always preferred over space filling curves [false by default]
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const AABB& domainAABB,
                        const uint_t numberOfXBlocks,             const uint_t numberOfYBlocks,        const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,      const uint_t numberOfYCellsPerBlock, const uint_t numberOfZCellsPerBlock,
                        const uint_t maxBlocksPerProcess /*= 0*/, const bool includeMetis /*= true*/,  const bool forceMetis /*= false*/,
                        const bool   xPeriodic /*= false*/,       const bool   yPeriodic /*= false*/,  const bool   zPeriodic /*= false*/,
                        const bool keepGlobalBlockInformation /*= false*/ ) {

   // initialize SetupBlockForest = determine domain decomposition

   SetupBlockForest sforest;

   sforest.addWorkloadMemorySUIDAssignmentFunction( uniformWorkloadAndMemoryAssignment );

   sforest.init( domainAABB, numberOfXBlocks, numberOfYBlocks, numberOfZBlocks, xPeriodic, yPeriodic, zPeriodic );

   // calculate process distribution

   const memory_t memoryLimit = ( maxBlocksPerProcess == 0 ) ? numeric_cast< memory_t >( sforest.getNumberOfBlocks() ) :
                                                               numeric_cast< memory_t >( maxBlocksPerProcess );

   GlobalLoadBalancing::MetisConfiguration< SetupBlock > metisConfig( includeMetis, forceMetis,
                                                                      boost::bind( cellWeightedCommunicationCost,_1,_2,
                                                                                   numberOfXCellsPerBlock,
                                                                                   numberOfYCellsPerBlock,
                                                                                   numberOfZCellsPerBlock ) );

   sforest.calculateProcessDistribution_Default( uint_c( MPIManager::instance()->numProcesses() ), memoryLimit, "hilbert", 10, false, metisConfig );

   if( !MPIManager::instance()->rankValid() )
      MPIManager::instance()->useWorldComm();

   // create StructuredBlockForest (encapsulates a newly created BlockForest)

   auto bf = shared_ptr< BlockForest >( new BlockForest( uint_c( MPIManager::instance()->rank() ), sforest, keepGlobalBlockInformation ) );

   auto sbf = shared_ptr< StructuredBlockForest >( new StructuredBlockForest( bf, numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock ) );
   sbf->createCellBoundingBoxes();

   return sbf;
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   The number of active MPI processes is used in order to determine the process distribution = in order to perform the
*   initial, static load balancing. Each block is assumed to generate the same amount of work and to require the same
*   amount of memory.
*
*   \param numberOfXBlocks            Number of blocks in x direction
*   \param numberOfYBlocks            Number of blocks in y direction
*   \param numberOfZBlocks            Number of blocks in z direction
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param dx                         Edge length of each cell (cells are assumed to be cubes)
*   \param maxBlocksPerProcess        Maximum number of blocks that are allowed to be assigned to one process. If a
*                                     value of '0' is provided, any number of blocks are allowed to be located on one
*                                     process - meaning static load balancing doesn't try to obey any memory limit. ['0' by default]
*   \param includeMetis               If true (and if available!), METIS is also used during load balancing. [true by default]
*   \param forceMetis                 If true, METIS is always preferred over space filling curves [false by default]
*   \param xPeriodic                  If true, the block structure is periodic in x direction [false by default]
*   \param yPeriodic                  If true, the block structure is periodic in y direction [false by default]
*   \param zPeriodic                  If true, the block structure is periodic in z direction [false by default]
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const uint_t numberOfXBlocks,             const uint_t numberOfYBlocks,        const uint_t numberOfZBlocks,
                        const uint_t numberOfXCellsPerBlock,      const uint_t numberOfYCellsPerBlock, const uint_t numberOfZCellsPerBlock,
                        const real_t dx,
                        const uint_t maxBlocksPerProcess /*= 0*/, const bool includeMetis /*= true*/,  const bool forceMetis /*= false*/,
                        const bool   xPeriodic /*= false*/,       const bool   yPeriodic /*= false*/,  const bool   zPeriodic /*= false*/,
                        const bool keepGlobalBlockInformation /*= false*/ ) {

   return createUniformBlockGrid( AABB( real_c(0), real_c(0), real_c(0), dx * real_c( numberOfXBlocks * numberOfXCellsPerBlock ),
                                                                         dx * real_c( numberOfYBlocks * numberOfYCellsPerBlock ),
                                                                         dx * real_c( numberOfZBlocks * numberOfZCellsPerBlock ) ),
                                  numberOfXBlocks, numberOfYBlocks, numberOfZBlocks,
                                  numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock,
                                  maxBlocksPerProcess, includeMetis, forceMetis, xPeriodic, yPeriodic, zPeriodic, keepGlobalBlockInformation );
}



//**********************************************************************************************************************
/*!
*   \brief Function for creating a structured block forest that represents a uniform block grid.
*
*   Uniform block grid: Cartesian domain decomposition into blocks of cells, each block has the same size and contains
*                       the same number of cells.
*   The entire block structure and its corresponding process distribution are loaded from file.
*
*   \param filename                   A file that stores a block structure and its corresponding process distribution
*   \param numberOfXCellsPerBlock     Number of cells of each block in x direction
*   \param numberOfYCellsPerBlock     Number of cells of each block in y direction
*   \param numberOfZCellsPerBlock     Number of cells of each block in z direction
*   \param keepGlobalBlockInformation If true, each process keeps information about remote blocks (blocks that reside
*                                     on other processes). This information includes the process rank, the state, and
*                                     the axis-aligned bounding box of any block (local or remote). [false by default]
*/
//**********************************************************************************************************************

shared_ptr< StructuredBlockForest >
createUniformBlockGrid( const std::string& filename,
                        const uint_t numberOfXCellsPerBlock,  const uint_t numberOfYCellsPerBlock, const uint_t numberOfZCellsPerBlock,
                        const bool keepGlobalBlockInformation /*= false*/ )
{

   if( !MPIManager::instance()->rankValid() )
      MPIManager::instance()->useWorldComm();

   auto bf = shared_ptr< BlockForest >( new BlockForest( uint_c( MPIManager::instance()->rank() ), filename.c_str(), true, keepGlobalBlockInformation ) );

   if( !bf->storesUniformBlockGrid() )
      WALBERLA_ABORT( "The block forest loaded from file \'" << filename << "\' does not contain a uniform block grid!" );

   auto sbf = shared_ptr< StructuredBlockForest >( new StructuredBlockForest( bf, numberOfXCellsPerBlock, numberOfYCellsPerBlock, numberOfZCellsPerBlock ) );
   sbf->createCellBoundingBoxes();

   return sbf;
}



///////////////////////////////////////////
// HELPER FUNCTIONS                      //
///////////////////////////////////////////



//*******************************************************************************************************************
/*! Tries to distribute a given amount of total cells to a given amount of blocks
*
* It may happen that divisibility of the nr of cells requested prevents a distribution
* in this case the number of cells is chosen bigger than requested
*
*
* \param cells:              total number of cells requested
* \param nrOfBlocks:         total number of blocks to distribute the cells to
* \param[out] blocks:        calculated number of blocks in x/y/z
* \param[out] cellsPerBlock: how many cells to put on each block
  it may happen that divisibility of the number of cells requested prevents a distribution
*                            in this case the number of cells is chosen (slightly) bigger than requested
*
* Example: in:  cells = (10,15,16)
*          in:  blocks = 8
*          out: blocks = (2,2,2)
*          out: cellsPerBlock = (5,8,8)
*          out: newCells = (10,16,16)
*/
//*******************************************************************************************************************
void calculateCellDistribution( const Vector3<uint_t> & cells, uint_t nrOfBlocks,
                                Vector3<uint_t> & blocksOut, Vector3<uint_t> & cellsPerBlock)
{
   std::vector< real_t > weighting;
   weighting.push_back( real_c( cells[0]) );
   weighting.push_back( real_c( cells[1]) );
   weighting.push_back( real_c( cells[2]) );
   std::vector<uint_t> blocks = math::getFactors( nrOfBlocks, 3, weighting );

   for( uint_t i = 0; i < 3; ++i )
   {
      if ( uint_c( cells[i] ) % blocks[i] == 0 )
         cellsPerBlock[i] = cells[i] /  blocks[i];
      else // extend the domain if processesCount does not divide the cell count in this direction
         cellsPerBlock[i] = ( cells[i] +   blocks[i] ) /  blocks[i];
   }
   for( uint_t i = 0; i < 3; ++i )
      blocksOut[i] = blocks[i];
}


void uniformWorkloadAndMemoryAssignment( SetupBlockForest& forest ) {

   std::vector< SetupBlock* > blocks;
   forest.getBlocks( blocks );

   for( uint_t i = 0; i != blocks.size(); ++i ) {
      blocks[i]->setWorkload( numeric_cast< workload_t >(1) );
      blocks[i]->setMemory( numeric_cast< memory_t >(1) );
   }
}


memory_t cellWeightedCommunicationCost( const SetupBlock* const a, const SetupBlock* const b,
                                        uint_t xCellsPerBlock, uint_t yCellsPerBlock, uint_t zCellsPerBlock )
{
   for ( auto dIter = stencil::D3Q19::beginNoCenter(); dIter != stencil::D3Q19::end(); ++dIter )
   {
      auto neighborHoodIdxA = getBlockNeighborhoodSectionIndex( *dIter );
      for ( uint_t j = 0; j != a->getNeighborhoodSectionSize( neighborHoodIdxA ); ++j )
      {
         if( a->getNeighbor(neighborHoodIdxA,j) == b )
         {
            switch ( *dIter )
            {
               //faces
               case stencil::W: return memory_c( yCellsPerBlock * zCellsPerBlock );
               case stencil::E: return memory_c( yCellsPerBlock * zCellsPerBlock );
               case stencil::N: return memory_c( xCellsPerBlock * zCellsPerBlock );
               case stencil::S: return memory_c( xCellsPerBlock * zCellsPerBlock );
               case stencil::T: return memory_c( xCellsPerBlock * yCellsPerBlock );
               case stencil::B: return memory_c( xCellsPerBlock * yCellsPerBlock );
               //edges
               case stencil::NW: return memory_c( zCellsPerBlock );
               case stencil::NE: return memory_c( zCellsPerBlock );
               case stencil::SW: return memory_c( zCellsPerBlock );
               case stencil::SE: return memory_c( zCellsPerBlock );
               case stencil::TN: return memory_c( xCellsPerBlock );
               case stencil::TS: return memory_c( xCellsPerBlock );
               case stencil::TW: return memory_c( yCellsPerBlock );
               case stencil::TE: return memory_c( yCellsPerBlock );
               case stencil::BN: return memory_c( xCellsPerBlock );
               case stencil::BS: return memory_c( xCellsPerBlock );
               case stencil::BW: return memory_c( yCellsPerBlock );
               case stencil::BE: return memory_c( yCellsPerBlock );
               default:
                  WALBERLA_ABORT( "Unknown direction. Should not happen!" )
            }
         }
      }
   }

   // Return 1 for corners
   return numeric_cast< memory_t >(1);

}


memory_t uniformFacesDominantCommunication( const SetupBlock* const a, const SetupBlock* const b ) {

   uint_t faces[] = { 4, 10, 12, 13, 15, 21 };

   for( uint_t i = 0; i != 6; ++i ) {
      for( uint_t j = 0; j != a->getNeighborhoodSectionSize(faces[i]); ++j )
         if( a->getNeighbor(faces[i],j) == b )
            return numeric_cast< memory_t >(1000);
   }

   return numeric_cast< memory_t >(1);
}



} // namespace blockforest
} // namespace walberla