AutoPas  3.0.0
Loading...
Searching...
No Matches
VerletListHelpers.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <atomic>
10
13#include "autopas/utils/SoA.h"
14namespace autopas {
15
20template <class Particle_T>
22 public:
26 using NeighborListAoSType = std::unordered_map<Particle_T *, std::vector<Particle_T *>>;
27
31 class VerletListGeneratorFunctor : public PairwiseFunctor<Particle_T, VerletListGeneratorFunctor> {
32 public:
36 using SoAArraysType = typename Particle_T::SoAArraysType;
37
43 VerletListGeneratorFunctor(NeighborListAoSType &verletListsAoS, double interactionLength)
44 : PairwiseFunctor<Particle_T, VerletListGeneratorFunctor>(interactionLength),
45 _verletListsAoS(verletListsAoS),
46 _interactionLengthSquared(interactionLength * interactionLength) {}
47
48 std::string getName() override { return "VerletListGeneratorFunctor"; }
49
50 bool isRelevantForTuning() override { return false; }
51
52 bool allowsNewton3() override {
54 "VLCAllCellsGeneratorFunctor::allowsNewton3() is not implemented, because it should not be called.");
55 return true;
56 }
57
58 bool allowsNonNewton3() override {
60 "VLCAllCellsGeneratorFunctor::allowsNonNewton3() is not implemented, because it should not be called.");
61 return true;
62 }
63
64 void AoSFunctor(Particle_T &i, Particle_T &j, bool /*newton3*/) override {
65 using namespace autopas::utils::ArrayMath::literals;
66
67 if (i.isDummy() or j.isDummy()) {
68 return;
69 }
70 auto dist = i.getR() - j.getR();
71
72 double distsquare = utils::ArrayMath::dot(dist, dist);
73 if (distsquare < _interactionLengthSquared) {
74 // this is thread safe, only if particle i is accessed by only one
75 // thread at a time. which is ensured, as particle i resides in a
76 // specific cell and each cell is only accessed by one thread at a time
77 // (ensured by traversals)
78 // also the list is not allowed to be resized!
79
80 _verletListsAoS.at(&i).push_back(&j);
81 // no newton3 here, as AoSFunctor(j,i) will also be called if newton3 is disabled.
82 }
83 }
84
90 void SoAFunctorSingle(SoAView<SoAArraysType> soa, bool newton3) override {
91 if (soa.size() == 0) return;
92
93 auto **const __restrict ptrptr = soa.template begin<Particle_T::AttributeNames::ptr>();
94 const double *const __restrict xptr = soa.template begin<Particle_T::AttributeNames::posX>();
95 const double *const __restrict yptr = soa.template begin<Particle_T::AttributeNames::posY>();
96 const double *const __restrict zptr = soa.template begin<Particle_T::AttributeNames::posZ>();
97
98 size_t numPart = soa.size();
99 for (unsigned int i = 0; i < numPart; ++i) {
100 auto &currentList = _verletListsAoS.at(ptrptr[i]);
101
102 for (unsigned int j = i + 1; j < numPart; ++j) {
103 const double drx = xptr[i] - xptr[j];
104 const double dry = yptr[i] - yptr[j];
105 const double drz = zptr[i] - zptr[j];
106
107 const double drx2 = drx * drx;
108 const double dry2 = dry * dry;
109 const double drz2 = drz * drz;
110
111 const double dr2 = drx2 + dry2 + drz2;
112
113 if (dr2 < _interactionLengthSquared) {
114 currentList.push_back(ptrptr[j]);
115 if (not newton3) {
116 // we need this here, as SoAFunctorSingle will only be called once for both newton3=true and false.
117 _verletListsAoS.at(ptrptr[j]).push_back(ptrptr[i]);
118 }
119 }
120 }
121 }
122 }
123
130 void SoAFunctorPair(SoAView<SoAArraysType> soa1, SoAView<SoAArraysType> soa2, bool /*newton3*/) override {
131 if (soa1.size() == 0 || soa2.size() == 0) return;
132
133 auto **const __restrict ptr1ptr = soa1.template begin<Particle_T::AttributeNames::ptr>();
134 const double *const __restrict x1ptr = soa1.template begin<Particle_T::AttributeNames::posX>();
135 const double *const __restrict y1ptr = soa1.template begin<Particle_T::AttributeNames::posY>();
136 const double *const __restrict z1ptr = soa1.template begin<Particle_T::AttributeNames::posZ>();
137
138 auto **const __restrict ptr2ptr = soa2.template begin<Particle_T::AttributeNames::ptr>();
139 const double *const __restrict x2ptr = soa2.template begin<Particle_T::AttributeNames::posX>();
140 const double *const __restrict y2ptr = soa2.template begin<Particle_T::AttributeNames::posY>();
141 const double *const __restrict z2ptr = soa2.template begin<Particle_T::AttributeNames::posZ>();
142
143 size_t numPart1 = soa1.size();
144 for (unsigned int i = 0; i < numPart1; ++i) {
145 auto &currentList = _verletListsAoS.at(ptr1ptr[i]);
146
147 size_t numPart2 = soa2.size();
148
149 for (unsigned int j = 0; j < numPart2; ++j) {
150 const double drx = x1ptr[i] - x2ptr[j];
151 const double dry = y1ptr[i] - y2ptr[j];
152 const double drz = z1ptr[i] - z2ptr[j];
153
154 const double drx2 = drx * drx;
155 const double dry2 = dry * dry;
156 const double drz2 = drz * drz;
157
158 const double dr2 = drx2 + dry2 + drz2;
159
160 if (dr2 < _interactionLengthSquared) {
161 currentList.push_back(ptr2ptr[j]);
162 }
163 }
164 }
165 }
166
170 constexpr static std::array<typename Particle_T::AttributeNames, 4> getNeededAttr() {
171 return std::array<typename Particle_T::AttributeNames, 4>{
172 Particle_T::AttributeNames::ptr, Particle_T::AttributeNames::posX, Particle_T::AttributeNames::posY,
173 Particle_T::AttributeNames::posZ};
174 }
175
179 constexpr static std::array<typename Particle_T::AttributeNames, 0> getComputedAttr() {
180 return std::array<typename Particle_T::AttributeNames, 0>{/*Nothing*/};
181 }
182
183 private:
184 NeighborListAoSType &_verletListsAoS;
185 double _interactionLengthSquared;
186 };
187
196 class VerletListValidityCheckerFunctor : public PairwiseFunctor<Particle_T, VerletListValidityCheckerFunctor> {
197 public:
201 using SoAArraysType = typename Particle_T::SoAArraysType;
202
210 _verletListsAoS(verletListsAoS),
211 _cutoffsquared(cutoff * cutoff),
212 _valid(true) {}
213
214 std::string getName() override { return "VerletListValidityCheckerFunctor"; }
215
216 bool isRelevantForTuning() override { return false; }
217
218 bool allowsNewton3() override {
220 "VLCAllCellsGeneratorFunctor::allowsNewton3() is not implemented, because it should not be called.");
221 return true;
222 }
223
224 bool allowsNonNewton3() override {
226 "VLCAllCellsGeneratorFunctor::allowsNonNewton3() is not implemented, because it should not be called.");
227 return true;
228 }
229
230 void AoSFunctor(Particle_T &i, Particle_T &j, bool newton3) override {
231 using namespace autopas::utils::ArrayMath::literals;
232
233 auto dist = i.getR() - j.getR();
234 double distsquare = utils::ArrayMath::dot(dist, dist);
235 if (distsquare < _cutoffsquared) {
236 // this is thread safe, we have variables on the stack
237 auto found = std::find(_verletListsAoS[&i].begin(), _verletListsAoS[&i].end(), &j);
238 if (found == _verletListsAoS[&i].end()) {
239 // this is thread safe, as _valid is atomic
240 _valid = false;
241 }
242 }
243 }
244
250 bool neighborlistsAreValid() { return _valid; }
251
252 private:
253 NeighborListAoSType &_verletListsAoS;
254 double _cutoffsquared;
255
256 // needs to be thread safe
257 std::atomic<bool> _valid;
258 };
259
260}; // class VerletListHelpers
261} // namespace autopas
PairwiseFunctor class.
Definition: PairwiseFunctor.h:31
View on a fixed part of a SoA between a start index and an end index.
Definition: SoAView.h:23
size_t size() const
Returns the number of particles in the view.
Definition: SoAView.h:83
This functor can generate verlet lists using the typical pairwise traversal.
Definition: VerletListHelpers.h:31
typename Particle_T::SoAArraysType SoAArraysType
Structure of the SoAs defined by the particle.
Definition: VerletListHelpers.h:36
void AoSFunctor(Particle_T &i, Particle_T &j, bool) override
PairwiseFunctor for arrays of structures (AoS).
Definition: VerletListHelpers.h:64
static constexpr std::array< typename Particle_T::AttributeNames, 4 > getNeededAttr()
Get attributes needed for computation.
Definition: VerletListHelpers.h:170
void SoAFunctorPair(SoAView< SoAArraysType > soa1, SoAView< SoAArraysType > soa2, bool) override
SoAFunctor for the verlet list generation.
Definition: VerletListHelpers.h:130
bool allowsNewton3() override
Specifies whether the functor is capable of Newton3-like functors.
Definition: VerletListHelpers.h:52
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getComputedAttr()
Get attributes computed by this functor.
Definition: VerletListHelpers.h:179
std::string getName() override
Returns name of functor.
Definition: VerletListHelpers.h:48
void SoAFunctorSingle(SoAView< SoAArraysType > soa, bool newton3) override
SoAFunctor for verlet list generation.
Definition: VerletListHelpers.h:90
bool isRelevantForTuning() override
Specifies whether the functor should be considered for the auto-tuning process.
Definition: VerletListHelpers.h:50
bool allowsNonNewton3() override
Specifies whether the functor is capable of non-Newton3-like functors.
Definition: VerletListHelpers.h:58
VerletListGeneratorFunctor(NeighborListAoSType &verletListsAoS, double interactionLength)
Constructor.
Definition: VerletListHelpers.h:43
This functor checks the validity of neighborhood lists.
Definition: VerletListHelpers.h:196
typename Particle_T::SoAArraysType SoAArraysType
Structure of the SoAs defined by the particle.
Definition: VerletListHelpers.h:201
void AoSFunctor(Particle_T &i, Particle_T &j, bool newton3) override
PairwiseFunctor for arrays of structures (AoS).
Definition: VerletListHelpers.h:230
std::string getName() override
Returns name of functor.
Definition: VerletListHelpers.h:214
bool allowsNonNewton3() override
Specifies whether the functor is capable of non-Newton3-like functors.
Definition: VerletListHelpers.h:224
bool neighborlistsAreValid()
Returns whether the neighbour list are valid.
Definition: VerletListHelpers.h:250
VerletListValidityCheckerFunctor(NeighborListAoSType &verletListsAoS, double cutoff)
Constructor.
Definition: VerletListHelpers.h:208
bool allowsNewton3() override
Specifies whether the functor is capable of Newton3-like functors.
Definition: VerletListHelpers.h:218
bool isRelevantForTuning() override
Specifies whether the functor should be considered for the auto-tuning process.
Definition: VerletListHelpers.h:216
Class of helpers for the VerletLists class.
Definition: VerletListHelpers.h:21
std::unordered_map< Particle_T *, std::vector< Particle_T * > > NeighborListAoSType
Neighbor list AoS style.
Definition: VerletListHelpers.h:26
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