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"
30#include "autopas/utils/Timer.h"
32#include "autopas/utils/inBox.h"
34
35namespace autopas {
36
56template <class Particle_T>
58 public:
62 using ParticleType = Particle_T;
67
71 struct ClusterRange {
83 size_t numClusters{};
84 };
85
98 VerletClusterLists(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax, double cutoff,
99 double skin, size_t clusterSize, LoadEstimatorOption loadEstimator = LoadEstimatorOption::none)
100 : ParticleContainerInterface<Particle_T>(skin),
101 _towerBlock{boxMin, boxMax, cutoff + skin},
102 _clusterSize{clusterSize},
103 _particlesToAdd(autopas_get_max_threads()),
104 _cutoff{cutoff},
105 _loadEstimator(loadEstimator) {
106 // always have at least one tower.
107 _towerBlock.addTower(_clusterSize);
108 }
109
110 [[nodiscard]] ContainerOption getContainerType() const override { return ContainerOption::verletClusterLists; }
111
117 // (Explicit) static cast required for Apple Clang (last tested version: 17.0.0)
118 switch (static_cast<LoadEstimatorOption::Value>(this->_loadEstimator)) {
120 return [&](const std::array<unsigned long, 3> &cellsPerDimension,
121 const std::array<unsigned long, 3> &lowerCorner, const std::array<unsigned long, 3> &upperCorner) {
122 // the neighborListLength function defined for verletListsCells in not compatible with this container.
123 unsigned long sum = 0;
124 for (unsigned long x = lowerCorner[0]; x <= upperCorner[0]; x++) {
125 for (unsigned long y = lowerCorner[1]; y <= upperCorner[1]; y++) {
126 unsigned long cellLoad = 0;
127 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
128 for (auto &cluster : tower.getClusters()) {
129 if (cluster.getNeighbors()) {
130 cellLoad += cluster.getNeighbors()->size();
131 }
132 }
133 sum += cellLoad;
134 }
135 }
136 return sum;
137 };
138 }
140 [[fallthrough]];
141 default: {
142 return
143 [&](const std::array<unsigned long, 3> &cellsPerDimension, const std::array<unsigned long, 3> &lowerCorner,
144 const std::array<unsigned long, 3> &upperCorner) { return 1; };
145 }
146 }
147 }
148
149 void computeInteractions(TraversalInterface *traversal) override {
150 if (_isValid == ValidityState::cellsAndListsValid) {
152 "VerletClusterLists::computeInteractions(): Trying to do a pairwise iteration, even though verlet lists are "
153 "not valid.");
154 }
155 auto *traversalInterface = dynamic_cast<VCLTraversalInterface<Particle_T> *>(traversal);
156 if (traversalInterface) {
157 traversalInterface->setClusterLists(*this);
158 traversalInterface->setTowers(_towerBlock.getTowersRef());
159 } else {
161 "Trying to use a traversal of wrong type in VerletClusterLists::computeInteractions. TraversalID: {}",
162 traversal->getTraversalType());
163 }
164 if (auto *balancedTraversal = dynamic_cast<BalancedTraversal *>(traversal)) {
165 balancedTraversal->setLoadEstimator(getLoadEstimatorFunction());
166 }
167
168 traversal->initTraversal();
169 traversal->traverseParticles();
170 traversal->endTraversal();
171 }
172
177 void setAoSSortingThresholds(std::shared_ptr<const SortingThresholdInfoInterface> aosSortingThresholds) override {}
178
183 void setSoASortingThresholds(std::shared_ptr<const SortingThresholdInfoInterface> soaSortingThresholds) override {}
184
185 void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override {
186 const auto particlesPerTower = (numParticles + numParticlesHaloEstimate) / _towerBlock.size();
187 for (auto &tower : _towerBlock) {
188 tower.reserve(particlesPerTower);
189 }
190 }
191
197 void addParticleImpl(const Particle_T &p) override {
198 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
199 _particlesToAdd[autopas_get_thread_num()].push_back(p);
200 }
201
202 void addHaloParticleImpl(const Particle_T &haloParticle) override {
203 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
204 _particlesToAdd[autopas_get_thread_num()].push_back(haloParticle);
205 }
206
216 bool updateHaloParticle(const Particle_T &haloParticle) override {
217 using namespace autopas::utils::ArrayMath::literals;
218
219 const auto &haloPos = haloParticle.getR();
220 const auto skinHalf = 0.5 * this->getVerletSkin();
221 // Towers span the whole z range, so only the x-y footprint of the search box matters.
222 const auto [lowX, lowY] = _towerBlock.getTowerIndex2DAtPosition(haloPos - skinHalf);
223 const auto [highX, highY] = _towerBlock.getTowerIndex2DAtPosition(haloPos + skinHalf);
224 for (size_t y = lowY; y <= highY; ++y) {
225 for (size_t x = lowX; x <= highX; ++x) {
226 // It's possible that a tower includes the owned version and a halo version of a particle. But they will be far
227 // enough apart that the position check will ensure no incorrect updates.
228 if (internal::checkParticleInCellAndUpdateByIDAndPosition(_towerBlock.getTowerByIndex2D(x, y), haloParticle,
229 skinHalf)) {
230 return true;
231 }
232 }
233 }
234 AutoPasLog(TRACE, "UpdateHaloParticle was not able to update particle: {}", haloParticle.toString());
235 return false;
236 }
237
238 void deleteHaloParticles() override {
239 // Step 1: Remove particles from _particlesToAdd
240 for (auto &particleVec : _particlesToAdd) {
241 for (size_t j = 0; j < particleVec.size();) {
242 if (particleVec[j].isHalo()) {
243 particleVec[j] = particleVec[particleVec.size() - 1];
244 particleVec.pop_back();
245 } else {
246 ++j;
247 }
248 }
249 }
250 // Step 2: Remove particles from _towers
251 bool deletedSomething = false;
252 AUTOPAS_OPENMP(parallel for reduction(|| : deletedSomething))
253 // Thanks to clang 13 this has to be a index based loop instead of a range based
254 for (size_t i = 0; i < _towerBlock.size(); ++i) {
255 auto &tower = _towerBlock[i];
256 const auto towerSize = tower.size();
257 auto numTailDummies = tower.getNumTailDummyParticles();
258 // iterate over all non-tail dummies. Avoid underflows.
259 for (size_t j = 0; numTailDummies < towerSize and j < towerSize - numTailDummies;) {
260 if (tower[j].isHalo()) {
261 // swap-"delete"
262 tower[j] = tower[towerSize - 1 - numTailDummies];
263 // Since we can't pop the moved particle here mark it for deletion.
264 internal::markParticleAsDeleted(tower[towerSize - 1 - numTailDummies]);
265 ++numTailDummies;
266 deletedSomething = true;
267 } else {
268 ++j;
269 }
270 }
271 // if anything was marked for deletion actually delete it now.
272 if (deletedSomething) {
273 tower.deleteDummyParticles();
274 }
275 }
276 if (deletedSomething) {
277 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
278 }
279 }
280
281 std::tuple<const Particle_T *, size_t, size_t> getParticle(size_t cellIndex, size_t particleIndex,
282 IteratorBehavior iteratorBehavior,
283 const std::array<double, 3> &boxMin,
284 const std::array<double, 3> &boxMax) const override {
285 return getParticleImpl<true>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
286 }
287 std::tuple<const Particle_T *, size_t, size_t> getParticle(size_t cellIndex, size_t particleIndex,
288 IteratorBehavior iteratorBehavior) const override {
289 // this is not a region iter hence we stretch the bounding box to the numeric max
290 constexpr std::array<double, 3> boxMin{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(),
291 std::numeric_limits<double>::lowest()};
292
293 constexpr std::array<double, 3> boxMax{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
294 std::numeric_limits<double>::max()};
295 return getParticleImpl<false>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
296 }
297
309 template <bool regionIter>
310 std::tuple<const Particle_T *, size_t, size_t> getParticleImpl(size_t cellIndex, size_t particleIndex,
311 IteratorBehavior iteratorBehavior,
312 const std::array<double, 3> &boxMin,
313 const std::array<double, 3> &boxMax) const {
314 using namespace autopas::utils::ArrayMath::literals;
315
316 // in this context cell == tower
317 // catching the edge case that towers are not yet built -> Iterator jumps to additional vectors
318 if (_towerBlock.empty()) {
319 return {nullptr, 0, 0};
320 }
321
322 std::array<double, 3> boxMinWithSafetyMargin = boxMin;
323 std::array<double, 3> boxMaxWithSafetyMargin = boxMax;
324 if constexpr (regionIter) {
325 // We extend the search box for cells here since particles might have moved
326 boxMinWithSafetyMargin -= 0.5 * this->getVerletSkin();
327 boxMaxWithSafetyMargin += 0.5 * this->getVerletSkin();
328 }
329
330 // first and last relevant cell index
331 const auto [startCellIndex, endCellIndex] = [&]() -> std::tuple<size_t, size_t> {
332 if constexpr (regionIter) {
333 // We extend the search box for cells here since particles might have moved
334 return {_towerBlock.getTowerIndex1DAtPosition(boxMinWithSafetyMargin),
335 _towerBlock.getTowerIndex1DAtPosition(boxMaxWithSafetyMargin)};
336 } else {
337 if (not(iteratorBehavior & IteratorBehavior::halo)) {
338 // only potentially owned region
339 return {_towerBlock.getFirstOwnedTowerIndex(), _towerBlock.getLastOwnedTowerIndex()};
340 } else {
341 // whole range of cells
342 return {0, _towerBlock.size() - 1};
343 }
344 }
345 }();
346
347 // if we are at the start of an iteration ...
348 if (cellIndex == 0 and particleIndex == 0) {
349 cellIndex =
350 startCellIndex + ((iteratorBehavior & IteratorBehavior::forceSequential) ? 0 : autopas_get_thread_num());
351 }
352 // abort if the start index is already out of bounds
353 if (cellIndex >= _towerBlock.size()) {
354 return {nullptr, 0, 0};
355 }
356 // check the data behind the indices
357 if (particleIndex >= _towerBlock[cellIndex].getNumActualParticles() or
358 not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
359 _towerBlock[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax)) {
360 // either advance them to something interesting or invalidate them.
361 std::tie(cellIndex, particleIndex) =
362 advanceIteratorIndices<regionIter>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax,
363 boxMinWithSafetyMargin, boxMaxWithSafetyMargin, endCellIndex);
364 }
365 // shortcut if the given index doesn't exist
366 if (cellIndex > endCellIndex) {
367 return {nullptr, 0, 0};
368 }
369 const Particle_T *retPtr = &_towerBlock[cellIndex][particleIndex];
370
371 return {retPtr, cellIndex, particleIndex};
372 }
373
374 void deleteParticle(Particle_T &particle) override {
375 // This function doesn't actually delete anything as it would mess up the clusters' references.
377 }
378
379 void deleteParticle(size_t cellIndex, size_t particleIndex) override {
380 // This function doesn't actually delete anything as it would mess up the clusters' references.
381 internal::markParticleAsDeleted(_towerBlock[cellIndex][particleIndex]);
382 }
383
384 [[nodiscard]] std::vector<Particle_T> updateContainer(bool keepNeighborListsValid) override {
385 if (keepNeighborListsValid) {
387 }
388 // First delete all halo particles.
389 this->deleteHaloParticles();
390 // Delete dummy particles.
391 AUTOPAS_OPENMP(parallel for)
392 for (size_t i = 0ul; i < _towerBlock.size(); ++i) {
393 _towerBlock[i].deleteDummyParticles();
394 }
395 // Delete dummy particles also from the _particlesToAdd vector
396 AUTOPAS_OPENMP(parallel for)
397 for (size_t i = 0ul; i < _particlesToAdd.size(); ++i) {
398 _particlesToAdd[i].erase(std::remove_if(_particlesToAdd[i].begin(), _particlesToAdd[i].end(),
399 [](const auto &p) { return p.isDummy(); }),
400 _particlesToAdd[i].end());
401 }
402
403 // next find invalid particles
404 std::vector<Particle_T> invalidParticles;
405
406 // custom openmp reduction to concatenate all local vectors to one at the end of a parallel region
407 AUTOPAS_OPENMP(declare reduction(
408 vecMergeParticle : std::vector<Particle_T> : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end())))
409 AUTOPAS_OPENMP(parallel reduction(vecMergeParticle : invalidParticles)) {
410 for (auto iter = this->begin(IteratorBehavior::owned); iter.isValid(); ++iter) {
411 if (not utils::inBox(iter->getR(), this->getBoxMin(), this->getBoxMax())) {
412 invalidParticles.push_back(*iter);
414 }
415 }
416 }
417 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
418 return invalidParticles;
419 }
420
421 [[nodiscard]] TraversalSelectorInfo getTraversalSelectorInfo() const override {
422 using namespace autopas::utils::ArrayMath::literals;
423 // Here, the towers might not yet be built, hence do not use members like _towerBlock.getTowersPerDim
424 const auto boxSizeWithHalo = this->getHaloBoxMax() - this->getHaloBoxMin();
425 const auto [towerSideLength, towersPerDim] = _towerBlock.estimateOptimalGridSideLength(
426 this->getNumberOfParticles(IteratorBehavior::ownedOrHalo), _clusterSize);
427 const std::array<double, 3> towerSize = {towerSideLength[0], towerSideLength[1],
428 this->getHaloBoxMax()[2] - this->getHaloBoxMin()[2]};
429 const std::array<unsigned long, 3> towerDimensions = {towersPerDim[0], towersPerDim[1], 1};
430 return TraversalSelectorInfo(towerDimensions, this->getInteractionLength(), towerSize, _clusterSize);
431 }
432
437 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
439 std::nullopt) override {
440 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
441 // invalid, we do writing operations on _particlesToAdd and can not read from it without race conditions.
442 if (_isValid != ValidityState::invalid) {
443 // we call particlesToAddEmpty() as a sanity check to ensure there are actually no particles in _particlesToAdd if
444 // the status is not invalid
445 if (not particlesToAddEmpty(autopas_get_thread_num())) {
447 "VerletClusterLists::begin(): Error: particle container is valid, but _particlesToAdd isn't empty!");
448 }
449 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
450 // from LogicHandler.
451 return ContainerIterator<Particle_T, true, false>(*this, behavior, additionalVectors);
452 } else {
453 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
454 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
456 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
457 return ContainerIterator<Particle_T, true, false>(*this, behavior, std::ref(additionalVectorsToPass));
458 }
459 }
460
466 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo,
468 std::nullopt) const override {
469 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
470 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
471 if (_isValid != ValidityState::invalid) {
472 // we call particlesToAddEmpty() as a sanity check to ensire there are actually no particles in _particlesToAdd if
473 // the status is not invalid
474 if (not particlesToAddEmpty(autopas_get_thread_num())) {
476 "VerletClusterLists::begin() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
477 }
478 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
479 // from LogicHandler.
480 return ContainerIterator<Particle_T, false, false>(*this, behavior, additionalVectors);
481 } else {
482 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
483 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
485 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
486 return ContainerIterator<Particle_T, false, false>(*this, behavior, std::ref(additionalVectorsToPass));
487 }
488 }
489
493 template <typename Lambda>
494 void forEach(Lambda forEachLambda, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
495 for (auto &tower : _towerBlock) {
496 tower.forEach(forEachLambda, behavior);
497 }
498 for (auto &vector : this->_particlesToAdd) {
499 for (auto &particle : vector) {
500 if (behavior.contains(particle)) {
501 forEachLambda(particle);
502 }
503 }
504 }
505 }
506
512 template <typename Lambda>
513 void forEach(Lambda forEachLambda, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
514 if (_isValid != ValidityState::invalid) {
515 if (not particlesToAddEmpty()) {
517 "VerletClusterLists::forEach() const: Error: particle container is valid, but _particlesToAdd isn't "
518 "empty!");
519 }
520 }
521
522 // If the particles are sorted into the towers, we can simply use the iteration over towers.
523 for (auto &tower : _towerBlock) {
524 tower.forEach(forEachLambda, behavior);
525 }
526
527 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
528 if (_isValid == ValidityState::invalid) {
529 for (auto &particlesToAddPerThread : _particlesToAdd) {
530 for (auto &particle : particlesToAddPerThread) {
531 if (behavior.contains(particle)) {
532 forEachLambda(particle);
533 }
534 }
535 }
536 }
537 }
538
542 template <typename Lambda, typename A>
543 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
544 for (auto &tower : _towerBlock) {
545 tower.reduce(reduceLambda, result, behavior);
546 }
547 for (auto &vector : this->_particlesToAdd) {
548 for (auto &p : vector) {
549 if (behavior.contains(p)) {
550 reduceLambda(p, result);
551 }
552 }
553 }
554 }
555
561 template <typename Lambda, typename A>
562 void reduce(Lambda reduceLambda, A &result,
563 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
564 if (_isValid != ValidityState::invalid) {
565 if (not particlesToAddEmpty()) {
567 "VerletClusterLists::reduce() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
568 }
569 }
570
571 // If the particles are sorted into the towers, we can simply use the iteration over towers.
572 for (auto tower : _towerBlock) {
573 tower.reduce(reduceLambda, result, behavior);
574 }
575
576 if (_isValid == ValidityState::invalid) {
577 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
578 for (auto &particlesToAddPerThread : _particlesToAdd) {
579 for (auto &particle : particlesToAddPerThread) {
580 if (behavior.contains(particle)) {
581 reduceLambda(particle, result);
582 }
583 }
584 }
585 }
586 }
587
592 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
594 std::nullopt) override {
595 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
596 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
597 if (_isValid != ValidityState::invalid) {
598 // we call particlesToAddEmpty() as a sanity check to ensure there are actually no particles in _particlesToAdd
599 // if the status is not invalid
600 if (not particlesToAddEmpty(autopas_get_thread_num())) {
602 "VerletClusterLists::reduce() const: Error: particle container is valid, but _particlesToAdd isn't empty!");
603 }
604 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
605 // from LogicHandler.
606 return ContainerIterator<Particle_T, true, true>(*this, behavior, additionalVectors, lowerCorner, higherCorner);
607 } else {
608 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
609 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
610 typename ContainerIterator<Particle_T, true, true>::ParticleVecType additionalVectorsToPass;
611 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
612 return ContainerIterator<Particle_T, true, true>(*this, behavior, std::ref(additionalVectorsToPass), lowerCorner,
613 higherCorner);
614 }
615 }
616
622 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
624 std::nullopt) const override {
625 // Note: particlesToAddEmpty() can only be called if the container status is not invalid. If the status is set to
626 // invalid, we do writing operations on _particlesToAdd and can not read from from it without race conditions.
627 if (_isValid != ValidityState::invalid) {
628 // we call particlesToAddEmpty() as a sanity check to ensire there are actually no particles in _particlesToAdd if
629 // the status is not invalid
630 if (not particlesToAddEmpty(autopas_get_thread_num())) {
632 "VerletClusterLists::getRegionIterator() const: Error: particle container is valid, but _particlesToAdd "
633 "isn't empty!");
634 }
635 // If the particles are sorted into the towers, we can simply use the iteration over towers + additionalVectors
636 // from LogicHandler.
637 return ContainerIterator<Particle_T, false, true>(*this, behavior, additionalVectors, lowerCorner, higherCorner);
638 } else {
639 // if the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
640 // store all pointers in a temporary which is passed to the ParticleIterator constructor.
642 appendBuffersHelper(additionalVectors, additionalVectorsToPass);
643 return ContainerIterator<Particle_T, false, true>(*this, behavior, std::ref(additionalVectorsToPass), lowerCorner,
644 higherCorner);
645 }
646 }
647
651 template <typename Lambda>
652 void forEachInRegion(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
653 const std::array<double, 3> &higherCorner,
654 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
655 for (size_t i = 0; i < _towerBlock.size(); ++i) {
656 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
657 continue;
658 }
659 auto &tower = _towerBlock[i];
660 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
661 // particles can move over cell borders. Calculate the volume this cell's particles can be.
662 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
663 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
664 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
665 tower.forEach(forEachLambda, lowerCorner, higherCorner, behavior);
666 }
667 }
668 for (auto &vector : _particlesToAdd) {
669 for (auto &particle : vector) {
670 if (behavior.contains(particle)) {
671 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
672 forEachLambda(particle);
673 }
674 }
675 }
676 }
677 }
678
684 template <typename Lambda>
685 void forEachInRegion(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
686 const std::array<double, 3> &higherCorner,
687 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
688 if (_isValid != ValidityState::invalid) {
689 if (not particlesToAddEmpty()) {
691 "VerletClusterLists::forEachInRegion() const: Error: particle container is valid, but _particlesToAdd "
692 "isn't empty!");
693 }
694 }
695
696 // If the particles are sorted into the towers, we can simply use the iteration over towers.
697 for (size_t i = 0; i < _towerBlock.size(); ++i) {
698 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
699 continue;
700 }
701 auto &tower = _towerBlock[i];
702 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
703 // particles can move over cell borders. Calculate the volume this cell's particles can be.
704 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
705 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
706 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
707 tower.forEach(forEachLambda, lowerCorner, higherCorner, behavior);
708 }
709 }
710
711 if (_isValid == ValidityState::invalid) {
712 // If the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
713 for (auto &particlesToAddPerThread : _particlesToAdd) {
714 for (auto &particle : particlesToAddPerThread) {
715 if (behavior.contains(particle)) {
716 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
717 forEachLambda(particle);
718 }
719 }
720 }
721 }
722 }
723 }
724
728 template <typename Lambda, typename A>
729 void reduceInRegion(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
730 const std::array<double, 3> &higherCorner,
731 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) {
732 for (size_t i = 0; i < _towerBlock.size(); ++i) {
733 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
734 continue;
735 }
736 auto &tower = _towerBlock[i];
737 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
738 // particles can move over cell borders. Calculate the volume this cell's particles can be.
739 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
740 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
741 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
742 tower.reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
743 }
744 }
745 for (auto &vector : _particlesToAdd) {
746 for (auto &particle : vector) {
747 if (behavior.contains(particle)) {
748 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
749 reduceLambda(particle, result);
750 }
751 }
752 }
753 }
754 }
755
761 template <typename Lambda, typename A>
762 void reduceInRegion(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
763 const std::array<double, 3> &higherCorner,
764 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHalo) const {
765 if (_isValid != ValidityState::invalid) {
766 if (not particlesToAddEmpty()) {
768 "VerletClusterLists::reduceInRegion() const: Error: particle container is valid, but _particlesToAdd isn't "
769 "empty!");
770 }
771 }
772 // If the particles are sorted into the towers, we can simply use the iteration over towers.
773 for (size_t i = 0; i < _towerBlock.size(); ++i) {
774 if (_towerBlock.ignoreCellForIteration(i, behavior)) {
775 continue;
776 }
777 auto &tower = _towerBlock[i];
778 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(i);
779 // particles can move over cell borders. Calculate the volume this cell's particles can be.
780 const auto towerLowCornerSkin = utils::ArrayMath::subScalar(towerLowCorner, this->getVerletSkin() * 0.5);
781 const auto towerHighCornerSkin = utils::ArrayMath::addScalar(towerHighCorner, this->getVerletSkin() * 0.5);
782 if (utils::boxesOverlap(towerLowCornerSkin, towerHighCornerSkin, lowerCorner, higherCorner)) {
783 tower.reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
784 }
785 }
786
787 if (_isValid == ValidityState::invalid) {
788 // If the particles are not sorted into the towers, we have to also iterate over _particlesToAdd.
789 for (auto &particlesToAddPerThread : _particlesToAdd) {
790 for (auto &particle : particlesToAddPerThread) {
791 if (behavior.contains(particle)) {
792 if (utils::inBox(particle.getR(), lowerCorner, higherCorner)) {
793 reduceLambda(particle, result);
794 }
795 }
796 }
797 }
798 }
799 }
800
801 void rebuildNeighborLists(TraversalInterface *traversal) override {
802 // The builder might have a different newton3 choice than the traversal. This typically only happens in unit tests
803 // when rebuildTowersAndClusters() was not called explicitly.
804 if (_isValid == ValidityState::invalid or traversal->getUseNewton3() != _builder->getNewton3()) {
805 // clear the lists buffer because clusters will be recreated
806 _neighborLists.clear();
808 }
809 _builder->rebuildNeighborListsAndFillClusters();
810
811 auto *clusterTraversalInterface = dynamic_cast<VCLTraversalInterface<Particle_T> *>(traversal);
812 if (clusterTraversalInterface) {
813 if (clusterTraversalInterface->needsStaticClusterThreadPartition()) {
815 }
816 } else {
818 "Trying to use a traversal of wrong type in VerletClusterLists::rebuildNeighborLists. TraversalID: {}",
819 traversal->getTraversalType());
820 }
821 }
822
830 template <bool inParallel, class LoopBody>
831 void traverseClusters(LoopBody &&loopBody) {
832 if (inParallel) {
833 traverseClustersParallel<LoopBody>(std::forward<LoopBody>(loopBody));
834 } else {
835 traverseClustersSequential<LoopBody>(std::forward<LoopBody>(loopBody));
836 }
837 }
838
843 [[nodiscard]] size_t size() const override {
844 size_t sum = std::accumulate(_towerBlock.begin(), _towerBlock.end(), 0,
845 [](size_t acc, const auto &tower) { return acc + tower.size(); });
846 sum = std::accumulate(_particlesToAdd.begin(), _particlesToAdd.end(), sum,
847 [](size_t acc, const auto &buffer) { return acc + buffer.size(); });
848 return sum;
849 }
850
854 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
855 // sum up all particles in towers that fulfill behavior
856 size_t sum = std::accumulate(_towerBlock.begin(), _towerBlock.end(), 0, [&behavior](size_t acc, const auto &tower) {
857 return acc + tower.getNumberOfParticles(behavior);
858 });
859
860 // Since we can not directly insert particles into towers without a rebuild of the whole data structure,
861 // _particlesToAdd is used to store all these particles temporarily until the next rebuild inserts them into the
862 // towers data structure. However, these particles already belong to the respective tower, so we have to count them
863 // as well.
864 sum = std::accumulate(
865 _particlesToAdd.begin(), _particlesToAdd.end(), sum, [&behavior](size_t acc, const auto &buffer) {
866 return acc +
867 (std::count_if(buffer.begin(), buffer.end(), [&behavior](auto p) { return behavior.contains(p); }));
868 });
869
870 return sum;
871 }
872
877 const auto &getClusterThreadPartition() const { return _clusterThreadPartition; }
878
883 auto getNumClusters() const { return _numClusters; }
884
889 auto getTowerSideLength() const { return _towerBlock.getTowerSideLength(); }
890
895 auto getTowersPerDimension() const { return _towerBlock.getTowersPerDim(); }
896
901 auto getClusterSize() const { return _clusterSize; }
902
908 auto getMaxNumTowersPerInteractionLength() const { return _towerBlock.getMaxNumTowersPerInteractionLength(); }
909
915 template <class Functor>
917 const auto numTowers = _towerBlock.size();
919 AUTOPAS_OPENMP(parallel for schedule(dynamic))
920 for (size_t index = 0; index < numTowers; index++) {
921 _towerBlock[index].loadSoA(functor);
922 }
923 }
924
930 template <class Functor>
932 const auto numTowers = _towerBlock.size();
934 AUTOPAS_OPENMP(parallel for schedule(dynamic))
935 for (size_t index = 0; index < numTowers; index++) {
936 _towerBlock[index].extractSoA(functor);
937 }
938 }
939
947 return _towerBlock.getTowerByIndex2D(x, y);
948 }
949
958
959 [[nodiscard]] const std::array<double, 3> &getBoxMax() const override { return _towerBlock.getBoxMax(); }
960
965 [[nodiscard]] const std::array<double, 3> &getHaloBoxMax() const { return _towerBlock.getHaloBoxMax(); }
966
967 [[nodiscard]] const std::array<double, 3> &getBoxMin() const override { return _towerBlock.getBoxMin(); }
968
973 [[nodiscard]] const std::array<double, 3> &getHaloBoxMin() const { return _towerBlock.getHaloBoxMin(); }
974
975 [[nodiscard]] double getCutoff() const override { return _cutoff; }
976
977 void setCutoff(double cutoff) override { _cutoff = cutoff; }
978
979 [[nodiscard]] double getVerletSkin() const override { return this->_skin; }
980
985 void setSkin(double skin) { this->_skin = skin; }
986
987 [[nodiscard]] double getInteractionLength() const override { return _cutoff + this->getVerletSkin(); }
988
989 void deleteAllParticles() override {
990 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
991 std::for_each(_particlesToAdd.begin(), _particlesToAdd.end(), [](auto &buffer) { buffer.clear(); });
992 std::for_each(_towerBlock.begin(), _towerBlock.end(), [](auto &tower) { tower.clear(); });
993 }
994
1000 return _neighborLists;
1001 }
1002
1008 void rebuildTowersAndClusters(bool newton3) {
1009 using namespace utils::ArrayMath::literals;
1010 // collect all particles to add from across the thread buffers
1011 typename decltype(_particlesToAdd)::value_type particlesToAdd;
1012 const size_t numParticlesToAdd =
1013 std::accumulate(_particlesToAdd.begin(), _particlesToAdd.end(), 0,
1014 [](size_t acc, const auto &buffer) { return acc + buffer.size(); });
1015 particlesToAdd.reserve(numParticlesToAdd);
1016 std::for_each(_particlesToAdd.begin(), _particlesToAdd.end(), [&](auto &particlesBuffer) {
1017 particlesToAdd.insert(particlesToAdd.end(), particlesBuffer.begin(), particlesBuffer.end());
1018 particlesBuffer.clear();
1019 });
1020
1021 const double interactionLength = _cutoff + this->_skin;
1022 _builder = std::make_unique<internal::VerletClusterListsRebuilder<Particle_T>>(
1023 _towerBlock, particlesToAdd, _neighborLists, _clusterSize, interactionLength * interactionLength, newton3);
1024
1025 _numClusters = _builder->rebuildTowersAndClusters();
1026
1027 _isValid.store(ValidityState::cellsValidListsInvalid, std::memory_order_relaxed);
1028 for (auto &tower : _towerBlock) {
1029 tower.setParticleDeletionObserver(this);
1030 }
1031 }
1032
1038 template <class LoopBody>
1039 void traverseClustersSequential(LoopBody &&loopBody) {
1040 for (size_t x = 0; x < _towerBlock.getTowersPerDim()[0]; x++) {
1041 for (size_t y = 0; y < _towerBlock.getTowersPerDim()[1]; y++) {
1042 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
1043 for (auto clusterIter = tower.getFirstOwnedCluster(); clusterIter < tower.getFirstTailHaloCluster();
1044 ++clusterIter) {
1045 loopBody(*clusterIter);
1046 }
1047 }
1048 }
1049 }
1050
1060 template <class LoopBody>
1061 void traverseClustersParallel(LoopBody &&loopBody) {
1062 const auto towersPerDimX = _towerBlock.getTowersPerDim()[0];
1063 const auto towersPerDimY = _towerBlock.getTowersPerDim()[1];
1065 AUTOPAS_OPENMP(parallel for schedule(dynamic) collapse(2))
1066 for (size_t x = 0; x < towersPerDimX; x++) {
1067 for (size_t y = 0; y < towersPerDimY; y++) {
1068 auto &tower = _towerBlock.getTowerByIndex2D(x, y);
1069
1070 for (auto clusterIter = tower.getFirstOwnedCluster(); clusterIter < tower.getFirstTailHaloCluster();
1071 ++clusterIter) {
1072 loopBody(*clusterIter);
1073 }
1074 }
1075 }
1076 }
1077
1078 protected:
1084 size_t numClusterPairs = 0;
1085 this->template traverseClusters<false>(
1086 [&numClusterPairs](auto &cluster) { numClusterPairs += cluster.getNeighbors()->size(); });
1087
1088 constexpr int minNumClusterPairsPerThread = 1000;
1089 auto numThreads =
1090 std::clamp(static_cast<int>(numClusterPairs / minNumClusterPairsPerThread), 1, autopas_get_max_threads());
1091
1092 size_t numClusterPairsPerThread =
1093 std::max(static_cast<unsigned long>(std::ceil(static_cast<double>(numClusterPairs) / numThreads)), 1ul);
1094 if (numClusterPairsPerThread * numThreads < numClusterPairs) {
1096 "VerletClusterLists::calculateClusterThreadPartition(): numClusterPairsPerThread ({}) * numThreads ({})={} "
1097 "should always "
1098 "be at least the amount of Cluster Pairs ({})!",
1099 numClusterPairsPerThread, numThreads, numClusterPairsPerThread * numThreads, numClusterPairs);
1100 }
1101 fillClusterRanges(numClusterPairsPerThread, numThreads);
1102 }
1103
1110 void fillClusterRanges(size_t numClusterPairsPerThread, int numThreads) {
1111 if (numClusterPairsPerThread < 1) {
1113 "VerletClusterLists::fillClusterRanges(): numClusterPairsPerThread({}) is less than one, this is not "
1114 "supported "
1115 "and will lead to errors!",
1116 numClusterPairsPerThread);
1117 }
1118 _clusterThreadPartition.resize(numThreads);
1119
1120 size_t currentThread = 0;
1121 size_t currentNumClustersToAdd = 0;
1122 size_t numClusterPairsTotal = 0;
1123 bool threadIsInitialized = false;
1124 // Iterate over the clusters of all towers
1125 for (size_t currentTowerIndex = 0; currentTowerIndex < _towerBlock.size(); currentTowerIndex++) {
1126 auto &currentTower = _towerBlock[currentTowerIndex];
1127 const auto firstOwnedClusterIndex =
1128 std::distance(currentTower.getClusters().begin(), currentTower.getFirstOwnedCluster());
1129 const auto firstTailHaloClusterIndex =
1130 std::distance(currentTower.getClusters().begin(), currentTower.getFirstTailHaloCluster());
1131 for (size_t currentClusterInTower = firstOwnedClusterIndex; currentClusterInTower < firstTailHaloClusterIndex;
1132 ++currentClusterInTower) {
1133 auto &currentCluster = currentTower.getCluster(currentClusterInTower);
1134
1135 // If on a new thread, start with the clusters for this thread here.
1136 if (not threadIsInitialized) {
1137 _clusterThreadPartition[currentThread] = {currentTowerIndex, currentClusterInTower, 0};
1138 threadIsInitialized = true;
1139 }
1140
1141 currentNumClustersToAdd++;
1142 numClusterPairsTotal += currentCluster.getNeighbors()->size();
1143
1144 // If the thread is finished, write number of clusters and start new thread.
1145 if (numClusterPairsTotal >= numClusterPairsPerThread * (currentThread + 1)) {
1146 // Add the number of clusters for the finished thread.
1147 _clusterThreadPartition[currentThread].numClusters += currentNumClustersToAdd;
1148 currentNumClustersToAdd = 0;
1149 // Go to next thread!
1150 currentThread++;
1151 // if we are already at the end of all threads, go back to last thread!
1152 // this is a safety precaution and should not really matter.
1153 if (currentThread >= numThreads) {
1154 --currentThread;
1155 threadIsInitialized = true;
1156 } else {
1157 threadIsInitialized = false;
1158 }
1159 }
1160 }
1161 }
1162 if (not threadIsInitialized) {
1163 _clusterThreadPartition[currentThread] = {0, 0, 0};
1164 }
1165 // Make sure the last cluster range contains the rest of the clusters, even if there is not the perfect number left.
1166 if (currentNumClustersToAdd != 0) {
1167 _clusterThreadPartition[currentThread].numClusters += currentNumClustersToAdd;
1168 }
1169 // Theoretically, some threads may still remain. This ensures that their numClusters are set to 0.
1170 while (++currentThread < numThreads) {
1171 _clusterThreadPartition[currentThread] = {0, 0, 0};
1172 }
1173 }
1174
1180 void notifyParticleDeleted() override {
1181 // this is potentially called from a threaded environment, so we have to make this atomic here!
1182 _isValid.store(ValidityState::invalid, std::memory_order_relaxed);
1183 }
1184
1185 private:
1200 template <bool regionIter>
1201 [[nodiscard]] std::tuple<size_t, size_t> advanceIteratorIndices(
1202 size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior, const std::array<double, 3> &boxMin,
1203 const std::array<double, 3> &boxMax, const std::array<double, 3> &boxMinWithSafetyMargin,
1204 const std::array<double, 3> &boxMaxWithSafetyMargin, size_t endCellIndex) const {
1205 // Finding the indices for the next particle
1206 const size_t stride = (iteratorBehavior & IteratorBehavior::forceSequential) ? 1 : autopas_get_num_threads();
1207
1208 // helper function to determine if the cell can even contain particles of interest to the iterator
1209 auto towerIsRelevant = [&]() -> bool {
1210 // special case: Towers are not yet built, then the tower acting as buffer is always relevant.
1211 if (_towerBlock.size() == 1) {
1212 return true;
1213 }
1214 bool isRelevant = true;
1215 if constexpr (regionIter) {
1216 // is the cell in the region?
1217 const auto [towerLowCorner, towerHighCorner] = _towerBlock.getTowerBoundingBox(cellIndex);
1218 isRelevant =
1219 utils::boxesOverlap(towerLowCorner, towerHighCorner, boxMinWithSafetyMargin, boxMaxWithSafetyMargin);
1220 }
1221 return isRelevant;
1222 };
1223
1224 do {
1225 // advance to the next particle
1226 ++particleIndex;
1227 // If this breaches the end of a cell, find the next non-empty cell and reset particleIndex.
1228
1229 // If cell has wrong type, or there are no more particles in this cell jump to the next
1230 while (not towerIsRelevant() or particleIndex >= _towerBlock[cellIndex].getNumActualParticles()) {
1231 cellIndex += stride;
1232 particleIndex = 0;
1233
1234 // If we notice that there is nothing else to look at set invalid values, so we get a nullptr next time and
1235 // break.
1236 if (cellIndex > endCellIndex) {
1237 return {std::numeric_limits<size_t>::max(), particleIndex};
1238 }
1239 }
1240 } while (not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
1241 _towerBlock[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax));
1242
1243 // the indices returned at this point should always be valid
1244 return {cellIndex, particleIndex};
1245 }
1246
1248
1252 autopas::LoadEstimatorOption _loadEstimator;
1253
1257 size_t _clusterSize;
1258
1262 size_t _numClusters{0};
1263
1269 mutable std::vector<std::vector<Particle_T>> _particlesToAdd;
1270
1276 [[nodiscard]] bool particlesToAddEmpty(int bufferID = -1) const {
1277 if (bufferID == -1) {
1278 for (auto &threadBuffer : _particlesToAdd) {
1279 if (not threadBuffer.empty()) {
1280 return false;
1281 }
1282 }
1283 return true;
1284 } else {
1285 return _particlesToAdd[bufferID].empty();
1286 }
1287 }
1288
1297 template <class VecVec>
1298 void appendBuffersHelper(utils::optRef<VecVec> additionalVectors, VecVec &outVec) const {
1299 if (additionalVectors.has_value()) {
1300 outVec.reserve(_particlesToAdd.size() + additionalVectors->get().size());
1301 outVec.insert(outVec.end(), additionalVectors->get().begin(), additionalVectors->get().end());
1302 } else {
1303 outVec.reserve(_particlesToAdd.size());
1304 }
1305 for (auto &vec : _particlesToAdd) {
1306 outVec.push_back(&vec);
1307 }
1308 }
1309
1313 std::vector<ClusterRange> _clusterThreadPartition;
1314
1318 double _cutoff{};
1319
1323 enum class ValidityState : unsigned char {
1324 invalid = 0, // nothing is valid.
1325 cellsValidListsInvalid = 1, // only the cell structure is valid, but the lists are not.
1326 cellsAndListsValid = 2 // the cells and lists are valid
1327 };
1328
1332 std::atomic<ValidityState> _isValid{ValidityState::invalid};
1333
1337 std::unique_ptr<internal::VerletClusterListsRebuilder<Particle_T>> _builder;
1338
1343};
1344
1345} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:74
#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:96
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:109
Functor base class.
Definition: Functor.h:41
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:40
constexpr bool end() const
Dummy to make range-based for loops work.
Definition: ParticleContainerInterface.h:252
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:65
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:57
TraversalSelectorInfo getTraversalSelectorInfo() const override
Generates a traversal selector info for this container.
Definition: VerletClusterLists.h:421
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo)
Reduce properties of particles as defined by a lambda function.
Definition: VerletClusterLists.h:543
ContainerOption getContainerType() const override
Get the ContainerType.
Definition: VerletClusterLists.h:110
void traverseClusters(LoopBody &&loopBody)
Helper method to iterate over all clusters.
Definition: VerletClusterLists.h:831
void deleteHaloParticles() override
Deletes all halo particles.
Definition: VerletClusterLists.h:238
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:513
size_t size() const override
Get the number of all particles stored in this container (owned + halo + dummy).
Definition: VerletClusterLists.h:843
auto getClusterSize() const
Returns the number of particles in each cluster.
Definition: VerletClusterLists.h:901
void rebuildNeighborLists(TraversalInterface *traversal) override
Rebuilds the neighbor lists for the next traversals.
Definition: VerletClusterLists.h:801
ContainerIterator< Particle_T, true, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior, utils::optRef< typename ContainerIterator< Particle_T, true, true >::ParticleVecType > additionalVectors=std::nullopt) override
Iterate over all particles in a specified region for(auto iter = container.getRegionIterator(lowCorne...
Definition: VerletClusterLists.h:591
BalancedTraversal::EstimatorFunction getLoadEstimatorFunction()
Generates the load estimation function depending on _loadEstimator.
Definition: VerletClusterLists.h:116
void setSkin(double skin)
Set the verlet skin length for the container.
Definition: VerletClusterLists.h:985
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:202
auto getTowersPerDimension() const
Returns the number of grids per dimension on the container.
Definition: VerletClusterLists.h:895
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:494
const std::array< double, 3 > & getHaloBoxMax() const
Get the upper corner of the halo box.
Definition: VerletClusterLists.h:965
ContainerIterator< Particle_T, true, false > begin(IteratorBehavior behavior=IteratorBehavior::ownedOrHalo, utils::optRef< typename ContainerIterator< Particle_T, true, false >::ParticleVecType > additionalVectors=std::nullopt) override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: VerletClusterLists.h:436
void setSoASortingThresholds(std::shared_ptr< const SortingThresholdInfoInterface > soaSortingThresholds) override
Set the SoA sorting-threshold for traversals that use the CellFunctor.
Definition: VerletClusterLists.h:183
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:310
void addParticleImpl(const Particle_T &p) override
Adds the given particle to the container.
Definition: VerletClusterLists.h:197
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:729
void deleteParticle(Particle_T &particle) override
Marks the given particle as deleted.
Definition: VerletClusterLists.h:374
void traverseClustersSequential(LoopBody &&loopBody)
Helper method to sequentially iterate over all owned clusters.
Definition: VerletClusterLists.h:1039
void computeInteractions(TraversalInterface *traversal) override
Iterates over all particle multiples (e.g.
Definition: VerletClusterLists.h:149
double getInteractionLength() const override
Return the interaction length (cutoff+skin) of the container.
Definition: VerletClusterLists.h:987
void deleteParticle(size_t cellIndex, size_t particleIndex) override
Marks the particle at the given index positions as deleted.
Definition: VerletClusterLists.h:379
double getVerletSkin() const override
Return the verletSkin of the container verletSkin.
Definition: VerletClusterLists.h:979
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:281
Particle_T ParticleType
Type of the Particle.
Definition: VerletClusterLists.h:62
internal::ClusterTowerBlock2D< Particle_T > & getTowerBlock()
Getter for the cell block.
Definition: VerletClusterLists.h:957
const std::array< double, 3 > & getBoxMin() const override
Get the lower corner of the container without halo.
Definition: VerletClusterLists.h:967
ContainerIterator< Particle_T, false, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior, utils::optRef< typename ContainerIterator< Particle_T, false, true >::ParticleVecType > additionalVectors=std::nullopt) const override
Iterate over all particles in a specified region for(auto iter = container.getRegionIterator(lowCorne...
Definition: VerletClusterLists.h:621
void deleteAllParticles() override
Deletes all particles.
Definition: VerletClusterLists.h:989
void fillClusterRanges(size_t numClusterPairsPerThread, int numThreads)
Fills in the cluster ranges of the cluster thread partition.
Definition: VerletClusterLists.h:1110
void setAoSSortingThresholds(std::shared_ptr< const SortingThresholdInfoInterface > aosSortingThresholds) override
Set the aos-sorting-threshold for traversals that use the CellFunctor.
Definition: VerletClusterLists.h:177
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:98
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:562
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: VerletClusterLists.h:854
auto getMaxNumTowersPerInteractionLength() const
Returns the towers per interaction length.
Definition: VerletClusterLists.h:908
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:685
void loadParticlesIntoSoAs(Functor &functor)
Loads all particles of the container in their correct SoA and generates the SoAViews for the clusters...
Definition: VerletClusterLists.h:916
void extractParticlesFromSoAs(Functor &functor)
Extracts all SoAs of the container into the particles.
Definition: VerletClusterLists.h:931
void traverseClustersParallel(LoopBody &&loopBody)
Helper method to iterate over all clusters in parallel.
Definition: VerletClusterLists.h:1061
const std::array< double, 3 > & getHaloBoxMin() const
Get the lower corner of the halo box.
Definition: VerletClusterLists.h:973
const internal::VerletClusterListsRebuilder< Particle_T >::NeighborListsBuffer_T & getNeighborLists() const
Get the neighbor lists buffer object.
Definition: VerletClusterLists.h:999
double getCutoff() const override
Return the cutoff of the container.
Definition: VerletClusterLists.h:975
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:946
auto getTowerSideLength() const
Returns the grid side length of the grids in the container.
Definition: VerletClusterLists.h:889
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:762
ContainerIterator< Particle_T, false, false > begin(IteratorBehavior behavior=autopas::IteratorBehavior::ownedOrHalo, utils::optRef< typename ContainerIterator< Particle_T, false, false >::ParticleVecType > additionalVectors=std::nullopt) const override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: VerletClusterLists.h:465
const std::array< double, 3 > & getBoxMax() const override
Get the upper corner of the container without halo.
Definition: VerletClusterLists.h:959
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:185
void setCutoff(double cutoff) override
Set the cutoff of the container.
Definition: VerletClusterLists.h:977
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:287
auto getNumClusters() const
Returns the number of clusters in this container.
Definition: VerletClusterLists.h:883
std::vector< Particle_T > updateContainer(bool keepNeighborListsValid) override
Updates the container.
Definition: VerletClusterLists.h:384
void rebuildTowersAndClusters(bool newton3)
Initializes a new VerletClusterListsRebuilder and uses it to rebuild the towers and the clusters.
Definition: VerletClusterLists.h:1008
void calculateClusterThreadPartition()
Calculates a cluster thread partition that aims to give each thread about the same amount of cluster ...
Definition: VerletClusterLists.h:1083
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:652
bool updateHaloParticle(const Particle_T &haloParticle) override
Update a halo particle of the container with the given haloParticle.
Definition: VerletClusterLists.h:216
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:1180
const auto & getClusterThreadPartition() const
Returns the cluster-thread-partition.
Definition: VerletClusterLists.h:877
Class to manage the grid of towers for the Verlet Cluster Lists container.
Definition: ClusterTowerBlock2D.h:26
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:64
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
static bool checkParticleInCellAndUpdateByIDAndPosition(CellType &cell, const typename CellType::ParticleType &particle, double absError)
Updates a found particle within cellI to the values of particleI.
Definition: ParticleCellHelpers.h:25
void deleteParticle(ParticleIterator &iterator)
Function to access private iterator.deleteCurrentParticle() via friend.
Definition: ContainerIterator.h:38
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
std::optional< std::reference_wrapper< T > > optRef
Short alias for std::optional<std::reference_wrapper<T>>
Definition: optRef.h:16
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
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:71
size_t startIndexInTower
The index of the first cluster in its tower.
Definition: VerletClusterLists.h:79
size_t startTowerIndex
The index of the tower that contains the first cluster.
Definition: VerletClusterLists.h:75
size_t numClusters
The number of clusters in the range.
Definition: VerletClusterLists.h:83