-
Notifications
You must be signed in to change notification settings - Fork 0
/
VelocityFastMarch.cu
166 lines (108 loc) · 4.44 KB
/
VelocityFastMarch.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
#include <stdio.h>
#include <math.h>
#include <float.h>
__device__ int sign(float val) {
return ((0.0 < val) ? 1 : -1 );
}
__global__ void FastMarchInit(float* phi, int pitch, int* Accept, int Apitch, float* Fext, int Nx, int Ny, float dx, float dy, float Xmin, float Ymin)
//float* XXo, float* YYo) // testing that the grid coord calculated right
{
// This will initialize the Velocity field near the interface
// ASSUMES THAT THE SIGNED DISTANCE FUNCTION DOES NOT NEED REINITIALIZATION
// Phi assumed accurate throughout domain. Only calculating Velocity extension Fext
int index_x = blockIdx.x * blockDim.x + threadIdx.x;
int index_y = blockIdx.y * blockDim.y + threadIdx.y;
int idx = index_x * pitch + index_y;
int idxA = index_x * Apitch + index_y;
// int row = idx/pitch;
// int col = idx%pitch;
int row = idxA/Apitch;
int col = idxA%Apitch;
float dd = sqrt(dx*dx/4+dy*dy/4); //to check if interface is nearby
int Aval = 0;
float Fval = 0;
if (col<Ny-1 && row<Nx-1 && col>1 && row>1){
if (abs(phi[idx])<dd){
Aval = 1;
Fval = Xmin + row * dx;
//Accept[idxA] = 1;// mark as on interface
//Fext[idx] = Xmin + row * dx; // NOTE TO DO LATER: build more flexible function than F(x,y)=x
}
else if ( abs(phi[idx - pitch])<dd || abs(phi[idx + pitch])<dd || abs(phi[idx - 1])<dd || abs(phi[idx + 1])<dd ){
Aval = -1;
Fval = Xmin + row * dx;
//Accept[idxA] = -11; // mark as tentative because it is a neigbor of interface
//Fext[idx] = Xmin + row * dx;
//printf("\nWHY!!!??? \t (r,c)=%d,%d\t , %d, %d, \t F=%3.3f, A=%d \n", row,col, idxA, idx, Fval, Aval );
}
Accept[idxA] = Aval;
Fext[idx] = Fval;
//if (Aval>2){
// printf("\nWHY!!!??? \t (r,c)=%d,%d\t , %d, %d, \t F=%3.3f, A=%d \n", row,col, idxA, idx, Fval, Aval );
//}
}
}
__global__ void FastMarchVelocity(int count, float* phi, int pitch, int* Accept, int Apitch, float* Fext, int* AcceptNew, float* FextNew, int Nx, int Ny, float dx, float dy)
{
int index_x = blockIdx.x * blockDim.x + threadIdx.x;
int index_y = blockIdx.y * blockDim.y + threadIdx.y;
int idx = index_x * pitch + index_y;
int idxA = index_x * Apitch + index_y;
int row = idx/pitch;
int col = idx%pitch;
float Dxp, Dxm, Dyp, Dym;
float Dx, Dy, FDx, FDy;
// put into local memory from global {center, top, right, bottom, left}
int A[5] = { Accept[idxA], Accept[idxA - Apitch], Accept[idxA + 1], Accept[idxA + Apitch], Accept[idxA - 1] } ;
float F[5] = { Fext[idx], Fext[idx-pitch], Fext[idx+1], Fext[idx+pitch], Fext[idx-1] } ;
float P[5] = { phi[idx], phi[idx-pitch], phi[idx+1], phi[idx+pitch], phi[idx-1] } ;
//Dxm = (row==0) ? 0 : -F[1] * abs(A[1]) * (P[0] - P[1])/dx ;
//Dxp = (row==Nx-1) ? 0 : F[3] * abs(A[3]) * (P[3] - P[0])/dx ;
//Dym = (col==0) ? 0 : -F[4] * abs(A[4]) * (P[0] - P[4])/dy ;
//Dyp = (col==Ny-1) ? 0 : F[2] * abs(A[2]) * (P[2] - P[0])/dy ;
Dxm = (row==0) ? 0 : abs(A[1]) * (P[0] - P[1])/dx ;
Dxp = (row==Nx-1) ? 0 : abs(A[3]) * (P[3] - P[0])/dx ;
Dym = (col==0) ? 0 : abs(A[4]) * (P[0] - P[4])/dy ;
Dyp = (col==Ny-1) ? 0 : abs(A[2]) * (P[2] - P[0])/dy ;
int tentative = 0;
tentative += (row==0) ? 0 : abs(A[1]);
tentative += (row==Nx-1) ? 0 : abs(A[3]);
tentative += (col==0) ? 0 : abs(A[4]);
tentative += (row==Nx-1) ? 0 : abs(A[2]);
if ((-Dxp>Dxm) && (-Dxp>0)){
FDx = F[3] * (-Dxp);
Dx = -Dxp;
}else if ((Dxm>-Dxp) &&(Dxm>0)){
FDx = F[1] * (Dxm);
Dx = Dxm;
}else{
FDx = 0;
Dx = 0;
}
if ((-Dyp>Dym) && (-Dyp>0)){
FDy = F[2] * (-Dyp);
Dy = -Dyp;
}else if ((Dym>-Dyp) &&(Dym>0)){
FDy = F[4] * (Dym);
Dy = Dym;
}else{
FDy = 0;
Dy = 0;
}
//if ( row<Nx && col<Ny && A[0]!=1 && tentative>0 && (Dx+Dy)>0 ) {
if ( row<Nx && col<Ny && A[0]!=1 && tentative>0 ) {
//if ( row<Nx && col<Ny && A[0]!=1 ) {
F[0] = (FDx + FDy)/(Dx + Dy + DBL_EPSILON);
A[0] = -1;
}
//__syncthreads();
// if (row<Nx && col<Ny && tentative==0 && count>49){
// printf("\n count= %d\t, (r,c)=(%d,%d)\t A= {%d,%d,%d,%d} \n ", count, row, col, A[1], A[2], A[3], A[4] );
// }
// if (A[0]>2){
// printf("\nWHY!!!??? \t (r,c)=%d,%d\t , %d, %d, \t F=%3.3f, A=%d \n", row,col, idxA, idx, F[0], A[0] );
// }
AcceptNew[idxA] = A[0];
FextNew[idx] = F[0];
//__syncthreads();
}