AutoPas  3.0.0
Loading...
Searching...
No Matches
ReferenceParticleCell.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <mutex>
10#include <vector>
11
14#include "autopas/utils/SoA.h"
16
17namespace autopas {
18
23template <class Particle_T>
24class ReferenceParticleCell : public ParticleCell<Particle_T> {
25 public:
29 using SoAArraysType = typename Particle_T::SoAArraysType;
33 using StorageType = std::vector<Particle_T *>;
34
39 : _cellLength({std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
40 std::numeric_limits<double>::max()}) {}
41
46 explicit ReferenceParticleCell(const std::array<double, 3> &cellLength) : _cellLength(cellLength) {}
47
48 void addParticle(const Particle_T &p) override {
49 autopas::utils::ExceptionHandler::exception("Should use addParticleReference instead");
50 }
51
55 void addParticleReference(Particle_T *p) {
56 // sanity check that ensures that only particles of the cells OwnershipState can be added. Note: is a cell is a
57 // dummy-cell, only dummies can be added, otherwise dummies can always be added
58 if ((not toInt64(p->getOwnershipState() & this->_ownershipState)) and
59 p->getOwnershipState() != OwnershipState::dummy) {
61 "ReferenceParticleCell::addParticleReference() can not add a particle with OwnershipState {} to a cell with "
62 "OwnershipState {}",
63 p->getOwnershipState(), this->_ownershipState);
64 }
65
66 std::lock_guard<AutoPasLock> guard(this->_cellLock);
67 _particles.push_back(p);
68 }
69
74
81 }
82
87
92 [[nodiscard]] CellIterator<StorageType, false> end() const {
94 }
95
102 template <typename Lambda>
103 void forEach(Lambda forEachLambda, IteratorBehavior behavior) {
104 const std::array<double, 3> dummy{};
105 forEachImpl<false>(forEachLambda, dummy, dummy, behavior);
106 }
107
116 template <typename Lambda>
117 void forEach(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
118 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
119 forEachImpl<true>(forEachLambda, lowerCorner, higherCorner, behavior);
120 }
129 template <typename Lambda, typename A>
130 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior) {
131 const std::array<double, 3> dummy{};
132 reduceImpl<true, false>(reduceLambda, result, dummy, dummy, behavior);
133 }
134
145 template <typename Lambda, typename A>
146 void reduce(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
147 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
148 reduceImpl<true, true>(reduceLambda, result, lowerCorner, higherCorner, behavior);
149 }
150
155 [[nodiscard]] size_t size() const override { return _particles.size(); }
156
160 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
161 std::lock_guard<AutoPasLock> guard(this->_cellLock);
162
163 size_t numParticles{0};
164 numParticles =
165 std::count_if(_particles.begin(), _particles.end(), [&behavior](auto p) { return behavior.contains(*p); });
166
167 return numParticles;
168 }
169
175 Particle_T &operator[](size_t n) { return *(_particles[n]); }
176
181
187 const Particle_T &operator[](size_t n) const { return *(_particles[n]); }
188
194 [[nodiscard]] Particle_T &at(size_t index) { return *(_particles.at(index)); }
195
201 [[nodiscard]] const Particle_T &at(size_t index) const { return *(_particles.at(index)); }
202
203 [[nodiscard]] bool isEmpty() const override { return size() == 0; }
204
205 void clear() override { _particles.clear(); }
206
207 void deleteDummyParticles() override {
208 _particles.erase(
209 std::remove_if(_particles.begin(), _particles.end(), [](const auto &particle) { return particle->isDummy(); }),
210 _particles.end());
211 }
212
213 void deleteByIndex(size_t index) override {
214 std::lock_guard<AutoPasLock> lock(this->_cellLock);
215 if (index >= size()) {
216 utils::ExceptionHandler::exception("Index out of range (range: [0, {}[, index: {})", size(), index);
217 }
218
219 _particles[index]->setOwnershipState(OwnershipState::dummy);
220
221 if (index < size() - 1) {
222 std::swap(_particles[index], _particles[size() - 1]);
223 }
224 _particles.pop_back();
225 }
226
227 void setCellLength(std::array<double, 3> &cellLength) override { _cellLength = cellLength; }
228
229 [[nodiscard]] std::array<double, 3> getCellLength() const override { return _cellLength; }
230
236 void resize(size_t n, const Particle_T &toInsert) { _particles.resize(n, std::unique_ptr<Particle_T>(toInsert)); }
237
242 void sortByDim(const size_t dim) {
243 std::sort(_particles.begin(), _particles.end(),
244 [dim](const auto *a, const auto *b) -> bool { return a->getR()[dim] < b->getR()[dim]; });
245 }
246
251 void reserve(size_t n) { _particles.reserve(n); }
252
257
262
263 private:
264 std::array<double, 3> _cellLength;
265
266 template <bool regionCheck, typename Lambda>
267 void forEachImpl(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
268 const std::array<double, 3> &higherCorner,
269 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHaloOrDummy) {
270 for (Particle_T *p : _particles) {
271 if (behavior.contains(*p)) {
272 if ((not regionCheck) or utils::inBox(p->getR(), lowerCorner, higherCorner)) {
273 forEachLambda(*p);
274 }
275 }
276 }
277 }
278
279 template <bool ownershipCheck, bool regionCheck, typename Lambda, typename A>
280 void reduceImpl(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
281 const std::array<double, 3> &higherCorner,
282 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHaloOrDummy) {
283 for (Particle_T *p : _particles) {
284 if ((not ownershipCheck) or behavior.contains(*p)) {
285 if ((not regionCheck) or utils::inBox(p->getR(), lowerCorner, higherCorner)) {
286 reduceLambda(*p, result);
287 }
288 }
289 }
290 }
291};
292} // namespace autopas
Wraps the iterator of arbitrary cells to provide a common interface.
Definition: CellIterator.h:19
Class for Cells of Particles.
Definition: ParticleCell.h:51
AutoPasLock _cellLock
Lock object for exclusive access to this cell.
Definition: ParticleCell.h:184
This class handles the storage of particles in their full form.
Definition: ReferenceParticleCell.h:24
void clear() override
Deletes all particles in this cell.
Definition: ReferenceParticleCell.h:205
ReferenceParticleCell(const std::array< double, 3 > &cellLength)
Constructs a new ReferenceParticleCell with the given cell side length.
Definition: ReferenceParticleCell.h:46
std::vector< Particle_T * > StorageType
Type that holds or refers to the actual particles.
Definition: ReferenceParticleCell.h:33
Particle_T & at(size_t index)
Returns the particle at position index.
Definition: ReferenceParticleCell.h:194
void deleteByIndex(size_t index) override
Deletes the index-th particle.
Definition: ReferenceParticleCell.h:213
bool isEmpty() const override
Check if the cell is empty.
Definition: ReferenceParticleCell.h:203
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: ReferenceParticleCell.h:160
void addParticleReference(Particle_T *p)
Adds a Particle to the cell.
Definition: ReferenceParticleCell.h:55
void forEach(Lambda forEachLambda, IteratorBehavior behavior)
Executes code for every particle in this cell as defined by lambda function.
Definition: ReferenceParticleCell.h:103
StorageType _particles
Storage of the molecules of the cell.
Definition: ReferenceParticleCell.h:256
void setCellLength(std::array< double, 3 > &cellLength) override
Set the side lengths of this cell.
Definition: ReferenceParticleCell.h:227
void forEach(Lambda forEachLambda, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior)
Executes code for every particle in this cell as defined by lambda function.
Definition: ReferenceParticleCell.h:117
CellIterator< StorageType, false > end() const
Get an iterator to the end of a ParticleCell.
Definition: ReferenceParticleCell.h:92
void reserve(size_t n)
Requests that the vector capacity be at least enough to contain n elements.
Definition: ReferenceParticleCell.h:251
void resize(size_t n, const Particle_T &toInsert)
Resizes the container so that it contains n elements.
Definition: ReferenceParticleCell.h:236
const Particle_T & at(size_t index) const
Returns the const particle at position index.
Definition: ReferenceParticleCell.h:201
size_t size() const override
Get the number of all particles stored in this cell (owned, halo and dummy).
Definition: ReferenceParticleCell.h:155
void reduce(Lambda reduceLambda, A &result, const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior)
Reduce properties of particles as defined by a lambda function.
Definition: ReferenceParticleCell.h:146
SoA< SoAArraysType > _particleSoABuffer
SoA buffer of this cell.
Definition: ReferenceParticleCell.h:261
CellIterator< StorageType, true > end()
Get an iterator to the end of a ParticleCell.
Definition: ReferenceParticleCell.h:86
CellIterator< StorageType, false > begin() const
Get an iterator to the start of a ParticleCell.
Definition: ReferenceParticleCell.h:79
void addParticle(const Particle_T &p) override
Adds a Particle to the cell.
Definition: ReferenceParticleCell.h:48
ReferenceParticleCell()
Constructs a new ReferenceParticleCell.
Definition: ReferenceParticleCell.h:38
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: ReferenceParticleCell.h:207
CellType getParticleCellTypeAsEnum() override
Get the ParticleCell type as an ParticleCellTypeEnum.
Definition: ReferenceParticleCell.h:180
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior)
Reduce properties of particles as defined by a lambda function.
Definition: ReferenceParticleCell.h:130
const Particle_T & operator[](size_t n) const
Returns a const reference to the element at position n in the cell.
Definition: ReferenceParticleCell.h:187
void sortByDim(const size_t dim)
Sort the particles in the cell by a dimension.
Definition: ReferenceParticleCell.h:242
CellIterator< StorageType, true > begin()
Get an iterator to the start of a ParticleCell.
Definition: ReferenceParticleCell.h:73
std::array< double, 3 > getCellLength() const override
Get the side lengths of this cell.
Definition: ReferenceParticleCell.h:229
typename Particle_T::SoAArraysType SoAArraysType
The structure of the SoAs is defined by the particle.
Definition: ReferenceParticleCell.h:29
Particle_T & operator[](size_t n)
Returns a reference to the element at position n in the cell.
Definition: ReferenceParticleCell.h:175
Structur of the array class.
Definition: SoA.h:28
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
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
CellType
The ParticleCell Type as an Enum.
Definition: ParticleCell.h:19
@ ReferenceParticleCell
ReferenceParticleCell : Cell holding only references instead of actual particle objects.
constexpr int64_t toInt64(const OwnershipState a)
Returns the int64_t value of a given OwnershipState.
Definition: OwnershipState.h:56
@ dummy
Dummy or deleted state, a particle with this state is not an actual particle!