AutoPas  3.0.0
Loading...
Searching...
No Matches
ParticleBase.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <sstream>
12#include <tuple>
13
21
22namespace autopas {
23
32template <typename floatType, typename idType>
34 public:
36 : _r({0.0, 0.0, 0.0}),
37 _v({0., 0., 0.}),
38 _f({0.0, 0.0, 0.0}),
39 _id(0),
41#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
42 ,
43 _rAtRebuild({0.0, 0.0, 0.0})
44#endif
45 {
46 }
47
55 ParticleBase(const std::array<double, 3> &r, const std::array<double, 3> &v, idType id,
57 : _r(r),
58 _v(v),
59 _f({0.0, 0.0, 0.0}),
60 _id(id),
61 _ownershipState(ownershipState)
62#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
63 ,
64 _rAtRebuild(r)
65#endif
66 {
67 }
68
72 virtual ~ParticleBase() = default;
73
74 protected:
78 std::array<floatType, 3> _r;
79
80#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
84 std::array<floatType, 3> _rAtRebuild;
85#endif
86
90 std::array<floatType, 3> _v;
91
95 std::array<floatType, 3> _f;
96
100 idType _id;
101
106
107 public:
112 template <typename T, typename P>
113 friend std::ostream &operator<<(std::ostream &os, const autopas::ParticleBase<T, P> &D);
114
119 [[nodiscard]] const std::array<double, 3> &getF() const { return _f; }
120
125 void setF(const std::array<double, 3> &f) { _f = f; }
126
131 void addF(const std::array<double, 3> &f) {
132 using namespace autopas::utils::ArrayMath::literals;
133 _f += f;
134 }
135
140 void subF(const std::array<double, 3> &f) {
141 using namespace autopas::utils::ArrayMath::literals;
142 _f -= f;
143 }
144
149 idType getID() const { return _id; }
150
155 void setID(idType id) { _id = id; }
156
161 [[nodiscard]] const std::array<double, 3> &getR() const { return _r; }
162
167 void setR(const std::array<double, 3> &r) { _r = r; }
168
169#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
174 [[nodiscard]] const std::array<double, 3> &getRAtRebuild() const { return _rAtRebuild; }
179 void setRAtRebuild(const std::array<double, 3> &r) { _rAtRebuild = r; }
180
184 void resetRAtRebuild() { this->setRAtRebuild(_r); }
185
191 const std::array<double, 3> calculateDisplacementSinceRebuild() const {
192 return utils::ArrayMath::sub(_rAtRebuild, _r);
193 }
194#endif
195
205 bool setRDistanceCheck(const std::array<double, 3> &r, double maxDistSquared) {
206 using namespace autopas::utils::ArrayMath::literals;
207 const auto distanceVec = r - _r;
208 const double distanceSquared = utils::ArrayMath::dot(distanceVec, distanceVec);
209 setR(r);
210 const bool distanceIsFine =
211 distanceSquared < maxDistSquared or autopas::utils::Math::isNearAbs(maxDistSquared, 0., 1e-12);
212 if (not distanceIsFine) {
213 AutoPasLog(WARN, "Particle {}: Distance between old and new position is larger than expected: {} > {}", _id,
214 distanceSquared, maxDistSquared);
215 }
216 return distanceIsFine;
217 }
218
223 void addR(const std::array<double, 3> &r) {
224 using namespace autopas::utils::ArrayMath::literals;
225 _r += r;
226 }
227
239 bool addRDistanceCheck(const std::array<double, 3> &r, double maxDistSquared) {
240 using namespace autopas::utils::ArrayMath::literals;
241 const auto newR = _r + r;
242 return setRDistanceCheck(newR, maxDistSquared);
243 }
244
249 [[nodiscard]] const std::array<double, 3> &getV() const { return _v; }
250
255 void setV(const std::array<double, 3> &v) { _v = v; }
256
261 void addV(const std::array<double, 3> &v) {
262 using namespace autopas::utils::ArrayMath::literals;
263 _v += v;
264 }
265
270 [[nodiscard]] virtual std::string toString() const {
271 std::ostringstream text;
272 // clang-format off
273 text << "Particle"
274 << "\nID : " << _id
275 << "\nPosition: "
277 << "\nVelocity: "
279 << "\nForce : "
281 << "\nOwnershipState : "
283 // clang-format on
284 return text.str();
285 }
290 [[nodiscard]] bool isOwned() const { return _ownershipState == OwnershipState::owned; }
291
297 [[nodiscard]] bool isHalo() const { return _ownershipState == OwnershipState::halo; }
298
303 [[nodiscard]] bool isDummy() const { return _ownershipState == OwnershipState::dummy; }
304
309 [[nodiscard]] OwnershipState getOwnershipState() const { return _ownershipState; }
310
315 void setOwnershipState(OwnershipState ownershipState) { _ownershipState = ownershipState; }
316
320 enum AttributeNames : int { ptr, id, posX, posY, posZ, forceX, forceY, forceZ, ownershipState };
321
325 using ParticleSoAFloatPrecision = floatType;
326
330 using ParticleIdType = idType;
331
337 typename autopas::utils::SoAType<ParticleBase<floatType, idType> *, idType /*id*/, floatType /*x*/,
338 floatType /*y*/, floatType /*z*/, floatType /*fx*/, floatType /*fy*/,
339 floatType /*fz*/, OwnershipState /*ownershipState*/>::Type;
340
346 template <AttributeNames attribute, std::enable_if_t<attribute == AttributeNames::ptr, bool> = true>
347 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() {
348 return this;
349 }
350
357 template <AttributeNames attribute, std::enable_if_t<attribute != AttributeNames::ptr, bool> = true>
358 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() const {
359 if constexpr (attribute == AttributeNames::id) {
360 return getID();
361 } else if constexpr (attribute == AttributeNames::posX) {
362 return getR()[0];
363 } else if constexpr (attribute == AttributeNames::posY) {
364 return getR()[1];
365 } else if constexpr (attribute == AttributeNames::posZ) {
366 return getR()[2];
367 } else if constexpr (attribute == AttributeNames::forceX) {
368 return getF()[0];
369 } else if constexpr (attribute == AttributeNames::forceY) {
370 return getF()[1];
371 } else if constexpr (attribute == AttributeNames::forceZ) {
372 return getF()[2];
373 } else if constexpr (attribute == AttributeNames::ownershipState) {
374 return this->_ownershipState;
375 } else {
376 utils::ExceptionHandler::exception("ParticleBase::get() unknown attribute {}", attribute);
377 }
378 }
379
386 template <AttributeNames attribute>
387 constexpr void set(typename std::tuple_element<attribute, SoAArraysType>::type::value_type value) {
388 if constexpr (attribute == AttributeNames::id) {
389 setID(value);
390 } else if constexpr (attribute == AttributeNames::posX) {
391 _r[0] = value;
392 } else if constexpr (attribute == AttributeNames::posY) {
393 _r[1] = value;
394 } else if constexpr (attribute == AttributeNames::posZ) {
395 _r[2] = value;
396 } else if constexpr (attribute == AttributeNames::forceX) {
397 _f[0] = value;
398 } else if constexpr (attribute == AttributeNames::forceY) {
399 _f[1] = value;
400 } else if constexpr (attribute == AttributeNames::forceZ) {
401 _f[2] = value;
402 } else if constexpr (attribute == AttributeNames::ownershipState) {
403 this->_ownershipState = value;
404 } else {
405 utils::ExceptionHandler::exception("MoleculeLJ::set() unknown attribute {}", attribute);
406 }
407 }
408
409 private:
416 void markAsDeleted() {
417 // Set ownership as dummy.
419 }
420
425 template <class T>
427};
428
437template <typename floatType, typename idType>
438std::ostream &operator<<(std::ostream &os, const ParticleBase<floatType, idType> &particle) {
439 using utils::ArrayUtils::operator<<;
440 os << "Particle"
441 << "\nID : " << particle._id << "\nPosition: " << particle._r << "\nVelocity: " << particle._v
442 << "\nForce : " << particle._f << "\nOwnershipState : " << particle._ownershipState;
443 // clang-format on
444 return os;
445}
446
447} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
Provides a struct to easily generate the desired SoAType.
Minimal definition of a basic particle.
Definition: ParticleBase.h:33
void addF(const std::array< double, 3 > &f)
Add a partial force to the force acting on the particle.
Definition: ParticleBase.h:131
bool isHalo() const
Defines whether the particle is a halo particle, i.e., not owned by the current AutoPas object (aka (...
Definition: ParticleBase.h:297
bool isOwned() const
Defines whether the particle is owned by the current AutoPas object (aka (MPI-)process)
Definition: ParticleBase.h:290
void addR(const std::array< double, 3 > &r)
Add a distance vector to the position of the particle.
Definition: ParticleBase.h:223
friend std::ostream & operator<<(std::ostream &os, const autopas::ParticleBase< T, P > &D)
Stream operator for instances of ParticleBase class.
void setID(idType id)
Set the id of the particle.
Definition: ParticleBase.h:155
virtual std::string toString() const
Creates a string containing all data of the particle.
Definition: ParticleBase.h:270
floatType ParticleSoAFloatPrecision
Floating Point Type used for this particle.
Definition: ParticleBase.h:325
bool isDummy() const
Returns whether the particle is a dummy particle.
Definition: ParticleBase.h:303
std::array< floatType, 3 > _r
Particle position as 3D coordinates.
Definition: ParticleBase.h:78
void setR(const std::array< double, 3 > &r)
Set the position of the particle.
Definition: ParticleBase.h:167
typename autopas::utils::SoAType< ParticleBase< floatType, idType > *, idType, floatType, floatType, floatType, floatType, floatType, floatType, OwnershipState >::Type SoAArraysType
The type for the soa storage.
Definition: ParticleBase.h:339
void setV(const std::array< double, 3 > &v)
Set the velocity of the particle.
Definition: ParticleBase.h:255
OwnershipState _ownershipState
Defines the state of the ownership of the particle.
Definition: ParticleBase.h:105
AttributeNames
Enums used as ids for accessing and creating a dynamically sized SoA.
Definition: ParticleBase.h:320
virtual ~ParticleBase()=default
Destructor of ParticleBase class.
void subF(const std::array< double, 3 > &f)
Substract a partial force from the force acting on the particle.
Definition: ParticleBase.h:140
bool setRDistanceCheck(const std::array< double, 3 > &r, double maxDistSquared)
Add a distance vector to the position of the particle and check if the distance between the old and n...
Definition: ParticleBase.h:205
OwnershipState getOwnershipState() const
Returns the particle's ownership state.
Definition: ParticleBase.h:309
idType _id
Particle id.
Definition: ParticleBase.h:100
constexpr std::tuple_element< attribute, SoAArraysType >::type::value_type get() const
Getter, which allows access to an attribute using the corresponding attribute name (defined in Attrib...
Definition: ParticleBase.h:358
std::array< floatType, 3 > _f
Force the particle experiences as 3D vector.
Definition: ParticleBase.h:95
void setOwnershipState(OwnershipState ownershipState)
Set the OwnershipState to the given value.
Definition: ParticleBase.h:315
void addV(const std::array< double, 3 > &v)
Add a vector to the current velocity of the particle.
Definition: ParticleBase.h:261
constexpr void set(typename std::tuple_element< attribute, SoAArraysType >::type::value_type value)
Setter, which allows set an attribute using the corresponding attribute name (defined in AttributeNam...
Definition: ParticleBase.h:387
idType getID() const
Get the id of the particle.
Definition: ParticleBase.h:149
bool addRDistanceCheck(const std::array< double, 3 > &r, double maxDistSquared)
Add a distance vector to the position of the particle and check if the distance between the old and n...
Definition: ParticleBase.h:239
void setF(const std::array< double, 3 > &f)
Set the force acting on the particle.
Definition: ParticleBase.h:125
const std::array< double, 3 > & getV() const
Get the velocity of the particle.
Definition: ParticleBase.h:249
constexpr std::tuple_element< attribute, SoAArraysType >::type::value_type get()
Non-const getter for the pointer of this object.
Definition: ParticleBase.h:347
std::array< floatType, 3 > _v
Particle velocity as 3D vector.
Definition: ParticleBase.h:90
idType ParticleIdType
Id Type used for this particle.
Definition: ParticleBase.h:330
const std::array< double, 3 > & getF() const
get the force acting on the particle
Definition: ParticleBase.h:119
ParticleBase(const std::array< double, 3 > &r, const std::array< double, 3 > &v, idType id, OwnershipState ownershipState=OwnershipState::owned)
Constructor of the Particle class.
Definition: ParticleBase.h:55
const std::array< double, 3 > & getR() const
Get the position of the particle.
Definition: ParticleBase.h:161
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
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, SIZE > sub(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Subtracts array b from array a and returns the result.
Definition: ArrayMath.h:45
void to_string(std::ostream &os, const Container &container, const std::string &delimiter, const std::array< std::string, 2 > &surround, Fun elemToString)
Generates a string representation of a container which fulfills the Container requirement (provide cb...
Definition: ArrayUtils.h:54
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
static std::ostream & operator<<(std::ostream &os, const OwnershipState &ownershipState)
Insertion operator for OwnershipState.
Definition: OwnershipState.h:65
OwnershipState
Enum that specifies the state of ownership.
Definition: OwnershipState.h:19
@ dummy
Dummy or deleted state, a particle with this state is not an actual particle!
@ halo
Halo state, a particle with this state is an actual particle, but not owned by the current AutoPas ob...
@ owned
Owned state, a particle with this state is an actual particle and owned by the current AutoPas object...
Type
Enum describing all types that are allowed in a rule program.
Definition: RuleBasedProgramTree.h:10
Helper struct to get a the SoAType.
Definition: SoAType.h:23