-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParticleSystem.cpp
448 lines (392 loc) · 16.1 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "ParticleSystem.h"
#include <omp.h>
#include <time.h>
#define PI 3.14159265
#define MAX_NAIVE 50
ParticleSystem::ParticleSystem() {
this->grav = Vector(0,0,-9.8);
setBoundaries(-.1, -1, -.1, .1, 1, .1);
}
ParticleSystem::ParticleSystem(Vector grav){
this->grav = grav;
this->numRowBoxes = 100;
this->grid = SpatialGrid(0.0457);
setBoundaries(-.3, -1, -.3, .3, 1, .3);
}
ParticleSystem::ParticleSystem(Vector grav, float min_x, float min_y, float min_z, float max_x, float max_y, float max_z) {
this->grav = grav;
this->numRowBoxes = 100;
this->grid = SpatialGrid(0.0457);
setBoundaries(min_x, min_y, min_z, max_x, max_y, max_z);
}
void ParticleSystem::setBoundaries(float min_x, float min_y, float min_z, float max_x, float max_y, float max_z) {
this->MIN_X = min_x;
this->MIN_Y = min_y;
this->MIN_Z = min_z;
this->MAX_X = max_x;
this->MAX_Y = max_y;
this->MAX_Z = max_z;
}
void ParticleSystem::initialize(float timestep) {
this->setDensities();
this->computePressure();
this->computeForces();
this->initializeLeapFrog(-timestep);
}
void ParticleSystem::update(float timestep){
this->setDensities();//for each particle, compute particle's density
this->computePressure(); // now compute each particle's pressure
this->computeForces(); // compute forces and set acceleration of the particles
this->leapFrog(timestep); // utilize leapfrog integration to figure out position and new velocity
}
Particle* ParticleSystem::getParticle(const unsigned int i) {
return &particles[i];
}
std::vector<Particle> ParticleSystem::getParticles() {
return this->particles;
}
// computes the internal and external forces.
// also sets acceleration
void ParticleSystem::computeForces(){
Vector gravity, force, total, tension;
#pragma omp parallel for firstprivate(gravity, total, force, tension)
for(signed i = 0; i < particles.size(); i++) {
gravity = gravityForce(particles[i]);
total = press_visc(particles[i], i); // viscosity - pressure forces
force = gravity + total;
Vector acceleration = force / particles[i].getDensity();
particles[i].setAcceleration(acceleration);
}
}
void ParticleSystem::computePressure() {
float pressure = 0;
unsigned int i = 0;
#pragma omp parallel for firstprivate(pressure)
// loop unroll by 4
for(signed i = 0; i < particles.size()/4*4; i+=4) {
// Equation: p = k ( (p / p0)^7 - 1)
pressure = particles[i].getStiffness() * (pow((particles[i].getDensity() / particles[i].getRestDensity()), 7.0f) - 1.0f);
particles[i].setPressure(pressure);
pressure = particles[i+1].getStiffness() * (pow((particles[i+1].getDensity() / particles[i+1].getRestDensity()), 7.0f) - 1.0f);
particles[i+1].setPressure(pressure);
pressure = particles[i+2].getStiffness() * (pow((particles[i+2].getDensity() / particles[i+2].getRestDensity()), 7.0f) - 1.0f);
particles[i+2].setPressure(pressure);
pressure = particles[i+3].getStiffness() * (pow((particles[i+3].getDensity() / particles[i+3].getRestDensity()), 7.0f) - 1.0f);
particles[i+3].setPressure(pressure);
}
for(; i < particles.size(); i++) {
pressure = particles[i].getStiffness() * (pow((particles[i].getDensity() / particles[i].getRestDensity()), 7.0f) - 1.0f);
particles[i].setPressure(pressure);
}
}
void ParticleSystem::setDensities(){
float density = 0;
std::vector<Particle> list;
std::map<int, std::vector<int> > hashMap;
for(unsigned int i = 0; i < particles.size(); i++){//create the hash map (O(n) time)
Point3D pos = particles[i].getPosition();
float x = pos.getX();
float y = pos.getY();
float z = pos.getZ();
int xindex = (int)floor((x+10)/(.0457));
int yindex = (int)floor((y+10)/(.0457));
int zindex = (int)floor((z+10)/(.0457));
int hash = (xindex * 10619863)^(yindex * 94418953)^(zindex * 54018521);
hashMap[hash].push_back(i);
}
//#pragma omp parallel for firstprivate(density, list)
for(unsigned int i = 0; i < particles.size(); i++){
density = 0;
if(particles.size() > MAX_NAIVE){//doing spatial hashing
//list = grid.getNeighbors(particles[i]);
Point3D pos = particles[i].getPosition();
float x = pos.getX();
float y = pos.getY();
float z = pos.getZ();
int xindex = (int)floor((x+10)/(.0457));
int yindex = (int)floor((y+10)/(.0457));
int zindex = (int)floor((z+10)/(.0457));
int hash = (xindex * 10619863)^(yindex * 94418953)^(zindex * 54018521);
std::vector<int> ints = hashMap[hash];
signed c;
//#pragma omp parallel for firstprivate(c)
for(c = 0; c < ints.size(); c++){//get surrounding parts
list.push_back(particles[ints[c]]);
}
for(int a = -1; a <= 1; a++){
for(int b = -1; b <= 1; b++){
for(int c = -1; c <= 1; c++){//get surrounding boxes
int hash2 = ((xindex+a) * 10619863)^((yindex+b) * 94418953)^((zindex+c) * 54018521);
std::vector<int> ints2 = hashMap[hash2];
signed d;
//#pragma omp parallel for firstprivate(d)
for(d = 0; d < ints2.size(); d++){
list.push_back(particles[ints2[d]]);
}
}
}
}
for(unsigned int j = 0; j < list.size(); j++){ //loop through neighbors
Vector diff = particles[i].getPosition() - list[j].getPosition();
if (diff.getMagnitude() <= particles[i].getSupportRadius()) {
density += defaultKernel(diff, particles[i].getSupportRadius()) * list[j].getMass();
}
}
list.clear();
}
else{
for(unsigned int j = 0; j < particles.size(); j++){
Vector diff = particles[i].getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= particles[i].getSupportRadius()) {
density += defaultKernel(diff, particles[i].getSupportRadius()) * particles[j].getMass();
}
}
}
if(density == 0){
density = particles[i].getMass() / .00000001;
}
particles[i].setDensity(density);
}
}
Vector ParticleSystem::gravityForce(Particle& p) {
return grav * p.getDensity();
}
Vector ParticleSystem::press_visc(Particle& p , unsigned const int& i) {
unsigned int j = 0;
Vector pressure, viscosity, diff;
float pCoeff;
Vector vCoeff;
for(j = 0; j < i; j++) {
diff = p.getPosition() - particles[j].getPosition();
if(diff.getMagnitude() <= p.getSupportRadius()) {
pCoeff = (p.getPressure() + particles[j].getPressure()) / 2.0 * particles[j].getVolume();
pressure += pressGradientKernel(diff, p.getSupportRadius()) * pCoeff;
vCoeff = (particles[j].getVelocity() - p.getVelocity()) * particles[j].getVolume();
viscosity += vCoeff * viscLaplacianKernel(diff, p.getSupportRadius());
}
}
for(j = j + 1; j < particles.size(); j++) {
diff = p.getPosition() - particles[j].getPosition();
if(diff.getMagnitude() <= p.getSupportRadius()) {
pCoeff = (p.getPressure() + particles[j].getPressure()) / 2.0 * particles[j].getVolume();
pressure += pressGradientKernel(diff, p.getSupportRadius()) * pCoeff;
vCoeff = (particles[j].getVelocity() - p.getVelocity()) * particles[j].getVolume();
viscosity += vCoeff * viscLaplacianKernel(diff, p.getSupportRadius());
}
}
viscosity = viscosity * p.getViscosity();
return viscosity - pressure;
}
Vector ParticleSystem::pressureForce(Particle& p, unsigned const int& i) {
Vector pressure;
float coeff = 0;
unsigned int j = 0;
// this is so stupid...........but ill think of a better way. i dont wanna do checks cause it might make a difference since this is computed every time for every particle
for(j = 0; j < i; j++) {
Vector diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
coeff = (p.getPressure() + particles[j].getPressure()) / 2.0f * particles[j].getVolume();
pressure += pressGradientKernel(diff, p.getSupportRadius()) * coeff;
}
}
for(j = j + 1; j < particles.size(); j++) {
Vector diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
coeff = (p.getPressure() + particles[j].getPressure()) / 2.0f * particles[j].getVolume();
pressure += pressGradientKernel(diff, p.getSupportRadius()) * coeff;
}
}
return pressure;
}
Vector ParticleSystem::viscosityForce(Particle& p, unsigned const int& i) {
Vector viscosity;
Vector coeff;
unsigned int j;
for(j = 0; j < i; j++) {
Vector diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
coeff = (particles[j].getVelocity() - p.getVelocity()) * particles[j].getVolume();
viscosity += coeff * viscLaplacianKernel(diff, p.getSupportRadius());
}
}
for(j = j + 1; j < particles.size(); j++) {
Vector diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
coeff = (particles[j].getVelocity() - p.getVelocity()) * particles[j].getVolume();
viscosity += coeff * viscLaplacianKernel(diff, p.getSupportRadius());
}
}
viscosity = viscosity * p.getViscosity();
return viscosity;
}
Vector ParticleSystem::tensionForce(Particle& p, unsigned const int& i) {
Vector normal = surfaceNormal(p, i);
if (normal.getMagnitude() > p.getThreshold()) { // threshold
normal.normalize();
return normal * curvature(p, i) * (p.getSurfTension()); // surface tension constant for water
}
return Vector(0, 0, 0);
}
float ParticleSystem::colorFunction(Particle& p) {
float color = 0;
Vector diff;
unsigned int j;
for(j = 0; j < particles.size(); j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
color += particles[j].getMass() / particles[j].getDensity() * defaultKernel(diff, p.getSupportRadius());
}
}
return color;
}
vector<Particle> ParticleSystem::getNeighbors(Particle &p) {
Vector diff;
vector<Particle> neighbors;
unsigned int j;
for(j = 0; j < particles.size(); j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
neighbors.push_back(particles[j]);
}
}
return neighbors;
}
Vector ParticleSystem::surfaceNormal(Particle& p, unsigned const int& i) {
Vector normal = Vector(0, 0, 0);
Vector diff;
unsigned int j;
for(j = 0; j < i; j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
normal += gradientKernel(diff, p.getSupportRadius()) * particles[j].getMass() / particles[j].getDensity();
}
}
for(j = j + 1; j < particles.size(); j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
normal += gradientKernel(diff, p.getSupportRadius()) * particles[j].getMass() / particles[j].getDensity();
}
}
return normal;
}
float ParticleSystem::curvature(Particle& p, unsigned const int& i) {
float curvature = 0;
Vector diff;
unsigned int j;
for(j = 0; j < i; j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
curvature += particles[j].getMass() / particles[j].getDensity() * laplacianKernel(diff, p.getSupportRadius());
}
}
for(j = j + 1; j < particles.size(); j++) {
diff = p.getPosition() - particles[j].getPosition();
if (diff.getMagnitude() <= p.getSupportRadius()) {
curvature += particles[j].getMass() / particles[j].getDensity() * laplacianKernel(diff, p.getSupportRadius());
}
}
return curvature;
}
// Smoothing distance h is half of the difference between particle i's most distant nearest neighbor and i
// Poly6 Kernels used for everything except pressure and viscosity forces
// Notes: http://image.diku.dk/projects/media/kelager.06.pdf (Page 16)
// Less expensive compared to gaussian one because of computation of e and no square roots
// Not sure whether I'm supposed to normalize Vector r though
float ParticleSystem::defaultKernel(Vector r, const float h) {
float rMag = r.getMagnitude();
return (315.0f * pow((pow(h, 2.0f) - pow(rMag, 2.0f)),3.0) / (64.0f * PI * pow(h, 9.0f)));
}
/* gradient and laplacian of poly6 kernels. prob not needed if we use the spiky and viscosity kernels for other calculations*/
Vector ParticleSystem::gradientKernel(Vector r, const float h) {
float rMag = r.getMagnitude();
float coeff = pow(pow(h, 2.0f) - pow(rMag, 2.0f), 2.0f) * -945 / (32 * PI * pow(h, 9.0f));
return r * coeff;
}
float ParticleSystem::laplacianKernel(Vector r, const float h) {
float rMag = r.getMagnitude();
return (-945 / (32 * PI * pow(h, 9.0f))) * (pow(h, 2.0f) - pow(rMag, 2.0f)) * (3 * pow(h, 2.0f) - 7 * pow(rMag, 2.0f));
}
// Spiky Kernel to calculate pressure
// Give more repulsive pressure at float distance and thus avoids clustering.
Vector ParticleSystem::pressGradientKernel(Vector r, const float h) {
float rMag = r.getMagnitude();
float coeff = (-45 * pow((h - rMag), 2.0f)) / (PI * pow(h, 6.0f));
//r.normalize();
return r * coeff;
}
// Viscosity kernel to calculate viscosity
// Gives more stable viscosity and makes it possible to damp simulation better
// Laplacian in poly6 kernel becomes negative really fast. The viscosity kernel's laplacian is positive everywhere
float ParticleSystem::viscLaplacianKernel(Vector r, const float h) {
float rMag = r.getMagnitude();
return (45 * (h - rMag)) / (PI * pow(h, 6.0f));
}
// Leap frog integration. Takes in a dt and will loop through all the particles in our system.
// Can be changed to work with leapfrogging just a certain particle.
void ParticleSystem::leapFrog(const float& dt) {
#pragma omp parallel for
for(signed int i = 0; i < particles.size(); i++) {
Particle& p = particles[i];
// get velocity at time t - dt/2. v_{t - dt/2}
Vector old = p.getVelocityHalf();
// velocity at time t + dt/2. v_{t + dt/2} = v_{t - dt / 2} + a * dt
Vector tempVelocityHalf = old + p.getAcceleration() * dt;
// set position at time t. pos_{t + dt} = pos_{t} + v_{t + dt / 2} * dt
Point3D tempPosition = p.getPosition() + (tempVelocityHalf * dt);
Vector tempVelocity = (old + p.getVelocityHalf()) / 2.0f;
checkBoundary(p, &tempPosition, &tempVelocity, &tempVelocityHalf);
p.setOldPosition(p.getPosition());
p.setVelocityHalf(tempVelocityHalf);
p.setPosition(tempPosition);
// use midpoint approximation for velocity at time t. v_{t} = (v_{t - dt / 2} + v_{t + dt / 2}) / 2.
p.setVelocity(tempVelocity);
if(particles.size() > MAX_NAIVE){
//p = this->grid.updateBoxes(p); // update the boxes of the particles in spatialgrid
}
}
}
// initialize. v_{-dt/2} = v_{0} - a_{0} * dt / 2
void ParticleSystem::initializeLeapFrog(const float& dt) {
for(unsigned int i = 0; i < particles.size(); i++) {
particles[i].setVelocityHalf(particles[i].getVelocity() - ((this->grav + particles[i].getAcceleration()) * dt) / 2.0f);
}
}
void ParticleSystem::checkBoundary(Particle& p, Point3D* position, Vector* velocity, Vector* velocityHalf) {
if (!position || !velocity || !velocityHalf) return;
// if goes past boundaries, reflect back.
if(position->getX() > MAX_X) {
position->setX(MAX_X);
velocity->setX(-p.getRestCoeff() * velocity->getX());
velocityHalf->setX(-p.getRestCoeff() * velocityHalf->getX());
}
else if (position->getX() < MIN_X) {
position->setX(MIN_X);
velocity->setX(-p.getRestCoeff() * velocity->getX());
velocityHalf->setX(-p.getRestCoeff() * velocityHalf->getX());
}
if(position->getY() > MAX_Y) {
position->setY(MAX_Y);
velocity->setY(-p.getRestCoeff() * velocity->getY());
velocityHalf->setY(-p.getRestCoeff() * velocityHalf->getY());
}
else if (position->getY() < MIN_Y) {
position->setY(MIN_Y);
velocity->setY(-p.getRestCoeff() * velocity->getY());
velocityHalf->setY(-p.getRestCoeff() * velocityHalf->getY());
}
if(position->getZ() > MAX_Z) {
position->setZ(MAX_Z);
velocity->setZ(-p.getRestCoeff()* velocity->getZ());
velocityHalf->setZ(-p.getRestCoeff() * velocityHalf->getZ());
}
else if (position->getZ() < MIN_Z) {
position->setZ(MIN_Z);
velocity->setZ(-p.getRestCoeff()* velocity->getZ());
velocityHalf->setZ(-p.getRestCoeff() * velocityHalf->getZ());
}
}
void ParticleSystem::addParticle(Particle& p) {
//long id = grid.addParticle(p);
//p.setHashID(id);
particles.push_back(p);
}