/usr/include/palabos/atomicBlock/blockLattice2D.hh is in libplb-dev 1.5~r1+repack1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | /* This file is part of the Palabos library.
*
* Copyright (C) 2011-2015 FlowKit Sarl
* Route d'Oron 2
* 1010 Lausanne, Switzerland
* E-mail contact: contact@flowkit.com
*
* The most recent release of Palabos can be downloaded at
* <http://www.palabos.org/>
*
* The library Palabos is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The library 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* The dynamics of a 2D block lattice -- generic implementation.
*/
#ifndef BLOCK_LATTICE_2D_HH
#define BLOCK_LATTICE_2D_HH
#include "atomicBlock/blockLattice2D.h"
#include "core/dynamics.h"
#include "core/cell.h"
#include "latticeBoltzmann/latticeTemplates.h"
#include "latticeBoltzmann/indexTemplates.h"
#include "core/util.h"
#include "core/latticeStatistics.h"
#include "core/dynamicsIdentifiers.h"
#include "core/plbProfiler.h"
#include <algorithm>
#include <typeinfo>
#include <cmath>
namespace plb {
////////////////////// Class BlockLattice2D /////////////////////////
/** \param nx_ lattice width (first index)
* \param ny_ lattice height (second index)
*/
template<typename T, template<typename U> class Descriptor>
BlockLattice2D<T,Descriptor>::BlockLattice2D (
plint nx_, plint ny_,
Dynamics<T,Descriptor>* backgroundDynamics_ )
: AtomicBlock2D(nx_, ny_),
backgroundDynamics(backgroundDynamics_),
dataTransfer(*this)
{
plint nx = this->getNx();
plint ny = this->getNy();
// Allocate memory and attribute dynamics.
allocateAndInitialize();
for (plint iX=0; iX<nx; ++iX) {
for (plint iY=0; iY<ny; ++iY) {
grid[iX][iY].attributeDynamics(backgroundDynamics);
}
}
// Attribute default value to the standard statistics (average uSqr,
// max uSqr, average rho). These have previously been subscribed
// in the constructor of BlockLatticeBase2D.
std::vector<double> average, sum, max;
std::vector<plint> intSum;
average.push_back(Descriptor<double>::rhoBar(1.));
// default average rho to 1, to avoid division by
// zero in constRhoBGK and related models
average.push_back(0.); // default average uSqr to 0
max.push_back(0.); // default max uSqr to 0
plint numCells = 1; // pretend fictitious cell to evaluate statistics
this->getInternalStatistics().evaluate (average, sum, max, intSum, numCells);
}
/** During destruction, the memory for the lattice and the contained
* cells is released. However, the dynamics objects pointed to by
* the cells must be deleted manually by the user.
*/
template<typename T, template<typename U> class Descriptor>
BlockLattice2D<T,Descriptor>::~BlockLattice2D()
{
releaseMemory();
}
/** The whole data of the lattice is duplicated. This includes
* both particle distribution function and external fields.
* \warning The dynamics objects and internalProcessors are not copied
* \param rhs the lattice to be duplicated
*/
template<typename T, template<typename U> class Descriptor>
BlockLattice2D<T,Descriptor>::BlockLattice2D(BlockLattice2D<T,Descriptor> const& rhs)
: BlockLatticeBase2D<T,Descriptor>(rhs),
AtomicBlock2D(rhs),
backgroundDynamics(rhs.backgroundDynamics->clone()),
dataTransfer(*this)
{
plint nx = this->getNx();
plint ny = this->getNy();
allocateAndInitialize();
for (plint iX=0; iX<nx; ++iX) {
for (plint iY=0; iY<ny; ++iY) {
Cell<T,Descriptor>& cell = grid[iX][iY];
// Assign cell from rhs
cell = rhs.grid[iX][iY];
// Get an independent clone of the dynamics,
// or assign backgroundDynamics
if (&cell.getDynamics()==rhs.backgroundDynamics) {
cell.attributeDynamics(backgroundDynamics);
}
else {
cell.attributeDynamics(cell.getDynamics().clone());
}
}
}
}
/** The current lattice is deallocated, then the lattice from the rhs
* is duplicated. This includes both particle distribution function
* and external fields.
* \warning The dynamics objects and internalProcessors are not copied
* \param rhs the lattice to be duplicated
*/
template<typename T, template<typename U> class Descriptor>
BlockLattice2D<T,Descriptor>& BlockLattice2D<T,Descriptor>::operator= (
BlockLattice2D<T,Descriptor> const& rhs )
{
BlockLattice2D<T,Descriptor> tmp(rhs);
swap(tmp);
return *this;
}
/** The swap is efficient, in the sense that only pointers to the
* lattice are copied, and not the lattice itself.
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::swap(BlockLattice2D& rhs) {
BlockLatticeBase2D<T,Descriptor>::swap(rhs);
AtomicBlock2D::swap(rhs);
std::swap(backgroundDynamics, rhs.backgroundDynamics);
std::swap(rawData, rhs.rawData);
std::swap(grid, rhs.grid);
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::specifyStatisticsStatus (
Box2D domain, bool status )
{
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
grid[iX][iY].specifyStatisticsStatus(status);
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::collide(Box2D domain) {
PLB_PRECONDITION( (plint)Descriptor<T>::q==(plint)Descriptor<T>::numPop );
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
grid[iX][iY].collide(this->getInternalStatistics());
grid[iX][iY].revert();
}
}
}
/** \sa collide(int,int,int,int) */
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::collide() {
collide(this->getBoundingBox());
}
/** The distribution functions never leave the rectangular domain. On the
* domain boundaries, the (outgoing) distribution functions that should
* be streamed outside are simply left untouched.
* The finalization of an iteration step is not automatically executed,
* as it is in the method stream(). If you want it to be executed, you
* must explicitly call the methods finalizeIteration() and
* executeInternalProcessors().
* \sa stream()
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::stream(Box2D domain) {
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
static const plint vicinity = Descriptor<T>::vicinity;
bulkStream( Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y0+vicinity,domain.y1-vicinity) );
boundaryStream(domain, Box2D(domain.x0,domain.x0+vicinity-1,
domain.y0,domain.y1));
boundaryStream(domain, Box2D(domain.x1-vicinity+1,domain.x1,
domain.y0,domain.y1));
boundaryStream(domain, Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y0,domain.y0+vicinity-1));
boundaryStream(domain, Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y1-vicinity+1,domain.y1));
}
/** At the end of this method, the methods finalizeIteration() and
* executeInternalProcessors() are automatically invoked.
* \sa stream(int,int,int,int)
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::stream()
{
stream(this->getBoundingBox());
implementPeriodicity();
this->executeInternalProcessors();
this->evaluateStatistics();
this->incrementTime();
}
/** This operation is more efficient than a successive application of
* collide(int,int,int,int) and stream(int,int,int,int), because memory
* is traversed only once instead of twice.
* The finalization of an iteration step is not automatically invoked by this
* method, as it is in the method stream(). If you want it to be executed, you
* must explicitly call the methods finalizeIteration() and
* executeInternalProcessors().
* \sa collideAndStream()
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::collideAndStream(Box2D domain)
{
PLB_PRECONDITION( (plint)Descriptor<T>::q==(plint)Descriptor<T>::numPop );
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
global::profiler().start("collStream");
global::profiler().increment("collStreamCells", domain.nCells());
static const plint vicinity = Descriptor<T>::vicinity;
// First, do the collision on cells within a boundary envelope of width
// equal to the range of the lattice vectors (e.g. 1 for D2Q9)
collide(Box2D(domain.x0,domain.x0+vicinity-1, domain.y0,domain.y1));
collide(Box2D(domain.x1-vicinity+1,domain.x1, domain.y0,domain.y1));
collide(Box2D(domain.x0+vicinity,domain.x1-vicinity, domain.y0,domain.y0+vicinity-1));
collide(Box2D(domain.x0+vicinity,domain.x1-vicinity, domain.y1-vicinity+1,domain.y1));
// Then, do the efficient collideAndStream algorithm in the bulk,
// excluding the envelope (this is efficient because there is no
// if-then-else statement within the loop, given that the boundary
// region is excluded)
bulkCollideAndStream(Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y0+vicinity,domain.y1-vicinity));
// Finally, do streaming in the boundary envelope to conclude the
// collision-stream cycle
boundaryStream(domain, Box2D(domain.x0,domain.x0+vicinity-1,
domain.y0,domain.y1));
boundaryStream(domain, Box2D(domain.x1-vicinity+1,domain.x1,
domain.y0,domain.y1));
boundaryStream(domain, Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y0,domain.y0+vicinity-1));
boundaryStream(domain, Box2D(domain.x0+vicinity,domain.x1-vicinity,
domain.y1-vicinity+1,domain.y1));
global::profiler().stop("collStream");
}
/** At the end of this method, the methods finalizeIteration() and
* executeInternalProcessors() are automatically invoked.
* \sa collideAndStream(int,int,int,int) */
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::collideAndStream() {
collideAndStream(this->getBoundingBox());
implementPeriodicity();
this->executeInternalProcessors();
this->evaluateStatistics();
this->incrementTime();
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::incrementTime() {
this->getTimeCounter().incrementTime();
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::allocateAndInitialize() {
this->getInternalStatistics().subscribeAverage(); // Subscribe average rho-bar
this->getInternalStatistics().subscribeAverage(); // Subscribe average uSqr
this->getInternalStatistics().subscribeMax(); // Subscribe max uSqr
plint nx = this->getNx();
plint ny = this->getNy();
rawData = new Cell<T,Descriptor> [nx*ny];
grid = new Cell<T,Descriptor>* [nx];
for (plint iX=0; iX<nx; ++iX) {
grid[iX] = rawData + iX*ny;
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::releaseMemory() {
plint nx = this->getNx();
plint ny = this->getNy();
for (plint iX=0; iX<nx; ++iX) {
for (plint iY=0; iY<ny; ++iY) {
Dynamics<T,Descriptor>* dynamics = &grid[iX][iY].getDynamics();
if (dynamics != backgroundDynamics) {
delete dynamics;
}
}
}
delete backgroundDynamics;
delete [] rawData;
delete [] grid;
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::attributeDynamics(plint iX, plint iY, Dynamics<T,Descriptor>* dynamics) {
Dynamics<T,Descriptor>* previousDynamics = &grid[iX][iY].getDynamics();
if (previousDynamics != backgroundDynamics) {
delete previousDynamics;
}
grid[iX][iY].attributeDynamics(dynamics);
}
template<typename T, template<typename U> class Descriptor>
Dynamics<T,Descriptor>& BlockLattice2D<T,Descriptor>::getBackgroundDynamics() {
return *backgroundDynamics;
}
template<typename T, template<typename U> class Descriptor>
Dynamics<T,Descriptor> const& BlockLattice2D<T,Descriptor>::getBackgroundDynamics() const {
return *backgroundDynamics;
}
/** This method is slower than bulkStream(int,int,int,int), because one needs
* to verify which distribution functions are to be kept from leaving
* the domain.
* \sa stream(int,int,int,int)
* \sa stream()
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::boundaryStream(Box2D bound, Box2D domain) {
// Make sure bound is contained within current lattice
PLB_PRECONDITION( contained(bound, this->getBoundingBox()) );
// Make sure domain is contained within bound
PLB_PRECONDITION( contained(domain, bound) );
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
for (plint iPop=1; iPop<=Descriptor<T>::q/2; ++iPop) {
plint nextX = iX + Descriptor<T>::c[iPop][0];
plint nextY = iY + Descriptor<T>::c[iPop][1];
if (nextX>=bound.x0 && nextX<=bound.x1 && nextY>=bound.y0 && nextY<=bound.y1) {
std::swap(grid[iX][iY][iPop+Descriptor<T>::q/2],
grid[nextX][nextY][iPop]);
}
}
}
}
}
/** This method is faster than boundaryStream(int,int,int,int), but it
* is erroneous when applied to boundary cells.
* \sa stream(int,int,int,int)
* \sa stream()
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::bulkStream(Box2D domain) {
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
for (plint iPop=1; iPop<=Descriptor<T>::q/2; ++iPop) {
plint nextX = iX + Descriptor<T>::c[iPop][0];
plint nextY = iY + Descriptor<T>::c[iPop][1];
std::swap(grid[iX][iY][iPop+Descriptor<T>::q/2],
grid[nextX][nextY][iPop]);
}
}
}
}
/** This method is fast, but it is erroneous when applied to boundary
* cells.
* \sa collideAndStream(int,int,int,int)
* \sa collideAndStream()
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::bulkCollideAndStream(Box2D domain) {
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
if (Descriptor<T>::vicinity==1) {
// On nearest-neighbor lattice, use the cache-efficient
// version of collidAndStream.
blockwiseBulkCollideAndStream(domain);
}
else {
// Otherwise, use the straightforward implementation.
// Note that at some point, we should implement the cache-efficient
// version for extended lattices as well.
linearBulkCollideAndStream(domain);
}
}
/** Straightforward implementation which works for all kinds of lattices,
* not only nearest-neighbor.
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::linearBulkCollideAndStream(Box2D domain) {
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
grid[iX][iY].collide(this->getInternalStatistics());
latticeTemplates<T,Descriptor>::swapAndStream2D(grid, iX, iY);
}
}
}
/** Sophisticated implementation which improves cache usage through block-wise
* loops. For now, this works only with nearest-neighbor lattices. On extended
* lattices, the naive version "linearBulkCollideAndStream" is used.
*/
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::blockwiseBulkCollideAndStream(Box2D domain) {
// Make sure domain is contained within current lattice
PLB_PRECONDITION( contained(domain, this->getBoundingBox()) );
// For cache efficiency, memory is traversed block-wise. The two outer loops enumerate
// the blocks, whereas the two inner loops enumerate the cells inside each block.
const plint blockSize = cachePolicy().getBlockSize();
// Outer loops.
for (plint outerX=domain.x0; outerX<=domain.x1; outerX+=blockSize) {
for (plint outerY=domain.y0; outerY<=domain.y1+blockSize-1; outerY+=blockSize) {
// Inner loops.
plint dx = 0;
for (plint innerX=outerX;
innerX <= std::min(outerX+blockSize-1, domain.x1);
++innerX, ++dx)
{
// Y-index is shifted in negative direction at each x-increment. to ensure
// that only post-collision cells are accessed during the swap-operation
// of the streaming.
plint minY = outerY-dx;
plint maxY = minY+blockSize-1;
for (plint innerY=std::max(minY,domain.y0);
innerY <= std::min(maxY, domain.y1);
++innerY)
{
// Collide the cell.
grid[innerX][innerY].collide (
this->getInternalStatistics() );
// Swap the populations on the cell, and then with post-collision
// neighboring cell, to perform the streaming step.
latticeTemplates<T,Descriptor>::swapAndStream2D (
grid, innerX, innerY );
}
}
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::periodicDomain(Box2D domain) {
plint nx = this->getNx();
plint ny = this->getNy();
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
for (plint iPop=1; iPop<Descriptor<T>::q; ++iPop) {
plint prevX = iX - Descriptor<T>::c[iPop][0];
plint prevY = iY - Descriptor<T>::c[iPop][1];
if ( (prevX>=0 && prevX<nx) &&
(prevY>=0 && prevY<ny) )
{
plint nextX = (iX+nx)%nx;
plint nextY = (iY+ny)%ny;
std::swap (
grid[prevX][prevY][indexTemplates::opposite<Descriptor<T> >(iPop)],
grid[nextX][nextY][iPop] );
}
}
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLattice2D<T,Descriptor>::implementPeriodicity() {
static const plint vicinity = Descriptor<T>::vicinity;
plint maxX = this->getNx()-1;
plint maxY = this->getNy()-1;
// Periodicity of edges orthogonal to x-axis.
periodicDomain(Box2D(-vicinity,-1,0,maxY));
// Periodicity of edges orthogonal to y-axis.
periodicDomain(Box2D(0,maxX,-vicinity,-1));
// Periodicity between (-1,-1) and (+1,+1) corner.
periodicDomain(Box2D(-vicinity,-1,-vicinity,-1));
// Periodicity between (-1,+1) and (+1,-1) corner.
periodicDomain(Box2D(-vicinity,-1,maxY+1,maxY+vicinity));
}
template<typename T, template<typename U> class Descriptor>
BlockLatticeDataTransfer2D<T,Descriptor>& BlockLattice2D<T,Descriptor>::getDataTransfer() {
return dataTransfer;
}
template<typename T, template<typename U> class Descriptor>
BlockLatticeDataTransfer2D<T,Descriptor> const& BlockLattice2D<T,Descriptor>::getDataTransfer() const {
return dataTransfer;
}
////////////////////// Class BlockLatticeDataTransfer2D /////////////////////////
template<typename T, template<typename U> class Descriptor>
BlockLatticeDataTransfer2D<T,Descriptor>::BlockLatticeDataTransfer2D(BlockLattice2D<T,Descriptor>& lattice_)
: lattice(lattice_)
{ }
template<typename T, template<typename U> class Descriptor>
plint BlockLatticeDataTransfer2D<T,Descriptor>::staticCellSize() const {
return sizeof(T)* (Descriptor<T>::numPop + Descriptor<T>::ExternalField::numScalars);
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::send (
Box2D domain, std::vector<char>& buffer, modif::ModifT kind ) const
{
PLB_PRECONDITION(contained(domain, lattice.getBoundingBox()));
// It's the responsibility of the functions called below to allocate
// the right amount of memory for the buffer.
buffer.clear();
switch(kind) {
case modif::staticVariables:
send_static(domain, buffer); break;
case modif::dynamicVariables:
send_dynamic(domain, buffer); break;
// Serialization is the same no matter if the dynamics object
// is being regenerated or not by the recipient.
case modif::allVariables:
case modif::dataStructure:
send_all(domain,buffer); break;
default: PLB_ASSERT(false);
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::send_static (
Box2D domain, std::vector<char>& buffer ) const
{
plint cellSize = staticCellSize();
pluint numBytes = domain.nCells()*cellSize;
// Avoid dereferencing uninitialized pointer.
if (numBytes==0) return;
buffer.resize(numBytes);
plint iData=0;
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
lattice.get(iX,iY).serialize(&buffer[iData]);
iData += cellSize;
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::send_dynamic (
Box2D domain, std::vector<char>& buffer ) const
{
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
// The serialize function automatically reallocates memory for buffer.
serialize(lattice.get(iX,iY).getDynamics(), buffer);
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::send_all (
Box2D domain, std::vector<char>& buffer ) const
{
plint cellSize = staticCellSize();
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
// 1. Send dynamic info (automaic allocation of buffer memory).
serialize(lattice.get(iX,iY).getDynamics(), buffer);
pluint pos = buffer.size();
// 2. Send static info (needs manual allocation of buffer memory).
if (staticCellSize()>0) {
buffer.resize(pos+cellSize);
lattice.get(iX,iY).serialize(&buffer[pos]);
}
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive (
Box2D domain, std::vector<char> const& buffer,
modif::ModifT kind, std::map<int,std::string> const& foreignIds )
{
if (kind==modif::dataStructure && !foreignIds.empty()) {
std::map<int,int> idIndirect;
meta::createIdIndirection<T,Descriptor>(foreignIds, idIndirect);
receive_regenerate(domain, buffer, idIndirect);
}
else {
receive(domain, buffer, kind);
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive (
Box2D domain, std::vector<char> const& buffer, modif::ModifT kind )
{
PLB_PRECONDITION(contained(domain, lattice.getBoundingBox()));
switch(kind) {
case modif::staticVariables:
receive_static(domain, buffer); break;
case modif::dynamicVariables:
receive_dynamic(domain, buffer); break;
case modif::allVariables:
receive_all(domain, buffer); break;
case modif::dataStructure:
receive_regenerate(domain, buffer); break;
default:
PLB_ASSERT( false );
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive_static (
Box2D domain, std::vector<char> const& buffer )
{
PLB_PRECONDITION( (plint) buffer.size() == domain.nCells()*staticCellSize() );
// Avoid dereferencing uninitialized pointer.
if (buffer.empty()) return;
plint cellSize = staticCellSize();
// All serialized data if of type T; therefore, buffer is considered
// as being a T-array right away.
plint iData=0;
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
lattice.get(iX,iY).unSerialize(&buffer[iData]);
iData += cellSize;
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive_dynamic (
Box2D domain, std::vector<char> const& buffer )
{
pluint serializerPos = 0;
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
// No assert is included here, because incompatible types of
// dynamics are detected by asserts inside HierarchicUnserializer.
serializerPos =
unserialize (
lattice.get(iX,iY).getDynamics(), buffer, serializerPos );
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive_all (
Box2D domain, std::vector<char> const& buffer )
{
pluint posInBuffer = 0;
plint cellSize = staticCellSize();
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
// 1. Unserialize dynamic data.
posInBuffer =
unserialize (
lattice.get(iX,iY).getDynamics(), buffer, posInBuffer );
// 2. Unserialize static data.
if (staticCellSize()>0) {
lattice.get(iX,iY).unSerialize(&buffer[posInBuffer]);
posInBuffer += cellSize;
}
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::receive_regenerate (
Box2D domain, std::vector<char> const& buffer, std::map<int,int> const& idIndirect )
{
pluint posInBuffer = 0;
plint cellSize = staticCellSize();
for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
// 1. Generate dynamics object, and unserialize dynamic data.
std::map<int,int> const* indirectPtr = idIndirect.empty() ? 0 : &idIndirect;
HierarchicUnserializer unserializer(buffer, posInBuffer, indirectPtr);
Dynamics<T,Descriptor>* newDynamics =
meta::dynamicsRegistration<T,Descriptor>().generate(unserializer);
posInBuffer = unserializer.getCurrentPos();
lattice.attributeDynamics(iX,iY, newDynamics);
// 2. Unserialize static data.
if (staticCellSize()>0) {
PLB_ASSERT( !buffer.empty() );
PLB_ASSERT( posInBuffer+staticCellSize()<=buffer.size() );
lattice.get(iX,iY).unSerialize(&buffer[posInBuffer]);
posInBuffer += cellSize;
}
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::attribute (
Box2D toDomain, plint deltaX, plint deltaY,
AtomicBlock2D const& from, modif::ModifT kind )
{
PLB_PRECONDITION (typeid(from) == typeid(BlockLattice2D<T,Descriptor> const&));
PLB_PRECONDITION(contained(toDomain, lattice.getBoundingBox()));
BlockLattice2D<T,Descriptor> const& fromLattice = (BlockLattice2D<T,Descriptor> const&) from;
switch(kind) {
case modif::staticVariables:
attribute_static(toDomain, deltaX, deltaY, fromLattice); break;
case modif::dynamicVariables:
attribute_dynamic(toDomain, deltaX, deltaY, fromLattice); break;
case modif::allVariables:
attribute_all(toDomain, deltaX, deltaY, fromLattice); break;
case modif::dataStructure:
attribute_regenerate(toDomain, deltaX, deltaY, fromLattice); break;
default:
PLB_ASSERT( false );
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::attribute_static (
Box2D toDomain, plint deltaX, plint deltaY,
BlockLattice2D<T,Descriptor> const& from )
{
for (plint iX=toDomain.x0; iX<=toDomain.x1; ++iX) {
for (plint iY=toDomain.y0; iY<=toDomain.y1; ++iY) {
lattice.get(iX,iY).attributeValues (
from.get(iX+deltaX,iY+deltaY) );
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::attribute_dynamic (
Box2D toDomain, plint deltaX, plint deltaY,
BlockLattice2D<T,Descriptor> const& from )
{
std::vector<char> serializedData;
for (plint iX=toDomain.x0; iX<=toDomain.x1; ++iX) {
for (plint iY=toDomain.y0; iY<=toDomain.y1; ++iY) {
serializedData.clear();
serialize (
from.get(iX+deltaX,iY+deltaY).getDynamics(),
serializedData );
unserialize (
lattice.get(iX,iY).getDynamics(),
serializedData );
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::attribute_all (
Box2D toDomain, plint deltaX, plint deltaY,
BlockLattice2D<T,Descriptor> const& from )
{
std::vector<char> serializedData;
for (plint iX=toDomain.x0; iX<=toDomain.x1; ++iX) {
for (plint iY=toDomain.y0; iY<=toDomain.y1; ++iY) {
// 1. Attribute dynamic content.
serializedData.clear();
serialize (
from.get(iX+deltaX,iY+deltaY).getDynamics(),
serializedData );
unserialize (
lattice.get(iX,iY).getDynamics(),
serializedData );
// 2. Attribute static content.
lattice.get(iX,iY).attributeValues (
from.get(iX+deltaX,iY+deltaY) );
}
}
}
template<typename T, template<typename U> class Descriptor>
void BlockLatticeDataTransfer2D<T,Descriptor>::attribute_regenerate (
Box2D toDomain, plint deltaX, plint deltaY,
BlockLattice2D<T,Descriptor> const& from )
{
std::vector<char> serializedData;
for (plint iX=toDomain.x0; iX<=toDomain.x1; ++iX) {
for (plint iY=toDomain.y0; iY<=toDomain.y1; ++iY) {
// 1. Generate new dynamics and attribute dynamic content.
serializedData.clear();
serialize (
from.get(iX+deltaX,iY+deltaY).getDynamics(),
serializedData );
HierarchicUnserializer unserializer(serializedData, 0);
Dynamics<T,Descriptor>* newDynamics =
meta::dynamicsRegistration<T,Descriptor>().generate(unserializer);
lattice.attributeDynamics(iX,iY, newDynamics);
// 2. Attribute static content.
lattice.get(iX,iY).attributeValues (
from.get(iX+deltaX,iY+deltaY) );
}
}
}
template<typename T, template<typename U> class Descriptor>
CachePolicy2D& BlockLattice2D<T,Descriptor>::cachePolicy() {
static CachePolicy2D cachePolicySingleton(200);
return cachePolicySingleton;
}
/////////// Free Functions //////////////////////////////
template<typename T, template<typename U> class Descriptor>
double getStoredAverageDensity(BlockLattice2D<T,Descriptor> const& blockLattice) {
return Descriptor<T>::fullRho (
blockLattice.getInternalStatistics().getAverage (
LatticeStatistics::avRhoBar ) );
}
template<typename T, template<typename U> class Descriptor>
double getStoredAverageEnergy(BlockLattice2D<T,Descriptor> const& blockLattice) {
return 0.5 * blockLattice.getInternalStatistics().getAverage (
LatticeStatistics::avUSqr );
}
template<typename T, template<typename U> class Descriptor>
double getStoredMaxVelocity(BlockLattice2D<T,Descriptor> const& blockLattice) {
return std::sqrt( blockLattice.getInternalStatistics().getMax (
LatticeStatistics::maxUSqr ) );
}
} // namespace plb
#endif // BLOCK_LATTICE_2D_HH
|