2 #ifndef __FUNCTIONS_H__
3 #define __FUNCTIONS_H__
9 #include "../Constants/Constants.h"
10 #include "../Particle/Particle.h"
12 namespace MDSimulation
20 return std::abs(t1) >= std::abs(t2) ? t1 : t2;
33 inline double Sqr(
const T x)
39 inline double Pow3(
const T x)
51 mean_velocity(
const std::vector<Particle>& arr)
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); });
57 return result / (double) arr.size();
61 median_velocity(
const std::vector<Particle>& arr)
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);
68 std::transform(arr.begin(), arr.end(), vel.begin(),
71 return norm2(p.Velocity);
77 nth_element(vel.begin(), vel.begin()+mid, vel.end());
79 nth_element(vel.begin(), vel.begin()+mid-1, vel.end());
81 return (v1 + vel[mid-1]) / 2.0;
85 nth_element(vel.begin(), vel.begin() + mid, vel.end());
109 const double b = dot(dr, dv);
112 diameter *= diameter;
116 const double rr = dot(dr, dr);
117 const double vv = dot(dv, dv);
121 diameter = rr * 0.99;
124 const double d = Sqr(b) - vv * (rr - diameter);
128 return -(sqrt(d) + b) / vv;
145 const double Minv = 1.0 / (m1 + m2);
146 const double fac = dot(dr, dv) / dot(dr, dr);
148 v1 -= (2.0 * m2 * fac * Minv) * dr;
149 v2 += (2.0 * m1 * fac * Minv) * dr;
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