-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparticles_kernel_impl.cuh
More file actions
423 lines (334 loc) · 12.6 KB
/
particles_kernel_impl.cuh
File metadata and controls
423 lines (334 loc) · 12.6 KB
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
/*
* Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
/*
* CUDA particle system kernel code.
*/
#ifndef _PARTICLES_KERNEL_H_
#define _PARTICLES_KERNEL_H_
#include <stdio.h>
#include <math.h>
#include <curand.h>
#include <curand_kernel.h>
#include <time.h>
#include "helper_math.h"
#include "math_constants.h"
#include "particles_kernel.cuh"
#if USE_TEX
// textures for particle position and velocity
texture<float4, 1, cudaReadModeElementType> oldPosTex;
texture<float4, 1, cudaReadModeElementType> oldVelTex;
texture<uint, 1, cudaReadModeElementType> gridParticleHashTex;
texture<uint, 1, cudaReadModeElementType> cellStartTex;
texture<uint, 1, cudaReadModeElementType> cellEndTex;
#endif
// simulation parameters in constant memory
__constant__ SimParams params;
struct integrate_functor
{
float deltaTime;
__host__ __device__
integrate_functor(float delta_time) : deltaTime(delta_time) {}
template <typename Tuple>
__device__
void operator()(Tuple t)
{
volatile float4 posData = thrust::get<0>(t);
volatile float4 velData = thrust::get<1>(t);
float3 pos = make_float3(posData.x, posData.y, posData.z);
float3 vel = make_float3(velData.x, velData.y, velData.z);
vel += params.gravity * deltaTime;
vel *= params.globalDamping;
// new position = old position + velocity * deltaTime
pos += vel ;//* deltaTime;
// set this to zero to disable collisions with cube sides
#if 1
if (pos.x > 1.0f - params.particleRadius)
{
pos.x = 1.0f - params.particleRadius;
vel.x *= params.boundaryDamping;
}
if (pos.x < -1.0f + params.particleRadius)
{
pos.x = -1.0f + params.particleRadius;
vel.x *= params.boundaryDamping;
}
if (pos.y > 1.0f - params.particleRadius)
{
pos.y = 1.0f - params.particleRadius;
vel.y *= params.boundaryDamping;
}
if (pos.z > 1.0f - params.particleRadius)
{
pos.z = 1.0f - params.particleRadius;
vel.z *= params.boundaryDamping;
}
if (pos.z < -1.0f + params.particleRadius)
{
pos.z = -1.0f + params.particleRadius;
vel.z *= params.boundaryDamping;
}
#endif
if (pos.y < -1.0f + params.particleRadius)
{
pos.y = -1.0f + params.particleRadius;
vel.y *= params.boundaryDamping;
}
// store new position and velocity
thrust::get<0>(t) = make_float4(pos, posData.w);
thrust::get<1>(t) = make_float4(vel, velData.w);
}
};
// calculate position in uniform grid
__device__ int3 calcGridPos(float3 p)
{
int3 gridPos;
gridPos.x = floor((p.x - params.worldOrigin.x) / params.cellSize.x);
gridPos.y = floor((p.y - params.worldOrigin.y) / params.cellSize.y);
gridPos.z = floor((p.z - params.worldOrigin.z) / params.cellSize.z);
return gridPos;
}
// calculate address in grid from position (clamping to edges)
__device__ uint calcGridHash(int3 gridPos)
{
gridPos.x = gridPos.x & (params.gridSize.x-1); // wrap grid, assumes size is power of 2
gridPos.y = gridPos.y & (params.gridSize.y-1);
gridPos.z = gridPos.z & (params.gridSize.z-1);
float hash = __umul24(__umul24(gridPos.z, params.gridSize.y), params.gridSize.x) + __umul24(gridPos.y, params.gridSize.x) + gridPos.x;
return hash;
}
//initialize states on device
__global__ void setup_kernel(curandState *m_dStates, uint numParticles)
{
uint index = blockDim.x * blockIdx.x + threadIdx.x;
if(index < numParticles)
{
curandState localState;
curand_init(1234, index, 0, &localState);
m_dStates[index] = localState;
}
}
// calculate grid hash value for each particle
__global__
void calcHashD(uint *gridParticleHash, // output
uint *gridParticleIndex, // output
float4 *pos, // input: positions
uint numParticles)
{
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
if (index >= numParticles) return;
volatile float4 p = pos[index];
// get address in grid
int3 gridPos = calcGridPos(make_float3(p.x, p.y, p.z));
uint hash = calcGridHash(gridPos);
// store grid hash and particle index
gridParticleHash[index] = hash;
gridParticleIndex[index] = index;
}
// rearrange particle data into sorted order, and find the start of each cell
// in the sorted hash array
__global__
void reorderDataAndFindCellStartD(uint *cellStart, // output: cell start index
uint *cellEnd, // output: cell end index
float4 *sortedPos, // output: sorted positions
float4 *sortedVel, // output: sorted velocities
curandState *sortedStates,
float *sortedType,
uint *gridParticleHash, // input: sorted grid hashes
uint *gridParticleIndex,// input: sorted particle indices
float4 *oldPos, // input: sorted position array
float4 *oldVel, // input: sorted velocity array
curandState *oldStates,
float *oldType,
uint numParticles)
{
extern __shared__ uint sharedHash[]; // blockSize + 1 elements
uint index = __umul24(blockIdx.x,blockDim.x) + threadIdx.x;
uint hash;
// handle case when no. of particles not multiple of block size
if (index < numParticles)
{
hash = gridParticleHash[index];
// Load hash data into shared memory so that we can look
// at neighboring particle's hash value without loading
// two hash values per thread
sharedHash[threadIdx.x+1] = hash;
if (index > 0 && threadIdx.x == 0)
{
// first thread in block must load neighbor particle hash
sharedHash[0] = gridParticleHash[index-1];
}
}
__syncthreads();
if (index < numParticles)
{
// If this particle has a different cell index to the previous
// particle then it must be the first particle in the cell,
// so store the index of this particle in the cell.
// As it isn't the first particle, it must also be the cell end of
// the previous particle's cell
if (index == 0 || hash != sharedHash[threadIdx.x])
{
cellStart[hash] = index;
if (index > 0)
cellEnd[sharedHash[threadIdx.x]] = index;
}
if (index == numParticles - 1)
{
cellEnd[hash] = index + 1;
}
// Now use the sorted index to reorder the pos and vel data
uint sortedIndex = gridParticleIndex[index];
float4 pos = FETCH(oldPos, sortedIndex); // macro does either global read or texture fetch
float4 vel = FETCH(oldVel, sortedIndex); // see particles_kernel.cuh
curandState myState = FETCH(oldStates, sortedIndex);
float type = FETCH (oldType, sortedIndex);
sortedPos[index] = pos;
sortedVel[index] = vel;
sortedStates[index] = myState;
sortedType[index] = type;
}
}
// collide two spheres using DEM method
__device__
float3 collideSpheres(float3 posA, float3 posB,
float3 velA, float3 velB,
float type, float type2,
float radiusA, float radiusB,
float attraction)
{
// calculate relative position
float3 relPos = posB - posA;
float dist = length(relPos);
float collideDist = radiusA + radiusB;
float3 force = make_float3(0.0f);
if (dist < collideDist)
{
float3 norm = relPos / dist;
// relative velocity
float3 relVel = velB - velA;
// relative tangential velocity
float3 tanVel = relVel - (dot(relVel, norm) * norm);
// spring force
//force = -params.spring*(collideDist - dist) * norm * 0.0001f;
// dashpot (damping) force
//force += params.damping*relVel;
// tangential shear force
//force += params.shear*tanVel;
// attraction
//force += attraction*relPos;
//force += noise;
//Intercellular Interaction Potential
//float dV2dr = 4* attraction * ( (6* pow(radiusA,6) )/pow(dist,7) - (12* pow(radiusB,12))/pow(dist,13));
//float dV2dr = 4 * attraction * ((pow(2*radiusA,12)/(pow(dist,12)))-(pow(2*radiusA,6)/ (pow(dist,6))));
float attraction2 = attraction;
if(type == 1)
{
attraction2 = 8 * attraction;
}
else if (type == 2)
{
attraction2 = 5 * attraction;
}
/*else
{
attraction2 = -1 * attraction;
}*/
float dV2dr = 4 * attraction2 * (6 / (radiusA * pow (2.5f, 7)) - 12 / (radiusB * pow(2.5f,13))); //<-- corrected Intercellular Interaction Force Ref: http://en.wikipedia.org/wiki/Lennard-Jones_potential
force -= dV2dr*relPos;
}
return force;
}
// collide a particle against all other particles in a given cell
__device__
float3 collideCell(int3 gridPos,
uint index,
float3 pos,
float3 vel,
float type,
float4 *oldPos,
float4 *oldVel,
float *oldType,
uint *cellStart,
uint *cellEnd)
{
uint gridHash = calcGridHash(gridPos);
// get start of bucket for this cell
uint startIndex = FETCH(cellStart, gridHash);
float3 force = make_float3(0.0f);
if (startIndex != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint endIndex = FETCH(cellEnd, gridHash);
for (uint j=startIndex; j<endIndex; j++)
{
if (j != index) // check not colliding with self
{
float3 pos2 = make_float3(FETCH(oldPos, j));
float3 vel2 = make_float3(FETCH(oldVel, j));
float type2 = FETCH(oldType, j);
// collide two spheres
force += collideSpheres(pos, pos2, vel, vel2, type, type2, params.particleRadius, params.particleRadius, params.attraction);
}
}
}
return force;
}
__global__
void collideD(float4 *newVel, // output: new velocity
curandState *newState,
float *newCellType,
float4 *oldPos, // input: sorted positions
float4 *oldVel, // input: sorted velocities
curandState *oldStates,
float *oldType,
uint *gridParticleIndex, // input: sorted particle indices
uint *cellStart,
uint *cellEnd,
uint numParticles)
{
uint index = __mul24(blockIdx.x,blockDim.x) + threadIdx.x;
if (index >= numParticles) return;
// read particle data from sorted arrays
float3 pos = make_float3(FETCH(oldPos, index));
float3 vel = make_float3(FETCH(oldVel, index));
curandState myState = FETCH(oldStates, index);
float type = FETCH(oldType, index);
// get address in grid
int3 gridPos = calcGridPos(pos);
// examine neighbouring cells
float3 force = make_float3(0.0f);
float3 relPos = make_float3(0.f, 0.f, 0.f);
for (int z=-1; z<=1; z++)
{
for (int y=-1; y<=1; y++)
{
for (int x=-1; x<=1; x++)
{
int3 neighbourPos = gridPos + make_int3(x, y, z);
force += collideCell(neighbourPos, index, pos, vel, type, oldPos, oldVel, oldType, cellStart, cellEnd);
}
}
}
// collide with cursor sphere
//force += collideSpheres(pos, params.colliderPos, vel, make_float3(0.0f, 0.0f, 0.0f), params.particleRadius, params.colliderRadius, 0.0f);
float3 noise = make_float3(curand_uniform(&myState) / 1000.f, curand_uniform(&myState) / 1000.f, curand_uniform(&myState) / 1000.f) ;
//float3 velocity = make_float3(0.f, 0.f, 0.f);
vel.x = (noise.x - force.x) / 5.0f;
vel.y = (noise.y - force.y) / 5.0f;
vel.z = (noise.z - force.z) / 5.0f;
// write new velocity back to original unsorted location
uint originalIndex = gridParticleIndex[index];
//newVel[originalIndex] = make_float4(vel + force, 0.0f);
newVel[originalIndex] = make_float4(vel.x, vel.y, vel.z, 0.f);
newState[originalIndex] = myState;
newCellType[originalIndex] = type;
}
#endif