AutoPas  3.0.0
Loading...
Searching...
No Matches
AxilrodTellerMutoFunctor.h
Go to the documentation of this file.
1
7#pragma once
8
15#include "autopas/utils/SoA.h"
18#include "autopas/utils/inBox.h"
19
20namespace mdLib {
21
97template <class Particle_T, bool useMixing = false, autopas::FunctorN3Modes useNewton3 = autopas::FunctorN3Modes::Both,
98 bool calculateGlobals = false, bool countFLOPs = false>
101 Particle_T, AxilrodTellerMutoFunctor<Particle_T, useMixing, useNewton3, calculateGlobals, countFLOPs>> {
105 using SoAArraysType = typename Particle_T::SoAArraysType;
106
110 using SoAFloatPrecision = typename Particle_T::ParticleSoAFloatPrecision;
111
112 public:
117
118 private:
124 explicit AxilrodTellerMutoFunctor(double cutoff, void * /*dummy*/)
126 Particle_T, AxilrodTellerMutoFunctor<Particle_T, useMixing, useNewton3, calculateGlobals, countFLOPs>>(
127 cutoff),
128 _cutoffSquared{cutoff * cutoff},
129 _potentialEnergySum{0.},
130 _virialSum{0., 0., 0.},
131 _aosThreadDataGlobals(),
132 _postProcessed{false} {
133 if constexpr (calculateGlobals) {
134 _aosThreadDataGlobals.resize(autopas::autopas_get_max_threads());
135 }
136 if constexpr (countFLOPs) {
137 _aosThreadDataFLOPs.resize(autopas::autopas_get_max_threads());
138 }
139 }
140
141 public:
150 explicit AxilrodTellerMutoFunctor(double cutoff) : AxilrodTellerMutoFunctor(cutoff, nullptr) {
151 static_assert(not useMixing,
152 "Mixing without a ParticlePropertiesLibrary is not possible! Use a different constructor or set "
153 "mixing to false.");
154 }
155
162 explicit AxilrodTellerMutoFunctor(double cutoff, ParticlePropertiesLibrary<double, size_t> &particlePropertiesLibrary)
163 : AxilrodTellerMutoFunctor(cutoff, nullptr) {
164 static_assert(useMixing,
165 "Not using Mixing but using a ParticlePropertiesLibrary is not allowed! Use a different constructor "
166 "or set mixing to true.");
167 _PPLibrary = &particlePropertiesLibrary;
168 }
169
170 std::string getName() final { return "AxilrodTellerMutoFunctorAutoVec"; }
171
172 bool isRelevantForTuning() final { return true; }
173
174 bool allowsNewton3() final {
175 return useNewton3 == autopas::FunctorN3Modes::Newton3Only or useNewton3 == autopas::FunctorN3Modes::Both;
176 }
177
178 bool allowsNonNewton3() final {
179 return useNewton3 == autopas::FunctorN3Modes::Newton3Off or useNewton3 == autopas::FunctorN3Modes::Both;
180 }
181
182 void AoSFunctor(Particle_T &i, Particle_T &j, Particle_T &k, bool newton3) final {
183 using namespace autopas::utils::ArrayMath::literals;
184
185 if (i.isDummy() or j.isDummy() or k.isDummy()) {
186 return;
187 }
188
189 const auto threadnum = autopas::autopas_get_thread_num();
190
191 if constexpr (countFLOPs) {
192 ++_aosThreadDataFLOPs[threadnum].numDistCalls;
193 }
194
195 auto nu = _nu;
196 if constexpr (useMixing) {
197 nu = _PPLibrary->getMixingNu(i.getTypeId(), j.getTypeId(), k.getTypeId());
198 }
199
200 const auto displacementIJ = j.getR() - i.getR();
201 const auto displacementJK = k.getR() - j.getR();
202 const auto displacementKI = i.getR() - k.getR();
203
204 const double distSquaredIJ = autopas::utils::ArrayMath::dot(displacementIJ, displacementIJ);
205 const double distSquaredJK = autopas::utils::ArrayMath::dot(displacementJK, displacementJK);
206 const double distSquaredKI = autopas::utils::ArrayMath::dot(displacementKI, displacementKI);
207
208 // Check cutoff for every distance
209 if (distSquaredIJ > _cutoffSquared or distSquaredJK > _cutoffSquared or distSquaredKI > _cutoffSquared) {
210 return;
211 }
212
213 // Calculate prefactor
214 const double allDistsSquared = distSquaredIJ * distSquaredJK * distSquaredKI;
215 const double allDistsTo5 = allDistsSquared * allDistsSquared * std::sqrt(allDistsSquared);
216 const double factor = 3.0 * nu / allDistsTo5;
217
218 // Dot products of both distance vectors going from one particle
219 const double IJDotKI = autopas::utils::ArrayMath::dot(displacementIJ, displacementKI);
220 const double IJDotJK = autopas::utils::ArrayMath::dot(displacementIJ, displacementJK);
221 const double JKDotKI = autopas::utils::ArrayMath::dot(displacementJK, displacementKI);
222
223 const double allDotProducts = IJDotKI * IJDotJK * JKDotKI;
224
225 const auto forceIDirectionJK = displacementJK * IJDotKI * (IJDotJK - JKDotKI);
226 const auto forceIDirectionIJ =
227 displacementIJ * (IJDotJK * JKDotKI - distSquaredJK * distSquaredKI + 5.0 * allDotProducts / distSquaredIJ);
228 const auto forceIDirectionKI =
229 displacementKI * (-IJDotJK * JKDotKI + distSquaredIJ * distSquaredJK - 5.0 * allDotProducts / distSquaredKI);
230
231 const auto forceI = (forceIDirectionJK + forceIDirectionIJ + forceIDirectionKI) * factor;
232 i.addF(forceI);
233
234 auto forceJ = forceI;
235 auto forceK = forceI;
236 if (newton3) {
237 const auto forceJDirectionKI = displacementKI * IJDotJK * (JKDotKI - IJDotKI);
238 const auto forceJDirectionIJ =
239 displacementIJ * (-IJDotKI * JKDotKI + distSquaredJK * distSquaredKI - 5.0 * allDotProducts / distSquaredIJ);
240 const auto forceJDirectionJK =
241 displacementJK * (IJDotKI * JKDotKI - distSquaredIJ * distSquaredKI + 5.0 * allDotProducts / distSquaredJK);
242
243 forceJ = (forceJDirectionKI + forceJDirectionIJ + forceJDirectionJK) * factor;
244 j.addF(forceJ);
245
246 forceK = (forceI + forceJ) * (-1.0);
247 k.addF(forceK);
248 }
249
250 if constexpr (countFLOPs) {
251 if (newton3) {
252 ++_aosThreadDataFLOPs[threadnum].numKernelCallsN3;
253 } else {
254 ++_aosThreadDataFLOPs[threadnum].numKernelCallsNoN3;
255 }
256 }
257
258 if constexpr (calculateGlobals) {
259 // Add 3 * potential energy to every owned particle of the interaction.
260 // Division to the correct value is handled in endTraversal().
261 const double potentialEnergy3 = factor * (allDistsSquared - 3.0 * allDotProducts);
262
263 // Virial is calculated as f_i * r_i
264 // see Thompson et al.: https://doi.org/10.1063/1.3245303
265 const auto virialI = forceI * i.getR();
266 if (i.isOwned()) {
267 _aosThreadDataGlobals[threadnum].potentialEnergySum += potentialEnergy3;
268 _aosThreadDataGlobals[threadnum].virialSum += virialI;
269 }
270 // for non-newton3 particles j and/or k will be considered in a separate calculation
271 if (newton3 and j.isOwned()) {
272 const auto virialJ = forceJ * j.getR();
273 _aosThreadDataGlobals[threadnum].potentialEnergySum += potentialEnergy3;
274 _aosThreadDataGlobals[threadnum].virialSum += virialJ;
275 }
276 if (newton3 and k.isOwned()) {
277 const auto virialK = forceK * k.getR();
278 _aosThreadDataGlobals[threadnum].potentialEnergySum += potentialEnergy3;
279 _aosThreadDataGlobals[threadnum].virialSum += virialK;
280 }
281 if constexpr (countFLOPs) {
282 if (newton3) {
283 ++_aosThreadDataFLOPs[threadnum].numGlobalCalcsN3;
284 } else {
285 ++_aosThreadDataFLOPs[threadnum].numGlobalCalcsNoN3;
286 }
287 }
288 }
289 }
290
298 void setParticleProperties(SoAFloatPrecision nu) { _nu = nu; }
299
303 constexpr static auto getNeededAttr() {
304 return std::array<typename Particle_T::AttributeNames, 9>{Particle_T::AttributeNames::id,
305 Particle_T::AttributeNames::posX,
306 Particle_T::AttributeNames::posY,
307 Particle_T::AttributeNames::posZ,
308 Particle_T::AttributeNames::forceX,
309 Particle_T::AttributeNames::forceY,
310 Particle_T::AttributeNames::forceZ,
311 Particle_T::AttributeNames::typeId,
312 Particle_T::AttributeNames::ownershipState};
313 }
314
318 constexpr static auto getNeededAttr(std::false_type) {
319 return std::array<typename Particle_T::AttributeNames, 6>{
320 Particle_T::AttributeNames::id, Particle_T::AttributeNames::posX,
321 Particle_T::AttributeNames::posY, Particle_T::AttributeNames::posZ,
322 Particle_T::AttributeNames::typeId, Particle_T::AttributeNames::ownershipState};
323 }
324
328 constexpr static auto getComputedAttr() {
329 return std::array<typename Particle_T::AttributeNames, 3>{
330 Particle_T::AttributeNames::forceX, Particle_T::AttributeNames::forceY, Particle_T::AttributeNames::forceZ};
331 }
332
337 constexpr static bool getMixing() { return useMixing; }
338
343 void initTraversal() final {
344 _potentialEnergySum = 0.;
345 _virialSum = {0., 0., 0.};
346 _postProcessed = false;
347 for (size_t i = 0; i < _aosThreadDataGlobals.size(); ++i) {
348 _aosThreadDataGlobals[i].setZero();
349 }
350 }
351
356 void endTraversal(bool newton3) final {
357 using namespace autopas::utils::ArrayMath::literals;
358
359 if (_postProcessed) {
361 "Already postprocessed, endTraversal(bool newton3) was called twice without calling initTraversal().");
362 }
363 if (calculateGlobals) {
364 // Accumulate potential energy and virial values.
365 for (const auto &data : _aosThreadDataGlobals) {
366 _potentialEnergySum += data.potentialEnergySum;
367 _virialSum += data.virialSum;
368 }
369
370 // For each interaction, we added the full contribution for all three particles. Divide by 3 here, so that each
371 // contribution is only counted once per triplet.
372 _potentialEnergySum /= 3.;
373
374 // Additionally, we have always calculated 3*potentialEnergy, so we divide by 3 again.
375 _potentialEnergySum /= 3.;
376
377 _postProcessed = true;
378
379 AutoPasLog(TRACE, "Final potential energy {}", _potentialEnergySum);
380 AutoPasLog(TRACE, "Final virial {}", _virialSum[0] + _virialSum[1] + _virialSum[2]);
381 }
382 }
383
389 if (not calculateGlobals) {
391 "Trying to get potential energy even though calculateGlobals is false. If you want this functor to calculate "
392 "global "
393 "values, please specify calculateGlobals to be true.");
394 }
395 if (not _postProcessed) {
397 "Cannot get potential energy, because endTraversal was not called.");
398 }
399 return _potentialEnergySum;
400 }
401
406 double getVirial() {
407 if (not calculateGlobals) {
409 "Trying to get virial even though calculateGlobals is false. If you want this functor to calculate global "
410 "values, please specify calculateGlobals to be true.");
411 }
412 if (not _postProcessed) {
414 "Cannot get virial, because endTraversal was not called.");
415 }
416 return _virialSum[0] + _virialSum[1] + _virialSum[2];
417 }
418
454 [[nodiscard]] size_t getNumFLOPs() const override {
455 if constexpr (countFLOPs) {
456 const size_t numDistCallsAcc =
457 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
458 [](size_t sum, const auto &data) { return sum + data.numDistCalls; });
459 const size_t numKernelCallsN3Acc =
460 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
461 [](size_t sum, const auto &data) { return sum + data.numKernelCallsN3; });
462 const size_t numKernelCallsNoN3Acc =
463 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
464 [](size_t sum, const auto &data) { return sum + data.numKernelCallsNoN3; });
465 const size_t numGlobalCalcsN3Acc =
466 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
467 [](size_t sum, const auto &data) { return sum + data.numGlobalCalcsN3; });
468 const size_t numGlobalCalcsNoN3Acc =
469 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
470 [](size_t sum, const auto &data) { return sum + data.numGlobalCalcsNoN3; });
471
472 constexpr size_t numFLOPsPerDistanceCall = 24;
473 constexpr size_t numFLOPsPerN3KernelCall = 100;
474 constexpr size_t numFLOPsPerNoN3KernelCall = 59;
475 constexpr size_t numFLOPsPerN3GlobalCalc = 24;
476 constexpr size_t numFLOPsPerNoN3GlobalCalc = 10;
477
478 return numDistCallsAcc * numFLOPsPerDistanceCall + numKernelCallsN3Acc * numFLOPsPerN3KernelCall +
479 numKernelCallsNoN3Acc * numFLOPsPerNoN3KernelCall + numGlobalCalcsN3Acc * numFLOPsPerN3GlobalCalc +
480 numGlobalCalcsNoN3Acc * numFLOPsPerNoN3GlobalCalc;
481 } else {
482 // This is needed because this function still gets called with FLOP logging disabled, just nothing is done with it
483 return std::numeric_limits<size_t>::max();
484 }
485 }
486
487 [[nodiscard]] double getHitRate() const override {
488 if constexpr (countFLOPs) {
489 const size_t numDistCallsAcc =
490 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
491 [](size_t sum, const auto &data) { return sum + data.numDistCalls; });
492 const size_t numKernelCallsN3Acc =
493 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
494 [](size_t sum, const auto &data) { return sum + data.numKernelCallsN3; });
495 const size_t numKernelCallsNoN3Acc =
496 std::accumulate(_aosThreadDataFLOPs.begin(), _aosThreadDataFLOPs.end(), 0ul,
497 [](size_t sum, const auto &data) { return sum + data.numKernelCallsNoN3; });
498
499 return (static_cast<double>(numKernelCallsNoN3Acc) + static_cast<double>(numKernelCallsN3Acc)) /
500 (static_cast<double>(numDistCallsAcc));
501 } else {
502 // This is needed because this function still gets called with FLOP logging disabled, just nothing is done with it
503 return std::numeric_limits<double>::quiet_NaN();
504 }
505 }
506
507 private:
508 template <bool newton3>
509 void SoAFunctorVerletImpl(autopas::SoAView<SoAArraysType> soa, const size_t indexFirst,
510 const std::vector<size_t, autopas::AlignedAllocator<size_t>> &neighborList) {
511 autopas::utils::ExceptionHandler::exception("AxilrodTellerMutoFunctor::SoAFunctorVerletImpl() is not implemented.");
512 }
513
517 class AoSThreadDataGlobals {
518 public:
519 AoSThreadDataGlobals() : virialSum{0., 0., 0.}, potentialEnergySum{0.}, __remainingTo64{} {}
520 void setZero() {
521 virialSum = {0., 0., 0.};
522 potentialEnergySum = 0.;
523 }
524
525 // variables
526 std::array<double, 3> virialSum;
527 double potentialEnergySum;
528
529 private:
530 // dummy parameter to get the right size (64 bytes)
531 double __remainingTo64[(64 - 4 * sizeof(double)) / sizeof(double)];
532 };
533
541 class AoSThreadDataFLOPs {
542 public:
543 AoSThreadDataFLOPs() : __remainingTo64{} {}
544
548 void setZero() {
549 numKernelCallsNoN3 = 0;
550 numKernelCallsN3 = 0;
551 numDistCalls = 0;
552 numGlobalCalcsN3 = 0;
553 numGlobalCalcsNoN3 = 0;
554 }
555
560 size_t numKernelCallsNoN3 = 0;
561
566 size_t numKernelCallsN3 = 0;
567
572 size_t numDistCalls = 0;
573
577 size_t numGlobalCalcsN3 = 0;
578
582 size_t numGlobalCalcsNoN3 = 0;
583
584 private:
588 double __remainingTo64[(64 - 5 * sizeof(size_t)) / sizeof(size_t)];
589 };
590
591 // make sure of the size of AoSThreadDataGlobals
592 static_assert(sizeof(AoSThreadDataGlobals) % 64 == 0, "AoSThreadDataGlobals has wrong size");
593 static_assert(sizeof(AoSThreadDataFLOPs) % 64 == 0, "AoSThreadDataFLOPs has wrong size");
594
595 const double _cutoffSquared;
596
597 // Parameter of the Axilrod-Teller-Muto potential
598 // not const because they might be reset through PPL
599 double _nu = 0.0;
600
602
603 // sum of the potential energy, only calculated if calculateGlobals is true
604 double _potentialEnergySum;
605
606 // sum of the virial, only calculated if calculateGlobals is true
607 std::array<double, 3> _virialSum;
608
609 // thread buffer for aos
610 std::vector<AoSThreadDataGlobals> _aosThreadDataGlobals;
611 std::vector<AoSThreadDataFLOPs> _aosThreadDataFLOPs{};
612
613 // defines whether or whether not the global values are already preprocessed
614 bool _postProcessed;
615};
616} // namespace mdLib
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
This class stores the (physical) properties of molecule types, and, in the case of multi-site molecul...
Definition: ParticlePropertiesLibrary.h:28
floatType getMixingNu(intType i, intType j, intType k) const
Returns the precomputed mixed epsilon * 24.
Definition: ParticlePropertiesLibrary.h:283
AlignedAllocator class.
Definition: AlignedAllocator.h:29
View on a fixed part of a SoA between a start index and an end index.
Definition: SoAView.h:23
TriwiseFunctor class.
Definition: TriwiseFunctor.h:28
TriwiseFunctor(double cutoff)
Constructor.
Definition: TriwiseFunctor.h:39
Default exception class for autopas exceptions.
Definition: ExceptionHandler.h:115
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
A functor to handle Axilrod-Teller-Muto interactions between three particles (molecules).
Definition: AxilrodTellerMutoFunctor.h:101
static constexpr auto getComputedAttr()
Get attributes computed by this functor.
Definition: AxilrodTellerMutoFunctor.h:328
void AoSFunctor(Particle_T &i, Particle_T &j, Particle_T &k, bool newton3) final
TriwiseFunctor for arrays of structures (AoS).
Definition: AxilrodTellerMutoFunctor.h:182
static constexpr bool getMixing()
Definition: AxilrodTellerMutoFunctor.h:337
double getHitRate() const override
Get the hit rate.
Definition: AxilrodTellerMutoFunctor.h:487
size_t getNumFLOPs() const override
Gets the number of useful FLOPs.
Definition: AxilrodTellerMutoFunctor.h:454
AxilrodTellerMutoFunctor()=delete
Deleted default constructor.
bool allowsNewton3() final
Specifies whether the functor is capable of Newton3-like functors.
Definition: AxilrodTellerMutoFunctor.h:174
void initTraversal() final
Reset the global values.
Definition: AxilrodTellerMutoFunctor.h:343
static constexpr auto getNeededAttr(std::false_type)
Get attributes needed for computation without N3 optimization.
Definition: AxilrodTellerMutoFunctor.h:318
bool isRelevantForTuning() final
Specifies whether the functor should be considered for the auto-tuning process.
Definition: AxilrodTellerMutoFunctor.h:172
bool allowsNonNewton3() final
Specifies whether the functor is capable of non-Newton3-like functors.
Definition: AxilrodTellerMutoFunctor.h:178
AxilrodTellerMutoFunctor(double cutoff, ParticlePropertiesLibrary< double, size_t > &particlePropertiesLibrary)
Constructor for Functor with mixing active.
Definition: AxilrodTellerMutoFunctor.h:162
double getVirial()
Get the virial.
Definition: AxilrodTellerMutoFunctor.h:406
void setParticleProperties(SoAFloatPrecision nu)
Sets the particle properties constants for this functor.
Definition: AxilrodTellerMutoFunctor.h:298
double getPotentialEnergy()
Get the potential Energy.
Definition: AxilrodTellerMutoFunctor.h:388
std::string getName() final
Returns name of functor.
Definition: AxilrodTellerMutoFunctor.h:170
void endTraversal(bool newton3) final
Accumulates global values, e.g.
Definition: AxilrodTellerMutoFunctor.h:356
AxilrodTellerMutoFunctor(double cutoff)
Constructor for Functor with mixing disabled.
Definition: AxilrodTellerMutoFunctor.h:150
static constexpr auto getNeededAttr()
Get attributes needed for computation.
Definition: AxilrodTellerMutoFunctor.h:303
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
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
FunctorN3Modes
Newton 3 modes for the Functor.
Definition: Functor.h:22
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132