-
Notifications
You must be signed in to change notification settings - Fork 0
/
eulerData.cpp
115 lines (88 loc) · 2.25 KB
/
eulerData.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
#include "eulerData.hpp"
namespace fvm {
// Operator overloads for Cell.
Cell operator+(Cell a, Cell b) {
Cell ans;
for (size_t i = 0; i < a.size(); ++i) {
ans[i] = a[i] + b[i];
}
return ans;
}
Cell operator-(Cell a, Cell b) {
Cell ans;
for (size_t i = 0; i < a.size(); ++i) {
ans[i] = a[i] - b[i];
}
return ans;
}
Cell operator*(double a, Cell u) {
Cell ans;
for (size_t i = 0; i < u.size(); ++i) {
ans[i] = a * u[i];
}
return ans;
}
Cell operator*(Cell u, double a) {
Cell ans;
for (size_t i = 0; i < u.size(); ++i) {
ans[i] = a * u[i];
}
return ans;
}
Cell operator/(Cell u, double a) {
Cell ans;
for (size_t i = 0; i < u.size(); ++i) {
ans[i] = u[i] / a;
}
return ans;
}
// Put the data in the correct mode.
void EulerData::setMode(EulerDataMode want) {
// The mode is already correctly set; return early.
if (mode_ == want) {
return;
}
switch (want) {
case EulerDataMode::primitive:
makePrimitive_();
break;
case EulerDataMode::conserved:
makeConserved_();
break;
}
}
// Private member function definitions:
// ====================================================================== //
void EulerData::makeConserved_() {
for (int i = 0; i < xSize(); ++i) {
for (int j = 0; j < ySize(); ++j) {
auto rho = data_[i][j][dIndex];
auto p = data_[i][j][pIndex];
// Convert velocity to momentum.
data_[i][j][vIndexX] *= rho;
data_[i][j][vIndexY] *= rho;
auto rhoVX = data_[i][j][vIndexX];
auto rhoVY = data_[i][j][vIndexY];
// Convert pressure to energy.
data_[i][j][pIndex] = (p / (gamma_ - 1)) + 0.5 * ((rhoVX*rhoVX + rhoVY*rhoVY) / rho);
}
}
mode_ = EulerDataMode::conserved;
}
void EulerData::makePrimitive_() {
for (int i = 0; i < xSize(); ++i) {
for (int j = 0; j < ySize(); ++j) {
auto rho = data_[i][j][dIndex];
auto rhoVX = data_[i][j][moIndexX];
auto rhoVY = data_[i][j][moIndexY];
auto e = data_[i][j][eIndex];
// Convert energy to pressure.
data_[i][j][eIndex] = (gamma_ - 1) * (e - 0.5 * ((rhoVX*rhoVX + rhoVY*rhoVY) / rho));
// Convert momentum to velocity.
data_[i][j][moIndexX] /= rho;
data_[i][j][moIndexY] /= rho;
}
}
mode_ = EulerDataMode::primitive;
}
}