-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.cpp
88 lines (70 loc) · 1.97 KB
/
Particle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cmath>
#include <random>
#include <algorithm>
#include "Particle.hpp"
Particle& Particle::update(double timestamp)
{
_vx += timestamp;
return *this;
}
Particle& Particle::resetForce()
{
_fx = 0.0;
_fy = 0.0;
return *this;
}
Particle& Particle::addForce(Particle& p)
{
double EPS = 3E4;
double dx = _rx - p._rx;
double dy = _ry - p._ry;
double dist = sqrt(dx*dx + dy*dy);
double F = (G * _mass * p._mass) / (dist*dist + EPS*EPS);
_fx += F * dx / dist;
_fy += F * dy / dist;
return *this;
}
std::vector<Particle> Particle::generateRandomParticle(int n)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 100);
std::vector<Particle> particles;
particles.reserve(n);
for(int i = 0; i < n; i++)
{
particles.emplace_back(Particle::getRand(dis(gen)), Particle::getRand(dis(gen)),
Particle::getRand(dis(gen)), Particle::getRand(dis(gen)),
Particle::getRand(dis(gen)), Particle::getRand(dis(gen)),
Particle::getRandMass(dis(gen)), Particle::randomName(5));
}
return particles;
}
void Particle::displayOutputFile()
{
std::cout << _rx << " " << _ry << " " << 0.00 << " ";
std::cout << _vx << " " << _vy << " " << 0.00 << std::endl;
}
std::string Particle::randomName(size_t length)
{
auto randchar = []() -> char
{
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
std::string str(length,0);
std::generate_n( str.begin(), length, randchar );
return str;
}
double Particle::getRand(int random)
{
return 1e18*exp(-1.8)*(.5- random );
}
double Particle::getRandMass(int random)
{
return 1.98892e30*random + 1e20;
}