-
Notifications
You must be signed in to change notification settings - Fork 84
/
ParticleSystem.cpp
187 lines (162 loc) · 4.62 KB
/
ParticleSystem.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <math.h>
#include <random>
#include "ParticleSystem.h"
//****************************************************************************************
// Plummer model for spherical galaxy
//
// rho = 3*M_h/4*pi * (a^2 / (r^2 + a^2)^2.5)
//
// M(r) = M_h * (r^3 / (r^2 + a^2)^1.5)
//****************************************************************************************
void ParticleSystem::plummerModel(float *mass, float *x, float* y, float *x_vel, float *y_vel, float *x_acc, float *y_acc, int n)
{
float a = 1.0;
float pi = 3.14159265;
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(0, 1.0);
std::uniform_real_distribution<float> distribution_phi(0.0, 2 * pi);
std::uniform_real_distribution<float> distribution_theta(-1.0, 1.0);
// loop through all particles
for (int i = 0; i < n; i++){
float phi = distribution_phi(generator);
float theta = acos(distribution_theta(generator));
float r = a*pow(distribution(generator), 0.3333) / sqrt(1 - pow(distribution(generator), 0.66667));
// set mass and position of particle
if(i==0){
mass[i] = 100000;
x[i] = 0;
y[i] = 0;
}
else{
mass[i] = 1.0;
x[i] = r*cos(phi);
y[i] = r*sin(phi);
}
// set velocity of particle
float rotation = 1; // 1: clockwise -1: counter-clockwise
float v = 1.0*sqrt(parameters.gravity*100000.0 / r);
if(i==0){
x_vel[0] = 0;
y_vel[0] = 0;
}
else{
x_vel[i] = rotation*v*sin(phi);
y_vel[i] = -rotation*v*cos(phi);
}
// set acceleration to zero
x_acc[i] = 0.0;
y_acc[i] = 0.0;
}
}
//****************************************************************************************
// Simple disk galaxy
//
//
//
//
//****************************************************************************************
void ParticleSystem::diskModel(float *mass, float *x, float* y, float *x_vel, float *y_vel, float *x_acc, float *y_acc, int n)
{
float a = 1.0;
float pi = 3.14159265;
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(1.5, 12.0);
std::uniform_real_distribution<float> distribution_theta(0.0, 2 * pi);
// loop through all particles
for (int i = 0; i < n; i++){
float theta = distribution_theta(generator);
float r = distribution(generator);
// set mass and position of particle
if(i==0){
mass[i] = 100000;
x[i] = 0;
y[i] = 0;
}
else{
mass[i] = 1.0;
x[i] = r*cos(theta);
y[i] = r*sin(theta);
}
// set velocity of particle
float rotation = 1; // 1: clockwise -1: counter-clockwise
float v = 1.0*sqrt(parameters.gravity*100000.0 / r);
if(i==0){
x_vel[0] = 0;
y_vel[0] = 0;
}
else{
x_vel[i] = rotation*v*sin(theta);
y_vel[i] = -rotation*v*cos(theta);
}
// set acceleration to zero
x_acc[i] = 0.0;
y_acc[i] = 0.0;
}
}
//****************************************************************************************
// Two galaxies colliding disk galaxy
//
//
//
//
//****************************************************************************************
void ParticleSystem::collidingDiskModel(float *mass, float *x, float* y, float *x_vel, float *y_vel, float *x_acc, float *y_acc, int n)
{
float a = 1.0;
float pi = 3.14159265;
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution1(1.5, 12.0);
std::uniform_real_distribution<float> distribution2(1, 5.0);
std::uniform_real_distribution<float> distribution_theta(0.0, 2 * pi);
// loop through all particles
for (int i = 0; i < n; i++){
float theta = distribution_theta(generator);
float r1 = distribution1(generator);
float r2 = distribution2(generator);
// set mass and position of particle
if(i==0){
mass[i] = 100000;
x[i] = 0;
y[i] = 0;
}
else if(i==1){
mass[i] = 1;
x[i] = 15*cos(theta);
y[i] = 15*sin(theta);
}
else if(i<=n){
mass[i] = 1.0;
x[i] = r1*cos(theta);
y[i] = r1*sin(theta);
}
else{
mass[i] = 1.0;
x[i] = r2*cos(theta) + x[1];
y[i] = r2*sin(theta) + y[1];
}
// set velocity of particle
float rotation = 1; // 1: clockwise -1: counter-clockwise
float v1 = 1.0*sqrt(parameters.gravity*100000.0 / r1);
float v2 = 1.0*sqrt(parameters.gravity*50000.0 / r2);
float v = 1.0*sqrt(parameters.gravity*100000.0 / sqrt(450));
if(i==0){
x_vel[0] = 0;
y_vel[0] = 0;
}
else if(i==1){
x_vel[i] = rotation*v*sin(theta);
y_vel[i] = -rotation*v*cos(theta);
}
else if(i<=n){
x_vel[i] = rotation*v1*sin(theta);
y_vel[i] = -rotation*v1*cos(theta);
}
else{
x_vel[i] = rotation*v2*sin(theta);
y_vel[i] = -rotation*v2*cos(theta);
}
// set acceleration to zero
x_acc[i] = 0.0;
y_acc[i] = 0.0;
}
}