Molecular Dynamics Simulation  1.0
Particle.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <array>
11 #include <memory>
12 #include <mutex>
13 #include <string>
14 #include <vector>
15 
16 #include "utils/ArrayUtils.h"
17 #include "utils/Enums.h"
18 
26 class Particle {
27  private:
31  std::array<double, 3> x{};
32 
36  std::array<double, 3> v{};
37 
41  std::array<double, 3> f{};
42 
46  std::array<double, 3> old_f{};
47 
51  double m;
52 
57  int type;
58 
62  double epsilon;
63 
67  double sigma;
68 
73 
74 #if PARALLEL_V2
78  mutable std::mutex mutex_f;
79 #endif
80 
89  std::vector<std::tuple<long, double, double>> connected_particles;
90 
91  public:
92  Particle(const Particle& other);
93 
94  Particle(std::array<double, 3> x_arg, std::array<double, 3> v_arg, double m_arg, int type = 0, double epsilon_arg = 1.0,
95  double sigma_arg = 1.2, LockState lock_state = LockState::UNLOCKED);
96 
97  Particle(std::array<double, 3> x_arg, std::array<double, 3> v_arg, std::array<double, 3> f_arg, std::array<double, 3> old_f_arg,
98  double m_arg, int type = 0, double epsilon_arg = 1.0, double sigma_arg = 1.2, LockState lock_state = LockState::UNLOCKED);
99 
100  virtual ~Particle();
101 
102  Particle& operator=(const Particle& other);
103 
109  inline void setX(const std::array<double, 3>& x) { this->x = x; }
110 
116  inline void setV(const std::array<double, 3>& v) { this->v = v; }
117 
123  inline void setF(const std::array<double, 3>& f) { this->f = f; }
124 
129  inline void addF(const std::array<double, 3>& force) {
130 #if PARALLEL_V2
131  std::lock_guard lock(mutex_f);
132 #endif
133  f = f + force;
134  }
135 
140  inline void subF(const std::array<double, 3>& force) {
141 #if PARALLEL_V2
142  std::lock_guard lock(mutex_f);
143 #endif
144  f = f - force;
145  }
146 
152  inline void setOldF(const std::array<double, 3>& oldF) { old_f = oldF; }
153 
157  [[nodiscard]] inline const std::array<double, 3>& getX() const { return x; }
158 
162  [[nodiscard]] inline const std::array<double, 3>& getV() const { return v; }
163 
167  [[nodiscard]] inline const std::array<double, 3>& getF() const { return f; }
168 
172  [[nodiscard]] inline const std::array<double, 3>& getOldF() const { return old_f; }
173 
177  [[nodiscard]] inline double getM() const { return m; }
178 
182  [[nodiscard]] inline int getType() const { return type; }
183 
187  [[nodiscard]] inline double getEpsilon() const { return epsilon; }
188 
192  [[nodiscard]] inline double getSigma() const { return sigma; }
193 
197  inline void setLocked(LockState new_lock_state) { lock_state = new_lock_state; }
198 
202  [[nodiscard]] inline bool isLocked() const { return lock_state == LockState::LOCKED; }
203 
207  [[nodiscard]] inline const std::vector<std::tuple<long, double, double>>& getConnectedParticles() const { return connected_particles; }
208 
216  void addConnectedParticle(long ptr_diff, double l_0, double k);
217 
218  bool operator==(Particle& other);
219 
220  bool operator==(const Particle& other) const;
221 
222  [[nodiscard]] std::string toString() const;
223 };
224 
225 std::ostream& operator<<(std::ostream& stream, Particle& p);
LockState
Enum class to define the lockstate of particles. Locked particles are not allowed to move.
Definition: Enums.h:12
std::ostream & operator<<(std::ostream &stream, Particle &p)
Definition: Particle.cpp:90
Class to represent a particle.
Definition: Particle.h:26
void setF(const std::array< double, 3 > &f)
Sets the force of the particle.
Definition: Particle.h:123
std::array< double, 3 > x
Position of the particle.
Definition: Particle.h:31
double epsilon
Lennard-Jones potential parameter epsilon.
Definition: Particle.h:62
std::array< double, 3 > v
Velocity of the particle.
Definition: Particle.h:36
double getSigma() const
Gets the Lennard-Jones potential parameter sigma.
Definition: Particle.h:192
const std::array< double, 3 > & getV() const
Gets the velocity of the particle.
Definition: Particle.h:162
Particle(const Particle &other)
Definition: Particle.cpp:15
int type
Type of the particle. Use it for whatever you want (e.g. to separate molecules belonging to different...
Definition: Particle.h:57
std::string toString() const
Definition: Particle.cpp:76
bool isLocked() const
Gets whether the particle is locked in space.
Definition: Particle.h:202
bool operator==(Particle &other)
Definition: Particle.cpp:82
Particle & operator=(const Particle &other)
Definition: Particle.cpp:59
void subF(const std::array< double, 3 > &force)
Subtracts a force from the particle.
Definition: Particle.h:140
void addConnectedParticle(long ptr_diff, double l_0, double k)
Adds a connected particle.
Definition: Particle.cpp:74
std::array< double, 3 > f
Force effective on this particle.
Definition: Particle.h:41
int getType() const
Gets the type of the particle.
Definition: Particle.h:182
const std::array< double, 3 > & getX() const
Gets the position of the particle.
Definition: Particle.h:157
std::array< double, 3 > old_f
Force which was effective on this particle.
Definition: Particle.h:46
void addF(const std::array< double, 3 > &force)
Adds a force to the particle.
Definition: Particle.h:129
void setOldF(const std::array< double, 3 > &oldF)
Sets the old force of the particle.
Definition: Particle.h:152
double getM() const
Gets the mass of the particle.
Definition: Particle.h:177
double sigma
Lennard-Jones potential parameter sigma.
Definition: Particle.h:67
void setX(const std::array< double, 3 > &x)
Sets the position of the particle.
Definition: Particle.h:109
void setV(const std::array< double, 3 > &v)
Sets the velocity of the particle.
Definition: Particle.h:116
LockState lock_state
Wheter the particle is loccked in space.
Definition: Particle.h:72
double getEpsilon() const
Gets the Lennard-Jones potential parameter epsilon.
Definition: Particle.h:187
virtual ~Particle()
Definition: Particle.cpp:57
const std::array< double, 3 > & getF() const
Gets the total force of the particle.
Definition: Particle.h:167
void setLocked(LockState new_lock_state)
Set wheter the particle is locked in space.
Definition: Particle.h:197
const std::array< double, 3 > & getOldF() const
Gets the old total force of the particle.
Definition: Particle.h:172
const std::vector< std::tuple< long, double, double > > & getConnectedParticles() const
Gets the list of connected particles.
Definition: Particle.h:207
double m
Mass of this particle.
Definition: Particle.h:51
std::vector< std::tuple< long, double, double > > connected_particles
List of connected particles.
Definition: Particle.h:89