AutoPas  3.0.0
Loading...
Searching...
No Matches
SortedCellView.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <vector>
10
14#include "autopas/utils/SoA.h"
16
17namespace autopas {
18
27template <class ParticleCellType>
28class SortedCellView : public ParticleCell<typename ParticleCellType::ParticleType> {
29 public:
33 using ParticleType = typename ParticleCellType::ParticleType;
37 using StorageType = std::vector<ParticleType *>;
43 SortedCellView(ParticleCellType &cell, const std::array<double, 3> &r) : _cell(&cell) {
44 _particles.reserve(cell.size());
45 for (auto &p : cell) {
46 _particles.push_back(std::make_pair(utils::ArrayMath::dot(p.getR(), r), &p));
47 }
48 std::sort(_particles.begin(), _particles.end(),
49 [](const auto &a, const auto &b) -> bool { return a.first < b.first; });
50
51 this->setPossibleParticleOwnerships(cell.getPossibleParticleOwnerships());
52 }
53
55
59 void addParticle(const ParticleType &p) override {}
60
67 }
68
76 }
77
83 }
84
91 }
92
97 size_t size() const override { return _particles.size(); }
98
102 [[nodiscard]] size_t getNumberOfParticles(IteratorBehavior behavior) const override {
103 return std::count_if(_particles.begin(), _particles.end(),
104 [&behavior](auto pair) { return behavior.contains(*(std::get<1>(pair))); });
105 }
106
107 bool isEmpty() const override { return size() == 0; }
108
109 void clear() override { _particles.clear(); }
110
111 void deleteDummyParticles() override {
112 _particles.erase(std::remove_if(_particles.begin(), _particles.end(),
113 [](const auto &particlePosPair) { return particlePosPair.second->isDummy(); }),
114 _particles.end());
115 }
116
117 void deleteByIndex(size_t index) override {
118 if (index >= size()) {
119 AutoPasLog(ERROR, "Index out of range");
120 utils::ExceptionHandler::exception("Error: Index out of range");
121 }
122
123 if (index < size() - 1) {
124 std::swap(_particles[index], _particles[size() - 1]);
125 }
126 _particles.pop_back();
127 }
128
129 void setCellLength(std::array<double, 3> &cellLength) override { _cell->setCellLength(cellLength); }
130
131 std::array<double, 3> getCellLength() const override { return _cell->getCellLength(); }
132
138 ParticleType &at(size_t index) { return _particles.at(index); }
139
145 const ParticleType &at(size_t index) const { return _particles.at(index); }
146
150 std::vector<std::pair<double, ParticleType *>> _particles;
151
155 ParticleCellType *_cell;
156
157 private:
158};
159} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
Wraps the iterator of arbitrary cells to provide a common interface.
Definition: CellIterator.h:19
Class for Cells of Particles.
Definition: ParticleCell.h:51
void setPossibleParticleOwnerships(OwnershipState state)
Set the type of particles contained in this cell.
Definition: ParticleCell.h:162
This class defines a sorted view on a given ParticleCell.
Definition: SortedCellView.h:28
typename ParticleCellType::ParticleType ParticleType
The particle type for this view is the particle type from the cell.
Definition: SortedCellView.h:33
void setCellLength(std::array< double, 3 > &cellLength) override
Set the side lengths of this cell.
Definition: SortedCellView.h:129
void clear() override
Deletes all particles in this cell.
Definition: SortedCellView.h:109
std::array< double, 3 > getCellLength() const override
Get the side lengths of this cell.
Definition: SortedCellView.h:131
SortedCellView(ParticleCellType &cell, const std::array< double, 3 > &r)
Constructs a FullSortedParticleCell.
Definition: SortedCellView.h:43
void deleteByIndex(size_t index) override
Deletes the index-th particle.
Definition: SortedCellView.h:117
const ParticleType & at(size_t index) const
Returns the const particle at position index.
Definition: SortedCellView.h:145
void deleteDummyParticles() override
Deletes all dummy particles in this cell.
Definition: SortedCellView.h:111
CellIterator< typename ParticleCellType::StorageType, false > end() const
Get an iterator to the end of a ParticleCell.
Definition: SortedCellView.h:89
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: SortedCellView.h:102
ParticleCellType * _cell
Underlying cell.
Definition: SortedCellView.h:155
CellIterator< typename ParticleCellType::StorageType, false > begin() const
Get an iterator to the start of a ParticleCell.
Definition: SortedCellView.h:74
CellIterator< typename ParticleCellType::StorageType, true > begin()
Get an iterator to the start of a ParticleCell.
Definition: SortedCellView.h:65
bool isEmpty() const override
Check if the cell is empty.
Definition: SortedCellView.h:107
void addParticle(const ParticleType &p) override
Adds a Particle to the cell.
Definition: SortedCellView.h:59
CellIterator< typename ParticleCellType::StorageType, true > end()
Get an iterator to the end of a ParticleCell.
Definition: SortedCellView.h:81
std::vector< ParticleType * > StorageType
Type that holds or refers to the actual particles.
Definition: SortedCellView.h:37
CellType getParticleCellTypeAsEnum() override
Get the ParticleCell type as an ParticleCellTypeEnum.
Definition: SortedCellView.h:54
ParticleType & at(size_t index)
Returns the particle at position index.
Definition: SortedCellView.h:138
size_t size() const override
Get the number of all particles stored in this cell (owned, halo and dummy).
Definition: SortedCellView.h:97
std::vector< std::pair< double, ParticleType * > > _particles
Sorted vector of projected positions and particle pointers.
Definition: SortedCellView.h:150
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
constexpr T dot(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Generates the dot product of two arrays.
Definition: ArrayMath.h:233
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
CellType
The ParticleCell Type as an Enum.
Definition: ParticleCell.h:19
@ SortedCellView
SortedCellView : Holds pointers to particles sorted by their position projected along a vector.