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
120 bool operator==(const ParticleBase &rhs) const {
121 return std::tie(_r, _v, _f, _id) == std::tie(rhs._r, rhs._v, rhs._f, rhs._id);
122 }
123
129 bool operator!=(const ParticleBase &rhs) const { return not(rhs == *this); }
130
135 [[nodiscard]] const std::array<double, 3> &getF() const { return _f; }
136
141 void setF(const std::array<double, 3> &f) { _f = f; }
142
147 void addF(const std::array<double, 3> &f) {
148 using namespace autopas::utils::ArrayMath::literals;
149 _f += f;
150 }
151
156 void subF(const std::array<double, 3> &f) {
157 using namespace autopas::utils::ArrayMath::literals;
158 _f -= f;
159 }
160
165 idType getID() const { return _id; }
166
171 void setID(idType id) { _id = id; }
172
177 [[nodiscard]] const std::array<double, 3> &getR() const { return _r; }
178
183 void setR(const std::array<double, 3> &r) { _r = r; }
184
185#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
190 [[nodiscard]] const std::array<double, 3> &getRAtRebuild() const { return _rAtRebuild; }
195 void setRAtRebuild(const std::array<double, 3> &r) { _rAtRebuild = r; }
196
200 void resetRAtRebuild() { this->setRAtRebuild(_r); }
201
207 const std::array<double, 3> calculateDisplacementSinceRebuild() const {
208 return utils::ArrayMath::sub(_rAtRebuild, _r);
209 }
210#endif
211
221 bool setRDistanceCheck(const std::array<double, 3> &r, double maxDistSquared) {
222 using namespace autopas::utils::ArrayMath::literals;
223 const auto distanceVec = r - _r;
224 const double distanceSquared = utils::ArrayMath::dot(distanceVec, distanceVec);
225 setR(r);
226 const bool distanceIsFine =
227 distanceSquared < maxDistSquared or autopas::utils::Math::isNearAbs(maxDistSquared, 0., 1e-12);
228 if (not distanceIsFine) {
229 AutoPasLog(WARN, "Particle {}: Distance between old and new position is larger than expected: {} > {}", _id,
230 distanceSquared, maxDistSquared);
231 }
232 return distanceIsFine;
233 }
234
239 void addR(const std::array<double, 3> &r) {
240 using namespace autopas::utils::ArrayMath::literals;
241 _r += r;
242 }
243
255 bool addRDistanceCheck(const std::array<double, 3> &r, double maxDistSquared) {
256 using namespace autopas::utils::ArrayMath::literals;
257 const auto newR = _r + r;
258 return setRDistanceCheck(newR, maxDistSquared);
259 }
260
265 [[nodiscard]] const std::array<double, 3> &getV() const { return _v; }
266
271 void setV(const std::array<double, 3> &v) { _v = v; }
272
277 void addV(const std::array<double, 3> &v) {
278 using namespace autopas::utils::ArrayMath::literals;
279 _v += v;
280 }
281
286 [[nodiscard]] virtual std::string toString() const {
287 std::ostringstream text;
288 // clang-format off
289 text << "Particle"
290 << "\nID : " << _id
291 << "\nPosition: "
293 << "\nVelocity: "
295 << "\nForce : "
297 << "\nOwnershipState : "
299 // clang-format on
300 return text.str();
301 }
306 [[nodiscard]] bool isOwned() const { return _ownershipState == OwnershipState::owned; }
307
313 [[nodiscard]] bool isHalo() const { return _ownershipState == OwnershipState::halo; }
314
319 [[nodiscard]] bool isDummy() const { return _ownershipState == OwnershipState::dummy; }
320
325 [[nodiscard]] OwnershipState getOwnershipState() const { return _ownershipState; }
326
331 void setOwnershipState(OwnershipState ownershipState) { _ownershipState = ownershipState; }
332
336 enum AttributeNames : int { ptr, id, posX, posY, posZ, forceX, forceY, forceZ, ownershipState };
337
341 using ParticleSoAFloatPrecision = floatType;
342
346 using ParticleIdType = idType;
347
353 typename autopas::utils::SoAType<ParticleBase<floatType, idType> *, idType /*id*/, floatType /*x*/,
354 floatType /*y*/, floatType /*z*/, floatType /*fx*/, floatType /*fy*/,
355 floatType /*fz*/, OwnershipState /*ownershipState*/>::Type;
356
362 template <AttributeNames attribute, std::enable_if_t<attribute == AttributeNames::ptr, bool> = true>
363 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() {
364 return this;
365 }
366
373 template <AttributeNames attribute, std::enable_if_t<attribute != AttributeNames::ptr, bool> = true>
374 constexpr typename std::tuple_element<attribute, SoAArraysType>::type::value_type get() const {
375 if constexpr (attribute == AttributeNames::id) {
376 return getID();
377 } else if constexpr (attribute == AttributeNames::posX) {
378 return getR()[0];
379 } else if constexpr (attribute == AttributeNames::posY) {
380 return getR()[1];
381 } else if constexpr (attribute == AttributeNames::posZ) {
382 return getR()[2];
383 } else if constexpr (attribute == AttributeNames::forceX) {
384 return getF()[0];
385 } else if constexpr (attribute == AttributeNames::forceY) {
386 return getF()[1];
387 } else if constexpr (attribute == AttributeNames::forceZ) {
388 return getF()[2];
389 } else if constexpr (attribute == AttributeNames::ownershipState) {
390 return this->_ownershipState;
391 } else {
392 utils::ExceptionHandler::exception("ParticleBase::get() unknown attribute {}", attribute);
393 }
394 }
395
402 template <AttributeNames attribute>
403 constexpr void set(typename std::tuple_element<attribute, SoAArraysType>::type::value_type value) {
404 if constexpr (attribute == AttributeNames::id) {
405 setID(value);
406 } else if constexpr (attribute == AttributeNames::posX) {
407 _r[0] = value;
408 } else if constexpr (attribute == AttributeNames::posY) {
409 _r[1] = value;
410 } else if constexpr (attribute == AttributeNames::posZ) {
411 _r[2] = value;
412 } else if constexpr (attribute == AttributeNames::forceX) {
413 _f[0] = value;
414 } else if constexpr (attribute == AttributeNames::forceY) {
415 _f[1] = value;
416 } else if constexpr (attribute == AttributeNames::forceZ) {
417 _f[2] = value;
418 } else if constexpr (attribute == AttributeNames::ownershipState) {
419 this->_ownershipState = value;
420 } else {
421 utils::ExceptionHandler::exception("MoleculeLJ::set() unknown attribute {}", attribute);
422 }
423 }
424
425 private:
432 void markAsDeleted() {
433 // Set ownership as dummy.
435 }
436
441 template <class T>
443};
444
453template <typename floatType, typename idType>
454std::ostream &operator<<(std::ostream &os, const ParticleBase<floatType, idType> &particle) {
455 using utils::ArrayUtils::operator<<;
456 os << "Particle"
457 << "\nID : " << particle._id << "\nPosition: " << particle._r << "\nVelocity: " << particle._v
458 << "\nForce : " << particle._f << "\nOwnershipState : " << particle._ownershipState;
459 // clang-format on
460 return os;
461}
462
463} // 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:147
bool isHalo() const
Defines whether the particle is a halo particle, i.e., not owned by the current AutoPas object (aka (...
Definition: ParticleBase.h:313
bool isOwned() const
Defines whether the particle is owned by the current AutoPas object (aka (MPI-)process)
Definition: ParticleBase.h:306
void addR(const std::array< double, 3 > &r)
Add a distance vector to the position of the particle.
Definition: ParticleBase.h:239
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:171
virtual std::string toString() const
Creates a string containing all data of the particle.
Definition: ParticleBase.h:286
floatType ParticleSoAFloatPrecision
Floating Point Type used for this particle.
Definition: ParticleBase.h:341
bool isDummy() const
Returns whether the particle is a dummy particle.
Definition: ParticleBase.h:319
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:183
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:355
void setV(const std::array< double, 3 > &v)
Set the velocity of the particle.
Definition: ParticleBase.h:271
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:336
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:156
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:221
OwnershipState getOwnershipState() const
Returns the particle's ownership state.
Definition: ParticleBase.h:325
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:374
bool operator==(const ParticleBase &rhs) const
Equality operator for ParticleBase class.
Definition: ParticleBase.h:120
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:331
void addV(const std::array< double, 3 > &v)
Add a vector to the current velocity of the particle.
Definition: ParticleBase.h:277
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:403
idType getID() const
Get the id of the particle.
Definition: ParticleBase.h:165
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:255
void setF(const std::array< double, 3 > &f)
Set the force acting on the particle.
Definition: ParticleBase.h:141
const std::array< double, 3 > & getV() const
Get the velocity of the particle.
Definition: ParticleBase.h:265
bool operator!=(const ParticleBase &rhs) const
Not-Equals operator for ParticleBase class.
Definition: ParticleBase.h:129
constexpr std::tuple_element< attribute, SoAArraysType >::type::value_type get()
Non-const getter for the pointer of this object.
Definition: ParticleBase.h:363
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:346
const std::array< double, 3 > & getF() const
get the force acting on the particle
Definition: ParticleBase.h:135
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:177
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:102
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