-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcylinder.cpp
329 lines (290 loc) · 7.27 KB
/
cylinder.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
#include <Eigen/Dense>
#include <fstream>
#include <iostream>
#include <vector>
// ========================= Global Settings =========================
using scalar_type = double;
template <int N>
using vecN = Eigen::Vector<scalar_type, N>;
using ivec = Eigen::Vector<int, 2>;
using vec = Eigen::Vector<scalar_type, 2>;
constexpr unsigned int FLAG_SOLID = 1u;
ivec idx2dir(int idx)
{
assert(idx < 9);
int x = idx % 3;
int y = idx / 3;
return { x - 1, y - 1 };
}
int dir2idx(ivec dir)
{
return dir.x() + 1 + (dir.y() + 1) * 3;
}
scalar_type weight(ivec dir)
{
int manhat = std::abs(dir.x()) + std::abs(dir.y());
assert(manhat < 3);
if (manhat == 0)
{
return 4.0 / 9.0;
}
if (manhat == 1)
{
return 1.0 / 9.0;
}
if (manhat == 2)
{
return 1.0 / 36.0;
}
return 0;
}
scalar_type weight(int idx)
{
return weight(idx2dir(idx));
}
struct CylinderWakeLBM
{
int width, height;
scalar_type dx, dt;
scalar_type unit_vel;
// nondimensionalized
scalar_type tau;
// nondimensionalized
scalar_type u0;
std::vector<vecN<9>> f, ftemp;
std::vector<vec> velocity;
std::vector<Eigen::Vector<float, 2>> vel_dim;
std::vector<scalar_type> density;
std::vector<float> fdensity;
std::vector<unsigned int> flag;
template <typename T>
T& at(std::vector<T>& v, int x, int y)
{
return v[x + y * width];
}
template <typename T>
T& at(std::vector<T>& v, ivec p)
{
return at(v, p.x(), p.y());
}
bool valid(int x, int y) const
{
return x >= 0 && x < width && y >= 0 && y < width;
}
bool valid(ivec v) const
{
return valid(v.x(), v.y());
}
void init(int w, int h, scalar_type nu, scalar_type inlet_vel, scalar_type dt)
{
width = w;
height = h;
dx = 10.0 / (width - 1);
this->dt = dt;
const int wh = width * height;
f.resize(wh);
ftemp.resize(wh);
velocity.resize(wh);
density.resize(wh);
vel_dim.resize(wh);
fdensity.resize(wh);
flag.resize(wh, 0);
// nondim
nu /= (dx * dx / dt);
unit_vel = dx / dt;
u0 = inlet_vel / unit_vel;
tau = (6 * nu + 1) / 2.0;
std::cout << "W: " << width << "\n";
std::cout << "H: " << height << "\n";
std::cout << "dx: " << dx << "\n";
std::cout << "dt: " << dt << "\n";
std::cout << "Inlet: " << inlet_vel << "\n";
std::cout << "Inlet(nondim): " << u0 << "\n";
std::cout << "tau: " << tau << "\n";
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
at(velocity, x, y) = vec(u0, 0);
at(density, x, y) = 1;
scalar_type fx = x * dx;
scalar_type fy = y * dx;
fx -= 2.5;
fy -= 2.5;
if (fx * fx + fy * fy < 0.5 * 0.5)
{
at(flag, x, y) |= FLAG_SOLID;
at(velocity, x, y) = vec(0, 0);
}
at(f, x, y) = equilibrium(at(velocity, x, y), at(density, x, y));
}
}
}
void prepare_print()
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
at(vel_dim, x, y) = (at(velocity, x, y) * unit_vel).cast<float>();
at(fdensity, x, y) = (float)at(density, x, y);
}
}
}
void step()
{
#define INVOKE(name) \
for (int y = 0; y < height; ++y) \
{ \
for (int x = 0; x < width; ++x) \
{ \
if (at(flag, x, y) & FLAG_SOLID) \
continue; \
name(x, y); \
} \
}
INVOKE(streaming);
boundary_condition();
INVOKE(macroscopic);
INVOKE(equilibrium);
INVOKE(collision);
}
vecN<9> equilibrium(vec vel, scalar_type dens)
{
vecN<9> ret;
for (int i = 0; i < 9; ++i)
{
vec dir = idx2dir(i).cast<scalar_type>();
scalar_type edotu = dir.dot(vel);
scalar_type feq = weight(i) * dens
* (1.0 + 3.0 * edotu + 4.5 * edotu * edotu
- 1.5 * vel.squaredNorm());
ret[i] = feq;
}
return ret;
}
void streaming(int x, int y)
{
// set (x,y)'s dir direction
for (int dir = 0; dir < 9; ++dir)
{
ivec fromdir = -idx2dir(dir);
ivec adjpos = { x, y };
adjpos -= idx2dir(dir);
// periodic boundary condition for upper and lower
if (adjpos.y() == -1)
{
adjpos.y() = height - 1;
}
if (adjpos.y() == height)
{
adjpos.y() = 0;
}
if ((valid(adjpos) == false) || (at(flag, adjpos) & FLAG_SOLID))
{
continue;
}
at(ftemp, x, y)[dir] = at(f, adjpos)[dir];
}
}
void boundary_condition()
{
// left inlet ( vel - dirichlet, dens - neumann )
for (int y = 0; y < height; ++y)
{
scalar_type dens = at(density, 1, y);
vec vel = { u0, 0 };
at(velocity, 0, y) = vel;
at(density, 0, y) = dens;
at(ftemp, 0, y) = equilibrium(vel, dens);
}
// right outlet ( vel - neumann, dens - dirichlet )
for (int y = 0; y < height; ++y)
{
vec vel = at(velocity, width - 2, y);
scalar_type dens = 1.0;
at(velocity, width - 1, y) = vel;
at(density, width - 1, y) = dens;
at(ftemp, width - 1, y) = equilibrium(vel, dens);
}
// HBB
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
for (int dir = 0; dir < 9; ++dir)
{
ivec adjpos = { x, y };
adjpos += idx2dir(dir);
if (valid(adjpos) == false)
{
continue;
}
if (at(flag, adjpos) & FLAG_SOLID)
{
at(ftemp, x, y)[dir2idx(-idx2dir(dir))] = at(ftemp, x, y)[dir];
}
}
}
}
}
void macroscopic(int x, int y)
{
scalar_type dens = 0;
vec vel = { 0, 0 };
for (int i = 0; i < 9; ++i)
{
vec dir = idx2dir(i).cast<scalar_type>();
dens += at(ftemp, x, y)[i];
vel += dir * at(ftemp, x, y)[i];
}
vel /= dens;
at(density, x, y) = dens;
at(velocity, x, y) = vel;
}
void equilibrium(int x, int y)
{
const vec vel = at(velocity, x, y);
const scalar_type dens = at(density, x, y);
at(f, x, y) = equilibrium(vel, dens);
}
void collision(int x, int y)
{
// collision f(feq), ftemp(fstar) -> f
auto fstar = at(ftemp, x, y);
auto feq = at(f, x, y);
fstar = fstar - (fstar - feq) / tau;
at(f, x, y) = fstar;
}
};
int main(int argc, char** argv)
{
std::vector<int> Res = { 5, 20, 40, 60, 100, 150, 200 };
for (int re : Res)
{
std::cout << "Calculating Reynolds: " << re << "\n";
std::ofstream stream(std::string("re") + std::to_string(re) + ".dat",
std::ios::binary);
CylinderWakeLBM cylinder;
scalar_type nu = 1.0 / (double)re;
cylinder.init(512, 256, nu, 1.0, 0.002);
// dt = 0.002
// dx = 10 / 511
// simulation end: 30
// simulation iteration: 20 / 0.002 = 15000
// plot_interval: 0.2
// plot iter: 0.2 / 0.002 = 100
for (int i = 0; i < 15000; ++i)
{
if (i % 100 == 0)
{
std::cout << i << "\n";
cylinder.prepare_print();
stream.write((char*)cylinder.vel_dim.data(),
sizeof(float) * cylinder.width * cylinder.height * 2);
}
cylinder.step();
}
}
return 0;
}