39template <
class Particle_T>
63 LinkedCells(
const std::array<double, 3> &boxMin,
const std::array<double, 3> &boxMax,
const double cutoff,
64 const double skin,
const double cellSizeFactor = 1.0,
const size_t sortingThreshold = 8,
70 [[nodiscard]] ContainerOption
getContainerType()
const override {
return ContainerOption::linkedCells; }
72 void reserve(
size_t numParticles,
size_t numParticlesHaloEstimate)
override {
73 _cellBlock.reserve(numParticles + numParticlesHaloEstimate);
96 auto cells =
_cellBlock.getNearbyHaloCells(haloParticle.getR(), this->getVerletSkin());
97 for (
auto cellptr : cells) {
103 AutoPasLog(TRACE,
"UpdateHaloParticle was not able to update particle: {}", haloParticle.toString());
124 return [&](
const std::array<unsigned long, 3> &cellsPerDimension,
125 const std::array<unsigned long, 3> &lowerCorner,
const std::array<unsigned long, 3> &upperCorner) {
133 [&](
const std::array<unsigned long, 3> &cellsPerDimension,
const std::array<unsigned long, 3> &lowerCorner,
134 const std::array<unsigned long, 3> &upperCorner) {
return 1; };
147 [[nodiscard]] std::vector<Particle_T>
updateContainer(
bool keepNeighborListsValid)
override {
148 if (keepNeighborListsValid) {
154 std::vector<Particle_T> invalidParticles;
157 std::vector<Particle_T> myInvalidParticles{}, myInvalidNotOwnedParticles{};
159 myInvalidParticles.reserve(128);
160 myInvalidNotOwnedParticles.reserve(128);
162 for (
size_t cellId = 0; cellId < this->
getCells().size(); ++cellId) {
164 this->
getCells()[cellId].deleteDummyParticles();
167 if (this->
getCells()[cellId].isEmpty())
continue;
169 const auto [cellLowerCorner, cellUpperCorner] = this->
getCellBlock().getCellBoundingBox(cellId);
171 auto &particleVec = this->
getCells()[cellId]._particles;
172 for (
auto pIter = particleVec.begin(); pIter < particleVec.end();) {
174 if (
utils::notInBox(pIter->getR(), cellLowerCorner, cellUpperCorner)) {
175 myInvalidParticles.push_back(*pIter);
177 *pIter = particleVec.back();
178 particleVec.pop_back();
188 for (
auto &&p : myInvalidParticles) {
190 if (
utils::inBox(p.getR(), this->getBoxMin(), this->getBoxMax())) {
191 this->
template addParticle<false>(p);
193 myInvalidNotOwnedParticles.push_back(p);
198 invalidParticles.insert(invalidParticles.end(), myInvalidNotOwnedParticles.begin(),
199 myInvalidNotOwnedParticles.end());
202 return invalidParticles;
210 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
211 IteratorBehavior iteratorBehavior,
212 const std::array<double, 3> &boxMin,
213 const std::array<double, 3> &boxMax)
const override {
214 return getParticleImpl<true>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
217 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
218 IteratorBehavior iteratorBehavior)
const override {
220 constexpr std::array<double, 3> boxMin{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(),
221 std::numeric_limits<double>::lowest()};
223 constexpr std::array<double, 3> boxMax{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
224 std::numeric_limits<double>::max()};
225 return getParticleImpl<false>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
239 template <
bool regionIter>
240 std::tuple<const Particle_T *, size_t, size_t>
getParticleImpl(
size_t cellIndex,
size_t particleIndex,
241 IteratorBehavior iteratorBehavior,
242 const std::array<double, 3> &boxMin,
243 const std::array<double, 3> &boxMax)
const {
244 using namespace autopas::utils::ArrayMath::literals;
246 std::array<double, 3> boxMinWithSafetyMargin = boxMin;
247 std::array<double, 3> boxMaxWithSafetyMargin = boxMax;
248 if constexpr (regionIter) {
255 const auto [startCellIndex, endCellIndex] = [&]() -> std::tuple<size_t, size_t> {
256 if constexpr (regionIter) {
258 return {
_cellBlock.get1DIndexOfPosition(boxMinWithSafetyMargin),
259 _cellBlock.get1DIndexOfPosition(boxMaxWithSafetyMargin)};
261 if (not(iteratorBehavior & IteratorBehavior::halo)) {
266 return {0, this->
_cells.size() - 1};
272 if (cellIndex == 0 and particleIndex == 0) {
277 if (cellIndex >= this->
_cells.size()) {
278 return {
nullptr, 0, 0};
281 if (particleIndex >= this->
_cells[cellIndex].
size() or
282 not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
283 this->
_cells[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax)) {
285 std::tie(cellIndex, particleIndex) =
286 advanceIteratorIndices<regionIter>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax,
287 boxMinWithSafetyMargin, boxMaxWithSafetyMargin, endCellIndex);
291 if (cellIndex > endCellIndex) {
292 return {
nullptr, 0, 0};
294 const Particle_T *retPtr = &this->
_cells[cellIndex][particleIndex];
296 return {retPtr, cellIndex, particleIndex};
304 auto &particleVec =
_cellBlock.getContainingCell(particle.getR())._particles;
305 const bool isRearParticle = &particle == &particleVec.back();
307 particle = particleVec.back();
308 particleVec.pop_back();
309 return not isRearParticle;
316 auto &particleVec = this->
_cells[cellIndex]._particles;
317 auto &particle = particleVec[particleIndex];
319 particle = particleVec.back();
320 particleVec.pop_back();
321 return particleIndex < particleVec.size();
328 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
337 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
339 nullptr)
const override {
349 template <
typename Lambda>
350 void forEach(Lambda forEachLambda, IteratorBehavior behavior = IteratorBehavior::ownedOrHalo) {
351 if (behavior == IteratorBehavior::ownedOrHaloOrDummy) {
353 cell.forEach(forEachLambda);
356 for (
size_t index = 0; index <
getCells().size(); index++) {
357 if (not
_cellBlock.ignoreCellForIteration(index, behavior)) {
358 getCells()[index].forEach(forEachLambda, behavior);
372 template <
typename Lambda,
typename A>
373 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior = IteratorBehavior::ownedOrHalo) {
374 if (behavior == IteratorBehavior::ownedOrHaloOrDummy) {
376 cell.reduce(reduceLambda, result);
379 for (
size_t index = 0; index <
getCells().size(); index++) {
380 if (not
_cellBlock.ignoreCellForIteration(index, behavior)) {
381 getCells()[index].reduce(reduceLambda, result, behavior);
391 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
400 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
402 nullptr)
const override {
414 template <
typename Lambda>
416 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
417 using namespace autopas::utils::ArrayMath::literals;
422 const size_t numCellsOfInterest = (stopIndex3D[0] - startIndex3D[0] + 1) * (stopIndex3D[1] - startIndex3D[1] + 1) *
423 (stopIndex3D[2] - startIndex3D[2] + 1);
424 std::vector<size_t> cellsOfInterest;
425 cellsOfInterest.reserve(numCellsOfInterest);
427 const auto &cellsPerDimensionWithHalo = this->
_cellBlock.getCellsPerDimensionWithHalo();
429 for (
size_t z = startIndex3D[2]; z <= stopIndex3D[2]; ++z) {
430 for (
size_t y = startIndex3D[1]; y <= stopIndex3D[1]; ++y) {
431 for (
size_t x = startIndex3D[0]; x <= stopIndex3D[0]; ++x) {
437 for (
auto cellIndex : cellsOfInterest) {
438 if (not
_cellBlock.ignoreCellForIteration(cellIndex, behavior)) {
439 getCells()[cellIndex].forEach(forEachLambda, lowerCorner, higherCorner, behavior);
454 template <
typename Lambda,
typename A>
455 void reduceInRegion(Lambda reduceLambda, A &result,
const std::array<double, 3> &lowerCorner,
456 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
457 using namespace autopas::utils::ArrayMath::literals;
461 const size_t numCellsOfInterest = (stopIndex3D[0] - startIndex3D[0] + 1) * (stopIndex3D[1] - startIndex3D[1] + 1) *
462 (stopIndex3D[2] - startIndex3D[2] + 1);
463 std::vector<size_t> cellsOfInterest;
464 cellsOfInterest.reserve(numCellsOfInterest);
466 const auto &cellsPerDimensionWithHalo = this->
_cellBlock.getCellsPerDimensionWithHalo();
468 for (
size_t z = startIndex3D[2]; z <= stopIndex3D[2]; ++z) {
469 for (
size_t y = startIndex3D[1]; y <= stopIndex3D[1]; ++y) {
470 for (
size_t x = startIndex3D[0]; x <= stopIndex3D[0]; ++x) {
476 for (
auto cellIndex : cellsOfInterest) {
477 if (not
_cellBlock.ignoreCellForIteration(cellIndex, behavior)) {
478 getCells()[cellIndex].reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
516 template <
bool regionIter>
518 size_t cellIndex,
size_t particleIndex, IteratorBehavior iteratorBehavior,
const std::array<double, 3> &boxMin,
519 const std::array<double, 3> &boxMax, std::array<double, 3> boxMinWithSafetyMargin,
520 std::array<double, 3> boxMaxWithSafetyMargin,
size_t endCellIndex)
const {
525 auto cellIsRelevant = [&]() ->
bool {
528 (iteratorBehavior & IteratorBehavior::owned and
_cellBlock.cellCanContainOwnedParticles(cellIndex)) or
529 (iteratorBehavior & IteratorBehavior::halo and
_cellBlock.cellCanContainHaloParticles(cellIndex));
530 if constexpr (regionIter) {
534 const auto [cellLowCorner, cellHighCorner] =
_cellBlock.getCellBoundingBox(cellIndex);
536 utils::boxesOverlap(cellLowCorner, cellHighCorner, boxMinWithSafetyMargin, boxMaxWithSafetyMargin);
548 while (not cellIsRelevant() or particleIndex >= this->
_cells[cellIndex].
size()) {
556 if (cellIndex > endCellIndex) {
557 return {std::numeric_limits<
decltype(cellIndex)>::max(), std::numeric_limits<
decltype(particleIndex)>::max()};
560 }
while (not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
561 this->
_cells[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax));
564 return {cellIndex, particleIndex};
572 template <
typename Traversal>
579 if (traversalInterface && cellTraversal) {
581 cellTraversal->setCellsToTraverse(this->
_cells);
584 "The selected traversal is not compatible with the LinkedCells container. TraversalID: {}",
585 traversal->getTraversalType());
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
#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
The CellBasedParticleContainer class stores particles in some object and provides methods to iterate ...
Definition: CellBasedParticleContainer.h:25
size_t _sortingThreshold
If the number of particles in a cell or cell pair exceeds this threshold, the particles will be sorte...
Definition: CellBasedParticleContainer.h:164
size_t size() const override
Get the total number of particles saved in the container (owned + halo + dummy).
Definition: CellBasedParticleContainer.h:133
double getVerletSkin() const final
Returns the verlet Skin length.
Definition: CellBasedParticleContainer.h:96
double getInteractionLength() const final
Return the interaction length (cutoff+skin) of the container.
Definition: CellBasedParticleContainer.h:91
std::vector< ParticleCellType > _cells
Vector of particle cells.
Definition: CellBasedParticleContainer.h:159
A cell pair traversal.
Definition: CellTraversal.h:23
virtual void setSortingThreshold(size_t sortingThreshold)=0
Set the sorting-threshold for traversals that use the CellFunctor If the sum of the number of particl...
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
This class handles the storage of particles in their full form.
Definition: FullParticleCell.h:26
void addParticle(const Particle_T &p) override
Adds a Particle to the cell.
Definition: FullParticleCell.h:51
Interface for traversals used by the LinkedCell class.
Definition: LCTraversalInterface.h:18
LinkedCells class.
Definition: LinkedCells.h:40
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: LinkedCells.h:315
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: LinkedCells.h:240
internal::CellBlock3D< ParticleCellType > & getCellBlock()
Get the cell block, not supposed to be used except by verlet lists.
Definition: LinkedCells.h:487
BalancedTraversal::EstimatorFunction getLoadEstimatorFunction()
Generates the load estimation function depending on _loadEstimator.
Definition: LinkedCells.h:120
void forEachInRegion(Lambda forEachLambda, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior)
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: LinkedCells.h:415
void addParticleImpl(const Particle_T &p) override
Adds a particle to the container.
Definition: LinkedCells.h:79
void deleteHaloParticles() override
Deletes all halo particles.
Definition: LinkedCells.h:110
bool updateHaloParticle(const Particle_T &haloParticle) override
Update a halo particle of the container with the given haloParticle.
Definition: LinkedCells.h:95
void addHaloParticleImpl(const Particle_T &haloParticle) override
Adds a particle to the container that lies in the halo region of the container.
Definition: LinkedCells.h:87
LoadEstimatorOption _loadEstimator
load estimation algorithm for balanced traversals.
Definition: LinkedCells.h:597
void prepareTraversal(Traversal &traversal)
Checks if a given traversal is allowed for LinkedCells and sets it up for the force interactions.
Definition: LinkedCells.h:573
std::vector< Particle_T > updateContainer(bool keepNeighborListsValid) override
Updates the container.
Definition: LinkedCells.h:147
void reduceInRegion(Lambda reduceLambda, A &result, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior)
Execute code on all particles in this container in a certain region as defined by a lambda function.
Definition: LinkedCells.h:455
ContainerIterator< Particle_T, false, false > begin(IteratorBehavior behavior=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: LinkedCells.h:336
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Reduce properties of particles as defined by a lambda function.
Definition: LinkedCells.h:373
std::vector< ParticleCellType > & getCells()
Returns a non-const reference to the cell data structure.
Definition: LinkedCells.h:499
const internal::CellBlock3D< ParticleCellType > & getCellBlock() const
Get the cell block, not supposed to be used except by verlet lists.
Definition: LinkedCells.h:493
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: LinkedCells.h:390
Particle_T ParticleType
Type of the Particle.
Definition: LinkedCells.h:50
bool deleteParticle(Particle_T &particle) override
Deletes the given particle as long as this does not compromise the validity of the container.
Definition: LinkedCells.h:302
TraversalSelectorInfo getTraversalSelectorInfo() const override
Generates a traversal selector info for this container.
Definition: LinkedCells.h:205
void computeInteractions(TraversalInterface *traversal) override
Iterates over all particle multiples (e.g.
Definition: LinkedCells.h:139
void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override
Reserve memory for a given number of particles in the container and logic layers.
Definition: LinkedCells.h:72
void rebuildNeighborLists(TraversalInterface *traversal) override
Rebuilds the neighbor lists for the next traversals.
Definition: LinkedCells.h:112
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: LinkedCells.h:399
std::tuple< size_t, size_t > advanceIteratorIndices(size_t cellIndex, size_t particleIndex, IteratorBehavior iteratorBehavior, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, std::array< double, 3 > boxMinWithSafetyMargin, std::array< double, 3 > boxMaxWithSafetyMargin, size_t endCellIndex) const
Given a pair of cell-/particleIndex and iterator restrictions either returns the next indices that ma...
Definition: LinkedCells.h:517
void forEach(Lambda forEachLambda, IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Execute code on all particles in this container as defined by a lambda function.
Definition: LinkedCells.h:350
LinkedCells(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, const double cutoff, const double skin, const double cellSizeFactor=1.0, const size_t sortingThreshold=8, LoadEstimatorOption loadEstimator=LoadEstimatorOption::squaredParticlesPerCell)
Constructor of the LinkedCells class.
Definition: LinkedCells.h:63
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: LinkedCells.h:327
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: LinkedCells.h:210
ContainerOption getContainerType() const override
Get the ContainerType.
Definition: LinkedCells.h:70
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: LinkedCells.h:217
internal::CellBlock3D< ParticleCellType > _cellBlock
object to manage the block of cells.
Definition: LinkedCells.h:592
Class representing the load estimator choices.
Definition: LoadEstimatorOption.h:18
Value
Possible choices for the load estimation algorithm.
Definition: LoadEstimatorOption.h:23
@ squaredParticlesPerCell
Number of particles per cell squared.
Definition: LoadEstimatorOption.h:31
@ none
No load estimator.
Definition: LoadEstimatorOption.h:27
This interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
virtual void endTraversal()=0
Finalizes the traversal.
virtual void traverseParticles()=0
Traverse the particles by pairs, triplets etc.
virtual void initTraversal()=0
Initializes the traversal.
Info for traversals of a specific container.
Definition: TraversalSelectorInfo.h:14
Class that manages a block of ParticleCells.
Definition: CellBlock3D.h:30
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
static bool checkParticleInCellAndUpdateByID(CellType &cell, const typename CellType::ParticleType &particle)
Updates a found particle within cellI to the values of particleI.
Definition: ParticleCellHelpers.h:21
unsigned long squaredParticlesPerCell(const std::vector< ParticleCell > &cells, const std::array< unsigned long, 3 > &cellsPerDimension, const std::array< unsigned long, 3 > &lowerCorner, const std::array< unsigned long, 3 > &upperCorner)
Sums up the squared number of particles for all cells within region.
Definition: LoadEstimators.h:31
constexpr T threeToOneD(T x, T y, T z, const std::array< T, 3 > &dims)
Convert a 3d index to a 1d index.
Definition: ThreeDimensionalMapping.h:29
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 notInBox(const std::array< T, 3 > &position, const std::array< T, 3 > &low, const std::array< T, 3 > &high)
Checks if position is not inside of a box defined by low and high.
Definition: inBox.h:50
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_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132