-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInit.h
More file actions
361 lines (312 loc) · 8.53 KB
/
Copy pathInit.h
File metadata and controls
361 lines (312 loc) · 8.53 KB
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
#pragma once
#include <Kokkos_Random.hpp>
#include <fstream>
#include "BoundaryConditions.h"
#include "SimInfo.h"
namespace fv2d
{
namespace
{
using RandomPool = Kokkos::Random_XorShift64_Pool<>;
/**
* @brief Sod Shock tube aligned along the X axis
*/
KOKKOS_INLINE_FUNCTION
void initSodX(Array Q, int i, int j, const DeviceParams ¶ms)
{
if (getPos(params, i, j)[IX] <= 0.5)
{
Q(j, i, IR) = 1.0;
Q(j, i, IP) = 1.0;
Q(j, i, IU) = 0.0;
}
else
{
Q(j, i, IR) = 0.125;
Q(j, i, IP) = 0.1;
Q(j, i, IU) = 0.0;
}
}
/**
* @brief Gresho-Vortex setup for Low-mach flows
*
* Based on Miczek et al. 2015 "New numerical solver for flows at various Mach numbers"
*/
KOKKOS_INLINE_FUNCTION
void initGreshoVortex(Array Q, int i, int j, const DeviceParams ¶ms)
{
Pos pos = getPos(params, i, j);
const real_t xmid = 0.5 * (params.xmin + params.xmax);
const real_t ymid = 0.5 * (params.ymin + params.ymax);
const real_t xr = pos[IX] - xmid;
const real_t yr = pos[IY] - ymid;
const real_t r = sqrt(xr * xr + yr * yr);
// Pressure is given from density and Mach
const real_t p0 = params.gresho_density / (params.gamma0 * params.gresho_Mach * params.gresho_Mach);
Q(j, i, IR) = params.gresho_density;
real_t u_phi;
if (r < 0.2)
{
u_phi = 5.0 * r;
Q(j, i, IP) = p0 + 12.5 * r * r;
}
else if (r < 0.4)
{
u_phi = 2.0 - 5.0 * r;
Q(j, i, IP) = p0 + 12.5 * r * r + 4.0 * (1.0 - 5.0 * r + log(5.0 * r));
}
else
{
u_phi = 0.0;
Q(j, i, IP) = p0 - 2.0 + 4.0 * log(2.0);
}
const real_t xnr = xr / r;
const real_t ynr = yr / r;
Q(j, i, IU) = -ynr * u_phi;
Q(j, i, IV) = xnr * u_phi;
}
/**
* @brief Sod Shock tube aligned along the Y axis
*/
KOKKOS_INLINE_FUNCTION
void initSodY(Array Q, int i, int j, const DeviceParams ¶ms)
{
if (getPos(params, i, j)[IY] <= 0.5)
{
Q(j, i, IR) = 1.0;
Q(j, i, IP) = 1.0;
Q(j, i, IU) = 0.0;
}
else
{
Q(j, i, IR) = 0.125;
Q(j, i, IP) = 0.1;
Q(j, i, IU) = 0.0;
}
}
/**
* @brief Sedov blast initial conditions
*/
KOKKOS_INLINE_FUNCTION
void initBlast(Array Q, int i, int j, const DeviceParams ¶ms)
{
real_t xmid = 0.5 * (params.xmin + params.xmax);
real_t ymid = 0.5 * (params.ymin + params.ymax);
Pos pos = getPos(params, i, j);
real_t x = pos[IX];
real_t y = pos[IY];
real_t xr = xmid - x;
real_t yr = ymid - y;
real_t r = sqrt(xr * xr + yr * yr);
if (r < 0.2)
{
Q(j, i, IR) = 1.0;
Q(j, i, IU) = 0.0;
Q(j, i, IV) = 0.0;
Q(j, i, IP) = 10.0;
}
else
{
Q(j, i, IR) = 1.2;
Q(j, i, IU) = 0.0;
Q(j, i, IV) = 0.0;
Q(j, i, IP) = 0.1;
}
}
/**
* @brief Stratified convection based on Hurlburt et al 1984
*/
KOKKOS_INLINE_FUNCTION
void initH84(Array Q, int i, int j, const DeviceParams ¶ms, const RandomPool &random_pool)
{
Pos pos = getPos(params, i, j);
real_t x = pos[IX];
real_t y = pos[IY];
real_t rho = pow(y, params.m1);
real_t prs = pow(y, params.m1 + 1.0);
auto generator = random_pool.get_state();
real_t pert = params.h84_pert * (generator.drand(-0.5, 0.5));
random_pool.free_state(generator);
Q(j, i, IR) = rho;
Q(j, i, IU) = 0.0;
Q(j, i, IV) = pert;
Q(j, i, IP) = prs;
}
/**
* @brief Stratified convection based on Cattaneo et al. 1991
*/
KOKKOS_INLINE_FUNCTION
void initC91(Array Q, int i, int j, const DeviceParams ¶ms, const RandomPool &random_pool)
{
Pos pos = getPos(params, i, j);
real_t x = pos[IX];
real_t y = pos[IY];
real_t T = (1.0 + params.theta1 * y);
real_t rho = pow(T, params.m1);
real_t prs = pow(T, params.m1 + 1.0);
auto generator = random_pool.get_state();
real_t pert = params.c91_pert * (generator.drand(-0.5, 0.5));
random_pool.free_state(generator);
prs = prs * (1.0 + pert);
Q(j, i, IR) = rho;
Q(j, i, IU) = 0.0;
Q(j, i, IV) = 0.0;
Q(j, i, IP) = prs;
}
/**
* @brief Simple diffusion test with a structure being advected on the grid
*/
KOKKOS_INLINE_FUNCTION
void initDiffusion(Array Q, int i, int j, const DeviceParams ¶ms)
{
real_t xmid = 0.5 * (params.xmin + params.xmax);
real_t ymid = 0.5 * (params.ymin + params.ymax);
Pos pos = getPos(params, i, j);
real_t x0 = (pos[IX] - xmid);
real_t y0 = (pos[IY] - ymid);
real_t r = sqrt(x0 * x0 + y0 * y0);
if (r < 0.2)
Q(j, i, IR) = 1.0;
else
Q(j, i, IR) = 0.1;
Q(j, i, IP) = 1.0;
Q(j, i, IU) = 1.0;
Q(j, i, IV) = 1.0;
}
/**
* @brief Rayleigh-Taylor instability setup
*/
KOKKOS_INLINE_FUNCTION
void initRayleighTaylor(Array Q, int i, int j, const DeviceParams ¶ms)
{
real_t ymid = 0.5 * (params.ymin + params.ymax);
Pos pos = getPos(params, i, j);
real_t x = pos[IX];
real_t y = pos[IY];
const real_t P0 = 2.5;
if (y < ymid)
{
Q(j, i, IR) = 1.0;
Q(j, i, IU) = 0.0;
Q(j, i, IP) = P0 + 0.1 * params.gy * y;
}
else
{
Q(j, i, IR) = 2.0;
Q(j, i, IU) = 0.0;
Q(j, i, IP) = P0 + 0.1 * params.gy * y;
}
if (y > -1.0 / 3.0 && y < 1.0 / 3.0)
Q(j, i, IV) = 0.01 * (1.0 + cos(4 * M_PI * x)) * (1 + cos(3.0 * M_PI * y)) / 4.0;
}
/**
* @brief Kelvin-Helmholtz instability setup
*
* Taken from Lecoanet et al, "A validated non-linear Kelvin–Helmholtz benchmark for numerical hydrodynamics"
* 2016, MNRAS
*/
KOKKOS_INLINE_FUNCTION
void initKelvinHelmholtz(Array Q, int i, int j, const DeviceParams ¶ms)
{
Pos pos = getPos(params, i, j);
real_t x = pos[IX];
real_t y = pos[IY];
const real_t q1 = Kokkos::tanh((y - params.kh_y1) / params.kh_a);
const real_t q2 = Kokkos::tanh((y - params.kh_y2) / params.kh_a);
const real_t s2 = params.kh_sigma * params.kh_sigma;
const real_t dy1 = (y - params.kh_y1) * (y - params.kh_y1);
const real_t dy2 = (y - params.kh_y2) * (y - params.kh_y2);
const real_t rho = 1.0 + params.kh_rho_fac * 0.5 * (q1 - q2);
const real_t u = params.kh_uflow * (q1 - q2 - 1.0);
const real_t v = params.kh_amp * Kokkos::sin(2.0 * M_PI * x) * (Kokkos::exp(-dy1 / s2) + Kokkos::exp(-dy2 / s2));
Q(j, i, IR) = rho;
Q(j, i, IU) = u;
Q(j, i, IV) = v;
Q(j, i, IP) = params.kh_P0;
}
} // namespace
/**
* @brief Enum describing the type of initialization possible
*/
enum InitType
{
SOD_X,
SOD_Y,
BLAST,
RAYLEIGH_TAYLOR,
DIFFUSION,
H84,
C91,
KELVIN_HELMHOLTZ,
GRESHO_VORTEX
};
struct InitFunctor
{
private:
Params full_params;
InitType init_type;
public:
InitFunctor(Params &full_params) : full_params(full_params)
{
std::map<std::string, InitType> init_map{{"sod_x", SOD_X},
{"sod_y", SOD_Y},
{"blast", BLAST},
{"rayleigh-taylor", RAYLEIGH_TAYLOR},
{"diffusion", DIFFUSION},
{"H84", H84},
{"C91", C91},
{"kelvin_helmholtz", KELVIN_HELMHOLTZ},
{"gresho_vortex", GRESHO_VORTEX}};
if (init_map.count(full_params.problem) == 0)
throw std::runtime_error("Error unknown problem " + full_params.problem);
init_type = init_map[full_params.problem];
};
~InitFunctor() = default;
void init(Array Q)
{
// cppcheck-suppress shadowVariable
auto init_type = this->init_type;
auto params = full_params.device_params;
RandomPool random_pool(full_params.seed);
// Filling active domain ...
Kokkos::parallel_for(
"Initialization",
full_params.range_dom,
KOKKOS_LAMBDA(const int i, const int j) {
switch (init_type)
{
case SOD_X:
initSodX(Q, i, j, params);
break;
case SOD_Y:
initSodY(Q, i, j, params);
break;
case BLAST:
initBlast(Q, i, j, params);
break;
case DIFFUSION:
initDiffusion(Q, i, j, params);
break;
case RAYLEIGH_TAYLOR:
initRayleighTaylor(Q, i, j, params);
break;
case H84:
initH84(Q, i, j, params, random_pool);
break;
case C91:
initC91(Q, i, j, params, random_pool);
break;
case KELVIN_HELMHOLTZ:
initKelvinHelmholtz(Q, i, j, params);
break;
case GRESHO_VORTEX:
initGreshoVortex(Q, i, j, params);
break;
}
});
// ... and boundaries
BoundaryManager bc(full_params);
bc.fillBoundaries(Q);
}
};
} // namespace fv2d