This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlux.cpp
551 lines (457 loc) · 24.3 KB
/
Flux.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <Eigen/Dense>
#include <cmath>
#include <fstream>
#include <cnpy.h>
#include <iostream>
#include <ostream>
#include "Mesh.h"
constexpr double GAMMA = 1.4;
inline double minmod(const double &a, const double &b)
{
bool a_b_same_sign = a * b > 0;
if (a_b_same_sign)
{
return std::abs(a) < std::abs(b) ? a : b;
}
else
{
return 0.0;
}
}
inline double minmod(const double &a, const double &b, const double &c)
{
return minmod(a, minmod(b, c));
}
inline double Driver::entropy_abs(const double &a, const double &dx, const double &dt)
{
double new_a = a * dt / dx;
if (new_a > epsilon)
{
return new_a * dx / dt;
}
else if (new_a < -epsilon)
{
return -new_a * dx / dt;
}
else
{
// return (new_a * new_a + epsilon * epsilon) / 2 / epsilon * dx / dt;
return epsilon * dx / dt;
}
}
Flux Driver::reconstruct_v_l(const State &mm, const State &m, const State &p, const State &pp, Vec2 normal)
{
// m: minus, p: plus
// Here shows how the units are arranged
// mm | m | p | pp
// normal is ->
double u_mm = mm[1], u_m = m[1], u_p = p[1], u_pp = pp[1];
double v_mm = mm[2], v_m = m[2], v_p = p[2], v_pp = pp[2];
double a_mm = std::sqrt(GAMMA * mm[3] / mm[0]),
a_m = std::sqrt(GAMMA * m[3] / m[0]),
a_p = std::sqrt(GAMMA * p[3] / p[0]),
a_pp = std::sqrt(GAMMA * pp[3] / pp[0]);
// convert to normal coordinate
double u_mm_n = u_mm * normal.x + v_mm * normal.y, u_mm_t = u_mm * normal.y - v_mm * normal.x;
double u_m_n = u_m * normal.x + v_m * normal.y, u_m_t = u_m * normal.y - v_m * normal.x;
double u_p_n = u_p * normal.x + v_p * normal.y, u_p_t = u_p * normal.y - v_p * normal.x;
double u_pp_n = u_pp * normal.x + v_pp * normal.y, u_pp_t = u_pp * normal.y - v_pp * normal.x;
// apply original variable reconstruction
double rho_m = m[0] + 0.5 * minmod(m[0] - mm[0], p[0] - m[0]);
double rho_p = p[0] - 0.5 * minmod( p[0] - m[0], pp[0] - p[0]);
// u_m_n = u_m_n;// + 0.5 * minmod(u_m_n - u_mm_n, u_p_n - u_m_n);
// u_p_n = u_p_n;// - 0.5 * minmod(u_p_n - u_m_n, u_pp_n - u_p_n);
// u_m_t = u_m_t;// + 0.5 * minmod(u_m_t - u_mm_t, u_p_t - u_m_t);
// u_p_t = u_p_t;// - 0.5 * minmod(u_p_t - u_m_t, u_pp_t - u_p_t);
double pressure_m = m[3] + 0.5 * minmod(m[3] - mm[3], p[3] - m[3]);
double pressure_p = p[3] - 0.5 * minmod(p[3] - m[3], pp[3] - p[3]);
a_m = std::sqrt(GAMMA * pressure_m / rho_m);
a_p = std::sqrt(GAMMA * pressure_p / rho_p);
// experimental
double a_mid = (rho_m * a_m + rho_p * a_p) / (rho_m + rho_p);
a_m = a_mid; a_p = a_mid;
double mach_normal_m = u_m_n / a_m, mach_normal_p = u_p_n / a_p;
auto soft_pos = [&](const double &a) {
if (a > 1) {
return a;
} else if (a < -1) {
return 0.0;
} else {
return 0.25 * (a + 1) * (a + 1);
}
};
auto soft_neg = [&](const double &a) {
if (a < -1) {
return a;
} else if (a > 1) {
return 0.0;
} else {
return -0.25 * (a - 1) * (a - 1);
}
};
mach_normal_m = soft_pos(mach_normal_m);
mach_normal_p = soft_neg(mach_normal_p);
double mach_normal = mach_normal_m + mach_normal_p;
if (mach_normal < -1)
{
return {rho_p * u_p_n, rho_p * u_p_n * u_p_t, rho_p * u_p_n * u_p_n + pressure_p, u_p_n * (pressure_p + 1/(GAMMA - 1) * pressure_p + 0.5 * rho_p * (u_p_n * u_p_n + u_p_t * u_p_t))};
}
else if (mach_normal > 1)
{
return {rho_m * u_m_n, rho_m * u_m_n * u_m_t, rho_m * u_m_n * u_m_n + pressure_m, u_m_n * (pressure_m + 1/(GAMMA - 1) * pressure_m + 0.5 * rho_m * (u_m_n * u_m_n + u_m_t * u_m_t))};
}
auto square = [](const double &a) { return a * a; };
double f_m = rho_m * a_m * mach_normal_m;
double f_p = rho_p * a_p * mach_normal_p;
// u_m_n = u_n, u_p_n = u_n;
// double u_n = mach_normal * a_mid;
Flux flux_m = {
f_m,
f_m * u_m_t,
f_m * (u_m_n + (-u_m_n + 2 * a_m) / GAMMA),
f_m * (square((GAMMA-1)*u_m_n + 2 * a_m) / 2 / (GAMMA * GAMMA - 1) + (u_m_n * u_m_n + u_m_t * u_m_t - u_m_n * u_m_n) / 2)
};
Flux flux_p = {
f_p,
f_p * u_p_t,
f_p * (u_m_n + (-u_m_n - 2 * a_p) / GAMMA),
f_p * (square((GAMMA-1)*u_p_n - 2 * a_p) / 2 / (GAMMA * GAMMA - 1) + (u_p_n * u_p_n + u_p_t * u_p_t - u_p_n * u_p_n) / 2)
};
Flux flux = {
flux_m[0] + flux_p[0],
flux_m[1] + flux_p[1],
flux_m[2] + flux_p[2],
flux_m[3] + flux_p[3]
};
return flux;
}
Flux Driver::reconstruct_kfvs(const State &mm, const State &m, const State &p, const State &pp, Vec2 normal)
{
// m: minus, p: plus
// Here shows how the units are arranged
// mm | m | p | pp
// normal is ->
double u_mm = mm[1], u_m = m[1], u_p = p[1], u_pp = pp[1];
double v_mm = mm[2], v_m = m[2], v_p = p[2], v_pp = pp[2];
double a_mm = std::sqrt(GAMMA * mm[3] / mm[0]),
a_m = std::sqrt(GAMMA * m[3] / m[0]),
a_p = std::sqrt(GAMMA * p[3] / p[0]),
a_pp = std::sqrt(GAMMA * pp[3] / pp[0]);
// convert to normal coordinate
double u_mm_n = u_mm * normal.x + v_mm * normal.y, u_mm_t = u_mm * normal.y - v_mm * normal.x;
double u_m_n = u_m * normal.x + v_m * normal.y, u_m_t = u_m * normal.y - v_m * normal.x;
double u_p_n = u_p * normal.x + v_p * normal.y, u_p_t = u_p * normal.y - v_p * normal.x;
double u_pp_n = u_pp * normal.x + v_pp * normal.y, u_pp_t = u_pp * normal.y - v_pp * normal.x;
// apply original variable reconstruction
double rho_m = m[0] + 0.5 * minmod(m[0] - mm[0], p[0] - m[0]);
double rho_p = p[0] - 0.5 * minmod( p[0] - m[0], pp[0] - p[0]);
// u_m_n = u_m_n + 0.5 * minmod(u_m_n - u_mm_n, u_p_n - u_m_n);
// u_p_n = u_p_n - 0.5 * minmod(u_p_n - u_m_n, u_pp_n - u_p_n);
// u_m_t = u_m_t + 0.5 * minmod(u_m_t - u_mm_t, u_p_t - u_m_t);
// u_p_t = u_p_t - 0.5 * minmod(u_p_t - u_m_t, u_pp_t - u_p_t);
double pressure_m = m[3] + 0.5 * minmod(m[3] - mm[3], p[3] - m[3]);
double pressure_p = p[3] - 0.5 * minmod(p[3] - m[3], pp[3] - p[3]);
double beta_m = rho_m / 2 / pressure_m;
double beta_p = rho_p / 2 / pressure_p;
double S2_m = u_m_n * std::sqrt(beta_m);
double S2_p = u_p_n * std::sqrt(beta_p);
double X2_m = 0.5 * (1 + std::erf(S2_m));
double X2_p = 0.5 * (1 - std::erf(S2_p));
constexpr double m_pi = 3.14159265358979323846;
double Y2_m = std::exp(-S2_m * S2_m) / std::sqrt(m_pi) / 2 / std::sqrt(beta_m);
double Y2_p = std::exp(-S2_p * S2_p) / std::sqrt(m_pi) / 2 / std::sqrt(beta_p);
Flux flux_m = {
rho_m * u_m_n * X2_m + rho_m * Y2_m,
rho_m * u_m_n * u_m_t * X2_m + rho_m * u_m_t * Y2_m,
(pressure_m + rho_m * u_m_n * u_m_n) * X2_m + rho_m * u_m_n * Y2_m,
(GAMMA / (GAMMA - 1) * pressure_m + 0.5 * rho_m * (u_m_n * u_m_n + u_m_t * u_m_t)) * u_m_n * X2_m
+ ((GAMMA + 1) / (2 * (GAMMA - 1)) * pressure_m + 0.5 * rho_m * (u_m_n * u_m_n + u_m_t * u_m_t)) * Y2_m
};
Flux flux_p = {
rho_p * u_p_n * X2_p - rho_p * Y2_p,
rho_p * u_p_n * u_p_t * X2_p - rho_p * u_p_t * Y2_p,
(pressure_p + rho_p * u_p_n * u_p_n) * X2_p - rho_p * u_p_n * Y2_p,
(GAMMA / (GAMMA - 1) * pressure_p + 0.5 * rho_p * (u_p_n * u_p_n + u_p_t * u_p_t)) * u_p_n * X2_p
- ((GAMMA + 1) / (2 * (GAMMA - 1)) * pressure_p + 0.5 * rho_p * (u_p_n * u_p_n + u_p_t * u_p_t)) * Y2_p
};
Flux flux = {
flux_m[0] + flux_p[0],
flux_m[1] + flux_p[1],
flux_m[2] + flux_p[2],
flux_m[3] + flux_p[3]
};
return flux;
}
Flux Driver::reconstruct_roe(const State &mm, const State &m, const State &p, const State &pp, Vec2 normal)
{
// m: minus, p: plus
// Here shows how the units are arranged
// mm | m | p | pp
// normal is ->
double u_mm = mm[1], u_m = m[1], u_p = p[1], u_pp = pp[1];
double v_mm = mm[2], v_m = m[2], v_p = p[2], v_pp = pp[2];
double a_mm = std::sqrt(GAMMA * mm[3] / mm[0]),
a_m = std::sqrt(GAMMA * m[3] / m[0]),
a_p = std::sqrt(GAMMA * p[3] / p[0]),
a_pp = std::sqrt(GAMMA * pp[3] / pp[0]);
// convert to normal coordinate
double u_mm_n = u_mm * normal.x + v_mm * normal.y, u_mm_t = u_mm * normal.y - v_mm * normal.x;
double u_m_n = u_m * normal.x + v_m * normal.y, u_m_t = u_m * normal.y - v_m * normal.x;
double u_p_n = u_p * normal.x + v_p * normal.y, u_p_t = u_p * normal.y - v_p * normal.x;
double u_pp_n = u_pp * normal.x + v_pp * normal.y, u_pp_t = u_pp * normal.y - v_pp * normal.x;
double rho_m = m[0];
double rho_p = p[0];
double pressure_m = m[3];
double pressure_p = p[3];
// these are reconstructed velocities for eigenvalues
double u_m_n_reconstruct = u_m_n + 0.5 * minmod(u_m_n - u_mm_n, u_p_n - u_m_n);
double u_p_n_reconstruct = u_p_n - 0.5 * minmod(u_p_n - u_m_n, u_pp_n - u_p_n);
double u_m_t_reconstruct = u_m_t + 0.5 * minmod(u_m_t - u_mm_t, u_p_t - u_m_t);
double u_p_t_reconstruct = u_p_t - 0.5 * minmod(u_p_t - u_m_t, u_pp_t - u_p_t);
double a_m_reconstruct = a_m + 0.5 * minmod(a_m - a_mm, a_p - a_m);
double a_p_reconstruct = a_p - 0.5 * minmod(a_p - a_m, a_pp - a_p);
double u_n_reconstruct = (std::sqrt(rho_m) * u_m_n_reconstruct + std::sqrt(rho_p) * u_p_n_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double u_t_reconstruct = (std::sqrt(rho_m) * u_m_t_reconstruct + std::sqrt(rho_p) * u_p_t_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double a_reconstruct = (std::sqrt(rho_m) * a_m_reconstruct + std::sqrt(rho_p) * a_p_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
u_m_n = u_m_n_reconstruct;
u_p_n = u_p_n_reconstruct;
u_m_t = u_m_t_reconstruct;
u_p_t = u_p_t_reconstruct;
a_m = a_m_reconstruct;
a_p = a_p_reconstruct;
// Riemann solver Roe
double /*rho_m = m[0], rho_p = p[0], */ rho_mid = std::sqrt(rho_m * rho_p);
double u_n_mid = (std::sqrt(rho_m) * u_m_n + std::sqrt(rho_p) * u_p_n) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double u_t_mid = (std::sqrt(rho_m) * u_m_t + std::sqrt(rho_p) * u_p_t) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double h_m = GAMMA / (GAMMA - 1) * m[3] / rho_m + 0.5 * (u_m * u_m + v_m * v_m);
double h_p = GAMMA / (GAMMA - 1) * p[3] / rho_m + 0.5 * (u_p * u_p + v_p * v_p);
double h_mid = (std::sqrt(rho_m) * h_m + std::sqrt(rho_p) * h_p) / (std::sqrt(rho_m) + std::sqrt(rho_p));
// double pressure_m = m[3];
double e_m = 1/(GAMMA - 1) * pressure_m + 0.5 * rho_m * (u_m_n * u_m_n + u_m_t * u_m_t);
// Flux flux = {rho * u_n, rho * u_n * u_t, rho * u_n * u_n + pressure, u_n * (pressure + e)};
Flux flux_m = {rho_m * u_m_n, rho_m * u_m_n * u_m_t, rho_m * u_m_n * u_m_n + pressure_m, u_m_n * (pressure_m + e_m)};
// double pressure_p = p[3];
double e_p = 1/(GAMMA - 1) * pressure_p + 0.5 * rho_p * (u_p_n * u_p_n + u_p_t * u_p_t);
// Flux flux = {rho * u_n, rho * u_n * u_t, rho * u_n * u_n + pressure, u_n * (pressure + e)};
Flux flux_p = {rho_p * u_p_n, rho_p * u_p_n * u_p_t, rho_p * u_p_n * u_p_n + pressure_p, u_p_n * (pressure_p + e_p)};
// ===========================================================
// A naive implementation: average the fluxes on the both side
// ===========================================================
Flux flux = {0.5*(flux_m[0] + flux_p[0]), 0.5*(flux_m[1] + flux_p[1]), 0.5*(flux_m[2] + flux_p[2]), 0.5*(flux_m[3] + flux_p[3])};
// ===========================================================
// Roe's average: need another term in the flux
// ===========================================================
typedef Eigen::Matrix<double, 4, 4> Matrix4d;
typedef Eigen::Matrix<double, 4, 1> Vector4d;
Matrix4d A_abs = Matrix4d::Zero(), Lambda_abs = Matrix4d::Zero();
auto e_abs = [&](const double &a) { return entropy_abs(a, std::abs(x(1, 0) - x(0, 0)), dt); };
Matrix4d L = Matrix4d::Zero(), R = Matrix4d::Zero();
// Private vars here
{
double u = u_t_mid, v = u_n_mid;
double h = h_mid;
double a = std::sqrt((GAMMA - 1) * (h - 0.5 * (u * u + v * v)));
Lambda_abs(0, 0) = e_abs(u_n_reconstruct), Lambda_abs(1, 1) = e_abs(u_n_reconstruct), Lambda_abs(2, 2) = e_abs(u_n_reconstruct - a_reconstruct), Lambda_abs(3, 3) = e_abs(u_n_reconstruct + a_reconstruct);
R << 0, 1, 1, 1,
1, 0, u, u,
0, v, v-a, v+a,
u, 0.5*(v*v-u*u), h-v*a, h+v*a;
double b2 = (GAMMA - 1) / a / a, b1 = 0.5 * b2 * (u * u + v * v);
L << -b1*u, 1+b2*u*u, b2*u*v, -b2*u,
1-b1, b2*u, b2*v, -b2,
0.5*(b1+v/a), -0.5*b2*u, -0.5*(b2*v+1/a), 0.5*b2,
0.5*(b1-v/a), -0.5*b2*u, -0.5*(b2*v-1/a), 0.5*b2;
}
A_abs = R * Lambda_abs * L;
// Compute the flux
Vector4d U_m;
U_m << m[0], m[0] * u_m_t, m[0] * u_m_n, e_m;
Vector4d U_p;
U_p << p[0], p[0] * u_p_t, p[0] * u_p_n, e_p;
Vector4d Flux_Roe_add = -0.5 * (A_abs * (U_p - U_m));
flux = {flux[0] + Flux_Roe_add[0], flux[1] + Flux_Roe_add[1], flux[2] + Flux_Roe_add[2], flux[3] + Flux_Roe_add[3]};
return flux;
}
Flux Driver::reconstruct_roe_boundary(const State &mm, const State &m, const State &p, const State &pp, Vec2 normal)
{
// m: minus, p: plus
// Here shows how the units are arranged
// mm | m | p | pp
// normal is ->
double u_mm = mm[1], u_m = m[1], u_p = p[1], u_pp = pp[1];
double v_mm = mm[2], v_m = m[2], v_p = p[2], v_pp = pp[2];
double a_mm = std::sqrt(GAMMA * mm[3] / mm[0]),
a_m = std::sqrt(GAMMA * m[3] / m[0]),
a_p = std::sqrt(GAMMA * p[3] / p[0]),
a_pp = std::sqrt(GAMMA * pp[3] / pp[0]);
// convert to normal coordinate
double u_mm_n = u_mm * normal.x + v_mm * normal.y, u_mm_t = u_mm * normal.y - v_mm * normal.x;
double u_m_n = u_m * normal.x + v_m * normal.y, u_m_t = u_m * normal.y - v_m * normal.x;
double u_p_n = u_p * normal.x + v_p * normal.y, u_p_t = u_p * normal.y - v_p * normal.x;
double u_pp_n = u_pp * normal.x + v_pp * normal.y, u_pp_t = u_pp * normal.y - v_pp * normal.x;
double rho_m = m[0];
double rho_p = p[0];
double pressure_m = m[3];
double pressure_p = p[3];
// these are reconstructed velocities for eigenvalues
double u_m_n_reconstruct = u_m_n + 0.5 * minmod(u_m_n - u_mm_n, u_p_n - u_m_n);
double u_p_n_reconstruct = u_p_n - 0.5 * minmod(u_p_n - u_m_n, u_pp_n - u_p_n);
double u_m_t_reconstruct = u_m_t + 0.5 * minmod(u_m_t - u_mm_t, u_p_t - u_m_t);
double u_p_t_reconstruct = u_p_t - 0.5 * minmod(u_p_t - u_m_t, u_pp_t - u_p_t);
double a_m_reconstruct = a_m + 0.5 * minmod(a_m - a_mm, a_p - a_m);
double a_p_reconstruct = a_p - 0.5 * minmod(a_p - a_m, a_pp - a_p);
double u_n_reconstruct = (std::sqrt(rho_m) * u_m_n_reconstruct + std::sqrt(rho_p) * u_p_n_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double u_t_reconstruct = (std::sqrt(rho_m) * u_m_t_reconstruct + std::sqrt(rho_p) * u_p_t_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double a_reconstruct = (std::sqrt(rho_m) * a_m_reconstruct + std::sqrt(rho_p) * a_p_reconstruct) / (std::sqrt(rho_m) + std::sqrt(rho_p));
// Riemann solver Roe
double /*rho_m = m[0], rho_p = p[0], */ rho_mid = std::sqrt(rho_m * rho_p);
double u_n_mid = (std::sqrt(rho_m) * u_m_n + std::sqrt(rho_p) * u_p_n) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double u_t_mid = (std::sqrt(rho_m) * u_m_t + std::sqrt(rho_p) * u_p_t) / (std::sqrt(rho_m) + std::sqrt(rho_p));
double h_m = GAMMA / (GAMMA - 1) * m[3] / rho_m + 0.5 * (u_m * u_m + v_m * v_m);
double h_p = GAMMA / (GAMMA - 1) * p[3] / rho_m + 0.5 * (u_p * u_p + v_p * v_p);
double h_mid = (std::sqrt(rho_m) * h_m + std::sqrt(rho_p) * h_p) / (std::sqrt(rho_m) + std::sqrt(rho_p));
// double pressure_m = m[3];
double e_m = 1/(GAMMA - 1) * pressure_m + 0.5 * rho_m * (u_m_n * u_m_n + u_m_t * u_m_t);
// Flux flux = {rho * u_n, rho * u_n * u_t, rho * u_n * u_n + pressure, u_n * (pressure + e)};
Flux flux_m = {rho_m * u_m_n, rho_m * u_m_n * u_m_t, rho_m * u_m_n * u_m_n + pressure_m, u_m_n * (pressure_m + e_m)};
// double pressure_p = p[3];
double e_p = 1/(GAMMA - 1) * pressure_p + 0.5 * rho_p * (u_p_n * u_p_n + u_p_t * u_p_t);
// Flux flux = {rho * u_n, rho * u_n * u_t, rho * u_n * u_n + pressure, u_n * (pressure + e)};
Flux flux_p = {rho_p * u_p_n, rho_p * u_p_n * u_p_t, rho_p * u_p_n * u_p_n + pressure_p, u_p_n * (pressure_p + e_p)};
// ===========================================================
// A naive implementation: average the fluxes on the both side
// ===========================================================
Flux flux = {0.5*(flux_m[0] + flux_p[0]), 0.5*(flux_m[1] + flux_p[1]), 0.5*(flux_m[2] + flux_p[2]), 0.5*(flux_m[3] + flux_p[3])};
// ===========================================================
// Roe's average: need another term in the flux
// ===========================================================
typedef Eigen::Matrix<double, 4, 4> Matrix4d;
typedef Eigen::Matrix<double, 4, 1> Vector4d;
Matrix4d A_abs = Matrix4d::Zero(), Lambda_abs = Matrix4d::Zero();
auto e_abs = [&](const double &a) { return entropy_abs(a, std::abs(x(1, 0) - x(0, 0)), dt); };
Matrix4d L = Matrix4d::Zero(), R = Matrix4d::Zero();
// Private vars here
{
double u = u_t_mid, v = u_n_mid;
double h = h_mid;
double a = std::sqrt((GAMMA - 1) * (h - 0.5 * (u * u + v * v)));
Lambda_abs(0, 0) = e_abs(u_n_reconstruct), Lambda_abs(1, 1) = e_abs(u_n_reconstruct), Lambda_abs(2, 2) = e_abs(u_n_reconstruct - a_reconstruct), Lambda_abs(3, 3) = e_abs(u_n_reconstruct + a_reconstruct);
R << 0, 1, 1, 1,
1, 0, u, u,
0, v, v-a, v+a,
u, 0.5*(v*v-u*u), h-v*a, h+v*a;
double b2 = (GAMMA - 1) / a / a, b1 = 0.5 * b2 * (u * u + v * v);
L << -b1*u, 1+b2*u*u, b2*u*v, -b2*u,
1-b1, b2*u, b2*v, -b2,
0.5*(b1+v/a), -0.5*b2*u, -0.5*(b2*v+1/a), 0.5*b2,
0.5*(b1-v/a), -0.5*b2*u, -0.5*(b2*v-1/a), 0.5*b2;
}
A_abs = R * Lambda_abs * L;
// Compute the flux
Vector4d U_m;
U_m << m[0], m[0] * u_m_t, m[0] * u_m_n, e_m;
Vector4d U_p;
U_p << p[0], p[0] * u_p_t, p[0] * u_p_n, e_p;
Vector4d Flux_Roe_add = -0.5 * (A_abs * (U_p - U_m));
flux = {flux[0] + Flux_Roe_add[0], flux[1] + Flux_Roe_add[1], flux[2] + Flux_Roe_add[2], flux[3] + Flux_Roe_add[3]};
return flux;
}
void Driver::calculate_flux()
{
#pragma omp parallel for schedule(static) collapse(2)
for (int i = 2; i < nx + 2; i++)
{
for (int j = 2; j < ny + 2; j++)
{
bool at_boundary = j < 3 || j > ny;
// n average
{
double u_n = u(i, j);
double v_n = v(i, j);
Vec2 n_bnd = {x(i-1, j-1) - x(i-2, j-1), y(i-1, j-1) - y(i-2, j-1)};
double len = n_bnd.norm();
n_bnd = n_bnd.normalize();
Vec2 n_normal = {-n_bnd.y, n_bnd.x};
Vec2 u_bnd = {u_n, v_n};
double u_normal = u_bnd.dot(n_normal);
double u_tangent = u_bnd.dot(n_bnd);
// double e = 1/(GAMMA - 1) * p_n + 0.5 * rho_n * (u_n * u_n + v_n * v_n);
// fluxes[i][j].n = {rho_n * u_normal, rho_n * u_normal * u_tangent, rho_n * u_normal * u_normal + p_n, u_normal*(e + p_n)};
if (at_boundary)
fluxes[i][j].n = reconstruct_roe_boundary(get_state(i, j-1), get_state(i, j), get_state(i, j+1), get_state(i, j+2), n_normal);
else if (scheme == "roe")
fluxes[i][j].n = reconstruct_roe(get_state(i, j-1), get_state(i, j), get_state(i, j+1), get_state(i, j+2), n_normal);
else if (scheme == "kfvs")
fluxes[i][j].n = reconstruct_kfvs(get_state(i, j-1), get_state(i, j), get_state(i, j+1), get_state(i, j+2), n_normal);
else
fluxes[i][j].n = reconstruct_v_l(get_state(i, j-1), get_state(i, j), get_state(i, j+1), get_state(i, j+2), n_normal);
}
// e average
{
double u_e = u(i, j);
double v_e = v(i, j);
Vec2 e_bnd = {x(i-1, j-2) - x(i-1, j-1), y(i-1, j-2) - y(i-1, j-1)};
double len = e_bnd.norm();
e_bnd = e_bnd.normalize();
Vec2 e_normal = {-e_bnd.y, e_bnd.x};
Vec2 u_bnd = {u_e, v_e};
double u_normal = u_bnd.dot(e_normal);
double u_tangent = u_bnd.dot(e_bnd);
// double e = 1/(GAMMA - 1) * p_e + 0.5 * rho_e * (u_e * u_e + v_e * v_e);
// fluxes[i][j].e = {rho_e * u_normal, rho_e * u_normal * u_tangent, rho_e * u_normal * u_normal + p_e, u_normal*(e + p_e)};
if (at_boundary)
fluxes[i][j].e = reconstruct_roe_boundary(get_state(i-1, j), get_state(i, j), get_state(i+1, j), get_state(i+2, j), e_normal);
else if (scheme == "roe")
fluxes[i][j].e = reconstruct_roe(get_state(i-1, j), get_state(i, j), get_state(i+1, j), get_state(i+2, j), e_normal);
else if (scheme == "kfvs")
fluxes[i][j].e = reconstruct_kfvs(get_state(i-1, j), get_state(i, j), get_state(i+1, j), get_state(i+2, j), e_normal);
else
fluxes[i][j].e = reconstruct_v_l(get_state(i-1, j), get_state(i, j), get_state(i+1, j), get_state(i+2, j), e_normal);
}
// s average
{
double u_s = u(i, j);
double v_s = v(i, j);
Vec2 s_bnd = {x(i-2, j-2) - x(i-1, j-2), y(i-2, j-2) - y(i-1, j-2)};
double len = s_bnd.norm();
s_bnd = s_bnd.normalize();
Vec2 s_normal = {-s_bnd.y, s_bnd.x};
Vec2 u_bnd = {u_s, v_s};
double u_normal = u_bnd.dot(s_normal);
double u_tangent = u_bnd.dot(s_bnd);
// double e = 1/(GAMMA - 1) * p_s + 0.5 * rho_s * (u_s * u_s + v_s * v_s);
// fluxes[i][j].s = {rho_s * u_normal, rho_s * u_normal * u_tangent, rho_s * u_normal * u_normal + p_s, u_normal*(e + p_s)};
if (at_boundary)
fluxes[i][j].s = reconstruct_roe_boundary(get_state(i, j+1), get_state(i, j), get_state(i, j-1), get_state(i, j-2), s_normal);
else if (scheme == "roe")
fluxes[i][j].s = reconstruct_roe(get_state(i, j+1), get_state(i, j), get_state(i, j-1), get_state(i, j-2), s_normal);
else if (scheme == "kfvs")
fluxes[i][j].s = reconstruct_kfvs(get_state(i, j+1), get_state(i, j), get_state(i, j-1), get_state(i, j-2), s_normal);
else
fluxes[i][j].s = reconstruct_v_l(get_state(i, j+1), get_state(i, j), get_state(i, j-1), get_state(i, j-2), s_normal);
}
// w average
{
double u_w = u(i, j);
double v_w = v(i, j);
Vec2 w_bnd = {x(i-2, j-1) - x(i-2, j-2), y(i-2, j-1) - y(i-2, j-2)};
double len = w_bnd.norm();
w_bnd = w_bnd.normalize();
Vec2 w_normal = {-w_bnd.y, w_bnd.x};
Vec2 u_bnd = {u_w, v_w};
double u_normal = u_bnd.dot(w_normal);
double u_tangent = u_bnd.dot(w_bnd);
// double e = 1/(GAMMA - 1) * p_w + 0.5 * rho_w * (u_w * u_w + v_w * v_w);
// fluxes[i][j].w = {rho_w * u_normal, rho_w * u_normal * u_tangent, rho_w * u_normal * u_normal + p_w, u_normal*(e + p_w)};
if (at_boundary)
fluxes[i][j].w = reconstruct_roe_boundary(get_state(i+1, j), get_state(i, j), get_state(i-1, j), get_state(i-2, j), w_normal);
else if (scheme == "roe")
fluxes[i][j].w = reconstruct_roe(get_state(i+1, j), get_state(i, j), get_state(i-1, j), get_state(i-2, j), w_normal);
else if (scheme == "kfvs")
fluxes[i][j].w = reconstruct_kfvs(get_state(i+1, j), get_state(i, j), get_state(i-1, j), get_state(i-2, j), w_normal);
else
fluxes[i][j].w = reconstruct_v_l(get_state(i+1, j), get_state(i, j), get_state(i-1, j), get_state(i-2, j), w_normal);
}
}
}
}