-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluid.cpp
419 lines (372 loc) · 10.7 KB
/
fluid.cpp
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <cmath>
#include <limits>
#ifdef __AVX__
#include <immintrin.h>
#else
#warning avx is not enabled
#endif
#include <algorithm>
#include <iostream>
#include <cassert>
#include <chrono>
using namespace std;
typedef unsigned int uint;
struct alignas(8) Vector2 {
float x, y;
Vector2(float _x = 0, float _y = 0) : x(_x), y(_y) {}
Vector2 operator+(const Vector2 &other) const {
return Vector2(x + other.x, y + other.y);
}
Vector2 operator-(const Vector2 &other) const {
return Vector2(x - other.x, y - other.y);
}
Vector2 operator+=(const Vector2 &other) {
x += other.x;
y += other.y;
return *this;
}
Vector2 operator-=(const Vector2 &other) {
x -= other.x;
y -= other.y;
return *this;
}
Vector2 operator*=(float c) {
x *= c;
y *= c;
return *this;
}
Vector2 operator/=(float c) {
x /= c;
y /= c;
return *this;
}
float mag2() { return x * x + y * y; }
friend ostream &operator<<(ostream &os, const Vector2 &vec) {
os << "(" << vec.x << ", " << vec.y << ")";
return os;
}
};
inline Vector2 operator-(const Vector2 &vec) { return Vector2(-vec.x, -vec.y); }
inline Vector2 operator*(float s, const Vector2 &vec) {
return Vector2(vec.x * s, vec.y * s);
}
inline Vector2 operator*(const Vector2 &vec, float s) {
return Vector2(vec.x * s, vec.y * s);
}
inline Vector2 operator/(float s, const Vector2 &vec) {
return Vector2(vec.x / s, vec.y / s);
}
inline Vector2 operator/(const Vector2 &vec, float s) {
return Vector2(vec.x / s, vec.y / s);
}
void gradient(Vector2 *output, float *input, uint W, uint H) {
for (uint x = 1; x < W - 1; ++x)
for (uint y = 1; y < H - 1; ++y) {
#ifdef __AVX__
output[x * H + y] = Vector2(
input[(x + 1) * H + y] - input[(x - 1) * H + y],
input[x * H + y + 1] - input[x * H + y - 1]
);
#else
output[x * H + y] = Vector2(
input[(x + 1) * H + y] - input[(x - 1) * H + y],
input[x * H + y + 1] - input[x * H + y - 1]
)*0.5;
#endif
}
#ifdef __AVX__
float *outf = reinterpret_cast<float*>(output);
for (uint i = 0;i < 2*W*H/8;++i) {
__m256 a = _mm256_load_ps(outf + i*8);
__m256 mul = _mm256_set1_ps(0.5);
__m256 result = _mm256_mul_ps(a,mul);
_mm256_store_ps(outf + i*8, result);
}
#endif
}
void subtract(Vector2 *dst_vec, Vector2 *src_vec, uint W, uint H) {
#ifdef __AVX__
float *src = reinterpret_cast<float *>(src_vec);
float *dst = reinterpret_cast<float *>(dst_vec);
uint iters = 2 * W * H / 8; //*2 because vec has two floats
for (uint i = 0; i < iters; ++i) {
uint s = i * 8;
__m256 a = _mm256_load_ps(dst + s);
__m256 b = _mm256_load_ps(src + s);
a = _mm256_sub_ps(a, b);
_mm256_store_ps(dst + s, a);
}
#else
for (uint i = 0; i < W * H; ++i)
dst_vec[i] -= src_vec[i];
#endif
}
class PhyBox {
Vector2 *v; // velocity
Vector2 *tmpMem; // used for calculations
Vector2 *tmpMem2; // used for calculations
alignas(32) float *p; // pressure
float viscosity = 0.01;
uint W, H;
public:
PhyBox(uint width, uint height) : W(width), H(height) {
assert((W * H) % 8 == 0);
v = new (std::align_val_t(32)) Vector2[W * H];
tmpMem = new (std::align_val_t(32)) Vector2[W * H];
tmpMem2 = new (std::align_val_t(32)) Vector2[W * H];
p = new (std::align_val_t(32)) float[W * H];
}
~PhyBox() {
delete[] v;
delete[] p;
delete[] tmpMem;
}
void updateBuffer(uint32_t *buffer) {
float max_x, min_x, max_y, min_y;
#ifdef SCALE
max_x = max_y = -std::numeric_limits<float>::infinity();
min_x = min_y = std::numeric_limits<float>::infinity();
for (uint i = 0; i < W * H; ++i) {
max_x = max(max_x, v[i].x);
min_x = min(min_x, v[i].x);
max_y = max(max_y, v[i].y);
min_y = min(min_y, v[i].y);
}
#else
max_x = max_y = 60;
min_x = min_y = -20;
#endif
float sx = 255 / (max_x - min_x);
float sy = 255 / (max_y - min_y);
for (uint i = 0; i < W * H; ++i) {
uint8_t r = static_cast<uint8_t>(sx * (v[i].x - min_x));
uint8_t g = static_cast<uint8_t>(sy * (v[i].y - min_y));
buffer[i] = (r << 16) | (g << 8) | 0x00;
}
}
void impulse(uint x, uint y, uint fx, uint fy) {
for (uint i = 0; i < 100; ++i)
for (uint j = 0; j < 100; ++j)
v[(min(x + i, W - 1)) * H + min(y + j, H - 1)] +=
Vector2(fx, fy);
}
void setBlocks(uint xs, uint ys, uint xe, uint ye) {
for (uint i = xs; i < xe; ++i)
for (uint j = ys; j < ye; ++j)
v[i*H + j] = Vector2(0,0);
}
void forward(float dt = 1) {
//setBlocks(200, 200, 400, 250);
advect(tmpMem, dt);
memcpy(v, tmpMem, W * H * sizeof(Vector2));
#ifdef DIFFUSE
diffuse(tmpMem, dt);
#endif
updatePressure(reinterpret_cast<float *>(tmpMem));
gradient(tmpMem, p, W, H);
subtract(v, tmpMem, W, H);
}
uint width() { return W; }
uint height() { return H; }
private:
#ifdef __AVX__
void jacobi(float *x, float *b, float *mem, float alpha, float invbeta, uint iters = 50) {
for (uint t = 0; t < iters; ++t) {
for (uint i = 1; i < W - 1; ++i) {
for (uint j = 1; j < H - 1; j+=1) {
/*
__m256 left = _mm256_loadu_ps(&x[i * H + j - 1]);
__m256 right = _mm256_loadu_ps(&x[i * H + j + 1]);
__m256 up = _mm256_loadu_ps(&x[(i - 1) * H + j]);
__m256 down = _mm256_loadu_ps(&x[(i + 1) * H + j]);
__m256 center = _mm256_loadu_ps(&b[i * H + j]);
__m256 sum = _mm256_add_ps(_mm256_add_ps(left, right), _mm256_add_ps(up, down));
__m256 alpha_b = _mm256_mul_ps(_mm256_set1_ps(alpha), center);
__m256 result = _mm256_add_ps(sum, alpha_b);
result = _mm256_mul_ps(result, mbeta);
_mm256_storeu_ps(&mem[i * H + j], result);
*/
mem[i * H + j] = (x[(i - 1) * H + j] + x[(i + 1) * H + j] + x[i * H + j - 1] + x[i * H + j + 1] + alpha * b[i * H + j]);
}
}
for (uint i = 0;i < W*H/8;i++) {
__m256 vals = _mm256_load_ps(mem + i*8);
__m256 mbeta = _mm256_set1_ps(invbeta);
__m256 res = _mm256_mul_ps(vals,mbeta);
_mm256_store_ps(mem + i*8, res);
}
std::swap(x,mem);
}
}
void jacobi(Vector2 *x, Vector2 *b, Vector2 *mem, float alpha, float invbeta, uint iters = 50) {
for (uint t = 0; t < iters; ++t) {
for (uint i = 1; i < W - 1; ++i) {
for (uint j = 1; j < H - 1; ++j) {
mem[i * H + j] = (x[(i - 1) * H + j] + x[(i + 1) * H + j] + x[i * H + j - 1] + x[i * H + j + 1] + alpha * b[i * H + j]);
}
}
float *fmem = reinterpret_cast<float*>(mem);
for (uint i = 0;i < 2*W*H/8;i++) {
__m256 vals = _mm256_load_ps(fmem + i*8);
__m256 mbeta = _mm256_set1_ps(invbeta);
__m256 res = _mm256_mul_ps(vals,mbeta);
_mm256_store_ps(fmem + i*8, res);
}
std::swap(x,mem);
}
}
#else
template <typename T>
void jacobi(T *x, T *b, T *mem, float alpha, float invbeta, uint iters = 50) {
for (uint t = 0; t < iters; ++t) {
for (uint i = 1; i < W - 1; ++i) {
for (uint j = 1; j < H - 1; ++j) {
mem[i * H + j] = (x[(i - 1) * H + j] + x[(i + 1) * H + j] + x[i * H + j - 1] + x[i * H + j + 1] + alpha * b[i * H + j]) * invbeta;
}
}
std::swap(x,mem);
}
}
#endif
Vector2 lerp_index(const Vector2 &index) {
int xl = (int)floor(index.x);
int yl = (int)floor(index.y);
#ifdef LERP
int xh = (int)ceil(index.x);
int yh = (int)ceil(index.y);
// TODO
xl = min(max(xl, 0), (int)W - 1);
xh = min(max(xh, 0), (int)W - 1);
yl = min(max(yl, 0), (int)H - 1);
yh = min(max(yh, 0), (int)H - 1);
//assert(xl >= 0 && xl < W);
//assert(xh >= 0 && xh < W);
//assert(yl >= 0 && yl < H);
//assert(yh >= 0 && yh < H);
float dx = index.x - xl;
float dy = index.y - yl;
Vector2 myl = dx * v[xh * H + yl] + (1 - dx) * v[xl * H + yl];
Vector2 myh = dx * v[xh * H + yh] + (1 - dx) * v[xl * H + yh];
return dy * myh + (1 - dy) * myl;
#else
return v[xl * H + yl];
#endif
}
void advect(Vector2 *result, float dt = 1) {
for (uint x = 3; x < W - 3; ++x)
for (uint y = 3; y < H - 3; ++y) {
Vector2 vel = v[x * H + y];
result[x * H + y] = lerp_index(Vector2(x, y) - vel * dt);
}
}
void divergence(float *result) {
for (uint x = 1; x < W - 1; ++x)
for (uint y = 1; y < H - 1; ++y) {
result[x * H + y] =
(v[(x + 1) * H + y].x - v[(x - 1) * H + y].x +
v[x * H + y + 1].y - v[x * H + y - 1].y) /
2;
}
}
void diffuse(Vector2 *vecfield, float dt = 1) {
float alpha = 1 / (viscosity * dt);
jacobi(v, vecfield, tmpMem2, alpha, 1.0 / (4.0 + alpha), 30);
}
void updatePressure(float *mem) {
divergence(mem);
jacobi(p, mem, reinterpret_cast<float *>(tmpMem2), -1, 0.25, 30);
}
};
struct Params {
float viscosity;
float shc;
};
void handle_click(SDL_MouseButtonEvent &e, PhyBox &pb) {
switch (e.button) {
case 1: // left click
pb.impulse(e.y, e.x, 30, 0);
break;
case 3: // right click
pb.impulse(e.y, e.x, 0, 30);
break;
}
}
int main() {
Params params;
params.viscosity = 0.005;
params.shc = 1;
PhyBox pb(600, 600);
//PhyBox pb(1200, 1200);
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
printf("error initializing SDL: %s\n", SDL_GetError());
SDL_Window *window =
SDL_CreateWindow("FLUID", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, pb.width(), pb.height(), 0);
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError()
<< std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Create a texture to render the buffer to
SDL_Texture *texture =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, pb.width(), pb.height());
if (!texture) {
std::cerr << "SDL_CreateTexture Error: " << SDL_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Create a pixel buffer
uint32_t *buffer = new uint32_t[pb.width() * pb.height()];
memset(buffer, 0, pb.width() * pb.height());
bool running = true;
SDL_Event event;
uint thresh = 100;
std::chrono::steady_clock::time_point tick =
std::chrono::steady_clock::now();
uint ctr = 0;
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN:
handle_click(event.button, pb);
break;
}
}
pb.forward(0.3);
pb.updateBuffer(buffer);
SDL_UpdateTexture(texture, nullptr, buffer,
pb.width() * sizeof(uint32_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
if (++ctr % thresh == 0) {
std::chrono::steady_clock::time_point now =
std::chrono::steady_clock::now();
uint us = std::chrono::duration_cast<std::chrono::microseconds>(
now - tick)
.count();
cout << "fps: " << thresh * 1000000.0 / us << endl;
tick = std::chrono::steady_clock::now();
}
}
// Clean up
delete[] buffer;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}