-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcudaQturbulence.cu
527 lines (466 loc) · 26.1 KB
/
cudaQturbulence.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
#include <pycuda-complex.hpp>
#include <pycuda-helpers.hpp>
#define pi 3.14159265f
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef pycuda::complex<cudaP> pyComplex;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void getAlphas_kernel( cudaP dx, cudaP dy, cudaP dz, cudaP xMin, cudaP yMin, cudaP zMin,
cudaP gammaX, cudaP gammaY, cudaP gammaZ,
pyComplex *psi1, cudaP *alphas){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
cudaP x = t_j*dx + xMin;
cudaP y = t_i*dy + yMin;
cudaP z = t_k*dz + zMin;
cudaP g = 8000;
cudaP psi_mod = abs(psi1[tid]);
cudaP result = (cudaP(0.5)*( gammaX*x*x + gammaY*y*y + gammaZ*z*z )) + g*psi_mod*psi_mod;
alphas[tid] = result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void getPartialsXY_kernel( cudaP Lx, cudaP Ly, pyComplex *fftTrnf,
pyComplex *partialxfft, cudaP *kxfft,
pyComplex *partialyfft, cudaP *kyfft ){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
cudaP kx = kxfft[t_j];
cudaP ky = kyfft[t_i];
pyComplex i_complex( cudaP(0.), cudaP(1.0));
pyComplex psiFFT = fftTrnf[tid];
partialxfft[tid] = kx*i_complex*psiFFT;
partialyfft[tid] = ky*i_complex*psiFFT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void getFFTderivatives_kernel( cudaP Lx, cudaP Ly, cudaP Lz, pyComplex *fftTrnf,
cudaP *kxfft, cudaP *kyfft, cudaP *kzfft,
pyComplex *partialxfft, pyComplex *partialyfft, pyComplex *laplacianfft){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
cudaP kx = kxfft[t_j];
cudaP ky = kyfft[t_i];
cudaP kz = kzfft[t_k];
cudaP k2 = kx*kx + ky*ky + kz*kz;
pyComplex i_complex( cudaP(0.), cudaP(1.0));
pyComplex psiFFT = fftTrnf[tid];
partialxfft[tid] = kx*i_complex*psiFFT;
partialyfft[tid] = ky*i_complex*psiFFT;
laplacianfft[tid] = -k2 * psiFFT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void setBoundryConditions_kernel(int nWidth, int nHeight, int nDepth, pycuda::complex<cudaP> *psi){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
if ( t_i==0 or t_i==(nHeight-1) or t_j==0 or t_j==(nWidth-1) or t_k==0 or t_k==(nDepth-1)){
psi[tid] = 0;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void implicitStep1_kernel( cudaP xMin, cudaP yMin, cudaP zMin, cudaP dx, cudaP dy, cudaP dz,
cudaP alpha, cudaP omega, cudaP gammaX, cudaP gammaY, cudaP gammaZ,
pyComplex *partialX_d,
pyComplex *partialY_d,
pyComplex *psi1_d, pyComplex *G1_d,
cudaP x0,cudaP y0){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
cudaP x = t_j*dx + xMin;
cudaP y = t_i*dy + yMin;
cudaP z = t_k*dz + zMin;
pyComplex iComplex( cudaP(0.0), cudaP(1.0) );
pyComplex complex1( cudaP(1.0), cudaP(0.0) );
pyComplex psi1, partialX, partialY, Vtrap, torque, lz, result;
cudaP g = 8000;
psi1 = psi1_d[tid];
cudaP psiMod = abs(psi1);
Vtrap = psi1*(gammaX*x*x + gammaY*y*y + gammaZ*z*z)*cudaP(0.5);
torque = psi1*g*psiMod*psiMod;
partialX = partialX_d[tid];
partialY = partialY_d[tid];
lz = iComplex * omega * (partialY*(x-x0) - partialX*(y-y0));
G1_d[tid] = psi1*alpha - Vtrap - torque - lz;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void implicitStep2_kernel( cudaP dt, cudaP *kx, cudaP *ky, cudaP *kz, cudaP alpha,
pyComplex *psiTransf, pyComplex *GTranf){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
cudaP kX = kx[t_j];
cudaP kY = ky[t_i];
cudaP kZ = kz[t_k];
cudaP k2 = kX*kX + kY*kY + kZ*kZ;
pyComplex factor, timeStep, psiT, Gt;
factor = cudaP(2.0) / ( 2 + dt*(k2 + 2*alpha) );
psiT = psiTransf[tid];
Gt = GTranf[tid];
psiTransf[tid] = factor * ( psiT + Gt*dt);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void findActivity_kernel( cudaP minDensity, pyComplex *psi_d, unsigned char *activity ){
int t_j = blockIdx.x* %(blockDim.x)s + threadIdx.x;
int t_i = blockIdx.y* %(blockDim.y)s + threadIdx.y;
int t_k = blockIdx.z* %(blockDim.z)s + threadIdx.z;
int tid = t_j + t_i* %(blockDim.x)s * %(gridDim.x)s + t_k* %(blockDim.x)s * %(gridDim.x)s * %(blockDim.y)s * %(gridDim.y)s ;
int tid_b = threadIdx.x + threadIdx.y* %(blockDim.x)s + threadIdx.z* %(blockDim.x)s * %(blockDim.y)s ;
// int bid = blockIdx.x + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ;
pyComplex psi = psi_d[tid];
__shared__ cudaP density[ %(THREADS_PER_BLOCK)s ];
density[tid_b] = norm(psi);
__syncthreads();
int i = %(blockDim.x)s * %(blockDim.y)s * %(blockDim.z)s / 2;
while ( i > 0 ){
if ( tid_b < i ) density[tid_b] = density[tid_b] + density[tid_b+i];
__syncthreads();
i /= 2;
}
if ( tid_b == 0 ){
if (density[0] >= minDensity ) {
activity[ blockIdx.x + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//right
if (blockIdx.x < %(gridDim.x)s -1) activity[ (blockIdx.x+1) + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//left
if (blockIdx.x > 0) activity[ (blockIdx.x-1) + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//up
if (blockIdx.y < %(gridDim.y)s -1) activity[ blockIdx.x + (blockIdx.y+1)* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//down
if (blockIdx.y > 0) activity[ blockIdx.x + (blockIdx.y-1)* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//top
if (blockIdx.z < %(gridDim.z)s -1) activity[ blockIdx.x + blockIdx.y* %(gridDim.x)s + (blockIdx.z+1)* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
//bottom
if (blockIdx.z > 0) activity[ blockIdx.x + blockIdx.y* %(gridDim.x)s + (blockIdx.z-1)* %(gridDim.x)s * %(gridDim.y)s ] = (unsigned char) 1;
}
}
}
__global__ void getActivity_kernel( cudaP *psiOther, unsigned char *activity ){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
int tid_b = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.x*blockDim.z;
int bid = blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y;
__shared__ unsigned char activeBlock;
if (tid_b == 0 ) activeBlock = activity[bid];
__syncthreads();
if ( activeBlock ) psiOther[tid] = cudaP(0.4);
else psiOther[tid] = cudaP(0.08);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void getVelocity_kernel( int neighbors, cudaP dx, cudaP dy, cudaP dz, pyComplex *psi, unsigned char *activity, cudaP *psiOther){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
int tid_b = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.x*blockDim.y;
int bid = blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y;
//Border blocks are skiped
if ( ( blockIdx.x == 0 or blockDim.x == gridDim.x -1 ) or ( blockIdx.y == 0 or blockDim.y == gridDim.y -1 ) or ( blockIdx.z == 0 or blockDim.z == %(gridDim.z)s -1 ) ) return;
__shared__ unsigned char activeBlock;
if (tid_b == 0 ) activeBlock = activity[bid];
__syncthreads();
if ( !activeBlock ) return;
pyComplex center = psi[tid];
__shared__ pyComplex psi_sh[ %(B_WIDTH)s ][ %(B_HEIGHT)s ][ %(B_DEPTH)s ];
__syncthreads();
psi_sh[threadIdx.x][threadIdx.y][threadIdx.z] = center;
__syncthreads();
cudaP dxInv = cudaP(1.0)/dx;
cudaP dyInv = cudaP(1.0)/dy;
cudaP dzInv = cudaP(1.0)/dz;
pyComplex gradient_x, gradient_y, gradient_z;
if ( threadIdx.x == 0 ) gradient_x = ( psi_sh[threadIdx.x+1][threadIdx.y][threadIdx.z] - center )*dxInv;
else if ( threadIdx.x == (blockDim.x-1) ) gradient_x = ( center - psi_sh[threadIdx.x-1][threadIdx.y][threadIdx.z] )*dxInv;
else gradient_x = ( psi_sh[threadIdx.x+1][threadIdx.y][threadIdx.z] - psi_sh[threadIdx.x-1][threadIdx.y][threadIdx.z] ) * dxInv* cudaP(0.5);
if ( threadIdx.y == 0 ) gradient_y = ( psi_sh[threadIdx.x][threadIdx.y+1][threadIdx.z] - center )*dyInv;
else if ( threadIdx.y == (blockDim.y-1) ) gradient_y = ( center - psi_sh[threadIdx.x][threadIdx.y-1][threadIdx.z] )*dyInv;
else gradient_y = ( psi_sh[threadIdx.x][threadIdx.y+1][threadIdx.z] - psi_sh[threadIdx.x][threadIdx.y-1][threadIdx.z] ) * dyInv* cudaP(0.5);
if ( threadIdx.z == 0 ) gradient_z = ( psi_sh[threadIdx.x][threadIdx.y][threadIdx.z+1] - center )*dzInv;
else if ( threadIdx.z == (blockDim.z-1) ) gradient_z = ( center - psi_sh[threadIdx.x][threadIdx.y][threadIdx.z-1] )*dzInv;
else gradient_z = ( psi_sh[threadIdx.x][threadIdx.y][threadIdx.z+1] - psi_sh[threadIdx.x][threadIdx.y][threadIdx.z-1] ) * dzInv* cudaP(0.5);
// __syncthreads();
cudaP rho = norm(center) + cudaP(5e-6);
cudaP velX = (center._M_re*gradient_x._M_im - center._M_im*gradient_x._M_re)/rho;
cudaP velY = (center._M_re*gradient_y._M_im - center._M_im*gradient_y._M_re)/rho;
cudaP velZ = (center._M_re*gradient_z._M_im - center._M_im*gradient_z._M_re)/rho;
psiOther[tid] = sqrt( velX*velX + velY*velY + velZ*velZ ) ;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__device__ pyComplex vortexCore( const int nWidth, const int nHeight, const int nDepth,
const cudaP xMin, const cudaP yMin, const cudaP zMin,
const cudaP dx, const cudaP dy, const cudaP dz,
const int t_i, const int t_j, const int t_k, const int tid, const int tid_b,
const cudaP gammaX, const cudaP gammaY, const cudaP gammaZ,
const cudaP omega, pyComplex *psiStep){
pyComplex center = psiStep[tid];
__shared__ pyComplex psi_sh[ %(B_WIDTH)s ][ %(B_HEIGHT)s ][ %(B_DEPTH)s ];
__syncthreads();
psi_sh[threadIdx.x][threadIdx.y][threadIdx.z] = center;
__syncthreads();
const cudaP dxInv = cudaP(1.0)/dx;
const cudaP dyInv = cudaP(1.0)/dy;
const cudaP dzInv = cudaP(1.0)/dz;
const cudaP x = t_j*dx + xMin;
const cudaP y = t_i*dy + yMin;
const cudaP z = t_k*dz + zMin;
const pyComplex iComplex( cudaP(0.), cudaP(1.) );
// pyComplex laplacian( cudaP(0.), cudaP(0.) );
// pyComplex Lz( cudaP(0.), cudaP(0.) );
pyComplex psiMinus, psiPlus;
// laplacian X-term
if (threadIdx.x==0 ){
psiMinus = psiStep[ (t_j-1) + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiPlus = psi_sh[threadIdx.x+1][threadIdx.y][threadIdx.z];
}
else if (threadIdx.x==blockDim.x-1){
psiPlus = psiStep[ (t_j+1) + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiMinus = psi_sh[threadIdx.x-1][threadIdx.y][threadIdx.z];
}
else {
psiPlus = psi_sh[threadIdx.x+1][threadIdx.y][threadIdx.z];
psiMinus = psi_sh[threadIdx.x-1][threadIdx.y][threadIdx.z];
}
// psiMinus = threadIdx.x == 0 ?
// psiStep[ (t_j-1) + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ] :
// psi_sh[threadIdx.x-1][threadIdx.y][threadIdx.z];
// psiPlus = threadIdx.x == (nWidth-1) ?
// psiStep[ (t_j+1) + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ] :
// psi_sh[threadIdx.x+1][threadIdx.y][threadIdx.z];
pyComplex laplacian = ( psiPlus + psiMinus - cudaP(2)*center )*dxInv*dxInv;
pyComplex Lz = -iComplex*( psiPlus - psiMinus )*dxInv*cudaP(0.5)*y;
// laplacian Y-term
if (threadIdx.y==0 ){
psiMinus = psiStep[ t_j + (t_i-1)*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiPlus = psi_sh[threadIdx.x][threadIdx.y+1][threadIdx.z];
}
else if (threadIdx.y==blockDim.y-1){
psiPlus = psiStep[ t_j + (t_i+1)*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiMinus = psi_sh[threadIdx.x][threadIdx.y-1][threadIdx.z];
}
else {
psiPlus = psi_sh[threadIdx.x][threadIdx.y+1][threadIdx.z];
psiMinus = psi_sh[threadIdx.x][threadIdx.y-1][threadIdx.z];
}
laplacian += ( psiPlus + psiMinus - cudaP(2)*center )*dyInv*dyInv;
Lz += iComplex*( psiPlus - psiMinus)*dyInv*cudaP(0.5)*x;
// laplacian Z-term
if (threadIdx.z==0 ){
psiMinus = psiStep[ t_j + t_i*blockDim.x*gridDim.x + (t_k-1)*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiPlus = psi_sh[threadIdx.x][threadIdx.y][threadIdx.z+1];
}
else if (threadIdx.z==blockDim.z-1){
psiPlus = psiStep[ t_j + t_i*blockDim.x*gridDim.x + (t_k+1)*blockDim.x*gridDim.x*blockDim.y*gridDim.y ];
psiMinus = psi_sh[threadIdx.x][threadIdx.y][threadIdx.z-1];
}
else {
psiPlus = psi_sh[threadIdx.x][threadIdx.y][threadIdx.z+1];
psiMinus = psi_sh[threadIdx.x][threadIdx.y][threadIdx.z-1];
}
laplacian += ( psiPlus + psiMinus - cudaP(2)*center )*dzInv*dzInv;
// pyComplex GP;
cudaP Vtrap_GP = (gammaX*x*x + gammaY*y*y + gammaZ*z*z)*cudaP(0.5) + 8000*norm(center);
// cudaP GP = ;
return iComplex*(laplacian*cudaP(0.5) - Vtrap_GP*center - Lz*omega);
// return iComplex*(laplacian*cudaP(0.5) - Vtrap - GP );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__device__ pyComplex vortexCore_fft( int nWidth, int nHeight, int nDepth, cudaP xMin, cudaP yMin, cudaP zMin,
cudaP dx, cudaP dy, cudaP dz, int t_i, int t_j, int t_k, int tid,
cudaP gammaX, cudaP gammaY, cudaP gammaZ, cudaP omega, cudaP x0, cudaP y0,
pyComplex *psiStep, pyComplex *laplacian, pyComplex *partialX, pyComplex *partialY){
pyComplex center = psiStep[tid];
// cudaP dxInv = cudaP(1.0)/dx;
// cudaP dyInv = cudaP(1.0)/dy;
// cudaP dzInv = cudaP(1.0)/dz;
cudaP x = t_j*dx + xMin;
cudaP y = t_i*dy + yMin;
cudaP z = t_k*dz + zMin;
pyComplex iComplex( cudaP(0.), cudaP(1.) );
pyComplex Vtrap, GP, Lz;
Vtrap = (gammaX*x*x + gammaY*y*y + gammaZ*z*z)*cudaP(0.5)*center;
GP = cudaP(8000.)*norm(center)*center;
Lz = iComplex*( partialY[tid] - partialX[tid] )*omega;
return iComplex * ( laplacian[tid]*cudaP(0.5) - Vtrap - GP - Lz );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
texture< fp_tex_cudaP, cudaTextureType3D, cudaReadModeElementType> tex_psiReal;
texture< fp_tex_cudaP, cudaTextureType3D, cudaReadModeElementType> tex_psiImag;
surface< void, cudaSurfaceType3D> surf_psiReal;
surface< void, cudaSurfaceType3D> surf_psiImag;
__device__ pyComplex vortexCore_tex
( cudaP xMin, cudaP yMin, cudaP zMin,
cudaP dx, cudaP dy, cudaP dz, int t_i, int t_j, int t_k,
cudaP gammaX, cudaP gammaY, cudaP gammaZ, cudaP omega ){
const cudaP dxInv = 1.0f/dx;
// cudaP dyInv = 1.0f/dy;
// cudaP dzInv = 1.0f/dz;
const cudaP x = t_j*dx + xMin;
const cudaP y = t_i*dy + yMin;
const cudaP z = t_k*dz + zMin;
const pyComplex iComplex( 0, 1.0f );
pyComplex center, psiPlus, psiMinus, laplacian;
pyComplex Lz;
center._M_re = fp_tex3D(tex_psiReal, t_j, t_i, t_k);
center._M_im = fp_tex3D(tex_psiImag, t_j, t_i, t_k);
psiPlus._M_re = fp_tex3D(tex_psiReal, t_j+1, t_i, t_k);
psiPlus._M_im = fp_tex3D(tex_psiImag, t_j+1, t_i, t_k);
psiMinus._M_re = fp_tex3D(tex_psiReal, t_j-1, t_i, t_k);
psiMinus._M_im = fp_tex3D(tex_psiImag, t_j-1, t_i, t_k);
laplacian = (psiPlus + psiMinus - cudaP(2)*center )*dxInv*dxInv;
Lz = iComplex*( psiMinus - psiPlus )*dxInv*cudaP(0.5)*y ;
psiPlus._M_re = fp_tex3D(tex_psiReal, t_j, t_i+1, t_k);
psiPlus._M_im = fp_tex3D(tex_psiImag, t_j, t_i+1, t_k);
psiMinus._M_re = fp_tex3D(tex_psiReal, t_j, t_i-1, t_k);
psiMinus._M_im = fp_tex3D(tex_psiImag, t_j, t_i-1, t_k);
laplacian += (psiPlus + psiMinus - cudaP(2)*center )*dxInv*dxInv;
Lz += iComplex*( (psiPlus - psiMinus)*dxInv*cudaP(0.5)*x );
psiPlus._M_re = fp_tex3D(tex_psiReal, t_j, t_i, t_k+1);
psiPlus._M_im = fp_tex3D(tex_psiImag, t_j, t_i, t_k+1);
psiMinus._M_re = fp_tex3D(tex_psiReal, t_j, t_i, t_k-1);
psiMinus._M_im = fp_tex3D(tex_psiImag, t_j, t_i, t_k-1);
laplacian += (psiPlus + psiMinus - cudaP(2)*center )*dxInv*dxInv;
const cudaP Vtrap_GP = 8000*norm(center) + (gammaX*x*x + gammaY*y*y + gammaZ*z*z)*cudaP(0.5);
return iComplex*(laplacian*cudaP(0.5) - (Vtrap_GP)*center - Lz*omega);
// return iComplex*(laplacian*cudaP(0.5) - (Vtrap_GP)*center );
}
////////////////////////////////////////////////////////////////////////////////
////////////////////// EULER //////////////////////////
////////////////////////////////////////////////////////////////////////////////
__global__ void eulerStep_kernel( const int nWidth, const int nHeight, const int nDepth,
const cudaP slopeCoef, const cudaP weight,
const cudaP xMin, const cudaP yMin, const cudaP zMin,
const cudaP dx, const cudaP dy, const cudaP dz, const cudaP dt,
const cudaP gammaX, const cudaP gammaY, const cudaP gammaZ, const cudaP omega,
pyComplex *psi_d, pyComplex *psiStepIn, pyComplex *psiStepOut, pyComplex *psiRunge,
unsigned char lastRK4Step, unsigned char *activity ){
const int t_j = blockIdx.x*blockDim.x + threadIdx.x;
const int t_i = blockIdx.y*blockDim.y + threadIdx.y;
const int t_k = blockIdx.z*blockDim.z + threadIdx.z;
const int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
const int tid_b = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.x*blockDim.y;
// int bid = blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y;
//Border blocks are skiped
if ( ( blockIdx.x == 0 or blockDim.x == gridDim.x -1 ) or ( blockIdx.y == 0 or blockDim.y == gridDim.y -1 ) or ( blockIdx.z == 0 or blockDim.z == gridDim.z -1 ) ) return;
//Unactive blocks are skiped
__shared__ unsigned char activeBlock;
if ( tid_b == 0 ) activeBlock = activity[blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y];
__syncthreads();
if ( !activeBlock ) return;
pyComplex value;
value = vortexCore( nWidth, nHeight, nDepth, xMin, yMin, zMin,
dx, dy, dz, t_i, t_j, t_k, tid, tid_b,
gammaX, gammaY, gammaZ, omega, psiStepIn );
value = dt*value;
if (lastRK4Step ){
value = psiRunge[tid] + slopeCoef*value/cudaP(6.);
psiRunge[tid] = value;
psiStepOut[tid] = value;
psi_d[tid] = value;
}
else{
psiStepOut[tid] = psi_d[tid] + weight*value;
//add to rk4 final value
psiRunge[tid] = psiRunge[tid] + slopeCoef*value/cudaP(6.);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void eulerStep_texture_kernel( const cudaP slopeCoef, const cudaP weight,
const cudaP xMin, const cudaP yMin, const cudaP zMin,
const cudaP dx, const cudaP dy, const cudaP dz, const cudaP dt,
const cudaP gammaX, const cudaP gammaY, const cudaP gammaZ, const cudaP omega,
pyComplex *psi_d, pyComplex *psiRunge,
unsigned char lastRK4Step, unsigned char *activity ){
const int t_j = blockIdx.x* %(blockDim.x)s + threadIdx.x;
const int t_i = blockIdx.y* %(blockDim.y)s + threadIdx.y;
const int t_k = blockIdx.z* %(blockDim.z)s + threadIdx.z;
const int tid = t_j + t_i* %(blockDim.x)s * %(gridDim.x)s + t_k* %(blockDim.x)s * %(gridDim.x)s * %(blockDim.y)s * %(gridDim.y)s ;
const int tid_b = threadIdx.x + threadIdx.y* %(blockDim.x)s + threadIdx.z* %(blockDim.x)s * %(blockDim.y)s ;
// int bid = blockIdx.x + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ;
//Border blocks are skiped
if ( ( blockIdx.x == 0 or %(blockDim.x)s == %(gridDim.x)s -1 ) or ( blockIdx.y == 0 or %(blockDim.y)s == %(gridDim.y)s -1 ) or ( blockIdx.z == 0 or %(blockDim.z)s == %(gridDim.z)s -1 ) ) return;
//Unactive blocks are skiped
__shared__ unsigned char activeBlock;
if ( tid_b == 0 ) activeBlock = activity[blockIdx.x + blockIdx.y* %(gridDim.x)s + blockIdx.z* %(gridDim.x)s * %(gridDim.y)s ];
__syncthreads();
if ( !activeBlock ) return;
pyComplex value;
value = vortexCore_tex( xMin, yMin, zMin,
dx, dy, dz, t_i, t_j, t_k,
gammaX, gammaY, gammaZ, omega );
value = dt*value;
if (lastRK4Step ){
value = psiRunge[tid] + slopeCoef*value/cudaP(6.);
psiRunge[tid] = value;
psi_d[tid] = value;
surf3Dwrite( value._M_re, surf_psiReal, t_j*sizeof(cudaP), t_i, t_k, cudaBoundaryModeClamp);
surf3Dwrite( value._M_im, surf_psiImag, t_j*sizeof(cudaP), t_i, t_k, cudaBoundaryModeClamp);
}
else{
//add to rk4 final value
psiRunge[tid] = psiRunge[tid] + slopeCoef*value/cudaP(6.);
value = psi_d[tid] + weight*value;
surf3Dwrite( value._M_re, surf_psiReal, t_j*sizeof(cudaP), t_i, t_k, cudaBoundaryModeClamp);
surf3Dwrite( value._M_im, surf_psiImag, t_j*sizeof(cudaP), t_i, t_k, cudaBoundaryModeClamp);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__global__ void eulerStep_fft_kernel( int nWidth, int nHeight, int nDepth, cudaP slopeCoef, cudaP weight,
cudaP xMin, cudaP yMin, cudaP zMin, cudaP dx, cudaP dy, cudaP dz, cudaP dt,
cudaP gammaX, cudaP gammaY, cudaP gammaZ, cudaP x0, cudaP y0, cudaP omega,
pyComplex *psi_d, pyComplex *psiStepIn, pyComplex *psiStepOut, pyComplex *psiRunge,
pyComplex *laplacian, pyComplex *partialX, pyComplex *partialY,
unsigned char lastRK4Step, unsigned char *activity ){
int t_j = blockIdx.x*blockDim.x + threadIdx.x;
int t_i = blockIdx.y*blockDim.y + threadIdx.y;
int t_k = blockIdx.z*blockDim.z + threadIdx.z;
int tid = t_j + t_i*blockDim.x*gridDim.x + t_k*blockDim.x*gridDim.x*blockDim.y*gridDim.y;
int tid_b = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.x*blockDim.y;
// int bid = blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y;
//Border blocks are skiped
if ( ( blockIdx.x == 0 or blockDim.x == gridDim.x -1 ) or ( blockIdx.y == 0 or blockDim.y == gridDim.y -1 ) or ( blockIdx.z == 0 or blockDim.z == gridDim.z -1 ) ) return;
//Unactive blocks are skiped
__shared__ unsigned char activeBlock;
if (tid_b == 0 ) activeBlock = activity[blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y];
__syncthreads();
if ( !activeBlock ) return;
pyComplex value;
value = vortexCore_fft( nWidth, nHeight, nDepth, xMin, yMin, zMin,
dx, dy, dz, t_i, t_j, t_k, tid,
gammaX, gammaY, gammaZ, omega, x0, y0,
psiStepIn, laplacian, partialX, partialY );
value = dt*value;
if (lastRK4Step ){
pyComplex valueOut = psiRunge[tid] + slopeCoef*value/cudaP(6.);
psiRunge[tid] = valueOut;
psiStepOut[tid] = valueOut;
psi_d[tid] = valueOut;
}
else{
psiStepOut[tid] = psi_d[tid] + weight*value;
//add to rk4 final value
psiRunge[tid] = psiRunge[tid] + slopeCoef*value/cudaP(6.);
}
}