-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZone.cpp
209 lines (180 loc) · 7.31 KB
/
Zone.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
#include "Zone.h"
Zone::Zone(std::shared_ptr<Grid> grid, int_t solType)
{
_grid = grid;
_solType = solType;
_polyOrder = 0;
_solution.resize(grid->getNumCell());
_DOF.resize(1);
_DOF[0].resize(grid->getNumCell());
// DG basis
_basis = std::make_shared<FRbasis>(_grid);
}
Zone::Zone(std::shared_ptr<Grid> grid, int_t polyOrder, int_t solType)
{
_grid = grid;
_solType = solType;
_polyOrder = polyOrder;
// Print error message if polynomial order is less than 0
if (_polyOrder < 0)
ERROR("Polynomial order");
_solution.resize(grid->getNumCell());
_DOF.resize(polyOrder + 1);
for (int_t iorder = 0; iorder <= polyOrder; ++iorder)
_DOF[iorder].resize(grid->getNumCell());
// DG basis
_basis = std::make_shared<FRbasis>(_grid);
}
Zone::~Zone()
{
}
real_t Zone::getPolySolution(int_t icell, real_t x) const
{
real_t u = 0;
real_t tester = 0;
// Calculate polynomial solution at x / beware of time level
for (int_t idegree = 0; idegree <= _polyOrder; ++idegree)
u += _DOF[idegree][icell] * _basis->LagrangeP(_basis->toCompCoord(icell,x), idegree, _polyOrder);
return u;
}
real_t Zone::getPolySolution(int_t icell, real_t x, std::shared_ptr<Zone> zone) const
{
real_t u = 0;
// Calculate polynomial solution at x / beware of time level
for (int_t idegree = 0; idegree <= _polyOrder; ++idegree)
u += zone->getDOF()[idegree][icell] * _basis->LagrangeP(_basis->toCompCoord(icell,x), idegree, _polyOrder);
return u;
}
void Zone::initialize(std::shared_ptr<InitialCondition> initialCondition)
{
// Temporary cell object
std::vector<std::shared_ptr<Cell> > temp_cell = _grid->getCell();
// Grid size
real_t temp_dx = _grid->getSizeX();
// Initializing Degree of freedom
for (int_t icell = 0; icell < _grid->getNumCell(); ++icell)
{
real_t temp_x = temp_cell[icell]->getPosX();
_DOF[0][icell] = initialCondition->initializer(temp_x + 0.5*temp_dx*_basis->getSolutionPoint(0, _polyOrder) + epsilon, _solType);
_DOF[_polyOrder][icell] = initialCondition->initializer(temp_x + 0.5*temp_dx*_basis->getSolutionPoint(_polyOrder, _polyOrder) - epsilon, _solType);
if (_polyOrder > 1)
{
for (int_t iorder = 1; iorder < _polyOrder; ++iorder)
_DOF[iorder][icell] = initialCondition->initializer(temp_x + 0.5*temp_dx*_basis->getSolutionPoint(iorder, _polyOrder), _solType);
}
}
// Calculate solution
calSolution();
}
void Zone::calSolution()
{
for (int_t icell = 0; icell < _grid->getNumCell(); ++icell)
_solution[icell] = getAverage(icell);
}
real_t Zone::getAverage(int_t num_cell) const
{
// Temporary object
std::vector<real_t> nodal_DOF(_polyOrder + 1, 0.0);
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
nodal_DOF[iorder] = _DOF[iorder][num_cell];
// Vandermonde matrix
std::vector<std::vector<real_t>> Vmatrix, inv_Vmatrix;
std::vector<real_t> lobatto_points(_polyOrder + 1, 0.0);
switch (_polyOrder)
{
case 0:
lobatto_points[0] = 0.0;
break;
case 1:
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
lobatto_points[iorder] = Lobatto2_X(iorder);
break;
case 2:
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
lobatto_points[iorder] = Lobatto3_X(iorder);
break;
case 3:
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
lobatto_points[iorder] = Lobatto4_X(iorder);
break;
case 4:
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
lobatto_points[iorder] = Lobatto5_X(iorder);
break;
default: ERROR("exceeds maximum order");
}
Vmatrix = Vandermonde(lobatto_points);
inv_Vmatrix = inv_Vandermonde(lobatto_points);
// Convert nodal to modal expression
std::vector<real_t> modal_DOF(_polyOrder + 1, 0.0);
for (int_t idegree = 0; idegree <= _polyOrder; ++idegree)
for (int_t jdegree = 0; jdegree <= _polyOrder; ++jdegree)
modal_DOF[idegree] += inv_Vmatrix[idegree][jdegree] * nodal_DOF[jdegree];
// return mode 0
return modal_DOF[0];
}
std::vector<std::vector<real_t>> Zone::Vandermonde(std::vector<real_t> r) const
{
std::vector<std::vector<real_t>> V;
V.resize(r.size());
for (int_t ipoint = 0; ipoint < r.size(); ++ipoint)
V[ipoint].resize(r.size());
for (int_t iorder = 0; iorder < r.size(); ++iorder)
for (int_t ipoint = 0; ipoint < r.size(); ++ipoint)
V[ipoint][iorder] = LegendreP(iorder, r[ipoint]);
return V;
}
std::vector<std::vector<real_t>> Zone::inv_Vandermonde(std::vector<real_t> r) const
{
std::vector<std::vector<real_t>> invV;
invV.resize(r.size());
for (int_t ipoint = 0; ipoint < r.size(); ++ipoint)
invV[ipoint].resize(r.size());
real_t den1, den2, den3, den4;
switch (r.size())
{
case 1:
invV[0] = { 1.0 };
break;
case 2:
den1 = 1.0 / (r[0] - r[1]);
invV[0] = { -r[1] * den1, r[0] * den1 };
invV[1] = { den1, -den1 };
break;
case 3:
den1 = 1.0 / (r[0] * r[1] + r[0] * r[2] - r[1] * r[2] - pow(r[0], 2.0));
den2 = 1.0 / (r[0] * r[1] - r[0] * r[2] + r[1] * r[2] - pow(r[1], 2.0));
den3 = 1.0 / (r[0] * r[1] - r[0] * r[2] - r[1] * r[2] + pow(r[2], 2.0));
invV[0] = { -CONST13*(3.0*r[1] * r[2] + 1.0)*den1, -CONST13*(3.0*r[0] * r[2] + 1.0)*den2, CONST13*(3.0*r[0] * r[1] + 1.0)*den3 };
invV[1] = { (r[1] + r[2])*den1, (r[0] + r[2])*den2, -(r[0] + r[1])*den3 };
invV[2] = { -CONST23*den1, -CONST23*den2, CONST23*den3 };
break;
case 4:
den1 = 1.0 / (pow(r[0], 2.0)*(r[1] + r[2] + r[3] - r[0]) - (r[3] * r[0] * r[1]) - (r[3] * r[0] * r[2]) + (r[3] * r[1] * r[2]) - (r[0] * r[1] * r[2]));
den2 = 1.0 / (pow(r[1], 2.0)*(r[0] + r[2] + r[3] - r[1]) - (r[3] * r[0] * r[1]) + (r[3] * r[0] * r[2]) - (r[3] * r[1] * r[2]) - (r[0] * r[1] * r[2]));
den3 = 1.0 / (pow(r[2], 2.0)*(r[0] + r[1] + r[3] - r[2]) + (r[3] * r[0] * r[1]) - (r[3] * r[0] * r[2]) - (r[3] * r[1] * r[2]) - (r[0] * r[1] * r[2]));
den4 = 1.0 / (pow(r[3], 2.0)*(r[0] + r[1] + r[2] - r[3]) - (r[3] * r[0] * r[1]) - (r[3] * r[0] * r[2]) - (r[3] * r[1] * r[2]) + (r[0] * r[1] * r[2]));
invV[0] = { CONST13*(r[3] + r[1] + r[2] + 3.0*r[3] * r[1] * r[2])*den1, CONST13*(r[3] + r[0] + r[2] + 3.0*r[3] * r[0] * r[2])*den2, CONST13*(r[3] + r[0] + r[1] + 3.0*r[3] * r[0] * r[1])*den3, CONST13*(r[0] + r[1] + r[2] + 3.0*r[0] * r[1] * r[2])*den4 };
invV[1] = { -0.2*(5.0*r[3] * r[1] + 5.0*r[3] * r[2] + 5.0*r[1] * r[2] + 3.0)*den1, -0.2*(5.0*r[3] * r[0] + 5.0*r[3] * r[2] + 5.0*r[0] * r[2] + 3.0)*den2, -0.2*(5.0*r[3] * r[0] + 5.0*r[3] * r[1] + 5.0*r[0] * r[1] + 3.0)*den3, -0.2*(5.0*r[0] * r[1] + 5.0*r[0] * r[2] + 5.0*r[1] * r[2] + 3.0)*den4 };
invV[2] = { CONST23*(r[3] + r[1] + r[2])*den1, CONST23*(r[3] + r[0] + r[2])*den2, CONST23*(r[3] + r[0] + r[1])*den3, CONST23*(r[0] + r[1] + r[2])*den4 };
invV[3] = { -0.4*den1, -0.4*den2, -0.4*den3, -0.4*den4 };
break;
default: ERROR("exceeds maximum order");
}
return invV;
}
void Zone::print() const
{
std::cout << "X coordinate" << "\t\t" << "Solution" << "\t\t";
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
std::cout << "DOF(" << iorder << ")\t\t\t";
std::cout << "\n";
// Print variables
for (int_t icell = 0; icell < _grid->getNumCell(); ++icell)
{
std::cout << std::to_string(_grid->getCell()[icell]->getPosX()) << "\t\t" << std::to_string(_solution[icell]) << "\t\t";
for (int_t iorder = 0; iorder <= _polyOrder; ++iorder)
std::cout << std::to_string(_DOF[iorder][icell]) << "\t\t";
std::cout << "\n";
}
}