39template <
class Particle_T>
75 Octree(
const std::array<double, 3> &boxMin,
const std::array<double, 3> &boxMax,
const double cutoff,
76 const double skin,
const double cellSizeFactor,
const size_t aosSortingThreshold,
77 const size_t soaSortingThreshold)
79 soaSortingThreshold) {
80 using namespace autopas::utils::ArrayMath::literals;
82 if (cellSizeFactor != 1.0) {
86 "Trying to construct an Octree with CSF != 1.0! This should never occur as the LogicHandler "
87 "should reject this (as Configuration::hasCompatibleValues should return false).");
91 int unsigned treeSplitThreshold = 16;
100 auto haloBoxMin = boxMin - interactionLength;
101 auto haloBoxMax = boxMax + interactionLength;
111 [[nodiscard]] std::vector<ParticleType>
updateContainer(
bool keepNeighborListValid)
override {
113 std::vector<Particle_T> invalidParticles{};
115 if (keepNeighborListValid) {
125 std::vector<Particle_T *> particleRefs;
126 this->
_cells[CellTypes::OWNED].collectAllParticles(particleRefs);
127 std::vector<Particle_T> particles{};
128 particles.reserve(particleRefs.size());
130 for (
auto *p : particleRefs) {
134 }
else if (
utils::inBox(p->getR(), this->getBoxMin(), this->getBoxMax())) {
135 particles.push_back(*p);
137 invalidParticles.push_back(*p);
145 for (
auto &particle : particles) {
150 return invalidParticles;
155 traversalInterface->setCells(&this->
_cells);
166 [[nodiscard]] ContainerOption
getContainerType()
const override {
return ContainerOption::octree; }
168 void reserve(
size_t numParticles,
size_t numParticlesHaloEstimate)
override {
181 this->
_cells[CellTypes::HALO].addParticle(haloParticle);
194 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
195 IteratorBehavior iteratorBehavior,
196 const std::array<double, 3> &boxMin,
197 const std::array<double, 3> &boxMax)
const override {
198 return getParticleImpl<true>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
200 std::tuple<const Particle_T *, size_t, size_t>
getParticle(
size_t cellIndex,
size_t particleIndex,
201 IteratorBehavior iteratorBehavior)
const override {
203 constexpr std::array<double, 3> boxMin{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(),
204 std::numeric_limits<double>::lowest()};
206 constexpr std::array<double, 3> boxMax{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
207 std::numeric_limits<double>::max()};
208 return getParticleImpl<false>(cellIndex, particleIndex, iteratorBehavior, boxMin, boxMax);
228 template <
bool regionIter>
229 std::tuple<const ParticleType *, size_t, size_t>
getParticleImpl(
size_t cellIndex,
size_t particleIndex,
230 IteratorBehavior iteratorBehavior,
231 const std::array<double, 3> &boxMin,
232 const std::array<double, 3> &boxMax)
const {
233 using namespace autopas::utils::ArrayMath::literals;
237 return {
nullptr, 0, 0};
241 if (cellIndex == 0 and not(iteratorBehavior & IteratorBehavior::owned)) {
246 if (cellIndex < 10 and cellIndex > HALO) {
247 return {
nullptr, 0, 0};
250 std::array<double, 3> boxMinWithSafetyMargin = boxMin;
251 std::array<double, 3> boxMaxWithSafetyMargin = boxMax;
252 if constexpr (regionIter) {
258 std::vector<size_t> currentCellIndex{};
263 if (particleIndex >= currentCellPtr->
size() or
264 not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
265 (*currentCellPtr)[particleIndex], iteratorBehavior, boxMin, boxMax)) {
267 std::tie(currentCellPtr, particleIndex) =
268 advanceIteratorIndices<regionIter>(currentCellIndex, currentCellPtr, particleIndex, iteratorBehavior, boxMin,
269 boxMax, boxMinWithSafetyMargin, boxMaxWithSafetyMargin);
273 if (currentCellPtr ==
nullptr) {
274 return {
nullptr, 0, 0};
277 const Particle_T *retPtr = &((*currentCellPtr)[particleIndex]);
280 if (currentCellIndex.empty()) {
285 for (
int i =
static_cast<int>(currentCellIndex.size()) - 1; i >= 0; --i) {
287 cellIndex += currentCellIndex[i];
291 return {retPtr, cellIndex, particleIndex};
305 std::vector<size_t> currentCellIndex;
307 currentCellIndex.reserve(10);
308 currentCellIndex.push_back(cellIndex % 10);
313 currentCellIndex.push_back(cellIndex % 10);
315 currentCell = currentCell->
getChild(currentCellIndex.back());
324 if (particle.isOwned()) {
325 return this->
_cells[CellTypes::OWNED].deleteParticle(particle);
326 }
else if (particle.isHalo()) {
327 return this->
_cells[CellTypes::HALO].deleteParticle(particle);
336 auto &particleVec = cell->_particles;
337 auto &particle = particleVec[particleIndex];
339 particle = particleVec.back();
340 particleVec.pop_back();
341 return particleIndex < particleVec.size();
348 IteratorBehavior behavior,
357 IteratorBehavior behavior,
359 std::nullopt)
const override {
367 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
369 std::nullopt)
override {
377 const std::array<double, 3> &lowerCorner,
const std::array<double, 3> &higherCorner, IteratorBehavior behavior,
379 std::nullopt)
const override {
387 using namespace autopas::utils::ArrayMath::literals;
390 const std::array<unsigned long, 3> dims = {1, 1, 1};
399 [[nodiscard]]
size_t size()
const override {
400 return this->
_cells[CellTypes::OWNED].size() + this->
_cells[CellTypes::HALO].size();
407 return this->
_cells[CellTypes::OWNED].getNumberOfParticles(behavior) +
408 this->
_cells[CellTypes::HALO].getNumberOfParticles(behavior);
415 throw std::runtime_error(
"[Octree.h]: This cell container (octree) contains only two cells");
417 return i == CellTypes::HALO;
422 throw std::runtime_error(
"[Octree.h]: This cell container (octree) contains only two cells");
424 return i == CellTypes::OWNED;
433 template <
typename Lambda>
434 void forEach(Lambda forEachLambda, IteratorBehavior behavior = IteratorBehavior::ownedOrHalo) {
435 if (behavior & IteratorBehavior::owned) this->
_cells[OWNED].forEach(forEachLambda);
436 if (behavior & IteratorBehavior::halo) this->
_cells[HALO].forEach(forEachLambda);
437 if (not(behavior & IteratorBehavior::ownedOrHalo))
449 template <
typename Lambda,
typename A>
450 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior = IteratorBehavior::ownedOrHalo) {
451 if (behavior & IteratorBehavior::owned) this->
_cells[OWNED].reduce(reduceLambda, result);
452 if (behavior & IteratorBehavior::halo) this->
_cells[HALO].reduce(reduceLambda, result);
453 if (not(behavior & IteratorBehavior::ownedOrHalo))
460 template <
typename Lambda>
462 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
463 if (behavior & IteratorBehavior::owned)
464 this->
_cells[OWNED].forEachInRegion(forEachLambda, lowerCorner, higherCorner);
465 if (behavior & IteratorBehavior::halo) this->
_cells[HALO].forEachInRegion(forEachLambda, lowerCorner, higherCorner);
466 if (not(behavior & IteratorBehavior::ownedOrHalo))
473 template <
typename Lambda,
typename A>
474 void reduceInRegion(Lambda reduceLambda, A &result,
const std::array<double, 3> &lowerCorner,
475 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
476 if (behavior & IteratorBehavior::owned)
477 this->
_cells[OWNED].reduceInRegion(reduceLambda, result, lowerCorner, higherCorner);
478 if (behavior & IteratorBehavior::halo)
479 this->
_cells[HALO].reduceInRegion(reduceLambda, result, lowerCorner, higherCorner);
480 if (not(behavior & IteratorBehavior::ownedOrHalo))
500 template <
bool regionIter>
501 std::tuple<OctreeLeafNode<Particle_T> *,
size_t> advanceIteratorIndices(
503 size_t particleIndex, IteratorBehavior iteratorBehavior,
const std::array<double, 3> &boxMin,
504 const std::array<double, 3> &boxMax,
const std::array<double, 3> &boxMinWithSafetyMargin,
505 const std::array<double, 3> &boxMaxWithSafetyMargin)
const {
507 const size_t minLevel = 0;
517 bool isRelevant = cellPtr->
size() > 0;
518 if constexpr (regionIter) {
519 isRelevant =
utils::boxesOverlap(cellPtr->getBoxMin(), cellPtr->getBoxMax(), boxMinWithSafetyMargin,
520 boxMaxWithSafetyMargin);
532 bool forceJumpToNextCell =
false;
534 while (particleIndex >= currentCellInterfacePtr->
size() or forceJumpToNextCell) {
537 while (currentCellIndex.back() == 7) {
538 currentCellInterfacePtr = currentCellInterfacePtr->
getParent();
539 currentCellIndex.pop_back();
541 if (currentCellIndex.size() < minLevel or currentCellIndex.empty()) {
542 currentCellIndex.clear();
543 return {
nullptr, std::numeric_limits<
decltype(particleIndex)>::max()};
548 ++currentCellIndex.back();
550 if (currentCellIndex.size() == 1) {
552 if (currentCellIndex.back() > HALO) {
553 return {
nullptr, std::numeric_limits<
decltype(particleIndex)>::max()};
556 if ((iteratorBehavior & IteratorBehavior::halo)
558 currentCellInterfacePtr = this->
_cells[HALO].getRaw();
561 currentCellIndex.clear();
562 return {
nullptr, std::numeric_limits<
decltype(particleIndex)>::max()};
565 currentCellInterfacePtr = currentCellInterfacePtr->
getParent()->getChild(currentCellIndex.back());
568 if (not cellIsRelevant(currentCellInterfacePtr)) {
569 forceJumpToNextCell =
true;
573 forceJumpToNextCell =
false;
574 while (currentCellInterfacePtr->
hasChildren() and not forceJumpToNextCell) {
576 size_t firstRelevantChild = 0;
578 const auto *childToCheck = currentCellInterfacePtr->
getChild(firstRelevantChild);
579 while (not cellIsRelevant(childToCheck)) {
580 ++firstRelevantChild;
581 childToCheck = currentCellInterfacePtr->
getChild(firstRelevantChild);
582 if (firstRelevantChild > 7) {
586 forceJumpToNextCell =
true;
590 currentCellIndex.push_back(firstRelevantChild);
591 currentCellInterfacePtr = currentCellInterfacePtr->
getChild(firstRelevantChild);
599 if (currentLeafCellPtr ==
nullptr) {
603 }
while (not containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(
604 (*currentLeafCellPtr)[particleIndex], iteratorBehavior, boxMin, boxMax));
605 return {currentLeafCellPtr, particleIndex};
The CellBasedParticleContainer class stores particles in some object and provides methods to iterate ...
Definition: CellBasedParticleContainer.h:25
const std::array< double, 3 > & getBoxMax() const final
Get the upper corner of the container without halo.
Definition: CellBasedParticleContainer.h:74
double getVerletSkin() const final
Returns the verlet Skin length.
Definition: CellBasedParticleContainer.h:99
void deleteAllParticles() override
Deletes all particles from the container.
Definition: CellBasedParticleContainer.h:104
const std::array< double, 3 > & getBoxMin() const final
Get the lower corner of the container without halo.
Definition: CellBasedParticleContainer.h:79
double getInteractionLength() const final
Return the interaction length (cutoff+skin) of the container.
Definition: CellBasedParticleContainer.h:94
std::vector< ParticleCellType > _cells
Vector of particle cells.
Definition: CellBasedParticleContainer.h:162
Public iterator class that iterates over a particle container and additional vectors (which are typic...
Definition: ContainerIterator.h:95
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:108
This interface exists to provide a row interface for octree to add its cells.
Definition: OTTraversalInterface.h:22
An octree leaf node.
Definition: OctreeLeafNode.h:27
size_t size() const override
Get the total number of particles saved in the container (owned + halo + dummy).
Definition: OctreeLeafNode.h:147
Log an octree to a .vtk file.
Definition: OctreeLogger.h:25
The base class that provides the necessary function definitions that can be applied to an octree.
Definition: OctreeNodeInterface.h:32
virtual OctreeNodeInterface< Particle_T > * getChild(int index)=0
Get a child by its index from the node.
OctreeNodeInterface< Particle_T > * getParent() const
Get the parent node of this node.
Definition: OctreeNodeInterface.h:376
virtual size_t size() const =0
Get the total number of particles saved in the container (owned + halo + dummy).
virtual bool hasChildren()=0
Check if the node is a leaf or an inner node.
This class wraps the functionality provided by the octree leaves and inner nodes in a structure that ...
Definition: OctreeNodeWrapper.h:37
typename ParticleCell::ParticleType ParticleType
The contained particle type.
Definition: OctreeNodeWrapper.h:46
The octree is a CellBasedParticleContainer that consists internally of two octrees.
Definition: Octree.h:41
static constexpr size_t invalidCellIndex
A cell index that is definitely always invalid.
Definition: Octree.h:63
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: Octree.h:461
std::tuple< std::vector< size_t >, OctreeLeafNode< Particle_T > * > getLeafCellByIndex(size_t cellIndex) const
Helper function to retrieve the pointer to a leaf cell as well as properly parse the cell index.
Definition: Octree.h:303
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: Octree.h:376
bool cellCanContainOwnedParticles(std::size_t i) const override
Checks if the cell with the one-dimensional index index1d can contain owned particles.
Definition: Octree.h:420
ContainerIterator< Particle_T, false, false > begin(IteratorBehavior behavior, 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: Octree.h:356
void reserve(size_t numParticles, size_t numParticlesHaloEstimate) override
Reserve memory for a given number of particles in the container and logic layers.
Definition: Octree.h:168
void rebuildNeighborLists(TraversalInterface *traversal) override
Rebuilds the neighbor lists for the next traversals.
Definition: Octree.h:192
bool cellCanContainHaloParticles(std::size_t i) const override
Checks if the cell with the one-dimensional index index1d can contain halo particles.
Definition: Octree.h:413
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Reduce properties of particles as defined by a lambda function.
Definition: Octree.h:450
CellTypes
This particle container contains two cells.
Definition: Octree.h:58
void computeInteractions(TraversalInterface *traversal) override
Iterates over all particle multiples (e.g.
Definition: Octree.h:153
Octree(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, const double cutoff, const double skin, const double cellSizeFactor, const size_t aosSortingThreshold, const size_t soaSortingThreshold)
Construct a new octree with two sub-octrees: One for the owned particles and one for the halo particl...
Definition: Octree.h:75
void addHaloParticleImpl(const ParticleType &haloParticle) override
Adds a particle to the container that lies in the halo region of the container.
Definition: Octree.h:180
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: Octree.h:406
bool updateHaloParticle(const ParticleType &haloParticle) override
Update a halo particle of the container with the given haloParticle.
Definition: Octree.h:187
typename ParticleCellType::ParticleType ParticleType
The particle type used in this container.
Definition: Octree.h:51
void forEach(Lambda forEachLambda, IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Execute code on all particles in this container as defined by a lambda function.
Definition: Octree.h:434
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: Octree.h:194
size_t size() const override
Get the total number of particles saved in the container (owned + halo + dummy).
Definition: Octree.h:399
void deleteHaloParticles() override
Deletes all halo particles.
Definition: Octree.h:411
void addParticleImpl(const ParticleType &p) override
Adds a particle to the container.
Definition: Octree.h:175
std::vector< ParticleType > updateContainer(bool keepNeighborListValid) override
Updates the container.
Definition: Octree.h:111
TraversalSelectorInfo getTraversalSelectorInfo() const override
Generates a traversal selector info for this container.
Definition: Octree.h:386
std::tuple< const ParticleType *, 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: Octree.h:229
ContainerIterator< Particle_T, true, false > begin(IteratorBehavior behavior, utils::optRef< typename ContainerIterator< Particle_T, true, false >::ParticleVecType > additionalVectors) override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: Octree.h:347
bool deleteParticle(Particle_T &particle) override
Deletes the given particle as long as this does not compromise the validity of the container.
Definition: Octree.h:323
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: Octree.h:366
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: Octree.h:200
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: Octree.h:474
ContainerOption getContainerType() const override
Get the ContainerType.
Definition: Octree.h:166
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: Octree.h:334
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
Interface class to handle cell borders and cell types of cells.
Definition: CellBorderAndFlagManager.h:17
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
static bool checkParticleInCellAndUpdateByIDAndPosition(CellType &cell, const typename CellType::ParticleType &particle, double absError)
Same as checkParticleInCellAndUpdateByID(CellType, ParticleType), but additionally checks whether the...
Definition: ParticleCellHelpers.h:39
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
@ halo
Halo state, a particle with this state is an actual particle, but not owned by the current AutoPas ob...
@ owned
Owned state, a particle with this state is an actual particle and owned by the current AutoPas object...
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132