AutoPas  3.0.0
Loading...
Searching...
No Matches
ClusterTower.h
Go to the documentation of this file.
1
7#pragma once
8
14
15namespace autopas::internal {
16
42template <class Particle_T>
43class ClusterTower : public FullParticleCell<Particle_T> {
44 public:
48 ClusterTower() : _clusterSize(0) {}
49
54 explicit ClusterTower(size_t clusterSize) : _clusterSize(clusterSize) {}
55
57
61 void clear() override {
62 _clusters.clear();
64 _firstOwnedCluster = _clusters.end();
65 _firstTailHaloCluster = _clusters.end();
66 _numDummyParticles = 0;
67 }
68
79 if (getNumActualParticles() == 0) {
80 _clusters.resize(0, Cluster<Particle_T>(nullptr, _clusterSize));
81 _firstOwnedCluster = _clusters.end();
82 _firstTailHaloCluster = _clusters.end();
83 return 0;
84 }
85 this->sortByDim(2);
86
87 // if the number of particles is divisible by the cluster size this is 0
88 const auto sizeLastCluster = this->_particles.size() % _clusterSize;
89 _numDummyParticles = sizeLastCluster == 0 ? 0 : _clusterSize - sizeLastCluster;
90
91 // fill the last cluster with dummy copies of the last particle
92 auto lastParticle = this->_particles[this->_particles.size() - 1];
93 markParticleAsDeleted(lastParticle);
94 for (size_t i = 0; i < _numDummyParticles; i++) {
95 this->addParticle(lastParticle);
96 }
97
98 // Mark start of the different clusters by adding pointers to _particles
99 const size_t numClusters = this->_particles.size() / _clusterSize;
100 _clusters.resize(numClusters, Cluster<Particle_T>(nullptr, _clusterSize));
101 bool foundFirstOwnedCluster = false;
102 bool foundFirstTailHaloCluster = false;
103
104 const auto isAnyOfClusterOwned = [&](const auto &cluster) {
105 for (size_t i = 0; i < _clusterSize; ++i) {
106 if (cluster[i].isOwned()) {
107 return true;
108 }
109 }
110 return false;
111 };
112
113 for (size_t index = 0; index < numClusters; index++) {
114 _clusters[index].reset(&(this->_particles[_clusterSize * index]));
115
116 const bool clusterContainsOwnedParticles =
117 not foundFirstTailHaloCluster and isAnyOfClusterOwned(_clusters[index]);
118
119 // identify the range of owned clusters
120 if (not foundFirstOwnedCluster and clusterContainsOwnedParticles) {
121 _firstOwnedCluster = _clusters.begin() + index;
122 foundFirstOwnedCluster = true;
123 }
124 if (not foundFirstTailHaloCluster and foundFirstOwnedCluster and not clusterContainsOwnedParticles) {
125 _firstTailHaloCluster = _clusters.begin() + index;
126 foundFirstTailHaloCluster = true;
127 }
128 }
129 // for safety, make sure that worst case (e.g. no particles) the iterators point to reasonable positions
130 if (not foundFirstOwnedCluster) {
131 _firstOwnedCluster = _clusters.end();
132 }
133 if (not foundFirstTailHaloCluster) {
134 _firstTailHaloCluster = _clusters.end();
135 }
136
137 return getNumClusters();
138 }
139
147 void setDummyValues(double dummyStartX, double dummyDistZ) {
148 auto &lastCluster = getCluster(getNumClusters() - 1);
149 for (size_t index = 1; index <= _numDummyParticles; index++) {
150 auto &dummy = lastCluster[_clusterSize - index];
151 dummy = lastCluster[0]; // use first Particle in last cluster as dummy particle!
152 dummy.setOwnershipState(OwnershipState::dummy);
153 dummy.setR({dummyStartX, 0, dummyDistZ * static_cast<double>(index)});
154 dummy.setID(std::numeric_limits<size_t>::max());
155 }
156 }
157
163 if (_numDummyParticles > 0) {
164 auto &lastCluster = getCluster(getNumClusters() - 1);
165 auto lastActualParticle = lastCluster[_clusterSize - _numDummyParticles - 1];
166 for (size_t index = 1; index <= _numDummyParticles; index++) {
167 lastCluster[_clusterSize - index] = lastActualParticle;
168 }
169 }
170 }
171
177 template <class Functor>
178 void loadSoA(Functor &functor) {
179 functor.SoALoader(*this, this->_particleSoABuffer, 0, /*skipSoAResize*/ false);
180 for (size_t index = 0; index < getNumClusters(); index++) {
181 auto &cluster = getCluster(index);
182 cluster.setSoAView({&(this->_particleSoABuffer), index * _clusterSize, (index + 1) * _clusterSize});
183 }
184 }
185
191 template <class Functor>
192 void extractSoA(Functor &functor) {
193 functor.SoAExtractor(*this, this->_particleSoABuffer, 0);
194 }
195
202 std::vector<Particle_T> &&collectAllActualParticles() {
203 if (not this->_particles.empty()) {
204 // Workaround to remove requirement of default constructible particles.
205 // This function will always only shrink the array, particles are not actually inserted.
206 this->_particles.resize(getNumActualParticles(), this->_particles[0]);
207 }
208 return std::move(this->_particles);
209 }
210
221 std::vector<Particle_T> collectOutOfBoundsParticles(const std::array<double, 3> &boxMin,
222 const std::array<double, 3> &boxMax) {
223 // make sure to get rid of all dummies
225 // move all particles that are not in the tower to the back of the storage
226 const auto firstOutOfBoundsParticleIter =
227 std::partition(this->_particles.begin(), this->_particles.end(),
228 [&](const auto &p) { return utils::inBox(p.getR(), boxMin, boxMax); });
229
230 // copy out of bounds particles
231 std::vector<Particle_T> outOfBoundsParticles(firstOutOfBoundsParticleIter, this->_particles.end());
232 // shrink the particle storage so all out of bounds particles are cut away
233 this->_particles.resize(std::distance(this->_particles.begin(), firstOutOfBoundsParticleIter),
234 *this->_particles.begin());
235 return outOfBoundsParticles;
236 }
237
242 [[nodiscard]] size_t getNumTailDummyParticles() const { return _numDummyParticles; }
243
248 [[nodiscard]] size_t size() const override { return this->_particles.size(); }
249
255 [[nodiscard]] unsigned long getNumActualParticles() const {
256 return this->_particles.size() - getNumTailDummyParticles();
257 }
258
263 [[nodiscard]] size_t getNumClusters() const { return _clusters.size(); }
264
269 [[nodiscard]] auto &getClusters() { return _clusters; }
270
275 [[nodiscard]] const typename std::vector<autopas::internal::Cluster<Particle_T>>::iterator &getFirstOwnedCluster()
276 const {
277 return _firstOwnedCluster;
278 }
279
284 [[nodiscard]] size_t getFirstOwnedClusterIndex() const {
285 return std::distance(_clusters.cbegin(), decltype(_clusters.cbegin()){_firstOwnedCluster});
286 }
287
292 [[nodiscard]] const typename std::vector<autopas::internal::Cluster<Particle_T>>::iterator &getFirstTailHaloCluster()
293 const {
294 return _firstTailHaloCluster;
295 }
296
301 [[nodiscard]] size_t getFirstTailHaloClusterIndex() const {
302 return std::distance(_clusters.cbegin(), decltype(_clusters.cbegin()){_firstTailHaloCluster});
303 }
304
310 [[nodiscard]] auto &getCluster(size_t index) { return _clusters[index]; }
311
315 [[nodiscard]] auto &getCluster(size_t index) const { return _clusters[index]; }
316
317 [[nodiscard]] bool isEmpty() const override { return getNumActualParticles() == 0; }
318
319 void deleteDummyParticles() override {
320 // call super function to do the actual delete
322 _numDummyParticles = 0;
323 }
324
325 void deleteByIndex(size_t index) override {
329
330 // swap particle that should be deleted to end of actual particles.
331 std::swap(this->_particles[index], this->_particles[getNumActualParticles() - 1]);
332 if (getNumTailDummyParticles() != 0) {
333 // swap particle that should be deleted (now at end of actual particles) with last dummy particle.
334 std::swap(this->_particles[getNumActualParticles() - 1], this->_particles[this->_particles.size() - 1]);
335 }
336
337 this->_particles.pop_back();
338
339 if (_particleDeletionObserver) {
340 _particleDeletionObserver->notifyParticleDeleted();
341 }
342 }
343
344 void setCellLength(std::array<double, 3> &) override {
345 autopas::utils::ExceptionHandler::exception("ClusterTower::setCellLength(): Not supported!");
346 }
347
348 [[nodiscard]] std::array<double, 3> getCellLength() const override {
349 autopas::utils::ExceptionHandler::exception("ClusterTower::getCellLength(): Not supported!");
350 return {0, 0, 0};
351 }
352
357 void setClusterSize(size_t clusterSize) { _clusterSize = clusterSize; }
358
363 [[nodiscard]] size_t getClusterSize() const { return _clusterSize; }
364
370 _particleDeletionObserver = observer;
371 };
372
377 std::vector<Particle_T> &particleVector() { return this->_particles; }
378
379 private:
383 size_t _clusterSize;
384
388 std::vector<Cluster<Particle_T>> _clusters;
389
395 typename decltype(_clusters)::iterator _firstOwnedCluster{_clusters.end()};
400 typename decltype(_clusters)::iterator _firstTailHaloCluster{_clusters.end()};
401
405 size_t _numDummyParticles{};
406
407 internal::ParticleDeletedObserver *_particleDeletionObserver{nullptr};
408};
409} // namespace autopas::internal
This class handles the storage of particles in their full form.
Definition: FullParticleCell.h:26
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: FullParticleCell.h:227
StorageType _particles
Storage of the molecules of the cell.
Definition: FullParticleCell.h:274
void sortByDim(const size_t dim)
Sort the particles in the cell by a dimension.
Definition: FullParticleCell.h:260
void clear() override
Deletes all particles in this cell.
Definition: FullParticleCell.h:225
void addParticle(const Particle_T &p) override
Adds a Particle to the cell.
Definition: FullParticleCell.h:51
SoA< SoAArraysType > _particleSoABuffer
SoA buffer of this cell.
Definition: FullParticleCell.h:279
Functor base class.
Definition: Functor.h:41
void SoALoader(ParticleCell &cell, SoA< SoAArraysType > &soa, size_t offset, bool skipSoAResize)
Copies the AoS data of the given cell in the given soa.
Definition: Functor.h:127
void SoAExtractor(ParticleCell &cell, SoA< SoAArraysType > &soa, size_t offset)
Copies the data stored in the soa back into the cell.
Definition: Functor.h:141
This class represents one tower for clusters in the VerletClusterLists container.
Definition: ClusterTower.h:43
const std::vector< autopas::internal::Cluster< Particle_T > >::iterator & getFirstTailHaloCluster() const
Returns an iterator to the first particle after the owned clusters, that contains no owned particles ...
Definition: ClusterTower.h:292
void clear() override
Clears all particles from the tower and resets it to be ready for new particles.
Definition: ClusterTower.h:61
ClusterTower()
Dummy constructor.
Definition: ClusterTower.h:48
size_t size() const override
Get the number of all particles stored in this tower (owned, halo and dummy).
Definition: ClusterTower.h:248
auto & getCluster(size_t index) const
Returns the cluster at position index.
Definition: ClusterTower.h:315
const std::vector< autopas::internal::Cluster< Particle_T > >::iterator & getFirstOwnedCluster() const
Returns an iterator to the first cluster that contains at least one owned particle.
Definition: ClusterTower.h:275
void setCellLength(std::array< double, 3 > &) override
Set the side lengths of this cell.
Definition: ClusterTower.h:344
void deleteByIndex(size_t index) override
Deletes the index-th particle.
Definition: ClusterTower.h:325
std::array< double, 3 > getCellLength() const override
Get the side lengths of this cell.
Definition: ClusterTower.h:348
std::vector< Particle_T > & particleVector()
Get reference to internal particle vector.
Definition: ClusterTower.h:377
void loadSoA(Functor &functor)
Loads the particles into the SoA stored in this tower and generates the SoAView for each cluster.
Definition: ClusterTower.h:178
size_t getFirstOwnedClusterIndex() const
Returns the index of the first cluster that contains at least one owned particle.
Definition: ClusterTower.h:284
ClusterTower(size_t clusterSize)
Constructor.
Definition: ClusterTower.h:54
std::vector< Particle_T > collectOutOfBoundsParticles(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax)
Collect all particles that should not be in this tower based on their current position.
Definition: ClusterTower.h:221
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: ClusterTower.h:319
size_t generateClusters()
Generates the clusters for the particles in this cluster tower.
Definition: ClusterTower.h:78
CellType getParticleCellTypeAsEnum() override
Get the ParticleCell type as an ParticleCellTypeEnum.
Definition: ClusterTower.h:56
std::vector< Particle_T > && collectAllActualParticles()
Returns a rvalue reference to a std::vector containing all particles of this tower that are not dummi...
Definition: ClusterTower.h:202
unsigned long getNumActualParticles() const
Get the number of all particles saved in the tower without tailing dummies that are used to fill up c...
Definition: ClusterTower.h:255
void setParticleDeletionObserver(internal::ParticleDeletedObserver *observer)
Set the ParticleDeletionObserver, which is called, when a particle is deleted.
Definition: ClusterTower.h:369
auto & getClusters()
Returns a reference to the std::vector holding the clusters of this container.
Definition: ClusterTower.h:269
size_t getNumClusters() const
Returns the number of clusters in the tower.
Definition: ClusterTower.h:263
size_t getClusterSize() const
Get cluster size.
Definition: ClusterTower.h:363
bool isEmpty() const override
Check if the cell is empty.
Definition: ClusterTower.h:317
size_t getFirstTailHaloClusterIndex() const
Returns the index of the first particle after the owned clusters, that contains no owned particles an...
Definition: ClusterTower.h:301
void extractSoA(Functor &functor)
Extracts the SoA into the particles/clusters.
Definition: ClusterTower.h:192
void setDummyParticlesToLastActualParticle()
More or less inverse operation of setDummyValues().
Definition: ClusterTower.h:162
void setDummyValues(double dummyStartX, double dummyDistZ)
Replaces the copies of the last particle made in generateClusters() with dummies.
Definition: ClusterTower.h:147
size_t getNumTailDummyParticles() const
Returns the number of dummy particles in the tower that were inserted in the tower to fill the last c...
Definition: ClusterTower.h:242
void setClusterSize(size_t clusterSize)
Set cluster size.
Definition: ClusterTower.h:357
auto & getCluster(size_t index)
Returns the cluster at position index.
Definition: ClusterTower.h:310
This class represents a cluster in the VerletClusterLists container.
Definition: Cluster.h:26
Class that is notified when a particle is deleted.
Definition: ParticleDeletedObserver.h:15
virtual void notifyParticleDeleted()=0
This function is called when a particle is deleted.
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
This namespace is used for implementation specifics.
Definition: CellFunctor.h:21
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
CellType
The ParticleCell Type as an Enum.
Definition: ParticleCell.h:19
@ ClusterTower
ClusterTower : Tower for the 2D tower structure of VerletClusterLists.
@ dummy
Dummy or deleted state, a particle with this state is not an actual particle!