-
Notifications
You must be signed in to change notification settings - Fork 0
/
advect_runge_kutta.cu
373 lines (295 loc) · 11.1 KB
/
advect_runge_kutta.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
/*
Author: Danny George
High Performance Simulation Laboratory
Boise State University
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
#include "advect_runge_kutta.cuh"
namespace {
struct AdvectRungeKuttaFunctor {
template <typename WindData>
AdvectRungeKuttaFunctor(const WindData &wd, const int &t_)
: u(thrust::raw_pointer_cast(&wd.u[0])),
v(thrust::raw_pointer_cast(&wd.v[0])),
w(thrust::raw_pointer_cast(&wd.w[0])),
t(t_),
num_x(wd.shape.x()), num_y(wd.shape.y()), num_z(wd.shape.z()), num_t(wd.shape.t())
{ }
const float *u; // pointer to WindData's 'u' data
const float *v; // pointer to WindData's 'v' data
const float *w; // pointer to WindData's 'w' data
const int t;
const size_t num_x;
const size_t num_y;
const size_t num_z;
const size_t num_t;
__host__ __device__
inline size_t get_index(size_t x, size_t y, size_t z, size_t t) const {
// TODO: hardcoded t to 0 for development ~v~~~~~~~~~
return x + y * num_x + z * num_y * num_x + 0 * num_x * num_y * num_z;
}
__host__ __device__
float3 get_velocity_border0(float x, float y, float z, float t) const {
float3 ret = make_float3(0.0f, 0.0f, 0.0f);
size_t x0, x1;
size_t y0, y1;
size_t z0, z1;
// border logic ----------
if (x <= 0) {
return ret;
} else if (x >= num_x-1) {
return ret;
} else {
x0 = (size_t)x;
x1 = x+1;
}
if (y <= 0) {
return ret;
} else if (y >= num_y-1) {
return ret;
} else {
y0 = (size_t)y;
y1 = y+1;
}
if (z <= 0) {
return ret;
} else if (z >= num_z-1) {
return ret;
} else {
z0 = (size_t)z;
z1 = z+1;
}
// distance from actual point to sampled point index
float x_d = x - x0;
float y_d = y - y0;
float z_d = z - z0;
size_t i000 = get_index(x0, y0, z0, t);
size_t i100 = get_index(x1, y1, z0, t);
size_t i010 = get_index(x0, y1, z0, t);
size_t i110 = get_index(x1, y1, z0, t);
size_t i001 = get_index(x0, y0, z1, t);
size_t i101 = get_index(x1, y0, z1, t);
size_t i011 = get_index(x0, y1, z1, t);
size_t i111 = get_index(x1, y1, z1, t);
float c00 = u[i000] * (1 - x_d) + u[i100] * x_d;
float c10 = u[i010] * (1 - x_d) + u[i110] * x_d;
float c01 = u[i001] * (1 - x_d) + u[i101] * x_d;
float c11 = u[i011] * (1 - x_d) + u[i111] * x_d;
float c0 = c00 * (1 - y_d) + c10 * y_d;
float c1 = c01 * (1 - y_d) + c11 * y_d;
ret.x = (c0 * (1 - z_d) + c1 * z_d);
c00 = v[i000] * (1 - x_d) + v[i100] * x_d;
c10 = v[i010] * (1 - x_d) + v[i110] * x_d;
c01 = v[i001] * (1 - x_d) + v[i101] * x_d;
c11 = v[i011] * (1 - x_d) + v[i111] * x_d;
c0 = c00 * (1 - y_d) + c10 * y_d;
c1 = c01 * (1 - y_d) + c11 * y_d;
ret.y = (c0 * (1 - z_d) + c1 * z_d);
c00 = w[i000] * (1 - x_d) + w[i100] * x_d;
c10 = w[i010] * (1 - x_d) + w[i110] * x_d;
c01 = w[i001] * (1 - x_d) + w[i101] * x_d;
c11 = w[i011] * (1 - x_d) + w[i111] * x_d;
c0 = c00 * (1 - y_d) + c10 * y_d;
c1 = c01 * (1 - y_d) + c11 * y_d;
ret.z = (c0 * (1 - z_d) + c1 * z_d);
return ret;
}
__host__ __device__
float3 get_velocity_clamp(float x, float y, float z, float t) const {
float3 ret;
size_t x0, x1;
size_t y0, y1;
size_t z0, z1;
// clamping logic ----------
if (x <= 0) {
x0 = 0;
x1 = 0;
} else if (x >= num_x-1) {
x0 = num_x-1;
x1 = num_x-1;
} else {
x0 = (size_t)x;
x1 = x+1;
}
if (y <= 0) {
y0 = 0;
y1 = 0;
} else if (y >= num_y-1) {
y0 = num_y-1;
y1 = num_y-1;
} else {
y0 = (size_t)y;
y1 = y+1;
}
if (z <= 0) {
z0 = 0;
z1 = 0;
} else if (z >= num_z-1) {
z0 = num_z-1;
z1 = num_z-1;
} else {
z0 = (size_t)z;
z1 = z+1;
}
// distance from actual point to sampled point index
float x_d = x - x0;
float y_d = y - y0;
float z_d = z - z0;
size_t i000 = get_index(x0, y0, z0, t);
size_t i100 = get_index(x1, y1, z0, t);
size_t i010 = get_index(x0, y1, z0, t);
size_t i110 = get_index(x1, y1, z0, t);
size_t i001 = get_index(x0, y0, z1, t);
size_t i101 = get_index(x1, y0, z1, t);
size_t i011 = get_index(x0, y1, z1, t);
size_t i111 = get_index(x1, y1, z1, t);
float c00 = u[i000] * (1 - x_d) + u[i100] * x_d;
float c10 = u[i010] * (1 - x_d) + u[i110] * x_d;
float c01 = u[i001] * (1 - x_d) + u[i101] * x_d;
float c11 = u[i011] * (1 - x_d) + u[i111] * x_d;
float c0 = c00 * (1 - y_d) + c10 * y_d;
float c1 = c01 * (1 - y_d) + c11 * y_d;
ret.x = (c0 * (1 - z_d) + c1 * z_d);
c00 = v[i000] * (1 - x_d) + v[i100] * x_d;
c10 = v[i010] * (1 - x_d) + v[i110] * x_d;
c01 = v[i001] * (1 - x_d) + v[i101] * x_d;
c11 = v[i011] * (1 - x_d) + v[i111] * x_d;
c0 = c00 * (1 - y_d) + c10 * y_d;
c1 = c01 * (1 - y_d) + c11 * y_d;
ret.y = (c0 * (1 - z_d) + c1 * z_d);
c00 = w[i000] * (1 - x_d) + w[i100] * x_d;
c10 = w[i010] * (1 - x_d) + w[i110] * x_d;
c01 = w[i001] * (1 - x_d) + w[i101] * x_d;
c11 = w[i011] * (1 - x_d) + w[i111] * x_d;
c0 = c00 * (1 - y_d) + c10 * y_d;
c1 = c01 * (1 - y_d) + c11 * y_d;
ret.z = (c0 * (1 - z_d) + c1 * z_d);
return ret;
}
template <typename Tuple>
__host__ __device__
void operator()(Tuple tup) const
{
float &x = thrust::get<0>(tup);
float &y = thrust::get<1>(tup);
float &z = thrust::get<2>(tup);
int &birthtime = thrust::get<3>(tup);
bool &has_deposited = thrust::get<4>(tup);
if (birthtime > t)
return;
if (has_deposited)
return;
float3 tmp;
float3 k1 = get_velocity_clamp(x, y, z, t);
tmp.x = x + 0.5f*k1.x;
tmp.y = y + 0.5f*k1.y;
tmp.z = z + 0.5f*k1.z;
float3 k2 = get_velocity_clamp(tmp.x, tmp.y, tmp.z, t);
tmp.x = x + 0.5f*k2.x;
tmp.y = y + 0.5f*k2.y;
tmp.z = z + 0.5f*k2.z;
float3 k3 = get_velocity_clamp(tmp.x, tmp.y, tmp.z, t);
// TODO: double check k4 equation...
float3 k4 = get_velocity_clamp(x+k3.x, y+k3.y, z+k3.z, t);
x = x + 1.0f/6.0f * (k1.x + k4.x) + 1.0f/3.0f * (k2.x + k3.x);
y = y + 1.0f/6.0f * (k1.y + k4.y) + 1.0f/3.0f * (k2.y + k3.y);
z = z + 1.0f/6.0f * (k1.z + k4.z) + 1.0f/3.0f * (k2.z + k3.z);
}
};
template <typename Particles, typename WindData>
void advect_runge_kutta_generic(Particles &p, WindData &wd, float t)
{
thrust::for_each(
thrust::make_zip_iterator(thrust::make_tuple(
p.pos_x.begin(),
p.pos_y.begin(),
p.pos_z.begin(),
p.birthtime.begin(),
p.has_deposited.begin())),
thrust::make_zip_iterator(thrust::make_tuple(
p.pos_x.end(),
p.pos_y.end(),
p.pos_z.end(),
p.birthtime.end(),
p.has_deposited.end())),
AdvectRungeKuttaFunctor(wd, t)
);
}
struct WindTextureAdvectRungeKuttaFunctor {
WindTextureAdvectRungeKuttaFunctor(WindDataTextureMemoryAccessor accessor, float t_)
: t(t_), get_wind(accessor)
{ }
const float t;
WindDataTextureMemoryAccessor get_wind;
template <typename Tuple>
__device__
void operator()(Tuple tup) const
{
float &x = thrust::get<0>(tup);
float &y = thrust::get<1>(tup);
float &z = thrust::get<2>(tup);
int &birthtime = thrust::get<3>(tup);
bool &has_deposited = thrust::get<4>(tup);
if (birthtime > t)
return;
if (has_deposited)
return;
float3 tmp;
//tmp.w = t; // TODO: need to adjust between each 'k' value?
float4 k1 = get_wind(x, y, z, t);
tmp.x = x + 0.5f*k1.x;
tmp.y = y + 0.5f*k1.y;
tmp.z = z + 0.5f*k1.z;
float4 k2 = get_wind(tmp.x, tmp.y, tmp.z, t);
tmp.x = x + 0.5f*k2.x;
tmp.y = y + 0.5f*k2.y;
tmp.z = z + 0.5f*k2.z;
float4 k3 = get_wind(tmp.x, tmp.y, tmp.z, t);
// TODO: double check k4 equation...
float4 k4 = get_wind(x+k3.x, y+k3.y, z+k3.z, t);
x = x + 1.0f/6.0f * (k1.x + k4.x) + 1.0f/3.0f * (k2.x + k3.x);
y = y + 1.0f/6.0f * (k1.y + k4.y) + 1.0f/3.0f * (k2.y + k3.y);
z = z + 1.0f/6.0f * (k1.z + k4.z) + 1.0f/3.0f * (k2.z + k3.z);
}
};
}
void advect_runge_kutta(ParticleSetThrustHost &particles, const WindDataThrustHost &wind, float t)
{
advect_runge_kutta_generic(particles, wind, t);
}
void advect_runge_kutta(ParticleSetThrustDevice &particles, const WindDataThrustDevice &wind, float t)
{
advect_runge_kutta_generic(particles, wind, t);
}
void advect_runge_kutta(ParticleSetThrustDevice &particles, WindDataTextureMemory &wind, float t)
{
wind.set_current_t(t);
thrust::for_each(
thrust::make_zip_iterator(thrust::make_tuple(
particles.pos_x.begin(),
particles.pos_y.begin(),
particles.pos_z.begin(),
particles.birthtime.begin(),
particles.has_deposited.begin())),
thrust::make_zip_iterator(thrust::make_tuple(
particles.pos_x.end(),
particles.pos_y.end(),
particles.pos_z.end(),
particles.birthtime.end(),
particles.has_deposited.end())),
WindTextureAdvectRungeKuttaFunctor(wind.get_accessor(), t)
);
}