AutoPas  3.0.0
Loading...
Searching...
No Matches
FullParticleCell.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <mutex>
11#include <vector>
12
16#include "autopas/utils/SoA.h"
17#include "autopas/utils/inBox.h"
18
19namespace autopas {
20
25template <class Particle_T>
26class FullParticleCell : public ParticleCell<Particle_T> {
27 public:
31 using SoAArraysType = typename Particle_T::SoAArraysType;
32
36 using StorageType = std::vector<Particle_T>;
37
42 : _cellLength({std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
43 std::numeric_limits<double>::max()}) {}
44
49 explicit FullParticleCell(const std::array<double, 3> &cellLength) : _cellLength(cellLength) {}
50
51 void addParticle(const Particle_T &p) override {
52 std::lock_guard<AutoPasLock> guard(this->_cellLock);
53
54 // sanity check that ensures that only particles of the cells OwnershipState can be added. Note: if a cell is a
55 // dummy-cell, only dummies can be added, otherwise dummies can always be added
56 if ((not toInt64(p.getOwnershipState() & this->_ownershipState)) and
57 p.getOwnershipState() != OwnershipState::dummy) {
59 "FullParticleCell::addParticle() can not add a particle with OwnershipState {} to a cell with OwnershipState "
60 "{}",
61 p.getOwnershipState(), this->_ownershipState);
62 }
63
64 _particles.push_back(p);
65 }
66
74
81 }
82
90
95 [[nodiscard]] CellIterator<StorageType, false> end() const {
97 }
98
104 template <typename Lambda>
105 void forEach(Lambda forEachLambda) {
106 const std::array<double, 3> dummy{};
107 forEachImpl<false, false>(forEachLambda, dummy, dummy);
108 }
109
116 template <typename Lambda>
117 void forEach(Lambda forEachLambda, IteratorBehavior behavior) {
118 const std::array<double, 3> dummy{};
119 forEachImpl<true, false>(forEachLambda, dummy, dummy, behavior);
120 }
121
130 template <typename Lambda>
131 void forEach(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
132 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
133 forEachImpl<true, true>(forEachLambda, lowerCorner, higherCorner, behavior);
134 }
135
143 template <typename Lambda, typename A>
144 void reduce(Lambda reduceLambda, A &result) {
145 const std::array<double, 3> dummy{};
146 reduceImpl<false, false>(reduceLambda, result, dummy, dummy);
147 }
148
157 template <typename Lambda, typename A>
158 void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior) {
159 const std::array<double, 3> dummy{};
160 reduceImpl<true, false>(reduceLambda, result, dummy, dummy, behavior);
161 }
162
173 template <typename Lambda, typename A>
174 void reduce(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
175 const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
176 reduceImpl<true, true>(reduceLambda, result, lowerCorner, higherCorner, behavior);
177 }
178
183 [[nodiscard]] size_t size() const override { return _particles.size(); }
184
188 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
189 std::lock_guard<AutoPasLock> guard(this->_cellLock);
190 return std::count_if(_particles.begin(), _particles.end(), [&behavior](auto p) { return behavior.contains(p); });
191 }
192
198 Particle_T &operator[](size_t n) { return _particles[n]; }
199
205 const Particle_T &operator[](size_t n) const { return _particles[n]; }
206
212 Particle_T &at(size_t index) { return _particles.at(index); }
213
215
221 const Particle_T &at(size_t index) const { return _particles.at(index); }
222
223 [[nodiscard]] bool isEmpty() const override { return size() == 0; }
224
225 void clear() override { _particles.clear(); }
226
227 void deleteDummyParticles() override {
228 _particles.erase(
229 std::remove_if(_particles.begin(), _particles.end(), [](const auto &particle) { return particle.isDummy(); }),
230 _particles.end());
231 }
232
233 void deleteByIndex(size_t index) override {
234 std::lock_guard<AutoPasLock> lock(this->_cellLock);
235 if (index >= size()) {
236 utils::ExceptionHandler::exception("Index out of range (range: [0, {}[, index: {})", size(), index);
237 }
238
239 if (index < size() - 1) {
240 std::swap(_particles[index], _particles[size() - 1]);
241 }
242 _particles.pop_back();
243 }
244
245 void setCellLength(std::array<double, 3> &cellLength) override { _cellLength = cellLength; }
246
247 [[nodiscard]] std::array<double, 3> getCellLength() const override { return _cellLength; }
248
254 void resize(size_t n, const Particle_T &toInsert) { _particles.resize(n, toInsert); }
255
260 void sortByDim(const size_t dim) {
261 std::sort(_particles.begin(), _particles.end(),
262 [dim](const Particle_T &a, const Particle_T &b) -> bool { return a.getR()[dim] < b.getR()[dim]; });
263 }
264
269 void reserve(size_t n) { _particles.reserve(n); }
270
275
280
281 private:
282 std::array<double, 3> _cellLength;
283
284 template <bool ownershipCheck, bool regionCheck, typename Lambda>
285 void forEachImpl(Lambda forEachLambda, const std::array<double, 3> &lowerCorner,
286 const std::array<double, 3> &higherCorner,
287 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHaloOrDummy) {
288 for (Particle_T &p : _particles) {
289 if ((not ownershipCheck) or behavior.contains(p)) {
290 if ((not regionCheck) or utils::inBox(p.getR(), lowerCorner, higherCorner)) {
291 forEachLambda(p);
292 }
293 }
294 }
295 }
296
297 template <bool ownershipCheck, bool regionCheck, typename Lambda, typename A>
298 void reduceImpl(Lambda reduceLambda, A &result, const std::array<double, 3> &lowerCorner,
299 const std::array<double, 3> &higherCorner,
300 IteratorBehavior behavior = autopas::IteratorBehavior::ownedOrHaloOrDummy) {
301 for (Particle_T &p : _particles) {
302 if ((not ownershipCheck) or behavior.contains(p)) {
303 if ((not regionCheck) or utils::inBox(p.getR(), lowerCorner, higherCorner)) {
304 reduceLambda(p, result);
305 }
306 }
307 }
308 }
309};
310} // namespace autopas
Wraps the iterator of arbitrary cells to provide a common interface.
Definition: CellIterator.h:19
This class handles the storage of particles in their full form.
Definition: FullParticleCell.h:26
std::vector< Particle_T > StorageType
Type that holds or refers to the actual particles.
Definition: FullParticleCell.h:36
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: FullParticleCell.h:227
void deleteByIndex(size_t index) override
Deletes the index-th particle.
Definition: FullParticleCell.h:233
Particle_T & at(size_t index)
Returns the particle at position index.
Definition: FullParticleCell.h:212
StorageType _particles
Storage of the molecules of the cell.
Definition: FullParticleCell.h:274
size_t size() const override
Get the number of all particles stored in this cell (owned, halo and dummy).
Definition: FullParticleCell.h:183
void forEach(Lambda forEachLambda)
Executes code for every particle in this cell as defined by lambda function.
Definition: FullParticleCell.h:105
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: FullParticleCell.h:131
void sortByDim(const size_t dim)
Sort the particles in the cell by a dimension.
Definition: FullParticleCell.h:260
bool isEmpty() const override
Check if the cell is empty.
Definition: FullParticleCell.h:223
void clear() override
Deletes all particles in this cell.
Definition: FullParticleCell.h:225
typename Particle_T::SoAArraysType SoAArraysType
The structure of the SoAs is defined by the particle.
Definition: FullParticleCell.h:31
void reserve(size_t n)
Requests that the vector capacity be at least enough to contain n elements.
Definition: FullParticleCell.h:269
std::array< double, 3 > getCellLength() const override
Get the side lengths of this cell.
Definition: FullParticleCell.h:247
CellIterator< StorageType, false > end() const
Get an iterator to the end of a ParticleCell.
Definition: FullParticleCell.h:95
void setCellLength(std::array< double, 3 > &cellLength) override
Set the side lengths of this cell.
Definition: FullParticleCell.h:245
void addParticle(const Particle_T &p) override
Adds a Particle to the cell.
Definition: FullParticleCell.h:51
Particle_T & operator[](size_t n)
Returns a reference to the element at position n in the cell.
Definition: FullParticleCell.h:198
void forEach(Lambda forEachLambda, IteratorBehavior behavior)
Executes code for every particle in this cell as defined by lambda function.
Definition: FullParticleCell.h:117
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: FullParticleCell.h:174
CellIterator< StorageType, false > begin() const
Get an iterator to the start of a ParticleCell.
Definition: FullParticleCell.h:79
CellType getParticleCellTypeAsEnum() override
Get the ParticleCell type as an ParticleCellTypeEnum.
Definition: FullParticleCell.h:214
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: FullParticleCell.h:188
void reduce(Lambda reduceLambda, A &result)
Reduce properties of particles as defined by a lambda function.
Definition: FullParticleCell.h:144
void reduce(Lambda reduceLambda, A &result, IteratorBehavior behavior)
Reduce properties of particles as defined by a lambda function.
Definition: FullParticleCell.h:158
const Particle_T & operator[](size_t n) const
Returns a const reference to the element at position n in the cell.
Definition: FullParticleCell.h:205
CellIterator< StorageType, true > end()
Get an iterator to the end of a ParticleCell.
Definition: FullParticleCell.h:89
SoA< SoAArraysType > _particleSoABuffer
SoA buffer of this cell.
Definition: FullParticleCell.h:279
void resize(size_t n, const Particle_T &toInsert)
Resizes the container so that it contains n elements.
Definition: FullParticleCell.h:254
const Particle_T & at(size_t index) const
Returns the const particle at position index.
Definition: FullParticleCell.h:221
FullParticleCell(const std::array< double, 3 > &cellLength)
Constructs a new FullParticleCell with the given cell side length.
Definition: FullParticleCell.h:49
FullParticleCell()
Constructs a new FullParticleCell.
Definition: FullParticleCell.h:41
CellIterator< StorageType, true > begin()
Get an iterator to the start of a ParticleCell.
Definition: FullParticleCell.h:73
Class for Cells of Particles.
Definition: ParticleCell.h:51
AutoPasLock _cellLock
Lock object for exclusive access to this cell.
Definition: ParticleCell.h:184
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
@ FullParticleCell
FullParticleCell : Default cell type for almost everything.
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!