AutoPas  3.0.0
Loading...
Searching...
No Matches
OctreeNodeWrapper.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <memory>
10
15
16namespace autopas {
36template <typename Particle_T>
37class OctreeNodeWrapper : public ParticleCell<Particle_T> {
38 public:
50 using StorageType = std::vector<Particle_T *>;
51
61 OctreeNodeWrapper(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax,
62 int unsigned const treeSplitThreshold, double const interactionLength,
63 double const cellSizeFactor) {
64 _pointer = std::make_unique<OctreeLeafNode<Particle_T>>(boxMin, boxMax, nullptr, treeSplitThreshold,
65 interactionLength, cellSizeFactor);
66 }
67
73 std::lock_guard<AutoPasLock> lock(_lock);
74 _pointer->collectAllParticles(ps);
75 }
76
81 void appendAllLeaves(std::vector<OctreeLeafNode<Particle_T> *> &leaves) { _pointer->appendAllLeaves(leaves); }
82
87 void addParticle(const Particle_T &p) override {
88 std::lock_guard<AutoPasLock> lock(_lock);
89
90 auto ret = _pointer->insert(p);
91 if (ret) _pointer = std::move(ret);
92
93 ++_enclosedParticleCount;
94 }
95
103 std::lock_guard<AutoPasLock> lock(_lock);
104 _ps.clear();
105 _pointer->collectAllParticles(_ps);
106 return CellIterator<StorageType, true>(_ps.begin());
107 }
108
114 std::lock_guard<AutoPasLock> lock(_lock);
115 _ps.clear();
116 _pointer->collectAllParticles(_ps);
117 return CellIterator<StorageType, false>(_ps.cbegin());
118 }
119
129
134 [[nodiscard]] size_t size() const override {
135 std::lock_guard<AutoPasLock> lock(_lock);
136 return _enclosedParticleCount;
137 }
138
142 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
143 std::lock_guard<AutoPasLock> lock(_lock);
144 return _pointer->getNumberOfParticles(behavior);
145 }
146
151 [[nodiscard]] bool isEmpty() const override {
152 std::lock_guard<AutoPasLock> lock(_lock);
153 return _enclosedParticleCount == 0;
154 }
155
159 void clear() override {
160 std::lock_guard<AutoPasLock> lock(_lock);
161 _pointer->clearChildren(_pointer);
162 _enclosedParticleCount = 0;
163 }
164
168 void deleteDummyParticles() override {}
169
175
182 bool deleteParticle(Particle_T &particle) {
183 --_enclosedParticleCount;
184
185 return _pointer->deleteParticle(particle);
186 };
187
192 void deleteByIndex(size_t index) override {
193 throw std::runtime_error("[OctreeNodeWrapper::deleteByIndex()] Operation not supported");
194 }
195
200 void setCellLength(std::array<double, 3> &cellLength) override {
201 throw std::runtime_error("[OctreeNodeWrapper::setCellLength()] Operation not supported");
202 }
203
208 [[nodiscard]] std::array<double, 3> getCellLength() const override {
209 using namespace autopas::utils::ArrayMath::literals;
210 return _pointer->getBoxMin() - _pointer->getBoxMax();
211 }
212
219 Particle_T &at(size_t index) {
220 std::lock_guard<AutoPasLock> lock(_lock);
221 return _ps.at(index);
222 }
223
230 const Particle_T &at(size_t index) const {
231 std::lock_guard<AutoPasLock> lock(_lock);
232 return _ps.at(index);
233 }
234
241 Particle_T &operator[](size_t index) {
242 std::lock_guard<AutoPasLock> lock(_lock);
243 return *_ps[index];
244 }
245
252 const Particle_T &operator[](size_t index) const {
253 std::lock_guard<AutoPasLock> lock(_lock);
254 return *_ps[index];
255 }
256
263 std::set<OctreeLeafNode<Particle_T> *> getLeavesInRange(const std::array<double, 3> &min,
264 const std::array<double, 3> &max) {
265 return _pointer->getLeavesInRange(min, max);
266 }
267
273 OctreeNodeInterface<Particle_T> *getRaw() const { return _pointer.get(); }
274
281 template <typename Lambda>
282 void forEach(Lambda forEachLambda) {
283 withStaticNodeType(_pointer, [&](auto nodePtr) { nodePtr->forEach(forEachLambda); });
284 }
285
294 template <typename Lambda, typename A>
295 void reduce(Lambda reduceLambda, A &result) {
296 withStaticNodeType(_pointer, [&](auto nodePtr) { nodePtr->reduce(reduceLambda, result); });
297 }
298
307 template <typename Lambda>
308 void forEachInRegion(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
309 const std::array<double, 3> &higherCorner) {
310 withStaticNodeType(_pointer, [&](auto nodePtr) {
311 // The iterator behavior is set to ownedOrHalo to include all particles inside this subtree. The baseclass
312 // (Octree) decides whether this instance of OctreeNodeWrapper should be included in the iteration or not.
313 nodePtr->forEach(forEachLambda, lowerCorner, higherCorner, IteratorBehavior::ownedOrHalo);
314 });
315 }
316
327 template <typename Lambda, typename A>
328 void reduceInRegion(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
329 const std::array<double, 3> &higherCorner) {
330 withStaticNodeType(_pointer, [&](auto nodePtr) {
331 // The iterator behavior is set to ownedOrHalo to include all particles inside this subtree. The baseclass
332 // (Octree) decides whether this instance of OctreeNodeWrapper should be included in the iteration or not.
333 nodePtr->reduce(reduceLambda, result, lowerCorner, higherCorner, IteratorBehavior::ownedOrHalo);
334 });
335 }
336
337 private:
341 std::unique_ptr<OctreeNodeInterface<Particle_T>> _pointer;
342
349 mutable StorageType _ps;
350
355 mutable AutoPasLock _lock;
356
361 long _enclosedParticleCount{0L};
362};
363} // namespace autopas
Wraps the iterator of arbitrary cells to provide a common interface.
Definition: CellIterator.h:19
AutoPasLock for the sequential case.
Definition: WrapOpenMP.h:155
An octree leaf node.
Definition: OctreeLeafNode.h:27
The base class that provides the necessary function definitions that can be applied to an octree.
Definition: OctreeNodeInterface.h:32
This class wraps the functionality provided by the octree leaves and inner nodes in a structure that ...
Definition: OctreeNodeWrapper.h:37
void appendAllLeaves(std::vector< OctreeLeafNode< Particle_T > * > &leaves)
Append all leaves in the octree to a list.
Definition: OctreeNodeWrapper.h:81
std::array< double, 3 > getCellLength() const override
Get the side lengths of this cell.
Definition: OctreeNodeWrapper.h:208
Particle_T & operator[](size_t index)
Get a particle from the iterator.
Definition: OctreeNodeWrapper.h:241
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: OctreeNodeWrapper.h:142
void clear() override
Deletes all particles in this cell.
Definition: OctreeNodeWrapper.h:159
void reduceInRegion(Lambda reduceLambda, A &result, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner)
Apply the reduce lambda to each particle in the region.
Definition: OctreeNodeWrapper.h:328
CellType getParticleCellTypeAsEnum() override
Get the ParticleCell type as an ParticleCellTypeEnum.
Definition: OctreeNodeWrapper.h:174
bool isEmpty() const override
Check if the cell is not empty.
Definition: OctreeNodeWrapper.h:151
const Particle_T & operator[](size_t index) const
Get a particle from the iterator.
Definition: OctreeNodeWrapper.h:252
std::set< OctreeLeafNode< Particle_T > * > getLeavesInRange(const std::array< double, 3 > &min, const std::array< double, 3 > &max)
Find all leaves below this subtree that are in the given range.
Definition: OctreeNodeWrapper.h:263
void reduce(Lambda reduceLambda, A &result)
Apply the reduce lambda to each particle.
Definition: OctreeNodeWrapper.h:295
bool deleteParticle(Particle_T &particle)
Delete the given particle from the data structure.
Definition: OctreeNodeWrapper.h:182
CellIterator< StorageType, false > begin() const
Get an iterator to the start of a ParticleCell.
Definition: OctreeNodeWrapper.h:113
void setCellLength(std::array< double, 3 > &cellLength) override
Set the side lengths of this cell.
Definition: OctreeNodeWrapper.h:200
void collectAllParticles(StorageType &ps)
Append all particles in the octree to a list using DFS.
Definition: OctreeNodeWrapper.h:72
CellIterator< StorageType, false > end() const
Get an iterator to the end of a ParticleCell.
Definition: OctreeNodeWrapper.h:128
OctreeNodeWrapper(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, int unsigned const treeSplitThreshold, double const interactionLength, double const cellSizeFactor)
Constructs a new, empty octree and stores the root.
Definition: OctreeNodeWrapper.h:61
size_t size() const override
Get the number of all particles stored in this cell (owned, halo and dummy).
Definition: OctreeNodeWrapper.h:134
void forEach(Lambda forEachLambda)
Apply the forEach lambda to each particle.
Definition: OctreeNodeWrapper.h:282
const Particle_T & at(size_t index) const
Get a particle from the iterator.
Definition: OctreeNodeWrapper.h:230
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: OctreeNodeWrapper.h:168
CellIterator< StorageType, true > end()
Get an iterator to the end of a ParticleCell.
Definition: OctreeNodeWrapper.h:123
CellIterator< StorageType, true > begin()
Get an iterator to the start of a ParticleCell.
Definition: OctreeNodeWrapper.h:102
void deleteByIndex(size_t index) override
Deletes the index-th particle.
Definition: OctreeNodeWrapper.h:192
Particle_T & at(size_t index)
Get a particle from the iterator.
Definition: OctreeNodeWrapper.h:219
std::vector< Particle_T * > StorageType
Type that holds or refers to the actual particles.
Definition: OctreeNodeWrapper.h:50
OctreeNodeInterface< Particle_T > * getRaw() const
Get a raw pointer to the enclosed cell.
Definition: OctreeNodeWrapper.h:273
void addParticle(const Particle_T &p) override
Adds a Particle to the cell.
Definition: OctreeNodeWrapper.h:87
void forEachInRegion(Lambda forEachLambda, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner)
Apply the forEach lambda to each particle in the region.
Definition: OctreeNodeWrapper.h:308
typename ParticleCell::ParticleType ParticleType
The contained particle type.
Definition: OctreeNodeWrapper.h:46
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
CellType
The ParticleCell Type as an Enum.
Definition: ParticleCell.h:19
@ FullParticleCell
FullParticleCell : Default cell type for almost everything.
decltype(auto) withStaticNodeType(const std::unique_ptr< OctreeNodeInterface< Particle_T > > &root, FunctionType &&function)
Will execute the passed function on the given root node.
Definition: OctreeStaticNodeSelector.h:29