AutoPas  3.0.0
Loading...
Searching...
No Matches
VLCCellPairNeighborList.h
Go to the documentation of this file.
1
7#pragma once
8
13
14namespace autopas {
15
22template <class Particle_T>
24 public:
29
33 using SoAPairOfParticleAndList = std::pair<size_t, std::vector<size_t, autopas::AlignedAllocator<size_t>>>;
34
39 using SoAListType = typename std::vector<std::vector<std::vector<SoAPairOfParticleAndList>>>;
40
41 [[nodiscard]] ContainerOption getContainerType() const override { return ContainerOption::pairwiseVerletLists; }
42
43 size_t getNumberOfPartners(const Particle_T *particle) const override {
44 size_t listSize{0};
45 bool particleFound{false};
46 for (auto &neighborListsForOneCell : _aosNeighborList) {
47 for (const auto &neighborListsForCellCellPair : neighborListsForOneCell) {
48 // Check for the desired particle in the first cell. We check all "partner" cells because the particle might not
49 // have any particles from one "partner" cell in its list.
50 for (size_t i{0}; i < neighborListsForCellCellPair.size(); ++i) {
51 if (neighborListsForCellCellPair[i].first == particle) {
52 particleFound = true;
53 // We accumulate the number of partners. Partners can be in all lists of neighboring cells (middle for loop)
54 listSize += neighborListsForCellCellPair[i].second.size();
55 // Since we found the particle, we can skip all the other particles in the current list
56 break;
57 }
58 }
59 }
60 if (particleFound) {
61 // We've found the particle in the cell with neighbor lists neighborListsForOneCell. So we can skip all the
62 // other cells (outer for loop)
63 return listSize;
64 }
65 }
66 return 0lu;
67 }
68
74 return _aosNeighborList;
75 }
76
81 auto &getSoANeighborList() { return _soaNeighborList; }
82
86 void buildAoSNeighborList(TraversalOption vlcTraversalOpt, LinkedCells<Particle_T> &linkedCells,
87 bool useNewton3) override {
88 using namespace utils::ArrayMath::literals;
89 // Sanity check.
90 if (linkedCells.getCellBlock().getCellsPerInteractionLength() > 1) {
92 "VLCCellPairNeighborList::buildAoSNeighborList() was called with a CSF < 1 but it only supports CSF>=1.");
93 }
94 // Define some aliases
95 auto &neighborLists = getAoSNeighborList();
96 auto &cells = linkedCells.getCells();
97 const auto interactionLength = linkedCells.getInteractionLength();
98 const auto interactionLengthSquared = interactionLength * interactionLength;
99 const auto boxSizeWithHalo = linkedCells.getBoxMax() - linkedCells.getBoxMin() +
100 std::array<double, 3>{interactionLength, interactionLength, interactionLength} * 2.;
101
102 // Helper lambda to compute the relative index from two cells within in a 3x3x3 cell-cube
103 auto relativeNeighborhoodIndex = [&](auto cellIndex1, auto cellIndex2) {
104 const auto cellsPerDimensionWithHalo = linkedCells.getCellBlock().getCellsPerDimensionWithHalo();
105 const auto threeDPosCell1 = utils::ThreeDimensionalMapping::oneToThreeD(static_cast<long unsigned>(cellIndex1),
106 cellsPerDimensionWithHalo);
107 const auto threeDPosCell2 = utils::ThreeDimensionalMapping::oneToThreeD(static_cast<long unsigned>(cellIndex2),
108 cellsPerDimensionWithHalo);
109 const auto offset = threeDPosCell2 - threeDPosCell1;
110 return (offset[0] + 1) * 9 + (offset[1] + 1) * 3 + (offset[2] + 1);
111 };
112
113 // This assumes homogeneous distribution and some overestimation.
114 const auto listLengthEstimate = VerletListsCellsHelpers::estimateListLength(
115 linkedCells.getNumberOfParticles(IteratorBehavior::ownedOrHalo), boxSizeWithHalo, interactionLength, 1.3);
116
117 // Reset lists. Don't free any memory, only mark as unused.
118 this->setLinkedCellsPointer(&linkedCells);
119 for (auto &neighborCellLists : neighborLists) {
120 for (auto &cellLists : neighborCellLists) {
121 for (auto &[particlePtr, neighbors] : cellLists) {
122 particlePtr = nullptr;
123 neighbors.clear();
124 }
125 }
126 }
127 neighborLists.resize(cells.size());
128 // each cell hast 27 neighbors including itself
129 for (auto &neighborList : neighborLists) {
130 neighborList.resize(27);
131 }
132
133 /* This must not be a doc comment (with two **) to not confuse doxygen.
134 * Helper function to insert a pointer into a list of the base cell.
135 * It considers the cases that neither particle is in the base cell
136 * and in that case finds or creates the appropriate list.
137 *
138 * @param p1 Reference to source particle.
139 * @param p2 Reference to target particle.
140 * @param neighborList Reference to the list where the particle pair should be stored.
141 */
142 auto insert = [&](auto &p1, auto &p2, auto &neighborList) {
143 // Check if the base cell already has a list for p1
144 auto iter = std::find_if(neighborList.begin(), neighborList.end(), [&](const auto &pair) {
145 const auto &[particlePtr, list] = pair;
146 return particlePtr == &p1;
147 });
148 // If yes, append p2 to it.
149 if (iter != neighborList.end()) {
150 iter->second.push_back(&p2);
151 } else {
152 // If no, create one (or reuse an empty pair), reserve space for the list and emplace p2
153 if (auto insertLoc = std::find_if(neighborList.begin(), neighborList.end(),
154 [&](const auto &pair) {
155 const auto &[particlePtr, list] = pair;
156 return particlePtr == nullptr;
157 });
158 insertLoc != neighborList.end()) {
159 auto &[particlePtr, neighbors] = *insertLoc;
160 particlePtr = &p1;
161 neighbors.reserve(listLengthEstimate);
162 neighbors.push_back(&p2);
163 } else {
164 neighborList.emplace_back(&p1, std::vector<Particle_T *>{});
165 neighborList.back().second.reserve(listLengthEstimate);
166 neighborList.back().second.push_back(&p2);
167 }
168 }
169 };
170
171 const auto &cellsPerDim =
172 utils::ArrayUtils::static_cast_copy_array<int>(linkedCells.getCellBlock().getCellsPerDimensionWithHalo());
173 // Vector of offsets from the base cell for the given base step
174 // and respective factors for the fraction of particles per cell that need neighbor lists in the base cell.
175 const auto offsets = VerletListsCellsHelpers::buildBaseStep(cellsPerDim, vlcTraversalOpt);
176
177 int xEnd{};
178 int yEnd{};
179 int zEnd{};
180
181 switch (vlcTraversalOpt) {
182 case TraversalOption::vlc_c08:
183 // Go over all cells except the very last layer and create lists per base step.
184 xEnd = cellsPerDim[0] - 1;
185 yEnd = cellsPerDim[1] - 1;
186 zEnd = cellsPerDim[2] - 1;
187 break;
188 default:
189 xEnd = cellsPerDim[0];
190 yEnd = cellsPerDim[1];
191 zEnd = cellsPerDim[2];
192 break;
193 }
194
195 // Since there are no loop dependencies merge all for loops and create 10 chunks per thread.
196 AUTOPAS_OPENMP(parallel for collapse(3) schedule(dynamic, std::max(cells.size() / (autopas::autopas_get_max_threads() * 10), 1ul)))
197 for (int z = 0; z < zEnd; ++z) {
198 for (int y = 0; y < yEnd; ++y) {
199 for (int x = 0; x < xEnd; ++x) {
200 // aliases
201 const auto cellIndexBase = utils::ThreeDimensionalMapping::threeToOneD(x, y, z, cellsPerDim);
202 auto &baseCell = cells[cellIndexBase];
203 auto &baseCellsLists = neighborLists[cellIndexBase];
204 auto threadNum = autopas_get_thread_num();
205
206 // Build lists for this base step according to predefined cell pairs
207 for (const auto &[offset1, offset2, _] : offsets) {
208 const auto cell1Index1D = cellIndexBase + offset1;
209 const auto cell2Index1D = cellIndexBase + offset2;
210 auto &cell1List = neighborLists[cell1Index1D];
211 auto &cell2List = neighborLists[cell2Index1D];
212
213 // For all traversals ensures the partner cell is not outside the boundary
214 const auto cell2Index3D = utils::ThreeDimensionalMapping::oneToThreeD(cell2Index1D, cellsPerDim);
215 if (cell2Index3D[0] >= cellsPerDim[0] or cell2Index3D[0] < 0 or cell2Index3D[1] >= cellsPerDim[1] or
216 cell2Index3D[1] < 0 or cell2Index3D[2] >= cellsPerDim[2] or cell2Index3D[2] < 0) {
217 continue;
218 }
219
220 // Skip if both cells only contain halos or dummys
221 if (not(cells[cell1Index1D].getPossibleParticleOwnerships() == OwnershipState::owned) and
222 not(cells[cell2Index1D].getPossibleParticleOwnerships() == OwnershipState::owned)) {
223 continue;
224 }
225
226 // Go over all particle pairs in the two cells and insert close pairs into their respective lists
227 for (size_t particleIndexCell1 = 0; particleIndexCell1 < cells[cell1Index1D].size(); ++particleIndexCell1) {
228 auto &p1 = cells[cell1Index1D][particleIndexCell1];
229
230 // Determine the starting index for the second particle in the pair.
231 // If both cells are the same and the traversal is newton3 compatible, this is the next particle in the
232 // cell. If the cells are different or the traversal is not newton3 compatible, this is index 0. For
233 // non-newton 3 compatible traversals (vlp_c01) we have to consider the interaction in both directions, so
234 // we always start from index 0.
235 size_t startIndexCell2 = 0;
236 if (cell1Index1D == cell2Index1D and vlcTraversalOpt != TraversalOption::vlp_c01) {
237 startIndexCell2 = particleIndexCell1 + 1;
238 }
239
240 for (size_t particleIndexCell2 = startIndexCell2; particleIndexCell2 < cells[cell2Index1D].size();
241 ++particleIndexCell2) {
242 auto &p2 = cells[cell2Index1D][particleIndexCell2];
243 // Ignore dummies and self interaction
244 if (&p1 == &p2 or p1.isDummy() or p2.isDummy()) {
245 continue;
246 }
247
248 // If the distance is less than interaction length add the pair to the list
249 const auto distVec = p2.getR() - p1.getR();
250 const auto distSquared = utils::ArrayMath::dot(distVec, distVec);
251 if (distSquared < interactionLengthSquared) {
252 {
253 const size_t secondCellIndexInFirst = relativeNeighborhoodIndex(cell1Index1D, cell2Index1D);
254 insert(p1, p2, cell1List[secondCellIndexInFirst]);
255 }
256 // If the traversal is Newton3 compatible, Newton3 is used for building regardless of if the actual
257 // interactions will use Newton3. In the case that Newton3 will not be used, the inverse interaction
258 // also needs to be stored in p2's list. If the traversal is Newton3 incompatible, the insertion into
259 // p2's list will occur when the p2 particle is p1. This is ensured above by the startIndexCell2.
260 if (not useNewton3 and not(vlcTraversalOpt == TraversalOption::vlp_c01)) {
261 {
262 const size_t secondCellIndexInFirst = relativeNeighborhoodIndex(cell2Index1D, cell1Index1D);
263 insert(p2, p1, cell2List[secondCellIndexInFirst]);
264 }
265 }
266 }
267 }
268 }
269 }
270 }
271 }
272 }
273
274 // Cleanup: Remove any unused ptr-list pairs to avoid accessing nullptr
275 for (auto &neighborCellLists : neighborLists) {
276 for (auto &cellLists : neighborCellLists) {
277 cellLists.erase(std::remove_if(cellLists.begin(), cellLists.end(),
278 [](const auto &pair) {
279 const auto &[particlePtr, neighbors] = pair;
280 return particlePtr == nullptr;
281 }),
282 cellLists.end());
283 }
284 }
285 }
286
287 void generateSoAFromAoS(LinkedCells<Particle_T> &linkedCells) override {
288 _soaNeighborList.clear();
289
290 // particle pointer to global index of particle
291 std::unordered_map<Particle_T *, size_t> particlePtrToIndex;
292 particlePtrToIndex.reserve(linkedCells.size());
293 size_t i = 0;
294 for (auto iter = linkedCells.begin(IteratorBehavior::ownedOrHaloOrDummy); iter.isValid(); ++iter, ++i) {
295 particlePtrToIndex[&(*iter)] = i;
296 }
297
298 _soaNeighborList.resize(linkedCells.getCells().size());
299
300 // iterate over cells and for each create the soa lists from the aos lists
301 for (size_t firstCellIndex = 0; firstCellIndex < _aosNeighborList.size(); ++firstCellIndex) {
302 const auto &aosLists = _aosNeighborList[firstCellIndex];
303 auto &soaLists = _soaNeighborList[firstCellIndex];
304 soaLists.resize(aosLists.size());
305
306 // iterate over each cell's neighboring cells
307 for (size_t secondCellIndex = 0; secondCellIndex < aosLists.size(); ++secondCellIndex) {
308 const auto &aosCellPairLists = aosLists[secondCellIndex];
309 auto &soaCellPairLists = soaLists[secondCellIndex];
310 soaCellPairLists.reserve(aosCellPairLists.capacity());
311
312 // iterate over pairs of particle and neighbor list
313 for (const auto &[particlePtr, neighbors] : aosCellPairLists) {
314 // global index of current particle
315 size_t currentParticleGlobalIndex = particlePtrToIndex.at(particlePtr);
316
317 // create SoA neighbor list for current particle
318 std::vector<size_t, autopas::AlignedAllocator<size_t>> currentSoANeighborList{};
319 currentSoANeighborList.reserve(neighbors.size());
320
321 // fill the SoA neighbor list with the indices of the particles from the corresponding AoS neighbor list
322 for (const auto &neighborOfCurrentParticle : neighbors) {
323 currentSoANeighborList.emplace_back(particlePtrToIndex.at(neighborOfCurrentParticle));
324 }
325
326 // add the newly constructed pair of particle index and SoA particle neighbor list to cell pair
327 soaCellPairLists.emplace_back(currentParticleGlobalIndex, currentSoANeighborList);
328 }
329 }
330 }
331 }
332
333 void setUpTraversal(TraversalInterface *traversal) override {
334 auto vTraversal = dynamic_cast<VLCCellPairTraversalInterface<Particle_T> *>(traversal);
335
336 if (vTraversal) {
337 vTraversal->setVerletList(*this);
338 } else {
339 auto traversal2 =
341 if (traversal2) {
342 traversal2->setVerletList(*this);
343 } else {
345 "Trying to use a traversal of wrong type in VerletListCells.h. TraversalID: {}",
346 traversal->getTraversalType());
347 }
348 }
349 }
350
351 private:
356 std::vector<std::vector<std::vector<std::pair<Particle_T *, std::vector<Particle_T *>>>>>();
357
362 SoAListType _soaNeighborList = std::vector<
363 std::vector<std::vector<std::pair<size_t, std::vector<size_t, autopas::AlignedAllocator<size_t>>>>>>();
364};
365} // namespace autopas
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
const std::array< double, 3 > & getBoxMax() const final
Get the upper corner of the container without halo.
Definition: CellBasedParticleContainer.h:71
size_t getNumberOfParticles(IteratorBehavior behavior) const override
Get the number of particles with respect to the specified IteratorBehavior.
Definition: CellBasedParticleContainer.h:115
size_t size() const override
Get the total number of particles saved in the container (owned + halo + dummy).
Definition: CellBasedParticleContainer.h:133
const std::array< double, 3 > & getBoxMin() const final
Get the lower corner of the container without halo.
Definition: CellBasedParticleContainer.h:76
double getInteractionLength() const final
Return the interaction length (cutoff+skin) of the container.
Definition: CellBasedParticleContainer.h:91
LinkedCells class.
Definition: LinkedCells.h:40
internal::CellBlock3D< ParticleCellType > & getCellBlock()
Get the cell block, not supposed to be used except by verlet lists.
Definition: LinkedCells.h:487
std::vector< ParticleCellType > & getCells()
Returns a non-const reference to the cell data structure.
Definition: LinkedCells.h:499
ContainerIterator< Particle_T, true, false > begin(IteratorBehavior behavior=IteratorBehavior::ownedOrHalo, typename ContainerIterator< Particle_T, true, false >::ParticleVecType *additionalVectors=nullptr) override
Iterate over all particles using for(auto iter = container.begin(); iter.isValid(); ++iter) .
Definition: LinkedCells.h:327
This interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
virtual TraversalOption getTraversalType() const =0
Return a enum representing the name of the traversal class.
Neighbor list to be used with VerletListsCells container.
Definition: VLCCellPairNeighborList.h:23
auto & getSoANeighborList()
Returns the neighbor list in SoA layout.
Definition: VLCCellPairNeighborList.h:81
ContainerOption getContainerType() const override
Returns the container type of this neighbor list and the container it belongs to.
Definition: VLCCellPairNeighborList.h:41
typename std::vector< std::vector< std::vector< SoAPairOfParticleAndList > > > SoAListType
Helper type definition.
Definition: VLCCellPairNeighborList.h:39
void buildAoSNeighborList(TraversalOption vlcTraversalOpt, LinkedCells< Particle_T > &linkedCells, bool useNewton3) override
Builds AoS neighbor list from underlying linked cells object.
Definition: VLCCellPairNeighborList.h:86
std::pair< size_t, std::vector< size_t, autopas::AlignedAllocator< size_t > > > SoAPairOfParticleAndList
Helper type definition.
Definition: VLCCellPairNeighborList.h:33
VerletListsCellsHelpers::PairwiseNeighborListsType< Particle_T > & getAoSNeighborList()
Returns the neighbor list in AoS layout.
Definition: VLCCellPairNeighborList.h:73
size_t getNumberOfPartners(const Particle_T *particle) const override
Gets the number of neighbors over all neighbor lists that belong to this particle.
Definition: VLCCellPairNeighborList.h:43
void setUpTraversal(TraversalInterface *traversal) override
Assigns the current traversal to the correct traversal interface.
Definition: VLCCellPairNeighborList.h:333
void generateSoAFromAoS(LinkedCells< Particle_T > &linkedCells) override
Generates neighbor list in SoA layout from available neighbor list in AoS layout.
Definition: VLCCellPairNeighborList.h:287
typename VerletListsCellsHelpers::PairwiseNeighborListsType< Particle_T > ListType
Type of the data structure used to save the neighbor lists.
Definition: VLCCellPairNeighborList.h:28
Interface for traversals used with VLCCellPairNeighborList.
Definition: VLCCellPairTraversalInterface.h:21
void setVerletList(VLCCellPairNeighborList< Particle_T > &verlet)
Sets the verlet list for the traversal to iterate over.
Definition: VLCCellPairTraversalInterface.h:27
Interface of neighbor lists to be used with VerletListsCells container.
Definition: VLCNeighborListInterface.h:24
void setLinkedCellsPointer(LinkedCells< Particle_T > *linkedCells)
Set the Linked Cells Pointer for this List.
Definition: VLCNeighborListInterface.h:115
This class provides the Traversal Interface for the verlet lists cells container.
Definition: VLCTraversalInterface.h:38
virtual void setVerletList(NeighborList &verlet)
Sets the verlet list for the traversal to iterate over.
Definition: VLCTraversalInterface.h:52
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
std::vector< std::vector< std::vector< std::pair< Particle_T *, std::vector< Particle_T * > > > > > PairwiseNeighborListsType
Pairwise verlet lists: For every cell a vector, for every neighboring cell a vector of particle-neigh...
Definition: VerletListsCellsHelpers.h:41
size_t estimateListLength(size_t numParticles, const std::array< double, 3 > &boxSize, double interactionLength, double correctionFactor)
Simple heuristic to calculate the average number of particles per verlet list assuming particles are ...
Definition: VerletListsCellsHelpers.cpp:16
std::vector< BaseStepOffsets > buildBaseStep(const std::array< int, 3 > &cellsPerDim, const TraversalOption traversal)
Builds the list of offsets from the base cell for the c01, c08, and c18 base step.
Definition: VerletListsCellsHelpers.cpp:26
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
constexpr std::array< T, 3 > oneToThreeD(T ind, const std::array< T, 3 > &dims)
Convert a 1d index to a 3d index.
Definition: ThreeDimensionalMapping.h:55
constexpr T threeToOneD(T x, T y, T z, const std::array< T, 3 > &dims)
Convert a 3d index to a 1d index.
Definition: ThreeDimensionalMapping.h:29
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
@ owned
Owned state, a particle with this state is an actual particle and owned by the current AutoPas object...
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132