forked from christophmschaefer/miluphcuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundary.cu
667 lines (598 loc) · 18.9 KB
/
boundary.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
/**
* @author Christoph Schaefer [email protected]
*
* @section LICENSE
* Copyright (c) 2019 Christoph Schaefer
*
* This file is part of miluphcuda.
*
* miluphcuda is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* miluphcuda is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with miluphcuda. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "timeintegration.h"
#include "boundary.h"
#include "miluph.h"
#include "pressure.h"
extern __device__ double substep_currentTimeD;
extern __device__ double currentTimeD;
extern __device__ double dt;
#if DENSITY_FLOOR
extern __device__ double density_floor_d;
#endif
#if GHOST_BOUNDARIES
/* these are the locations and the properties of the boundary walls */
const __device__ int numWalls = 1;
__device__ double d[numWalls] = {-0.007};
__device__ double nx[numWalls] = {0};
__device__ double ny[numWalls] = {0};
#if DIM == 3
__device__ double nz[numWalls] = {1};
#endif
//boundary type: 0 = no slip, 1 = free slip
#define NO_SLIP_BOUNDARY_TYPE 0
#define FREE_SLIP_BOUNDARY_TYPE 1
__device__ int boundaryType[numWalls] = {NO_SLIP_BOUNDARY_TYPE};
#endif
/* set quantities for Fixed Virtual Particles with matId == BOUNDARY_PARTICLE_ID */
__device__ void setQuantitiesFixedVirtualParticles(int i, int j, double *vxj, double *vyj, double *vzj, double *densityj, double *pressurej, double *Sj)
{
/* j is the virtual particle, i is the real particle */
int e;
/* distance to plane */
double dI, dJ;
double beta;
double oneminusbeta = 0;
#define BETA_MAX 1.5
#if DIM > 2
/* test values only for plane at z=0 */
dI = p.z[i];
dJ = p.z[j];
beta = min(BETA_MAX, 1.0 + dJ/dI);
oneminusbeta = 1-beta;
#endif
*vxj = oneminusbeta*p.vx[i];
#if DIM > 1
*vyj = oneminusbeta*p.vy[i];
#if DIM > 2
*vzj = oneminusbeta*p.vz[i];
#endif
#endif
#if SOLID
for (e = 0; e < DIM*DIM; e++) {
Sj[e] = p.S[i*DIM*DIM+e];
}
#endif
*pressurej = p.p[i];
*densityj = p.rho[i];
}
// declare some boundary conditions here: this is called at the beginning of each RHS step
__global__ void BoundaryConditionsBeforeRHS(int *interactions)
{
#if 1
register int i, inc;
int matId, d, e;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
if (matId == EOS_TYPE_IGNORE) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
#if DIM == 3
p.az[i] = 0;
p.dzdt[i] = 0;
p.vz[i] = 0;
#endif
#if SOLID
for (d = 0; d < DIM*DIM; d++) {
p.dSdt[i*DIM*DIM + d] = 0;
}
#endif
#if INTEGRATE_DENSITY
p.drhodt[i] = 0;
#endif
}
if (matId == BOUNDARY_PARTICLE_ID) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
#if DIM == 3
p.az[i] = 0;
p.dzdt[i] = 0;
p.vz[i] = 0;
#endif
#if SOLID
for (d = 0; d < DIM*DIM; d++) {
p.dSdt[i*DIM*DIM + d] = 0;
}
#endif
#if INTEGRATE_DENSITY
p.drhodt[i] = 0;
#endif
#if DENSITY_FLOOR
} else if (p.rho[i] < 1e-2*density_floor_d) {
p.rho[i] = 1e-2*density_floor_d;
#if INTEGRATE_DENSITY
p.drhodt[i] = 0.0;
#endif
#endif
}
}
#endif
}
// boundary conditions called after the integration step of rk2adaptive only
__global__ void BoundaryConditionsAfterIntegratorStep(int *interactions)
{
register int i, inc;
int matId, d, e;
double distance;
double ddistance;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
}
}
// declare some boundary conditions here: this is called at the end of each RHS step
__global__ void BoundaryConditionsAfterRHS(int *interactions)
{
#if 1
register int i, inc;
int matId, d, e;
double distance;
double ddistance;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
if (matId == EOS_TYPE_IGNORE) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
#if DIM == 3
p.az[i] = 0;
p.dzdt[i] = 0;
p.vz[i] = 0;
#endif
#if SOLID
for (d = 0; d < DIM*DIM; d++) {
p.dSdt[i*DIM*DIM + d] = 0;
}
#endif
#if INTEGRATE_DENSITY
p.drhodt[i] = 0;
#endif
}
// adding central star with one solar mass
// at (0,0)
#if 0
distance = 0.0;
ddistance = p.x[i]*p.x[i] + p.y[i]*p.y[i];
distance = sqrt(ddistance);
distance *= ddistance;
p.ax[i] -= 1.327474512e+20 * p.x[i] / distance;
p.ay[i] -= 1.327474512e+20 * p.y[i] / distance;
#endif
// p.az[i] -= 9.81;
/* let's stick to the ground */
#if 0
if (p.z[i] <= 1e-3) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
#if DIM == 3
p.az[i] = 0;
p.dzdt[i] = 0;
p.vz[i] = 0;
#endif
}
#endif
if (matId == BOUNDARY_PARTICLE_ID) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
#if DIM == 3
p.az[i] = 0;
p.dzdt[i] = 0;
p.vz[i] = 0;
#endif
#if SOLID
for (d = 0; d < DIM*DIM; d++) {
p.dSdt[i*DIM*DIM + d] = 0;
}
#endif
#if INTEGRATE_DENSITY
p.drhodt[i] = 0;
#endif
#if DENSITY_FLOOR
} else if (p.rho[i] < 1e-2*density_floor_d) {
p.rho[i] = 1e-2*density_floor_d;
#if INTEGRATE_DENSITY
p.drhodt[i] = 0.0;
#endif
#endif
}
}
#endif
}
#if GHOST_BOUNDARIES
__global__ void removeGhostParticles()
{
//call with only one thread and one block
numParticles = numRealParticles;
}
/* set the density, pressure and other quantities for the ghost particles */
__global__ void setQuantitiesGhostParticles()
{
register int i, inc, k, idx, currentNumParticles;
register int pidx;
#if SOLID
int a, b;
#endif
double normalVel;
double x, y;
#if DIM == 3
double z;
#endif
/* for NO_SLIP_BOUNDARY_TYPE, we stored i
for FREE_SLIP_BOUNDARY_TYPE, we stored -i see function insertGhostParticles() below */
inc = blockDim.x * gridDim.x;
/* loop over all ghost particles */
for (i = numRealParticles + threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i +=inc) {
/* the index of the corresponding real particle shifted by 1 (since we need the sign) */
idx = p.real_partner[i];
if (idx < 0) {
pidx = -idx;
} else {
pidx = idx;
}
pidx -= 1;
//mirror particle
p.cs[i] = p.cs[pidx];
p.p[i] = p.p[pidx];
p.e[i] = p.e[pidx];
p.rho[i] = p.rho[pidx];
#if SOLID
/* set deviatoric stress tensor depending on boundary type */
if (idx > 0) { /* NO_SLIP_BOUNDARY */
for (a = 0; a < DIM; a++) {
for (b = 0; b < DIM; b++) {
p.S[i*DIM*DIM+a*DIM+b] = p.S[pidx*DIM*DIM+a*DIM+b];
}
}
} else if (idx < 0) { /* FREE_SLIP_BOUNDARY */
for (a = 0; a < DIM; a++) {
for (b = 0; b < DIM; b++) {
p.S[i*DIM*DIM+a*DIM+b] = -p.S[pidx*DIM*DIM+a*DIM+b];
}
if (matEOS[p_rhs.materialId[i]] == EOS_TYPE_REGOLITH) {
p.S[i*DIM*DIM+a*DIM+a] *= -1;
}
}
} else {
printf("Error, cannot happen. Go away!\n");
assert(false);
}
#endif
}
}
/* sets the location, mass, sml for the ghost particles */
__global__ void insertGhostParticles()
{
//call with only one block
int i, inc, k;
volatile int idx;
#if SOLID
int a, b;
#endif
double sml;
double distance;
double normalVel;
double x, y;
#if DIM == 3
double z;
#endif
//boundary type: 0 = no slip, 1 = free slip
inc = blockDim.x * gridDim.x;
for (k = 0; k < numWalls; k++) {
__syncthreads();
int currentNumParticles = numParticles;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < currentNumParticles; i += inc) {
double sml;
sml = p.h[i];
x = p.x[i];
y = p.y[i];
#if DIM == 3
z = p.z[i];
#endif
//get distance to wall
distance = x*nx[k] + y*ny[k]-d[k];
#if DIM == 3
distance += z*nz[k];
#endif
//if distance small enough
if (fabs(distance) <= sml/2.0) {
//atomic read and increment of numParticles
idx = atomicAdd(&numParticles, 1);
assert(idx < maxNumParticles);
#if 1 // moved to extra function!
//mirror particle
#if (VARIABLE_SML || INTEGRATE_SML || DEAL_WITH_TOO_MANY_INTERACTIONS)
p.h[idx] = sml;
#endif
p.noi[idx] = p.noi[i];
p.cs[idx] = p.cs[i];
p.depth[idx] = p.depth[i];
p.p[idx] = p.p[i];
// p.e[idx] = p.e[i];
p_rhs.materialId[idx] = p_rhs.materialId[i];
p.m[idx] = p.m[i];
p.rho[idx] = p.rho[i];
#endif
/* set location of ghost particle */
p.x[idx] = x - 2*distance*nx[k];
p.y[idx] = y - 2*distance*ny[k];
#if DIM == 3
p.z[idx] = z - 2*distance*nz[k];
#endif
/* remember the real particle where the ghost particle
originates from */
/* for NO_SLIP_BOUNDARY_TYPE, we store i
for FREE_SLIP_BOUNDARY_TYPE, we store -i */
#if 1
if (boundaryType[k] == NO_SLIP_BOUNDARY_TYPE) {
p.real_partner[idx] = i+1;
} else if (boundaryType[k] == FREE_SLIP_BOUNDARY_TYPE) {
p.real_partner[idx] = -i-1;
} else {
printf("Error: no such boundary type for particle.\n");
assert(false);
}
#endif
/* set mass and material type and sml */
p.h[idx] = sml;
p_rhs.materialId[idx] = p_rhs.materialId[i];
/* all other quantities are set in function setQuantitiesGhostParticles() */
if (boundaryType[k] == NO_SLIP_BOUNDARY_TYPE) {
//free slip boundary
p.vx[idx] = -p.vx[i];
#if DIM > 1
p.vy[idx] = -p.vy[i];
#if DIM == 3
p.vz[idx] = -p.vz[i];
#endif
#endif
#if 0
#if SOLID
for (a = 0; a < DIM; a++) {
for (b = 0; b < DIM; b++) {
p.S[idx*DIM*DIM+a*DIM+b] = p.S[i*DIM*DIM+a*DIM+b];
}
}
#endif
#endif
} else if (boundaryType[k] == FREE_SLIP_BOUNDARY_TYPE) {
//free slip boundary
normalVel = nx[k]*p.vx[i];
#if DIM > 1
normalVel += ny[k]*p.vy[i];
#endif
#if DIM == 3
normalVel += nz[k]*p.vz[i];
#endif
p.vx[idx] = p.vx[i] - 2*normalVel*nx[k];
#if DIM > 1
p.vy[idx] = p.vy[i] - 2*normalVel*ny[k];
#if DIM == 3
p.vz[idx] = p.vz[i] - 2*normalVel*nz[k];
#endif
#endif
#if 0
#if SOLID
for (a = 0; a < DIM; a++) {
for (b = 0; b < DIM; b++) {
p.S[idx*DIM*DIM+a*DIM+b] = -p.S[i*DIM*DIM+a*DIM+b];
}
p.S[idx*DIM*DIM+a*DIM+a] *= -1;
}
#endif
#endif
}
} //end distance if
} //end particle loop
__syncthreads();
} //end wall loop
if (threadIdx.x + blockIdx.x*blockDim.x == 0) {
printf("number of particles after inserting: %d\t\t", numParticles);
printf("added %d particles\n", numParticles - numRealParticles);
}
}
#endif
/* this function places the brushes according to their rotation speed */
__global__ void BoundaryConditionsBrushesBefore(int *interactions)
{
#if 0
#warning: brushes on
register int i, inc;
int matId, d, e;
inc = blockDim.x * gridDim.x;
// revolutions per minute
const double rpm = 100;
const double omega = rpm * 2 * M_PI / 60;
// the offset, this is printed out by brush3D.py
const double yoff = 0.0523512;
double phi0 = 0;
double phi = 0;
double phit = 0; // no, it's really a p
double r = 0;
double y = 0;
double vz = -1e-2; // speed of the brushes in z direction
double zoff = 0;
double zoffangle = 0.170125; // the z-offset, this is printed out by brush3D.py
double zmax = -0.02; ; // = 1/6 brushdiameter from brush3D.py
double myz = 0.0;
zoff = substep_currentTimeD * vz;
if (zoff < zmax)
zoff = zmax;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
if (matId > 1) {
// new rotating angle
phi = omega*substep_currentTimeD;
// original angle
// new angle
// brush left (y<0) is matId = 1 and rotates counterclockwise
// brush right (y>0) is matId = 2 and rotates clockwise
// rotation is around x axis
myz = p.z0[i] - zoffangle;
if (matId == 2) {
y = p.y0[i] + yoff;
phi0 = atan2(myz,y);
phit = phi + phi0;
r = myz * myz + y*y;
r = sqrt(r);
p.y[i] = r * cos(phit) - yoff;
// coordinates
p.z[i] = r * sin(phit) + zoff + zoffangle;
p.x[i] = p.x0[i];
// velocity
p.vx[i] = 0.0;
p.vy[i] = -omega * r * sin(phit);
p.vz[i] = omega * r * cos(phit);
} else if (matId == 3) {
y = p.y0[i] - yoff;
phi0 = atan2(myz,y);
phit = phi0 - phi;
r = myz * myz + y*y;
r = sqrt(r);
p.y[i] = r * cos(phit) + yoff;
// coordinates
p.z[i] = r * sin(phit) + zoff + zoffangle;
p.x[i] = p.x0[i];
// velocity
p.vx[i] = 0.0;
p.vy[i] = omega * r * sin(phit);
p.vz[i] = -omega * r * cos(phit);
}
}
}
#endif
}
__global__ void BoundaryConditionsBrushesAfter(int *interactions)
{
#if 0
#warning: brushes on
register int i, inc;
int matId, d, e;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
if (matId > 0) {
p.ax[i] = 0;
p.ay[i] = 0;
p.dxdt[i] = 0;
p.dydt[i] = 0;
p.vx[i] = 0;
p.vy[i] = 0;
for (d = 0; d < DIM*DIM; d++) {
p.dSdt[i*DIM*DIM + d] = 0;
}
p.drhodt[i] = 0;
} else {
}
}
#endif
}
__global__ void BoundaryForce(int *interactions)
{
#if 0
#warning: brushes on
register int i, inc;
int matId, d, e, matIdj;
int k, j, numInteractions;
double distance;
double ljf = 0;
// D is somehow related to the largest velocity
double D = 10.0;
const double tiny = 1e-6;
const double r0 = 0.022574999999999998;
double dx, dy, dz;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
matId = p_rhs.materialId[i];
// only for regolith with matId == 0
if (matId > 0)
continue;
numInteractions = p.noi[i];
for (k = 0; k < numInteractions; k++) {
// the interaction partner
j = interactions[i * MAX_NUM_INTERACTIONS + k];
// check if interaction partner is boundary_particle and if not, continue
matIdj = p_rhs.materialId[j];
if (matIdj == BOUNDARY_PARTICLE_ID) {
// calculate lennard jones force
dx = p.x[i] - p.x[j];
dy = p.y[i] - p.y[j];
dz = 0.0;
#if DIM > 2
dz = p.z[i] - p.z[j];
#endif
distance = dx*dx + dy*dy + dz*dz;
distance += tiny;
distance = sqrt(distance);
if (r0/distance < 1) {
ljf = D * (pow(r0/distance, 12) - pow(r0/distance, 6)) * pow(distance, -2);
p.ax[i] -= ljf*dx;
p.ay[i] -= ljf*dy;
#if DIM > 2
p.az[i] -= ljf*dz;
#endif
}
}
#if 0
// check if interaction partner is brush and if not, continue
matIdj = p_rhs.materialId[j];
if (matIdj == 1 || matIdj == 2) {
// calculate lennard jones force
dx = p.x[i] - p.x[j];
dy = p.y[i] - p.y[j];
dz = p.z[i] - p.z[j];
distance = dx*dx + dy*dy + dz*dz;
distance += tiny;
distance = sqrt(distance);
if (r0/distance < 1) {
ljf = p.m[i] * D * (pow(r0/distance, 12) - pow(r0/distance, 4)) * pow(distance, -2);
p.ax[i] += ljf*dx;
p.ay[i] += ljf*dy;
p.az[i] += ljf*dz;
}
}
#endif
}
}
#endif
}