.Simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Functions.h
1 
2 #ifndef __FUNCTIONS_H__
3 #define __FUNCTIONS_H__
4 
5 #include <vector>
6 #include <functional>
7 #include <numeric>
8 
9 #include "../Constants/Constants.h"
10 #include "../Particle/Particle.h"
11 
12 namespace MDSimulation
13 {
17  inline double
18  max_mag(const double t1, const double t2)
19  {
20  return std::abs(t1) >= std::abs(t2) ? t1 : t2;
21  }
22 
26  template <typename T>
27  inline T sign(const T n)
28  {
29  return ((T) (n > 0));
30  }
31 
32  template <typename T>
33  inline double Sqr(const T x)
34  {
35  return x * x;
36  }
37 
38  template <typename T>
39  inline double Pow3(const T x)
40  {
41  return x * x * x;
42  }
43 
44  template <typename T>
45  T& identity(T& x)
46  {
47  return x;
48  }
49 
50  inline double
51  mean_velocity(const std::vector<Particle>& arr)
52  {
53  assert(arr.size() > 0);
54  double result = std::accumulate(arr.begin(), arr.end(), 0.0,
55  [](double d, Particle p) { return d + norm2(p.Velocity); });
56 
57  return result / (double) arr.size();
58  }
59 
60  inline double
61  median_velocity(const std::vector<Particle>& arr)
62  {
63  assert(arr.size() > 0);
64  const size_t size = arr.size();
65  const size_t mid = size / 2;
66  std::vector<double> vel(size);
67 
68  std::transform(arr.begin(), arr.end(), vel.begin(),
69  [](const Particle& p)
70  {
71  return norm2(p.Velocity);
72  });
73 
74  if (size % 2 == 0)
75  {
76  double v1;
77  nth_element(vel.begin(), vel.begin()+mid, vel.end());
78  v1 = vel[mid];
79  nth_element(vel.begin(), vel.begin()+mid-1, vel.end());
80 
81  return (v1 + vel[mid-1]) / 2.0;
82  }
83  else
84  {
85  nth_element(vel.begin(), vel.begin() + mid, vel.end());
86  return vel[mid];
87  }
88  }
89 
102  inline double
104  const DoubleVector& p2, const DoubleVector& v2,
105  double diameter)
106  {
107  const DoubleVector dr(p1 - p2);
108  const DoubleVector dv(v1 - v2);
109  const double b = dot(dr, dv);
110 
111  // We are interested in the minimum squared diameter.
112  diameter *= diameter;
113 
114  if (b < 0.0)
115  {
116  const double rr = dot(dr, dr);
117  const double vv = dot(dv, dv);
118 
119  if (rr < diameter)
120  {
121  diameter = rr * 0.99;
122  }
123 
124  const double d = Sqr(b) - vv * (rr - diameter);
125 
126  if (d >= 0.0)
127  {
128  return -(sqrt(d) + b) / vv;
129  }
130  }
131 
132  return Constants::NEVER;
133  }
134 
139  inline void
140  process_collision(const DoubleVector& p1, DoubleVector& v1, const double m1,
141  const DoubleVector& p2, DoubleVector& v2, const double m2)
142  {
143  const DoubleVector dr(p1 - p2);
144  const DoubleVector dv(v1 - v2);
145  const double Minv = 1.0 / (m1 + m2);
146  const double fac = dot(dr, dv) / dot(dr, dr);
147 
148  v1 -= (2.0 * m2 * fac * Minv) * dr;
149  v2 += (2.0 * m1 * fac * Minv) * dr;
150  }
151 }
152 
153 #endif
void process_collision(const DoubleVector &p1, DoubleVector &v1, const double m1, const DoubleVector &p2, DoubleVector &v2, const double m2)
Computes the new velocity vectors for a pair of colliding particles with the given positions...
Definition: Functions.h:140
tvmet::Vector< double, 3UL > DoubleVector
Vector type for representing particle positions and velocities as double precision floating point val...
Definition: Vector.hpp:23
T sign(const T n)
Extract the sign of the (hopefully) numerical value given.
Definition: Functions.h:27
double intersection_time(const DoubleVector &p1, const DoubleVector &v1, const DoubleVector &p2, const DoubleVector &v2, double diameter)
Computes the time to intersection given a pair of positions, velocities, and the minimum pass distanc...
Definition: Functions.h:103
const double NEVER
Time value for an event that will never occur.
Definition: Constants.h:35
double max_mag(const double t1, const double t2)
Produces the value with the largest magnitude of the two given parameters.
Definition: Functions.h:18