AutoPas  3.0.0
Loading...
Searching...
No Matches
VerletClusterLists.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <algorithm>
10#include <cmath>
11#include <iterator>
12
13#include "ClusterTowerBlock2D.h"
29#include "autopas/utils/Timer.h"
31#include "autopas/utils/inBox.h"
33
34namespace autopas {
35
55template <class Particle_T>
57 public:
61 using ParticleType = Particle_T;
66
70 struct ClusterRange {
82 size_t numClusters{};
83 };
84
97 VerletClusterLists(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax, double cutoff,
98 double skin, size_t clusterSize, LoadEstimatorOption loadEstimator = LoadEstimatorOption::none)
99 : ParticleContainerInterface<Particle_T>(skin),
100 _towerBlock{boxMin, boxMax, cutoff + skin},
101 _clusterSize{clusterSize},
102 _particlesToAdd(autopas_get_max_threads()),
103 _cutoff{cutoff},
104 _loadEstimator(loadEstimator) {
105 // always have at least one tower.
106 _towerBlock.addTower(_clusterSize);
107 }
108
109 [[nodiscard]] ContainerOption getContainerType() const override { return ContainerOption::verletClusterLists; }
110
116 // (Explicit) static cast required for Apple Clang (last tested version: 17.0.0)
117 switch (static_cast<LoadEstimatorOption::Value>(this->_loadEstimator)) {
119 return [&](const std::array<unsigned long, 3> &cellsPerDimension,
120 const std::array<unsigned long, 3> &lowerCorner, const std::array<unsigned long, 3> &upperCorner) {
121 // the neighborListLength function defined for verletListsCells in not compatible with this container.
122 unsigned long sum = 0;
123 for (unsigned long x = lowerCorner[0]; x <= upperCorner[0]; x++) {
124 for (unsigned long y = lowerCorner[1]; y <= upperCorner[1]; y++) {
125 unsigned long cellLoad = 0;
126 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
127 for (auto &cluster : tower.getClusters()) {
128 if (cluster.getNeighbors()) {
129 cellLoad += cluster.getNeighbors()->size();
130 }
131 }
132 sum += cellLoad;
133 }
134 }
135 return sum;
136 };
137 }
139 [[fallthrough]];
140 default: {
141 return
142 [&](const std::array<unsigned long, 3> &cellsPerDimension, const std::array<unsigned long, 3> &lowerCorner,
143 const std::array<unsigned long, 3> &upperCorner) { return 1; };
144 }
145 }
146 }
147
148 void computeInteractions(TraversalInterface *traversal) override {
149 if (_isValid == ValidityState::cellsAndListsValid) {
151 "VerletClusterLists::computeInteractions(): Trying to do a pairwise iteration, even though verlet lists are "
152 "not valid.");
153 }
154 auto *traversalInterface = dynamic_cast<VCLTraversalInterface<Particle_T> *>(traversal);
155 if (traversalInterface) {
156 traversalInterface->setClusterLists(*this);
157 traversalInterface->setTowers(_towerBlock.getTowersRef());
158 } else {
160 "Trying to use a traversal of wrong type in VerletClusterLists::computeInteractions. TraversalID: {}",
161 traversal->getTraversalType());
162 }
163 if (auto *balancedTraversal = dynamic_cast<BalancedTraversal *>(traversal)) {
164 balancedTraversal->setLoadEstimator(getLoadEstimatorFunction());
165 }
166
167 traversal->initTraversal();
168 traversal->traverseParticles();
169 traversal->endTraversal();
170 }
171
172 void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override {
173 const auto particlesPerTower = (numParticles + numParticlesHaloEstimate) / _towerBlock.size();
174 for (auto &tower : _towerBlock) {
175 tower.reserve(particlesPerTower);
176 }
177 }
178
184 void addParticleImpl(const Particle_T &p) override {
185 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
186 _particlesToAdd[autopas_get_thread_num()].push_back(p);
187 }
188
189 void addHaloParticleImpl(const Particle_T &haloParticle) override {
190 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
191 _particlesToAdd[autopas_get_thread_num()].push_back(haloParticle);
192 }
193
194 bool updateHaloParticle(const Particle_T &haloParticle) override {
195 using namespace autopas::utils::ArrayMath::literals;
196
197 const auto &haloPos = haloParticle.getR();
198 // this might be called from a parallel region so force this iterator to be sequential
199 for (auto it = getRegionIterator(haloPos - (this->getVerletSkin() / 2.), haloPos + (this->getVerletSkin() / 2.),
200 IteratorBehavior::halo | IteratorBehavior::forceSequential, nullptr);
201 it.isValid(); ++it) {
202 if (haloParticle.getID() == it->getID()) {
203 // don't simply copy haloParticle over iter. This would trigger a dataRace with other regionIterators that
204 // overlap with this region.
205 it->setR(haloPos);
206 it->setV(haloParticle.getV());
207 it->setF(haloParticle.getF());
208 return true;
209 }
210 }
211 return false;
212 }
213
214 void deleteHaloParticles() override {
215 // Step 1: Remove particles from _particlesToAdd
216 for (auto &particleVec : _particlesToAdd) {
217 for (size_t j = 0; j < particleVec.size();) {
218 if (particleVec[j].isHalo()) {
219 particleVec[j] = particleVec[particleVec.size() - 1];
220 particleVec.pop_back();
221 } else {
222 ++j;
223 }
224 }
225 }
226 // Step 2: Remove particles from _towers
227 bool deletedSomething = false;
228 AUTOPAS_OPENMP(parallel for reduction(|| : deletedSomething))
229 // Thanks to clang 13 this has to be a index based loop instead of a range based
230 for (size_t i = 0; i < _towerBlock.size(); ++i) {
231 auto &tower = _towerBlock[i];
232 const auto towerSize = tower.size();
233 auto numTailDummies = tower.getNumTailDummyParticles();
234 // iterate over all non-tail dummies. Avoid underflows.
235 for (size_t j = 0; numTailDummies < towerSize and j < towerSize - numTailDummies;) {
236 if (tower[j].isHalo()) {
237 // swap-"delete"
238 tower[j] = tower[towerSize - 1 - numTailDummies];
239 // Since we can't pop the moved particle here mark it for deletion.
240 internal::markParticleAsDeleted(tower[towerSize - 1 - numTailDummies]);
241 ++numTailDummies;
242 deletedSomething = true;
243 } else {
244 ++j;
245 }
246 }
247 // if anything was marked for deletion actually delete it now.
248 if (deletedSomething) {
249 tower.deleteDummyParticles();
250 }
251 }
252 if (deletedSomething) {
253 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
254 }
255 }
256
257 std::tuple<const Particle_T *, size_t, size_t> getParticle(size_t cellIndex, size_t particleIndex,
258 IteratorBehavior iteratorBehavior,
259 const std::array<double, 3> &boxMin,
260 const std::array<double, 3> &boxMax) const override {
261 return getParticleImpl<true>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
262 }
263 std::tuple<const Particle_T *, size_t, size_t> getParticle(size_t cellIndex, size_t particleIndex,
264 IteratorBehavior iteratorBehavior) const override {
265 // this is not a region iter hence we stretch the bounding box to the numeric max
266 constexpr std::array<double, 3> boxMin{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(),
267 std::numeric_limits<double>::lowest()};
268
269 constexpr std::array<double, 3> boxMax{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
270 std::numeric_limits<double>::max()};
271 return getParticleImpl<false>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
272 }
273
285 template <bool regionIter>
286 std::tuple<const Particle_T *, size_t, size_t> getParticleImpl(size_t cellIndex, size_t particleIndex,
287 IteratorBehavior iteratorBehavior,
288 const std::array<double, 3> &boxMin,
289 const std::array<double, 3> &boxMax) const {
290 using namespace autopas::utils::ArrayMath::literals;
291
292 // in this context cell == tower
293 // catching the edge case that towers are not yet built -> Iterator jumps to additional vectors
294 if (_towerBlock.empty()) {
295 return {nullptr, 0, 0};
296 }
297
298 std::array<double, 3> boxMinWithSafetyMargin = boxMin;
299 std::array<double, 3> boxMaxWithSafetyMargin = boxMax;
300 if constexpr (regionIter) {
301 // We extend the search box for cells here since particles might have moved
302 boxMinWithSafetyMargin -= 0.5 * this->getVerletSkin();
303 boxMaxWithSafetyMargin += 0.5 * this->getVerletSkin();
304 }
305
306 // first and last relevant cell index
307 const auto [startCellIndex, endCellIndex] = [&]() -> std::tuple<size_t, size_t> {
308 if constexpr (regionIter) {
309 // We extend the search box for cells here since particles might have moved
310 return {_towerBlock.getTowerIndex1DAtPosition(boxMinWithSafetyMargin),
311 _towerBlock.getTowerIndex1DAtPosition(boxMaxWithSafetyMargin)};
312 } else {
313 if (not(iteratorBehavior & IteratorBehavior::halo)) {
314 // only potentially owned region
315 return {_towerBlock.getFirstOwnedTowerIndex(), _towerBlock.getLastOwnedTowerIndex()};
316 } else {
317 // whole range of cells
318 return {0, _towerBlock.size() - 1};
319 }
320 }
321 }();
322
323 // if we are at the start of an iteration ...
324 if (cellIndex == 0 and particleIndex == 0) {
325 cellIndex =
326 startCellIndex + ((iteratorBehavior & IteratorBehavior::forceSequential) ? 0 : autopas_get_thread_num());
327 }
328 // abort if the start index is already out of bounds
329 if (cellIndex >= _towerBlock.size()) {
330 return {nullptr, 0, 0};
331 }
332 // check the data behind the indices
333 if (particleIndex >= _towerBlock[cellIndex].getNumActualParticles() or
334 not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
335 _towerBlock[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax)) {
336 // either advance them to something interesting or invalidate them.
337 std::tie(cellIndex, particleIndex) =
338 advanceIteratorIndices<regionIter>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax,
339 boxMinWithSafetyMargin, boxMaxWithSafetyMargin, endCellIndex);
340 }
341 // shortcut if the given index doesn't exist
342 if (cellIndex > endCellIndex) {
343 return {nullptr, 0, 0};
344 }
345 const Particle_T *retPtr = &_towerBlock[cellIndex][particleIndex];
346
347 return {retPtr, cellIndex, particleIndex};
348 }
349
350 bool deleteParticle(Particle_T &particle) override {
351 // This function doesn't actually delete anything as it would mess up the clusters' references.
353 return false;
354 }
355
356 bool deleteParticle(size_t cellIndex, size_t particleIndex) override {
357 // This function doesn't actually delete anything as it would mess up the clusters' references.
358 internal::markParticleAsDeleted(_towerBlock[cellIndex][particleIndex]);
359 return false;
360 }
361
362 [[nodiscard]] std::vector<Particle_T> updateContainer(bool keepNeighborListsValid) override {
363 if (keepNeighborListsValid) {
365 }
366 // First delete all halo particles.
367 this->deleteHaloParticles();
368 // Delete dummy particles.
369 AUTOPAS_OPENMP(parallel for)
370 for (size_t i = 0ul; i < _towerBlock.size(); ++i) {
371 _towerBlock[i].deleteDummyParticles();
372 }
373 // Delete dummy particles also from the _particlesToAdd vector
374 AUTOPAS_OPENMP(parallel for)
375 for (size_t i = 0ul; i < _particlesToAdd.size(); ++i) {
376 _particlesToAdd[i].erase(std::remove_if(_particlesToAdd[i].begin(), _particlesToAdd[i].end(),
377 [](const auto &p) { return p.isDummy(); }),
378 _particlesToAdd[i].end());
379 }
380
381 // next find invalid particles
382 std::vector<Particle_T> invalidParticles;
383
384 // custom openmp reduction to concatenate all local vectors to one at the end of a parallel region
385 AUTOPAS_OPENMP(declare reduction(
386 vecMergeParticle : std::vector<Particle_T> : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end())))
387 AUTOPAS_OPENMP(parallel reduction(vecMergeParticle : invalidParticles)) {
388 for (auto iter = this->begin(IteratorBehavior::owned); iter.isValid(); ++iter) {
389 if (not utils::inBox(iter->getR(), this->getBoxMin(), this->getBoxMax())) {
390 invalidParticles.push_back(*iter);
392 }
393 }
394 }
395 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
396 return invalidParticles;
397 }
398
399 [[nodiscard]] TraversalSelectorInfo getTraversalSelectorInfo() const override {
400 using namespace autopas::utils::ArrayMath::literals;
401 // Here, the towers might not yet be built, hence do not use members like _towerBlock.getTowersPerDim
402 const auto boxSizeWithHalo = this->getHaloBoxMax() - this->getHaloBoxMin();
403 const auto [towerSideLength, towersPerDim] = _towerBlock.estimateOptimalGridSideLength(
404 this->getNumberOfParticles(IteratorBehavior::ownedOrHalo), _clusterSize);
405 const std::array<double, 3> towerSize = {towerSideLength[0], towerSideLength[1],
406 this->getHaloBoxMax()[2] - this->getHaloBoxMin()[2]};
407 const std::array<unsigned long, 3> towerDimensions = {towersPerDim[0], towersPerDim[1], 1};
408 return TraversalSelectorInfo(towerDimensions, this->getInteractionLength(), towerSize, _clusterSize);
409 }
410
415 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
416 typename ContainerIterator<Particle_T, true, false>::ParticleVecType *additionalVectors = nullptr) override {
417 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
418 // invalid, we do writing operations on _particlesToAdd and can not read from it without race conditions.
419 if (_isValid != ValidityState::invalid) {
420 // we call particlesToAddEmpty() as a sanity check to ensure there are actually no particles in _particlesToAdd if
421 // the status is not invalid
422 if (not particlesToAddEmpty(autopas_get_thread_num())) {
424 "VerletClusterLists::begin(): Error: particle container is valid, but _particlesToAdd isn't empty!");
425 }
426 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
427 // from LogicHandler.
428 return ContainerIterator<Particle_T, true, false>(*this, behavior, additionalVectors);
429 } else {
430 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
431 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
433 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
434 return ContainerIterator<Particle_T, true, false>(*this, behavior, &additionalVectorsToPass);
435 }
436 }
437
443 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo,
445 nullptr) const override {
446 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
447 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
448 if (_isValid != ValidityState::invalid) {
449 // we call particlesToAddEmpty() as a sanity check to ensire there are actually no particles in _particlesToAdd if
450 // the status is not invalid
451 if (not particlesToAddEmpty(autopas_get_thread_num())) {
453 "VerletClusterLists::begin() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
454 }
455 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
456 // from LogicHandler.
457 return ContainerIterator<Particle_T, false, false>(*this, behavior, additionalVectors);
458 } else {
459 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
460 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
462 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
463 return ContainerIterator<Particle_T, false, false>(*this, behavior, &additionalVectorsToPass);
464 }
465 }
466
470 template <typename Lambda>
471 void forEach(Lambda forEachLambda, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
472 for (auto &tower : _towerBlock) {
473 tower.forEach(forEachLambda, behavior);
474 }
475 for (auto &vector : this->_particlesToAdd) {
476 for (auto &particle : vector) {
477 if (behavior.contains(particle)) {
478 forEachLambda(particle);
479 }
480 }
481 }
482 }
483
489 template <typename Lambda>
490 void forEach(Lambda forEachLambda, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
491 if (_isValid != ValidityState::invalid) {
492 if (not particlesToAddEmpty()) {
494 "VerletClusterLists::forEach() const: Error: particle container is valid, but _particlesToAdd isn't "
495 "empty!");
496 }
497 }
498
499 // If the particles are sorted into the towers, we can simply use the iteration over towers.
500 for (auto &tower : _towerBlock) {
501 tower.forEach(forEachLambda, behavior);
502 }
503
504 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
505 if (_isValid == ValidityState::invalid) {
506 for (auto &particlesToAddPerThread : _particlesToAdd) {
507 for (auto &particle : particlesToAddPerThread) {
508 if (behavior.contains(particle)) {
509 forEachLambda(particle);
510 }
511 }
512 }
513 }
514 }
515
519 template <typename Lambda, typename A>
520 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
521 for (auto &tower : _towerBlock) {
522 tower.reduce(reduceLambda, result, behavior);
523 }
524 for (auto &vector : this->_particlesToAdd) {
525 for (auto &p : vector) {
526 if (behavior.contains(p)) {
527 reduceLambda(p, result);
528 }
529 }
530 }
531 }
532
538 template <typename Lambda, typename A>
539 void reduce(Lambda reduceLambda, A &result,
540 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
541 if (_isValid != ValidityState::invalid) {
542 if (not particlesToAddEmpty()) {
544 "VerletClusterLists::reduce() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
545 }
546 }
547
548 // If the particles are sorted into the towers, we can simply use the iteration over towers.
549 for (auto tower : _towerBlock) {
550 tower.reduce(reduceLambda, result, behavior);
551 }
552
553 if (_isValid == ValidityState::invalid) {
554 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
555 for (auto &particlesToAddPerThread : _particlesToAdd) {
556 for (auto &particle : particlesToAddPerThread) {
557 if (behavior.contains(particle)) {
558 reduceLambda(particle, result);
559 }
560 }
561 }
562 }
563 }
564
569 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
570 typename ContainerIterator<Particle_T, true, true>::ParticleVecType *additionalVectors = nullptr) override {
571 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
572 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
573 if (_isValid != ValidityState::invalid) {
574 // we call particlesToAddEmpty() as a sanity check to ensure there are actually no particles in _particlesToAdd
575 // if the status is not invalid
576 if (not particlesToAddEmpty(autopas_get_thread_num())) {
578 "VerletClusterLists::reduce() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
579 }
580 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
581 // from LogicHandler.
582 return ContainerIterator<Particle_T, true, true>(*this, behavior, additionalVectors, lowerCorner, higherCorner);
583 } else {
584 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
585 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
586 typename ContainerIterator<Particle_T, true, true>::ParticleVecType additionalVectorsToPass;
587 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
588 return ContainerIterator<Particle_T, true, true>(*this, behavior, &additionalVectorsToPass, lowerCorner,
589 higherCorner);
590 }
591 }
592
598 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
600 nullptr) const override {
601 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
602 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
603 if (_isValid != ValidityState::invalid) {
604 // we call particlesToAddEmpty() as a sanity check to ensire there are actually no particles in _particlesToAdd if
605 // the status is not invalid
606 if (not particlesToAddEmpty(autopas_get_thread_num())) {
608 "VerletClusterLists::getRegionIterator() const: Error: particle container is valid, but _particlesToAdd "
609 "isn't empty!");
610 }
611 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
612 // from LogicHandler.
613 return ContainerIterator<Particle_T, false, true>(*this, behavior, additionalVectors, lowerCorner, higherCorner);
614 } else {
615 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
616 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
618 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
619 return ContainerIterator<Particle_T, false, true>(*this, behavior, &additionalVectorsToPass, lowerCorner,
620 higherCorner);
621 }
622 }
623
627 template <typename Lambda>
628 void forEachInRegion(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
629 const std::array<double, 3> &higherCorner,
630 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
631 for (size_t i = 0; i < _towerBlock.size(); ++i) {
632 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
633 continue;
634 }
635 auto &tower = _towerBlock[i];
636 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
637 // particles can move over cell borders. Calculate the volume this cell's particles can be.
638 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
639 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
640 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
641 tower.forEach(forEachLambda, lowerCorner, higherCorner, behavior);
642 }
643 }
644 for (auto &vector : _particlesToAdd) {
645 for (auto &particle : vector) {
646 if (behavior.contains(particle)) {
647 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
648 forEachLambda(particle);
649 }
650 }
651 }
652 }
653 }
654
660 template <typename Lambda>
661 void forEachInRegion(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
662 const std::array<double, 3> &higherCorner,
663 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
664 if (_isValid != ValidityState::invalid) {
665 if (not particlesToAddEmpty()) {
667 "VerletClusterLists::forEachInRegion() const: Error: particle container is valid, but _particlesToAdd "
668 "isn't empty!");
669 }
670 }
671
672 // If the particles are sorted into the towers, we can simply use the iteration over towers.
673 for (size_t i = 0; i < _towerBlock.size(); ++i) {
674 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
675 continue;
676 }
677 auto &tower = _towerBlock[i];
678 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
679 // particles can move over cell borders. Calculate the volume this cell's particles can be.
680 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
681 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
682 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
683 tower.forEach(forEachLambda, lowerCorner, higherCorner, behavior);
684 }
685 }
686
687 if (_isValid == ValidityState::invalid) {
688 // If the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
689 for (auto &particlesToAddPerThread : _particlesToAdd) {
690 for (auto &particle : particlesToAddPerThread) {
691 if (behavior.contains(particle)) {
692 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
693 forEachLambda(particle);
694 }
695 }
696 }
697 }
698 }
699 }
700
704 template <typename Lambda, typename A>
705 void reduceInRegion(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
706 const std::array<double, 3> &higherCorner,
707 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
708 for (size_t i = 0; i < _towerBlock.size(); ++i) {
709 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
710 continue;
711 }
712 auto &tower = _towerBlock[i];
713 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
714 // particles can move over cell borders. Calculate the volume this cell's particles can be.
715 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
716 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
717 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
718 tower.reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
719 }
720 }
721 for (auto &vector : _particlesToAdd) {
722 for (auto &particle : vector) {
723 if (behavior.contains(particle)) {
724 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
725 reduceLambda(particle, result);
726 }
727 }
728 }
729 }
730 }
731
737 template <typename Lambda, typename A>
738 void reduceInRegion(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
739 const std::array<double, 3> &higherCorner,
740 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
741 if (_isValid != ValidityState::invalid) {
742 if (not particlesToAddEmpty()) {
744 "VerletClusterLists::reduceInRegion() const: Error: particle container is valid, but _particlesToAdd isn't "
745 "empty!");
746 }
747 }
748 // If the particles are sorted into the towers, we can simply use the iteration over towers.
749 for (size_t i = 0; i < _towerBlock.size(); ++i) {
750 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
751 continue;
752 }
753 auto &tower = _towerBlock[i];
754 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
755 // particles can move over cell borders. Calculate the volume this cell's particles can be.
756 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
757 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
758 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
759 tower.reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
760 }
761 }
762
763 if (_isValid == ValidityState::invalid) {
764 // If the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
765 for (auto &particlesToAddPerThread : _particlesToAdd) {
766 for (auto &particle : particlesToAddPerThread) {
767 if (behavior.contains(particle)) {
768 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
769 reduceLambda(particle, result);
770 }
771 }
772 }
773 }
774 }
775 }
776
777 void rebuildNeighborLists(TraversalInterface *traversal) override {
778 // The builder might have a different newton3 choice than the traversal. This typically only happens in unit tests
779 // when rebuildTowersAndClusters() was not called explicitly.
780 if (_isValid == ValidityState::invalid or traversal->getUseNewton3() != _builder->getNewton3()) {
781 // clear the lists buffer because clusters will be recreated
782 _neighborLists.clear();
784 }
785 _builder->rebuildNeighborListsAndFillClusters();
786
787 auto *clusterTraversalInterface = dynamic_cast<VCLTraversalInterface<Particle_T> *>(traversal);
788 if (clusterTraversalInterface) {
789 if (clusterTraversalInterface->needsStaticClusterThreadPartition()) {
791 }
792 } else {
794 "Trying to use a traversal of wrong type in VerletClusterLists::rebuildNeighborLists. TraversalID: {}",
795 traversal->getTraversalType());
796 }
797 }
798
806 template <bool inParallel, class LoopBody>
807 void traverseClusters(LoopBody &&loopBody) {
808 if (inParallel) {
809 traverseClustersParallel<LoopBody>(std::forward<LoopBody>(loopBody));
810 } else {
811 traverseClustersSequential<LoopBody>(std::forward<LoopBody>(loopBody));
812 }
813 }
814
819 [[nodiscard]] size_t size() const override {
820 size_t sum = std::accumulate(_towerBlock.begin(), _towerBlock.end(), 0,
821 [](size_t acc, const auto &tower) { return acc + tower.size(); });
822 sum = std::accumulate(_particlesToAdd.begin(), _particlesToAdd.end(), sum,
823 [](size_t acc, const auto &buffer) { return acc + buffer.size(); });
824 return sum;
825 }
826
830 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
831 // sum up all particles in towers that fulfill behavior
832 size_t sum = std::accumulate(_towerBlock.begin(), _towerBlock.end(), 0, [&behavior](size_t acc, const auto &tower) {
833 return acc + tower.getNumberOfParticles(behavior);
834 });
835
836 // Since we can not directly insert particles into towers without a rebuild of the whole data structure,
837 // _particlesToAdd is used to store all these particles temporarily until the next rebuild inserts them into the
838 // towers data structure. However, these particles already belong to the respective tower, so we have to count them
839 // as well.
840 sum = std::accumulate(
841 _particlesToAdd.begin(), _particlesToAdd.end(), sum, [&behavior](size_t acc, const auto &buffer) {
842 return acc +
843 (std::count_if(buffer.begin(), buffer.end(), [&behavior](auto p) { return behavior.contains(p); }));
844 });
845
846 return sum;
847 }
848
853 const auto &getClusterThreadPartition() const { return _clusterThreadPartition; }
854
859 auto getNumClusters() const { return _numClusters; }
860
865 auto getTowerSideLength() const { return _towerBlock.getTowerSideLength(); }
866
871 auto getTowersPerDimension() const { return _towerBlock.getTowersPerDim(); }
872
877 auto getClusterSize() const { return _clusterSize; }
878
883 auto getNumTowersPerInteractionLength() const { return _towerBlock.getNumTowersPerInteractionLength(); }
884
890 template <class Functor>
892 const auto numTowers = _towerBlock.size();
894 AUTOPAS_OPENMP(parallel for schedule(dynamic))
895 for (size_t index = 0; index < numTowers; index++) {
896 _towerBlock[index].loadSoA(functor);
897 }
898 }
899
905 template <class Functor>
907 const auto numTowers = _towerBlock.size();
909 AUTOPAS_OPENMP(parallel for schedule(dynamic))
910 for (size_t index = 0; index < numTowers; index++) {
911 _towerBlock[index].extractSoA(functor);
912 }
913 }
914
922 return _towerBlock.getTowerByIndex2D(x, y);
923 }
924
933
934 [[nodiscard]] const std::array<double, 3> &getBoxMax() const override { return _towerBlock.getBoxMax(); }
935
940 [[nodiscard]] const std::array<double, 3> &getHaloBoxMax() const { return _towerBlock.getHaloBoxMax(); }
941
942 [[nodiscard]] const std::array<double, 3> &getBoxMin() const override { return _towerBlock.getBoxMin(); }
943
948 [[nodiscard]] const std::array<double, 3> &getHaloBoxMin() const { return _towerBlock.getHaloBoxMin(); }
949
950 [[nodiscard]] double getCutoff() const override { return _cutoff; }
951
952 void setCutoff(double cutoff) override { _cutoff = cutoff; }
953
954 [[nodiscard]] double getVerletSkin() const override { return this->_skin; }
955
960 void setSkin(double skin) { this->_skin = skin; }
961
962 [[nodiscard]] double getInteractionLength() const override { return _cutoff + this->getVerletSkin(); }
963
964 void deleteAllParticles() override {
965 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
966 std::for_each(_particlesToAdd.begin(), _particlesToAdd.end(), [](auto &buffer) { buffer.clear(); });
967 std::for_each(_towerBlock.begin(), _towerBlock.end(), [](auto &tower) { tower.clear(); });
968 }
969
975 return _neighborLists;
976 }
977
983 void rebuildTowersAndClusters(bool newton3) {
984 using namespace utils::ArrayMath::literals;
985 // collect all particles to add from across the thread buffers
986 typename decltype(_particlesToAdd)::value_type particlesToAdd;
987 const size_t numParticlesToAdd =
988 std::accumulate(_particlesToAdd.begin(), _particlesToAdd.end(), 0,
989 [](size_t acc, const auto &buffer) { return acc + buffer.size(); });
990 particlesToAdd.reserve(numParticlesToAdd);
991 std::for_each(_particlesToAdd.begin(), _particlesToAdd.end(), [&](auto &particlesBuffer) {
992 particlesToAdd.insert(particlesToAdd.end(), particlesBuffer.begin(), particlesBuffer.end());
993 particlesBuffer.clear();
994 });
995
996 const double interactionLength = _cutoff + this->_skin;
997 _builder = std::make_unique<internal::VerletClusterListsRebuilder<Particle_T>>(
998 _towerBlock, particlesToAdd, _neighborLists, _clusterSize, interactionLength * interactionLength, newton3);
999
1000 _numClusters = _builder->rebuildTowersAndClusters();
1001
1002 _isValid.store(ValidityState::cellsValidListsInvalid, std::memory_order_relaxed);
1003 for (auto &tower : _towerBlock) {
1004 tower.setParticleDeletionObserver(this);
1005 }
1006 }
1007
1013 template <class LoopBody>
1014 void traverseClustersSequential(LoopBody &&loopBody) {
1015 for (size_t x = 0; x < _towerBlock.getTowersPerDim()[0]; x++) {
1016 for (size_t y = 0; y < _towerBlock.getTowersPerDim()[1]; y++) {
1017 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
1018 for (auto clusterIter = tower.getFirstOwnedCluster(); clusterIter < tower.getFirstTailHaloCluster();
1019 ++clusterIter) {
1020 loopBody(*clusterIter);
1021 }
1022 }
1023 }
1024 }
1025
1035 template <class LoopBody>
1036 void traverseClustersParallel(LoopBody &&loopBody) {
1037 const auto towersPerDimX = _towerBlock.getTowersPerDim()[0];
1038 const auto towersPerDimY = _towerBlock.getTowersPerDim()[1];
1040 AUTOPAS_OPENMP(parallel for schedule(dynamic) collapse(2))
1041 for (size_t x = 0; x < towersPerDimX; x++) {
1042 for (size_t y = 0; y < towersPerDimY; y++) {
1043 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
1044
1045 for (auto clusterIter = tower.getFirstOwnedCluster(); clusterIter < tower.getFirstTailHaloCluster();
1046 ++clusterIter) {
1047 loopBody(*clusterIter);
1048 }
1049 }
1050 }
1051 }
1052
1053 protected:
1059 size_t numClusterPairs = 0;
1060 this->template traverseClusters<false>(
1061 [&numClusterPairs](auto &cluster) { numClusterPairs += cluster.getNeighbors()->size(); });
1062
1063 constexpr int minNumClusterPairsPerThread = 1000;
1064 auto numThreads =
1065 std::clamp(static_cast<int>(numClusterPairs / minNumClusterPairsPerThread), 1, autopas_get_max_threads());
1066
1067 size_t numClusterPairsPerThread =
1068 std::max(static_cast<unsigned long>(std::ceil(static_cast<double>(numClusterPairs) / numThreads)), 1ul);
1069 if (numClusterPairsPerThread * numThreads < numClusterPairs) {
1071 "VerletClusterLists::calculateClusterThreadPartition(): numClusterPairsPerThread ({}) * numThreads ({})={} "
1072 "should always "
1073 "be at least the amount of Cluster Pairs ({})!",
1074 numClusterPairsPerThread, numThreads, numClusterPairsPerThread * numThreads, numClusterPairs);
1075 }
1076 fillClusterRanges(numClusterPairsPerThread, numThreads);
1077 }
1078
1085 void fillClusterRanges(size_t numClusterPairsPerThread, int numThreads) {
1086 if (numClusterPairsPerThread < 1) {
1088 "VerletClusterLists::fillClusterRanges(): numClusterPairsPerThread({}) is less than one, this is not "
1089 "supported "
1090 "and will lead to errors!",
1091 numClusterPairsPerThread);
1092 }
1093 _clusterThreadPartition.resize(numThreads);
1094
1095 size_t currentThread = 0;
1096 size_t currentNumClustersToAdd = 0;
1097 size_t numClusterPairsTotal = 0;
1098 bool threadIsInitialized = false;
1099 // Iterate over the clusters of all towers
1100 for (size_t currentTowerIndex = 0; currentTowerIndex < _towerBlock.size(); currentTowerIndex++) {
1101 auto &currentTower = _towerBlock[currentTowerIndex];
1102 const auto firstOwnedClusterIndex =
1103 std::distance(currentTower.getClusters().begin(), currentTower.getFirstOwnedCluster());
1104 const auto firstTailHaloClusterIndex =
1105 std::distance(currentTower.getClusters().begin(), currentTower.getFirstTailHaloCluster());
1106 for (size_t currentClusterInTower = firstOwnedClusterIndex; currentClusterInTower < firstTailHaloClusterIndex;
1107 ++currentClusterInTower) {
1108 auto &currentCluster = currentTower.getCluster(currentClusterInTower);
1109
1110 // If on a new thread, start with the clusters for this thread here.
1111 if (not threadIsInitialized) {
1112 _clusterThreadPartition[currentThread] = {currentTowerIndex, currentClusterInTower, 0};
1113 threadIsInitialized = true;
1114 }
1115
1116 currentNumClustersToAdd++;
1117 numClusterPairsTotal += currentCluster.getNeighbors()->size();
1118
1119 // If the thread is finished, write number of clusters and start new thread.
1120 if (numClusterPairsTotal >= numClusterPairsPerThread * (currentThread + 1)) {
1121 // Add the number of clusters for the finished thread.
1122 _clusterThreadPartition[currentThread].numClusters += currentNumClustersToAdd;
1123 currentNumClustersToAdd = 0;
1124 // Go to next thread!
1125 currentThread++;
1126 // if we are already at the end of all threads, go back to last thread!
1127 // this is a safety precaution and should not really matter.
1128 if (currentThread >= numThreads) {
1129 --currentThread;
1130 threadIsInitialized = true;
1131 } else {
1132 threadIsInitialized = false;
1133 }
1134 }
1135 }
1136 }
1137 if (not threadIsInitialized) {
1138 _clusterThreadPartition[currentThread] = {0, 0, 0};
1139 }
1140 // Make sure the last cluster range contains the rest of the clusters, even if there is not the perfect number left.
1141 if (currentNumClustersToAdd != 0) {
1142 _clusterThreadPartition[currentThread].numClusters += currentNumClustersToAdd;
1143 }
1144 // Theoretically, some threads may still remain. This ensures that their numClusters are set to 0.
1145 while (++currentThread < numThreads) {
1146 _clusterThreadPartition[currentThread] = {0, 0, 0};
1147 }
1148 }
1149
1155 void notifyParticleDeleted() override {
1156 // this is potentially called from a threaded environment, so we have to make this atomic here!
1157 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
1158 }
1159
1160 private:
1175 template <bool regionIter>
1176 [[nodiscard]] std::tuple<size_t, size_t> advanceIteratorIndices(
1177 size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior, const std::array<double, 3> &boxMin,
1178 const std::array<double, 3> &boxMax, const std::array<double, 3> &boxMinWithSafetyMargin,
1179 const std::array<double, 3> &boxMaxWithSafetyMargin, size_t endCellIndex) const {
1180 // Finding the indices for the next particle
1181 const size_t stride = (iteratorBehavior & IteratorBehavior::forceSequential) ? 1 : autopas_get_num_threads();
1182
1183 // helper function to determine if the cell can even contain particles of interest to the iterator
1184 auto towerIsRelevant = [&]() -> bool {
1185 // special case: Towers are not yet built, then the tower acting as buffer is always relevant.
1186 if (_towerBlock.size() == 1) {
1187 return true;
1188 }
1189 bool isRelevant = true;
1190 if constexpr (regionIter) {
1191 // is the cell in the region?
1192 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(cellIndex);
1193 isRelevant =
1194 utils::boxesOverlap(towerLowCorner, towerHighCorner, boxMinWithSafetyMargin, boxMaxWithSafetyMargin);
1195 }
1196 return isRelevant;
1197 };
1198
1199 do {
1200 // advance to the next particle
1201 ++particleIndex;
1202 // If this breaches the end of a cell, find the next non-empty cell and reset particleIndex.
1203
1204 // If cell has wrong type, or there are no more particles in this cell jump to the next
1205 while (not towerIsRelevant() or particleIndex >= _towerBlock[cellIndex].getNumActualParticles()) {
1206 cellIndex += stride;
1207 particleIndex = 0;
1208
1209 // If we notice that there is nothing else to look at set invalid values, so we get a nullptr next time and
1210 // break.
1211 if (cellIndex > endCellIndex) {
1212 return {std::numeric_limits<size_t>::max(), particleIndex};
1213 }
1214 }
1215 } while (not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
1216 _towerBlock[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax));
1217
1218 // the indices returned at this point should always be valid
1219 return {cellIndex, particleIndex};
1220 }
1221
1223
1227 autopas::LoadEstimatorOption _loadEstimator;
1228
1232 size_t _clusterSize;
1233
1237 size_t _numClusters{0};
1238
1244 mutable std::vector<std::vector<Particle_T>> _particlesToAdd;
1245
1251 [[nodiscard]] bool particlesToAddEmpty(int bufferID = -1) const {
1252 if (bufferID == -1) {
1253 for (auto &threadBuffer : _particlesToAdd) {
1254 if (not threadBuffer.empty()) {
1255 return false;
1256 }
1257 }
1258 return true;
1259 } else {
1260 return _particlesToAdd[bufferID].empty();
1261 }
1262 }
1263
1272 template <class VecVec>
1273 void appendBuffersHelper(VecVec *additionalVectors, VecVec &outVec) const {
1274 if (additionalVectors) {
1275 outVec.reserve(_particlesToAdd.size() + additionalVectors->size());
1276 outVec.insert(outVec.end(), additionalVectors->begin(), additionalVectors->end());
1277 } else {
1278 outVec.reserve(_particlesToAdd.size());
1279 }
1280 for (auto &vec : _particlesToAdd) {
1281 outVec.push_back(&vec);
1282 }
1283 }
1284
1288 std::vector<ClusterRange> _clusterThreadPartition;
1289
1293 double _cutoff{};
1294
1298 enum class ValidityState : unsigned char {
1299 invalid = 0, // nothing is valid.
1300 cellsValidListsInvalid = 1, // only the cell structure is valid, but the lists are not.
1301 cellsAndListsValid = 2 // the cells and lists are valid
1302 };
1303
1307 std::atomic<ValidityState> _isValid{ValidityState::invalid};
1308
1312 std::unique_ptr<internal::VerletClusterListsRebuilder<Particle_T>> _builder;
1313
1318};
1319
1320} // namespace autopas
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
Base class for traversals utilising load balancing.
Definition: BalancedTraversal.h:19
std::function< unsigned long(const std::array< unsigned long, 3 > &, const std::array< unsigned long, 3 > &, const std::array< unsigned long, 3 > &)> EstimatorFunction
Type signature for load estimators.
Definition: BalancedTraversal.h:26
Public iterator class that iterates over a particle container and additional vectors (which are typic...
Definition: ContainerIterator.h:93
std::conditional_t< modifiable, std::vector< std::vector< Particle_T > * >, std::vector< std::vector< Particle_T > const * > > ParticleVecType
Type of the additional vector collection.
Definition: ContainerIterator.h:106
Functor base class.
Definition: Functor.h:40
Class representing the load estimator choices.
Definition: LoadEstimatorOption.h:18
Value
Possible choices for the load estimation algorithm.
Definition: LoadEstimatorOption.h:23
@ neighborListLength
Sum of neighbor list lengths.
Definition: LoadEstimatorOption.h:35
@ none
No load estimator.
Definition: LoadEstimatorOption.h:27
Class for manual memory management of neighbor lists.
Definition: NeighborListsBuffer.h:31
The ParticleContainerInterface class provides a basic interface for all Containers within AutoPas.
Definition: ParticleContainerInterface.h:37
constexpr bool end() const
Dummy to make range-based for loops work.
Definition: ParticleContainerInterface.h:240
This interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
virtual void endTraversal()=0
Finalizes the traversal.
virtual TraversalOption getTraversalType() const =0
Return a enum representing the name of the traversal class.
virtual void traverseParticles()=0
Traverse the particles by pairs, triplets etc.
virtual void initTraversal()=0
Initializes the traversal.
bool getUseNewton3() const
Return whether the traversal uses newton 3.
Definition: TraversalInterface.h:63
Info for traversals of a specific container.
Definition: TraversalSelectorInfo.h:14
Interface for traversals of the VerletClusterLists container.
Definition: VCLTraversalInterface.h:20
virtual void setClusterLists(VerletClusterLists< Particle_T > &verletClusterLists)
Sets the cluster list for the traversal to iterate over.
Definition: VCLTraversalInterface.h:31
Particles are divided into clusters.
Definition: VerletClusterLists.h:56
TraversalSelectorInfo getTraversalSelectorInfo() const override
Generates a traversal selector info for this container.
Definition: VerletClusterLists.h:399
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo)
Reduce properties of particles as defined by a lambda function.
Definition: VerletClusterLists.h:520
ContainerOption getContainerType() const override
Get the ContainerType.
Definition: VerletClusterLists.h:109
void traverseClusters(LoopBody &&loopBody)
Helper method to iterate over all clusters.
Definition: VerletClusterLists.h:807
void deleteHaloParticles() override
Deletes all halo particles.
Definition: VerletClusterLists.h:214
void forEach(Lambda forEachLambda, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo) const
Execute code on all particles in this container as defined by a lambda function.
Definition: VerletClusterLists.h:490
size_t size() const override
Get the number of all particles stored in this container (owned + halo + dummy).
Definition: VerletClusterLists.h:819
auto getClusterSize() const
Returns the number of particles in each cluster.
Definition: VerletClusterLists.h:877
void rebuildNeighborLists(TraversalInterface *traversal) override
Rebuilds the neighbor lists for the next traversals.
Definition: VerletClusterLists.h:777
BalancedTraversal::EstimatorFunction getLoadEstimatorFunction()
Generates the load estimation function depending on _loadEstimator.
Definition: VerletClusterLists.h:115
void setSkin(double skin)
Set the verlet skin length for the container.
Definition: VerletClusterLists.h:960
void addHaloParticleImpl(const Particle_T &haloParticle) override
Adds a particle to the container that lies in the halo region of the container.
Definition: VerletClusterLists.h:189
auto getTowersPerDimension() const
Returns the number of grids per dimension on the container.
Definition: VerletClusterLists.h:871
void forEach(Lambda forEachLambda, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo)
Execute code on all particles in this container as defined by a lambda function.
Definition: VerletClusterLists.h:471
const std::array< double, 3 > & getHaloBoxMax() const
Get the upper corner of the halo box.
Definition: VerletClusterLists.h:940
std::tuple< const Particle_T *, size_t, size_t > getParticleImpl(size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax) const
Container specific implementation for getParticle.
Definition: VerletClusterLists.h:286
void addParticleImpl(const Particle_T &p) override
Adds the given particle to the container.
Definition: VerletClusterLists.h:184
void reduceInRegion(Lambda reduceLambda, A &result, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo)
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: VerletClusterLists.h:705
ContainerIterator< Particle_T, true, false > begin(IteratorBehavior behavior=IteratorBehavior::ownedOrHalo, typename ContainerIterator< Particle_T, true, false >::ParticleVecType *additionalVectors=nullptr) override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: VerletClusterLists.h:414
void traverseClustersSequential(LoopBody &&loopBody)
Helper method to sequentially iterate over all owned clusters.
Definition: VerletClusterLists.h:1014
ContainerIterator< Particle_T, false, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior, typename ContainerIterator< Particle_T, false, true >::ParticleVecType *additionalVectors=nullptr) const override
Iterate over all particles in a specified region for(auto iter = container.getRegionIterator(lowCorne...
Definition: VerletClusterLists.h:597
void computeInteractions(TraversalInterface *traversal) override
Iterates over all particle multiples (e.g.
Definition: VerletClusterLists.h:148
double getInteractionLength() const override
Return the interaction length (cutoff+skin) of the container.
Definition: VerletClusterLists.h:962
bool deleteParticle(size_t cellIndex, size_t particleIndex) override
Deletes the particle at the given index positions as long as this does not compromise the validity of...
Definition: VerletClusterLists.h:356
double getVerletSkin() const override
Return the verletSkin of the container verletSkin.
Definition: VerletClusterLists.h:954
void loadParticlesIntoSoAs(Functor *functor)
Loads all particles of the container in their correct SoA and generates the SoAViews for the clusters...
Definition: VerletClusterLists.h:891
std::tuple< const Particle_T *, size_t, size_t > getParticle(size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax) const override
Fetch the pointer to a particle, identified via a cell and particle index.
Definition: VerletClusterLists.h:257
Particle_T ParticleType
Type of the Particle.
Definition: VerletClusterLists.h:61
internal::ClusterTowerBlock2D< Particle_T > & getTowerBlock()
Getter for the cell block.
Definition: VerletClusterLists.h:932
auto getNumTowersPerInteractionLength() const
Returns the towers per interaction length.
Definition: VerletClusterLists.h:883
const std::array< double, 3 > & getBoxMin() const override
Get the lower corner of the container without halo.
Definition: VerletClusterLists.h:942
void deleteAllParticles() override
Deletes all particles.
Definition: VerletClusterLists.h:964
void fillClusterRanges(size_t numClusterPairsPerThread, int numThreads)
Fills in the cluster ranges of the cluster thread partition.
Definition: VerletClusterLists.h:1085
ContainerIterator< Particle_T, true, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior, typename ContainerIterator< Particle_T, true, true >::ParticleVecType *additionalVectors=nullptr) override
Iterate over all particles in a specified region for(auto iter = container.getRegionIterator(lowCorne...
Definition: VerletClusterLists.h:568
VerletClusterLists(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, double cutoff, double skin, size_t clusterSize, LoadEstimatorOption loadEstimator=LoadEstimatorOption::none)
Constructor of the VerletClusterLists class.
Definition: VerletClusterLists.h:97
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo) const
Reduce properties of particles as defined by a lambda function.
Definition: VerletClusterLists.h:539
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: VerletClusterLists.h:830
ContainerIterator< Particle_T, false, false > begin(IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo, typename ContainerIterator< Particle_T, false, false >::ParticleVecType *additionalVectors=nullptr) const override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: VerletClusterLists.h:442
void forEachInRegion(Lambda forEachLambda, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo) const
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: VerletClusterLists.h:661
void traverseClustersParallel(LoopBody &&loopBody)
Helper method to iterate over all clusters in parallel.
Definition: VerletClusterLists.h:1036
const std::array< double, 3 > & getHaloBoxMin() const
Get the lower corner of the halo box.
Definition: VerletClusterLists.h:948
const internal::VerletClusterListsRebuilder< Particle_T >::NeighborListsBuffer_T & getNeighborLists() const
Get the neighbor lists buffer object.
Definition: VerletClusterLists.h:974
double getCutoff() const override
Return the cutoff of the container.
Definition: VerletClusterLists.h:950
internal::ClusterTower< Particle_T > & getTowerByIndex(size_t x, size_t y)
Returns a reference to the tower for the given tower grid coordinates.
Definition: VerletClusterLists.h:921
auto getTowerSideLength() const
Returns the grid side length of the grids in the container.
Definition: VerletClusterLists.h:865
void reduceInRegion(Lambda reduceLambda, A &result, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo) const
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: VerletClusterLists.h:738
const std::array< double, 3 > & getBoxMax() const override
Get the upper corner of the container without halo.
Definition: VerletClusterLists.h:934
void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override
Reserve memory for a given number of particles in the container and logic layers.
Definition: VerletClusterLists.h:172
void setCutoff(double cutoff) override
Set the cutoff of the container.
Definition: VerletClusterLists.h:952
bool deleteParticle(Particle_T &particle) override
Deletes the given particle as long as this does not compromise the validity of the container.
Definition: VerletClusterLists.h:350
std::tuple< const Particle_T *, size_t, size_t > getParticle(size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior) const override
Fetch the pointer to a particle, identified via a cell and particle index.
Definition: VerletClusterLists.h:263
auto getNumClusters() const
Returns the number of clusters in this container.
Definition: VerletClusterLists.h:859
std::vector< Particle_T > updateContainer(bool keepNeighborListsValid) override
Updates the container.
Definition: VerletClusterLists.h:362
void rebuildTowersAndClusters(bool newton3)
Initializes a new VerletClusterListsRebuilder and uses it to rebuild the towers and the clusters.
Definition: VerletClusterLists.h:983
void calculateClusterThreadPartition()
Calculates a cluster thread partition that aims to give each thread about the same amount of cluster ...
Definition: VerletClusterLists.h:1058
void extractParticlesFromSoAs(Functor *functor)
Extracts all SoAs of the container into the particles.
Definition: VerletClusterLists.h:906
void forEachInRegion(Lambda forEachLambda, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo)
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: VerletClusterLists.h:628
bool updateHaloParticle(const Particle_T &haloParticle) override
Update a halo particle of the container with the given haloParticle.
Definition: VerletClusterLists.h:194
void notifyParticleDeleted() override
If a particle is deleted, we want _isValid to be set to invalid, as the tower structure is invalidate...
Definition: VerletClusterLists.h:1155
const auto & getClusterThreadPartition() const
Returns the cluster-thread-partition.
Definition: VerletClusterLists.h:853
Class to manage the grid of towers for the Verlet Cluster Lists container.
Definition: ClusterTowerBlock2D.h:25
This class represents one tower for clusters in the VerletClusterLists container.
Definition: ClusterTower.h:43
Class that is notified when a particle is deleted.
Definition: ParticleDeletedObserver.h:15
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
std::vector< typename ContainerType::ParticleType > collectParticlesAndMarkNonOwnedAsDummy(ContainerType &container)
Collects leaving particles and marks halo particles as dummy.
Definition: LeavingParticleCollector.h:85
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
void deleteParticle(ParticleIterator &iterator)
Function to access private iterator.deleteCurrentParticle() via friend.
Definition: ContainerIterator.h:35
constexpr std::array< T, SIZE > subScalar(const std::array< T, SIZE > &a, T s)
Subtracts a scalar s from each element of array a and returns the result.
Definition: ArrayMath.h:164
constexpr std::array< T, SIZE > addScalar(const std::array< T, SIZE > &a, T s)
Adds a scalar s to each element of array a and returns the result.
Definition: ArrayMath.h:147
bool boxesOverlap(const std::array< T, 3 > &boxALow, const std::array< T, 3 > &boxAHigh, const std::array< T, 3 > &boxBLow, const std::array< T, 3 > &boxBHigh)
Checks if two boxes have overlap.
Definition: inBox.h:67
bool inBox(const std::array< T, 3 > &position, const std::array< T, 3 > &low, const std::array< T, 3 > &high)
Checks if position is inside of a box defined by low and high.
Definition: inBox.h:26
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
int autopas_get_num_threads()
Dummy for omp_get_num_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:138
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132
Defines a cluster range used in the static cluster-thread-partition.
Definition: VerletClusterLists.h:70
size_t startIndexInTower
The index of the first cluster in its tower.
Definition: VerletClusterLists.h:78
size_t startTowerIndex
The index of the tower that contains the first cluster.
Definition: VerletClusterLists.h:74
size_t numClusters
The number of clusters in the range.
Definition: VerletClusterLists.h:82