-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuda_simulation.cu
2225 lines (1870 loc) · 52.8 KB
/
cuda_simulation.cu
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cuda_runtime.h>
#include <cstdlib>
#include <helper_cuda.h>
#include <helper_functions.h>
#include <device_atomic_functions.h>
#include <helper_math.h>
#include <stdio.h>
#include <thrust/device_ptr.h>
#include <thrust/for_each.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/sort.h>
#include <cooperative_groups.h>
#include "cuda_simulation.cuh"
#include "sph_kernel.cuh"
#include <chrono>
#include "imgui/imgui.h"
namespace cg = cooperative_groups;
// calculate position in uniform grid
inline __device__ int3 calcGridPos(float3 p)
{
int3 gridPos;
gridPos.x = floor((p.x - params.world_origin.x) / params.cell_size.x);
gridPos.y = floor((p.y - params.world_origin.y) / params.cell_size.y);
gridPos.z = floor((p.z - params.world_origin.z) / params.cell_size.z);
return gridPos;
}
// calculate address in grid from position (clamping to edges)
inline __device__ uint calcGridHash(int3 gridPos)
{
gridPos.x = gridPos.x & (params.grid_size.x - 1); // wrap grid, assumes size is power of 2
gridPos.y = gridPos.y & (params.grid_size.y - 1);
gridPos.z = gridPos.z & (params.grid_size.z - 1);
return __umul24(__umul24(gridPos.z, params.grid_size.y), params.grid_size.x) + __umul24(gridPos.y, params.grid_size.x) + gridPos.x;
}
// collide two spheres using DEM method
inline __device__
float3 collideSpheres(
float3 posA, float3 posB,
float3 velA, float3 velB,
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);
//printf("dist: %f\ncollideDist: %f", dist, collideDist);
if (dist < collideDist)
{
float3 norm = relPos / (dist+0.00001f);
// relative velocity
float3 relVel = velB - velA;
// relative tangential velocity
float3 tanVel = relVel - (dot(relVel, norm) * norm);
// spring force
force = -params.spring * (collideDist - dist) * norm;
// dashpot (damping) force
force += params.damping * relVel;
// tangential shear force
force += params.shear * tanVel;
// attraction
force += attraction * relPos;
//printf("%f %f %f\n", force.x, force.y, force.z);
}
return force;
}
inline __device__
float3 collideCell(
int3 gridPos,
uint index,
float3 pos,
float3 vel,
float3* oldPos,
float3* oldVel,
uint* cellStart,
uint* cellEnd)
{
uint gridHash = calcGridHash(gridPos);
// get start of bucket for this cell
uint startIndex = cellStart[gridHash];
float3 force = make_float3(0.0f);
if (startIndex != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint endIndex = cellEnd[gridHash];
for (uint j = startIndex; j < endIndex; j++)
{
if (j != index) // check not colliding with self
{
float3 pos2 = oldPos[j];
float3 vel2 = oldVel[j];
// collide two spheres
force += collideSpheres(
pos, pos2,
vel, vel2,
params.particle_radius, params.particle_radius,
params.attraction);
}
}
}
return force;
}
inline __device__
float sph_boundary_volume(
int3 grid_pos,
uint index,
float3 pos1,
float* mass,
CellData data
)
{
uint grid_hash = calcGridHash(grid_pos);
uint start_index = data.cellStart[grid_hash];
float rho = 0.f;
if (start_index != 0xffffffff)
{
uint end_index = data.cellEnd[grid_hash];
for (uint j = start_index; j < end_index; ++j)
{
if (j != index)
{
uint original_index = data.grid_index[j];
float3 pos2 = data.sorted_pos[j];
float3 vec = pos1 - pos2;
float dist = length(vec);
rho += mass[original_index] * Poly6_W_CUDA(dist, params.effective_radius);
}
}
}
return rho;
}
__global__ void calcHashD(
CellData cell_data, // output
float3* pos, // input: positions
uint num_particles)
{
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
if (index >= num_particles) return;
volatile float3 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
cell_data.grid_hash[index] = hash;
cell_data.grid_index[index] = index;
}
__global__
void calcHash_boundary_D(
CellData cell_data,
float3* pos, // input: positions
uint num_particles)
{
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
if (index >= num_particles) return;
//printf("%u \n", index);
volatile float3 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
cell_data.grid_hash[index] = hash;
cell_data.grid_index[index] = index;
}
/*
* Reorder data to find cell start and end (for neighbor searching)
*/
__global__
void reorderDataAndFindCellStartD(
CellData cell_data,
float3* oldPos, // input: sorted position array
uint numParticles)
{
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
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 = cell_data.grid_hash[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] = cell_data.grid_hash[index - 1];
}
}
cg::sync(cta);
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])
{
cell_data.cellStart[hash] = index;
if (index > 0)
cell_data.cellEnd[sharedHash[threadIdx.x]] = index;
}
if (index == numParticles - 1)
{
cell_data.cellEnd[hash] = index + 1;
}
// Now use the sorted index to reorder the pos and vel data
uint sortedIndex = cell_data.grid_index[index];
float3 pos = oldPos[sortedIndex];
cell_data.sorted_pos[index] = pos;
}
}
/*
__global__
void reorderData_boundary_D(
CellData cell_data,
float3* oldPos, // input: sorted position array
uint numParticles)
{
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
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 = cell_data.grid_hash[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] = cell_data.grid_hash[index - 1];
}
}
cg::sync(cta);
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])
{
cell_data.cellStart[hash] = index;
if (index > 0)
cell_data.cellEnd[sharedHash[threadIdx.x]] = index;
}
if (index == numParticles - 1)
{
cell_data.cellEnd[hash] = index + 1;
}
// Now use the sorted index to reorder the pos data
uint sortedIndex = cell_data.grid_index[index];
float3 pos = oldPos[sortedIndex];
cell_data.sorted_pos[index] = pos;
}
}
*/
__global__
void compute_boundary_volume_d(
CellData data,
float* mass, float* volume,
uint numParticles)
{
uint index = __mul24(blockIdx.x, blockDim.x) + threadIdx.x;
if (index >= numParticles) return;
uint originalIndex = data.grid_index[index];
// read particle data from sorted arrays
float3 pos = data.sorted_pos[index];
// initial volume
float rho = mass[originalIndex] * Poly6_W_CUDA(0, params.effective_radius);
// get address in grid
int3 gridPos = calcGridPos(pos);
// traverse 27 neighbors
for (int z = -1; z <= 1; z++)
{
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
int3 neighbor_pos = gridPos + make_int3(x, y, z);
rho += sph_boundary_volume(
neighbor_pos, index,
pos, mass,
data
);
}
}
}
// Update volume
volume[originalIndex] = mass[originalIndex] / rho;
//printf("rho = %f\n", rho);
//printf("C[%u]: %f\n", originalIndex, C[originalIndex]);
}
void compute_grid_size(uint n, uint block_size, uint& num_blocks, uint& num_threads)
{
num_threads = min(block_size, n);
num_blocks = (n % num_threads != 0) ? (n / num_threads + 1) : (n / num_threads);
}
void calculate_hash(
CellData cell_data,
float3* pos,
uint num_particles)
{
uint num_blocks, num_threads;
compute_grid_size(num_particles, MAX_THREAD_NUM, num_blocks, num_threads);
calcHashD << < num_blocks, num_threads >> > (
cell_data,
pos,
num_particles);
getLastCudaError("Kernel execution failed: calc_hash");
}
void reorder_data(
CellData cell_data,
float3* oldPos,
uint numParticles,
uint numCells)
{
uint numThreads, numBlocks;
compute_grid_size(numParticles, MAX_THREAD_NUM, numBlocks, numThreads);
// set all cells to empty
checkCudaErrors(cudaMemset(cell_data.cellStart, 0xffffffff, numCells * sizeof(uint)));
uint smemSize = sizeof(uint) * (numThreads + 1);
reorderDataAndFindCellStartD << < numBlocks, numThreads, smemSize >> > (
cell_data,
oldPos,
numParticles);
getLastCudaError("Kernel execution failed: reorderDataAndFindCellStartD");
}
/*
void reorderData_boundary(
CellData cell_data,
float3* oldPos,
uint numParticles,
uint numCells)
{
uint numThreads, numBlocks;
compute_grid_size(numParticles, MAX_THREAD_NUM, numBlocks, numThreads);
// set all cells to empty
checkCudaErrors(cudaMemset(cell_data.cellStart, 0xffffffff, numCells * sizeof(uint)));
uint smemSize = sizeof(uint) * (numThreads + 1);
reorderData_boundary_D << < numBlocks, numThreads, smemSize >> > (
cell_data,
oldPos,
numParticles);
getLastCudaError("Kernel execution failed: reorderDataAndFindCellStartD");
}
*/
void compute_boundary_volume(CellData data, float* mass, float* volume, uint numParticles)
{
uint numThreads, numBlocks;
compute_grid_size(numParticles, MAX_THREAD_NUM, numBlocks, numThreads);
compute_boundary_volume_d << <numBlocks, numThreads >> > (
data,
mass, volume,
numParticles);
getLastCudaError("Kernel execution failed: copmute_boundary_volume");
}
__global__ void test_offset(float3* positions)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
/*
if (i == 0)
printf("particles[0]: %f, %f, %f\n", positions[i].x , positions[i].y, positions[i].z);
*/
positions[i].x = positions[i].x + 0.001f;
positions[i].y = positions[i].y + 0.001f;
positions[i].z = positions[i].z + 0.001f;
}
__global__
void integrate_pbd_d(
float3* pos, float3* vel, float3* force, float* massInv,
float3* predict_pos, float3* new_pos,
float dt,
uint numParticles)
{
uint index = __mul24(blockIdx.x, blockDim.x) + threadIdx.x;
float3 t_vel = vel[index] + dt * params.gravity;
t_vel = t_vel * params.global_damping;
float3 t_pos = pos[index] + dt * t_vel;
if (t_pos.x >= 1.0f)
{
t_pos.x = 1.f;
t_vel.x = -abs(t_vel.x);
t_vel *= params.boundary_damping;
}
if (t_pos.x <= -1.0f)
{
t_pos.x = -1.f;
t_vel.x = abs(t_vel.x);
t_vel *= params.boundary_damping;
}
if (t_pos.z >= 1.0f)
{
t_pos.z = 1.f;
t_vel.z = -abs(t_vel.z);
t_vel *= params.boundary_damping;
}
if (t_pos.z <= -1.0f)
{
t_pos.z = -1.f;
t_vel.z = abs(t_vel.z);
t_vel *= params.boundary_damping;
}
if (t_pos.y <= 0.f)
{
t_pos.y = 0.f;
t_vel.y = abs(t_vel.y);
t_vel *= params.boundary_damping;
}
/* Velocity limitation
if (length(t_vel) > 5.f)
{
t_vel = (5.f / length(t_vel)) * t_vel ;
}
*/
predict_pos[index] = t_pos;// pos[index] + dt * t_vel;
vel[index] = t_vel;
new_pos[index] = predict_pos[index];
}
// collide a particle against all other particles in a given cell
/* Collision device code */
__global__
void collideD(
float3* newVel, // output: new velocity
float3* oldPos, // input: sorted positions
float3* oldVel, // input: sorted velocities
uint* gridParticleIndex, // input: sorted particle indices
uint* cellStart,
uint* cellEnd,
uint numParticles,
float dt)
{
uint index = __mul24(blockIdx.x, blockDim.x) + threadIdx.x;
if (index >= numParticles) return;
// read particle data from sorted arrays
float3 pos = oldPos[index];
float3 vel = oldVel[index];
// get address in grid
int3 gridPos = calcGridPos(pos);
// examine neighbouring cells
float3 force = make_float3(0.0f);
// traverse 27 neighbors
for (int z = -1; z <= 1; z++)
{
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
int3 neighbor_pos = gridPos + make_int3(x, y, z);
force += collideCell(neighbor_pos, index, pos, vel, oldPos, oldVel, cellStart, cellEnd);
}
}
}
// write new velocity back to original unsorted location
uint originalIndex = gridParticleIndex[index];
newVel[originalIndex] = vel + force * dt; // + force/mass * dt ?
}
inline __device__
float pbf_density_0(
int3 grid_pos,
uint index,
float3 pos,
float3* sorted_pos,
float* mass,
float* rest_density,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex
) // type: 0->fluid fluid 1->boundary boundary
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float density = 0.0f;
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
if (j != index) // check not colliding with self
{
uint original_index = gridParticleIndex[j];
float3 pos2 = sorted_pos[j];
float3 vec = pos - pos2;
float dist = length(vec);
float rho = 0.f;
rho = mass[original_index] * Poly6_W_CUDA(dist, params.effective_radius);
density += rho;
}
}
}
return density;
}
inline __device__
float pbf_density_1(
int3 grid_pos,
uint index,
float3 pos,
float3* sorted_pos,
float* mass,
float* rest_density,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex,
float* b_volume = nullptr) // type: 0->fluid fluid 1->boundary boundary
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float density = 0.0f;
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
if (j != index) // check not colliding with self
{
uint original_index = gridParticleIndex[j];
float3 pos2 = sorted_pos[j];
float3 vec = pos - pos2;
float dist = length(vec);
float rho = 0.f;
rho = (*rest_density) * b_volume[original_index] * Poly6_W_CUDA(dist, params.effective_radius);
density += rho;
}
}
}
return density;
}
inline __device__
float pbf_density_boundary(
int3 grid_pos,
float3 pos1,
float* rest_density,
float* volume,
CellData cell_data
)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_data.cellStart[grid_hash];
float density = 0.0f;
// if cell of boundary cell data is not empty
if (start_index != 0xffffffff)
{
// iterate over particles in this cell
uint end_index = cell_data.cellEnd[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
// no need to check collision (boundary cell data is not the same as fluid cell data)
uint original_index = cell_data.grid_index[j];
float3 pos2 = cell_data.sorted_pos[j];
float3 vec = pos1 - pos2;
float dist = length(vec);
float rho = (*rest_density) * volume[original_index] * Poly6_W_CUDA(dist, params.effective_radius);
density += rho;
}
}
// return contributions of boundary paritcles
return density;
}
// boundary - fluid
inline __device__
float pbf_boundary_density(
// boundary
int3 grid_pos, // searching grid pos
float3 pos1, // position of boundary particle
// fluid
float* mass,
float3* sorted_pos,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex
)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float density = 0.0f;
// if cell of boundary cell data is not empty
if (start_index != 0xffffffff)
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
// no need to check collision (boundary cell data is not the same as fluid cell data)
uint original_index = gridParticleIndex[j];
float3 pos2 = sorted_pos[j];
float3 vec = pos1 - pos2;
float dist = length(vec);
float rho = mass[original_index] * Poly6_W_CUDA(dist, params.effective_radius);
density += rho;
}
}
// return contributions of boundary paritcles
return density;
}
inline __device__
float pbf_lambda_0(
int3 grid_pos,
uint index,
float3 pos,
float* rest_density,
float* mass,
float3* sorted_pos,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex
)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float gradientC_sum = 0.f;
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
if (j != index) // check not colliding with self
{
uint original_index = gridParticleIndex[j];
//float particle_mass = mass[original_index];
float3 pos2 = sorted_pos[j];
float3 vec = pos - pos2;
float dist = length(vec);
float3 gradientC_j;
gradientC_j = (1.f / (*rest_density)) *
Poly6_W_Gradient_CUDA(vec, dist, params.effective_radius);
float dot_val = dot(gradientC_j, gradientC_j);
gradientC_sum += dot_val;
}
}
}
return gradientC_sum;
}
inline __device__
float pbf_lambda_1(
int3 grid_pos,
uint index,
float3 pos,
float* rest_density,
float* mass,
float3* sorted_pos,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex,
float* b_volume = nullptr)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float gradientC_sum = 0.f;
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
if (j != index) // check not colliding with self
{
uint original_index = gridParticleIndex[j];
float particle_mass = mass[original_index];
float3 pos2 = sorted_pos[j];
float3 vec = pos - pos2;
float dist = length(vec);
float3 gradientC_j;
float vol = b_volume[original_index];
gradientC_j = (1.f / (*rest_density)) *
((*rest_density) * vol / particle_mass) *
Poly6_W_Gradient_CUDA(vec, dist, params.effective_radius);
float dot_val = dot(gradientC_j, gradientC_j);
gradientC_sum += dot_val;
}
}
}
return gradientC_sum;
}
// fluid - boundary
inline __device__
float pbf_lambda_boundary(
int3 grid_pos, // searching grid pos
float3 pos1, // position of fluid particle
float* rest_density,
float particle_mass,
CellData cell_data, // cell data of boundary particle,
float* volume
)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_data.cellStart[grid_hash];
float gradientC_sum = 0.f;
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_data.cellEnd[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
uint original_index = cell_data.grid_index[j];
float vol = volume[original_index];
float3 pos2 = cell_data.sorted_pos[j];
float3 vec = pos1 - pos2;
float dist = length(vec);
float3 gradientC_j = (1.f / (*rest_density)) *
((*rest_density) * vol / particle_mass) *
Poly6_W_Gradient_CUDA(vec, dist, params.effective_radius);
float dot_val = dot(gradientC_j, gradientC_j);
gradientC_sum += dot_val;
}
}
return gradientC_sum;
}
// Boundary - fluid
inline __device__
float pbf_boundary_lambda(
// boundary
int3 grid_pos, // searching grid pos
float3 pos1, // position of boundary particle
float* rest_density,
float particle_mass,
float volume,
// fluid
float3* sorted_pos,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex
)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float gradientC_sum = 0.f;
// search in fluid cell
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
float3 pos2 = sorted_pos[j];
float3 vec = pos1 - pos2;
float dist = length(vec);
float3 gradientC_j = (1.f / (*rest_density)) *
Poly6_W_Gradient_CUDA(vec, dist, params.effective_radius);
float dot_val = dot(gradientC_j, gradientC_j);
gradientC_sum += dot_val;
}
}
return gradientC_sum;
}
inline __device__
float3 pbf_correction(
int3 grid_pos,
uint index,
float3 pos,
float lambda_i,
float* rest_density,
float3* sorted_pos,
float* lambda,
uint* cell_start,
uint* cell_end,
uint* gridParticleIndex,
float dt)
{
uint grid_hash = calcGridHash(grid_pos);
// get start of bucket for this cell
uint start_index = cell_start[grid_hash];
float3 correction = make_float3(0, 0, 0);
if (start_index != 0xffffffff) // cell is not empty
{
// iterate over particles in this cell
uint end_index = cell_end[grid_hash];
for (uint j = start_index; j < end_index; j++)
{
if (j != index) // check not colliding with self
{
uint original_index = gridParticleIndex[j];
float3 pos2 = sorted_pos[j];
float3 vec = pos - pos2;
float dist = length(vec);
float3 gradient = Poly6_W_Gradient_CUDA(vec, dist, params.effective_radius);
float scorr = -0.1f;
float x = Poly6_W_CUDA(dist, params.effective_radius) /
Poly6_W_CUDA(0.3f * params.effective_radius, params.effective_radius);
x = pow(x, 4);
scorr = scorr * x * dt * dt * dt;
//printf("scorr: %f\n", scorr);
float3 res = //(1.f / (*rest_density)) *
(lambda_i + lambda[original_index] +scorr)*
gradient;
correction += res;
}
}
//printf("Num neighbors: %u\n", end_index - start_index);
}
return correction;
}
// compute correction from boundary particles
inline __device__
float3 pbf_correction_boundary(
int3 grid_pos,
uint index,
float3 pos,
float lambda_i,
float* rest_density,
// boundary
CellData b_cell_data,
float* b_lambda,