38template <
class Particle_T>
61 LinkedCellsReferences(
const std::array<double, 3> &boxMin,
const std::array<double, 3> &boxMax,
const double cutoff,
62 const double skin,
const double cellSizeFactor = 1.0,
const size_t sortingThreshold = 8,
71 [[nodiscard]] ContainerOption
getContainerType()
const override {
return ContainerOption::linkedCellsReferences; }
73 void reserve(
size_t numParticles,
size_t numParticlesHaloEstimate)
override {
74 _cellBlock.reserve(numParticles + numParticlesHaloEstimate);
101 auto cells =
_cellBlock.getNearbyHaloCells(haloParticle.getR(), this->getVerletSkin());
102 for (
auto cellptr : cells) {
108 AutoPasLog(TRACE,
"UpdateHaloParticle was not able to update particle: {}", haloParticle.toString());
136 return [&](
const std::array<unsigned long, 3> &cellsPerDimension,
137 const std::array<unsigned long, 3> &lowerCorner,
const std::array<unsigned long, 3> &upperCorner) {
145 [&](
const std::array<unsigned long, 3> &cellsPerDimension,
const std::array<unsigned long, 3> &lowerCorner,
146 const std::array<unsigned long, 3> &upperCorner) {
return 1; };
160 for (
auto &cell : this->
_cells) {
170 auto address = &(*it);
184 if (traversalInterface && cellPairTraversal) {
186 cellPairTraversal->setCellsToTraverse(this->
_cells);
189 "Trying to use a traversal of wrong type in LinkedCellsReferences::computeInteractions. TraversalID: {}",
198 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
199 IteratorBehavior iteratorBehavior,
200 const std::array<double, 3> &boxMin,
201 const std::array<double, 3> &boxMax)
const override {
202 return getParticleImpl<true>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
204 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
205 IteratorBehavior iteratorBehavior)
const override {
207 constexpr std::array<double, 3> boxMin{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(),
208 std::numeric_limits<double>::lowest()};
210 constexpr std::array<double, 3> boxMax{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
211 std::numeric_limits<double>::max()};
212 return getParticleImpl<false>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
226 template <
bool regionIter>
227 std::tuple<const Particle_T *, size_t, size_t>
getParticleImpl(
size_t cellIndex,
size_t particleIndex,
228 IteratorBehavior iteratorBehavior,
229 const std::array<double, 3> &boxMin,
230 const std::array<double, 3> &boxMax)
const {
231 using namespace autopas::utils::ArrayMath::literals;
233 std::array<double, 3> boxMinWithSafetyMargin = boxMin;
234 std::array<double, 3> boxMaxWithSafetyMargin = boxMax;
235 if constexpr (regionIter) {
242 const auto [startCellIndex, endCellIndex] = [&]() -> std::tuple<size_t, size_t> {
243 if constexpr (regionIter) {
245 return {
_cellBlock.get1DIndexOfPosition(boxMinWithSafetyMargin),
246 _cellBlock.get1DIndexOfPosition(boxMaxWithSafetyMargin)};
248 if (not(iteratorBehavior & IteratorBehavior::halo)) {
253 return {0, this->
_cells.size() - 1};
259 if (cellIndex == 0 and particleIndex == 0) {
264 if (cellIndex >= this->
_cells.size()) {
265 return {
nullptr, 0, 0};
268 if (particleIndex >= this->
_cells[cellIndex].
size() or
269 not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
270 this->
_cells[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax)) {
272 std::tie(cellIndex, particleIndex) =
273 advanceIteratorIndices<regionIter>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax,
274 boxMinWithSafetyMargin, boxMaxWithSafetyMargin, endCellIndex);
278 if (cellIndex > endCellIndex) {
279 return {
nullptr, 0, 0};
281 const Particle_T *retPtr = &this->
_cells[cellIndex][particleIndex];
283 return {retPtr, cellIndex, particleIndex};
305 if (keepNeighborListsValid) {
309 std::vector<Particle_T> invalidParticles;
312 bool exceptionCaught{
false};
313 std::string exceptionMsg{
""};
317 std::vector<Particle_T> myInvalidParticles, myInvalidNotOwnedParticles;
319 for (
size_t cellId = 0; cellId < this->
getCells().size(); ++cellId) {
321 this->
getCells()[cellId].deleteDummyParticles();
324 if (this->
getCells()[cellId].isEmpty())
continue;
326 auto [cellLowerCorner, cellUpperCorner] = this->
getCellBlock().getCellBoundingBox(cellId);
328 auto &particleVec = this->
getCells()[cellId]._particles;
329 for (
auto pIter = particleVec.begin(); pIter != particleVec.end();) {
330 if ((*pIter)->isOwned() and
utils::notInBox((*pIter)->getR(), cellLowerCorner, cellUpperCorner)) {
331 myInvalidParticles.push_back(**pIter);
335 **pIter = *particleVec.back();
339 particleVec.pop_back();
352 for (
auto &&p : myInvalidParticles) {
354 if (
utils::inBox(p.getR(), this->getBoxMin(), this->getBoxMax())) {
355 this->
template addParticle<false>(p);
357 myInvalidNotOwnedParticles.push_back(p);
360 }
catch (
const std::exception &e) {
361 exceptionCaught =
true;
363 exceptionMsg.append(e.what());
367 invalidParticles.insert(invalidParticles.end(), myInvalidNotOwnedParticles.begin(),
368 myInvalidNotOwnedParticles.end());
372 if (exceptionCaught) {
386 return invalidParticles;
401 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
410 IteratorBehavior behavior = IteratorBehavior::ownedOrHalo,
412 nullptr)
const override {
419 template <
typename Lambda>
420 void forEach(Lambda forEachLambda, IteratorBehavior behavior = IteratorBehavior::ownedOrHaloOrDummy) {
421 if (behavior == IteratorBehavior::ownedOrHaloOrDummy) {
425 for (
size_t index = 0; index <
getCells().size(); index++) {
426 if (!
_cellBlock.ignoreCellForIteration(index, behavior)) {
427 getCells()[index].forEach(forEachLambda, behavior);
436 template <
typename Lambda,
typename A>
437 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior = IteratorBehavior::ownedOrHaloOrDummy) {
438 if (behavior == IteratorBehavior::ownedOrHaloOrDummy) {
442 for (
size_t index = 0; index <
getCells().size(); index++) {
443 if (!
_cellBlock.ignoreCellForIteration(index, behavior)) {
444 getCells()[index].reduce(reduceLambda, result, behavior);
454 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
463 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
465 nullptr)
const override {
472 template <
typename Lambda>
474 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
475 using namespace autopas::utils::ArrayMath::literals;
481 size_t numCellsOfInterest = (stopIndex3D[0] - startIndex3D[0] + 1) * (stopIndex3D[1] - startIndex3D[1] + 1) *
482 (stopIndex3D[2] - startIndex3D[2] + 1);
483 std::vector<size_t> cellsOfInterest(numCellsOfInterest);
486 for (
size_t z = startIndex3D[2]; z <= stopIndex3D[2]; ++z) {
487 for (
size_t y = startIndex3D[1]; y <= stopIndex3D[1]; ++y) {
488 for (
size_t x = startIndex3D[0]; x <= stopIndex3D[0]; ++x) {
489 cellsOfInterest[i++] =
495 for (
size_t index : cellsOfInterest) {
496 if (!
_cellBlock.ignoreCellForIteration(index, behavior)) {
497 getCells()[index].forEach(forEachLambda, lowerCorner, higherCorner, behavior);
505 template <
typename Lambda,
typename A>
506 void reduceInRegion(Lambda reduceLambda, A &result,
const std::array<double, 3> &lowerCorner,
507 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
508 using namespace autopas::utils::ArrayMath::literals;
514 size_t numCellsOfInterest = (stopIndex3D[0] - startIndex3D[0] + 1) * (stopIndex3D[1] - startIndex3D[1] + 1) *
515 (stopIndex3D[2] - startIndex3D[2] + 1);
516 std::vector<size_t> cellsOfInterest(numCellsOfInterest);
519 for (
size_t z = startIndex3D[2]; z <= stopIndex3D[2]; ++z) {
520 for (
size_t y = startIndex3D[1]; y <= stopIndex3D[1]; ++y) {
521 for (
size_t x = startIndex3D[0]; x <= stopIndex3D[0]; ++x) {
522 cellsOfInterest[i++] =
528 for (
size_t index : cellsOfInterest) {
529 if (!
_cellBlock.ignoreCellForIteration(index, behavior)) {
530 getCells()[index].reduce(reduceLambda, result, lowerCorner, higherCorner, behavior);
568 template <
bool regionIter>
570 size_t cellIndex,
size_t particleIndex, IteratorBehavior iteratorBehavior,
const std::array<double, 3> &boxMin,
571 const std::array<double, 3> &boxMax,
const std::array<double, 3> &boxMinWithSafetyMargin,
572 const std::array<double, 3> &boxMaxWithSafetyMargin,
size_t endCellIndex)
const {
577 auto cellIsRelevant = [&]() ->
bool {
580 (iteratorBehavior & IteratorBehavior::owned and
_cellBlock.cellCanContainOwnedParticles(cellIndex)) or
581 (iteratorBehavior & IteratorBehavior::halo and
_cellBlock.cellCanContainHaloParticles(cellIndex));
582 if constexpr (regionIter) {
586 const auto [cellLowCorner, cellHighCorner] =
_cellBlock.getCellBoundingBox(cellIndex);
588 utils::boxesOverlap(cellLowCorner, cellHighCorner, boxMinWithSafetyMargin, boxMaxWithSafetyMargin);
600 while (not cellIsRelevant() or particleIndex >= this->
_cells[cellIndex].
size()) {
608 if (cellIndex > endCellIndex) {
609 return {std::numeric_limits<
decltype(cellIndex)>::max(), std::numeric_limits<
decltype(particleIndex)>::max()};
612 }
while (not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
613 this->
_cells[cellIndex][particleIndex], iteratorBehavior, boxMin, boxMax));
616 return {cellIndex, particleIndex};
#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
ParticleVector class.
Definition: ParticleVector.h:22
AutoPasLock for the sequential case.
Definition: WrapOpenMP.h:155
void unlock()
Release the lock.
Definition: WrapOpenMP.h:200
void lock()
Acquire the lock.
Definition: WrapOpenMP.h:190
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
void deleteAllParticles() override
Deletes all particles from the container.
Definition: CellBasedParticleContainer.h:101
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
Interface for traversals used by the LinkedCell class.
Definition: LCTraversalInterface.h:18
LinkedCells class.
Definition: LinkedCellsReferences.h:39
void forEach(Lambda forEachLambda, IteratorBehavior behavior=IteratorBehavior::ownedOrHaloOrDummy)
Execute code on all particles in this container as defined by a lambda function.
Definition: LinkedCellsReferences.h:420
LoadEstimatorOption _loadEstimator
load estimation algorithm for balanced traversals.
Definition: LinkedCellsReferences.h:630
void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override
Reserve memory for a given number of particles in the container and logic layers.
Definition: LinkedCellsReferences.h:73
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: LinkedCellsReferences.h:506
AutoPasLock addParticleLock
Workaround for adding particles in parallel -> https://github.com/AutoPas/AutoPas/issues/555.
Definition: LinkedCellsReferences.h:634
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: LinkedCellsReferences.h:198
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: LinkedCellsReferences.h:409
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: LinkedCellsReferences.h:227
void updateDirtyParticleReferences()
Updates all the References in the cells that are out of date.
Definition: LinkedCellsReferences.h:157
void addParticleImpl(const Particle_T &p) override
Adds a particle to the container.
Definition: LinkedCellsReferences.h:80
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: LinkedCellsReferences.h:298
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: LinkedCellsReferences.h:204
ContainerOption getContainerType() const override
Get the ContainerType.
Definition: LinkedCellsReferences.h:71
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, const std::array< double, 3 > &boxMinWithSafetyMargin, const 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: LinkedCellsReferences.h:569
TraversalSelectorInfo getTraversalSelectorInfo() const override
Generates a traversal selector info for this container.
Definition: LinkedCellsReferences.h:392
ParticleVector< Particle_T > _particleList
object that stores the actual Particles and keeps track of the references.
Definition: LinkedCellsReferences.h:622
internal::CellBlock3D< ParticleCellType > _cellBlock
object to manage the block of cells.
Definition: LinkedCellsReferences.h:626
BalancedTraversal::EstimatorFunction getLoadEstimatorFunction()
Generates the load estimation function depending on _loadEstimator.
Definition: LinkedCellsReferences.h:132
void rebuildNeighborLists(TraversalInterface *traversal) override
Rebuilds the neighbor lists for the next traversals.
Definition: LinkedCellsReferences.h:151
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: LinkedCellsReferences.h:400
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: LinkedCellsReferences.h:453
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: LinkedCellsReferences.h:462
bool deleteParticle(Particle_T &particle) override
Deletes the given particle as long as this does not compromise the validity of the container.
Definition: LinkedCellsReferences.h:289
internal::CellBlock3D< ParticleCellType > & getCellBlock()
Get the cell block, not supposed to be used except by verlet lists.
Definition: LinkedCellsReferences.h:539
Particle_T ParticleType
Type of the Particle.
Definition: LinkedCellsReferences.h:44
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: LinkedCellsReferences.h:473
void computeInteractions(TraversalInterface *traversal) override
Iterates over all particle multiples (e.g.
Definition: LinkedCellsReferences.h:177
std::vector< Particle_T > updateContainer(bool keepNeighborListsValid) override
Updates the container.
Definition: LinkedCellsReferences.h:304
void deleteAllParticles() override
Deletes all particles from the container.
Definition: LinkedCellsReferences.h:115
void deleteHaloParticles() override
Deletes all halo particles.
Definition: LinkedCellsReferences.h:123
void addHaloParticleImpl(const Particle_T &haloParticle) override
Adds a particle to the container that lies in the halo region of the container.
Definition: LinkedCellsReferences.h:90
LinkedCellsReferences(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: LinkedCellsReferences.h:61
std::vector< ParticleCellType > & getCells()
Returns reference to the data of LinkedCellsReferences.
Definition: LinkedCellsReferences.h:551
const internal::CellBlock3D< ParticleCellType > & getCellBlock() const
Get the cell block, not supposed to be used except by verlet lists.
Definition: LinkedCellsReferences.h:545
bool updateHaloParticle(const Particle_T &haloParticle) override
Update a halo particle of the container with the given haloParticle.
Definition: LinkedCellsReferences.h:100
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=IteratorBehavior::ownedOrHaloOrDummy)
Reduce properties of particles as defined by a lambda function.
Definition: LinkedCellsReferences.h:437
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 class handles the storage of particles in their full form.
Definition: ReferenceParticleCell.h:24
void addParticleReference(Particle_T *p)
Adds a Particle to the cell.
Definition: ReferenceParticleCell.h:55
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.
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
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
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
@ dummy
Dummy or deleted state, a particle with this state is not an actual particle!
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132