-
Notifications
You must be signed in to change notification settings - Fork 3
/
Problem.cpp
executable file
·199 lines (135 loc) · 5.59 KB
/
Problem.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
#include "Problem.h"
#include <iostream>
using namespace std;
// ------------------ Constructors & Destructors ----------------
Problem::Problem(Mesh2D &mesh2D)
{
mesh = &mesh2D;
double hx = mesh->hx;
double hy = mesh->hy;
phi[0] = [=](numvector<double, 2> r, int iCell){ return 1.0 / sqrt(hx*hy); };
phi[1] = [=](numvector<double, 2> r, int iCell){ return sqrt(12.0 / hy / pow(hx, 3)) * (r[0] - mesh->cellCenters[iCell][0]); };
phi[2] = [=](numvector<double, 2> r, int iCell){ return sqrt(12.0 / hx / pow(hy, 3)) * (r[1] - mesh->cellCenters[iCell][1]); };
gradPhi[0] = [=](numvector<double, 2> r, int iCell){ return numvector<double, 2>{ 0.0, 0.0 }; };
gradPhi[1] = [=](numvector<double, 2> r, int iCell){ return numvector<double, 2>{ sqrt(12.0 / hy / pow(hx, 3)), 0.0 }; };
gradPhi[2] = [=](numvector<double, 2> r, int iCell){ return numvector<double, 2>{ 0.0, sqrt(12.0 / hx / pow(hy, 3)) }; };
writer.open("alphaCoeffs");
} // end constructor by mesh
Problem::~Problem()
{
writer.close();
}
numvector<double, 5> Problem::reconstructSolution(const numvector<double, \
nShapes * 5>& alpha, \
numvector<double, 2>& point, \
int iCell)
{
numvector<double, 5> sol(0.0);
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < nShapes; ++j)
sol[i] += phi[j](point,iCell)*alpha[i*nShapes + j];
}
return sol;
} // end reconstructSolution
// ------------------ Private class methods --------------------
void Problem::write(ostream& writer, const numvector<double,5*nShapes>& coeffs)
{
for (int i = 0; i < 5*nShapes; ++i)
writer << coeffs[i] << ' ';
writer << endl;
}
void Problem::write(ostream& writer, const vector<numvector<double,5*nShapes>>& coeffs)
{
for (int j = 0; j < mesh->nInternalCells; ++j)
{
for (int i = 0; i < 5*nShapes; ++i)
writer << coeffs[j][i] << ' ';
writer << endl;
}
}
double Problem::getPressure(numvector<double, 5> sol)
{
double magU = pow(sol[2], 2) + pow(sol[3], 2) + pow(sol[1], 2);
return (cpcv - 1)*(sol[4] - 0.5*magU / sol[0]);
} // end getPressure
double Problem::c(numvector<double, 5> sol)
{
return sqrt( cpcv * getPressure(sol) / sol[0]);
} // end c for cell
double Problem::c_av(numvector<double, 5> solOne, numvector<double, 5> solTwo)
{
double semiRho = 0.5*(solOne[0] + solTwo[0]);
double semiP = 0.5*(getPressure(solOne) + getPressure(solTwo));
return sqrt( cpcv * semiP / semiRho);
} // end c for edge
numvector<double, 5> Problem::lambdaF(numvector<double, 5> solOne, numvector<double, 5> solTwo)
{
double u = fabs(0.5*(solOne[1] / solOne[0] + solTwo[1] / solTwo[0])); //estimation!!!
double soundSpeed = c_av(solOne, solTwo);
return {u - soundSpeed, u, u, u, u + soundSpeed};
} // end lambdaF
numvector<double, 5> Problem::lambdaG(numvector<double, 5> solOne, numvector<double, 5> solTwo)
{
double v = fabs(0.5*(solOne[2] / solOne[0] + solTwo[2] / solTwo[0])); //estimation!!!
double soundSpeed = c_av(solOne, solTwo);
return {v - soundSpeed, v, v, v, v + soundSpeed};
} // end lambdaG
numvector<double, 5> Problem::fluxF(numvector<double, 5> sol)
{
double u = sol[1] / sol[0];
double p = getPressure(sol);
return { sol[1], u*sol[1] + p, u*sol[2], u*sol[3], (sol[4] + p)*u };
} // end fluxF
numvector<double, 5> Problem::fluxG(numvector<double, 5> sol)
{
double v = sol[2] / sol[0];
double p = getPressure(sol);
return { sol[2], v*sol[1], v*sol[2] + p, v*sol[3], (sol[4] + p)*v };
} // end fluxG
void Problem::setInitialConditions()
{
// define functions for initial conditions
double rho0 = 1.0;
function<double(const numvector<double,2> r)> initRho = \
[](const numvector<double,2> r) \
{ return 0.001 * exp( -2.0 * pow(r[0] - 2.0, 2) - 2.0 * pow(r[1] - 2.0, 2)); };
function<numvector<double, 5>(const numvector<double, 2>& r)> init = \
[&](const numvector<double, 2> r) { return numvector<double, 5> { rho0 + initRho(r), 0.0, 0.0, 0.0, (rho0 + initRho(r)) / cpcv / (cpcv - 1.0) }; };
int nCells = mesh->nInternalCells + mesh->nGhostCells;
alphaPrev.resize(nCells);
alphaNext.resize(nCells);
GaussIntegrator GP;
numvector<double, 5> buffer;
// for internal cells
for (int k = 0; k < mesh->nInternalCells; ++k)
{
numvector<numvector<double,2>,4> nodes = mesh->getCellCoordinates(k);
for (int q = 0; q < nShapes; ++q)
{
function<numvector<double, 5>(numvector<double, 2>)> f = \
[&](const numvector<double,2>& x){return phi[q](x,k) * init(x);};
buffer = GP.integrate(f, nodes);
for (int p = 0; p < 5; ++p)
{
alphaPrev[k][p*nShapes + q] = buffer[p];
//cout << buffer[p] << ' ' ;
}// for p
}
write(writer,alphaPrev[k]);
}
// for ghost cells --- see Apply Boundary.
} // end setInitialConditions
void Problem::applyBoundary(vector<numvector<double,5*nShapes>>& alpha)
{
double rho0 = 1.0;
int nCells = mesh->nInternalCells + mesh->nGhostCells;
numvector<double, 5*nShapes> infCondition = {rho0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (rho0) / cpcv / (cpcv - 1.0), 0.0, 0.0};
for (int k = mesh->nInternalCells; k < nCells; ++k)
{
double sqrtJ = sqrt(mesh->hx * mesh->hy);
alpha[k] = sqrtJ * infCondition;
//write(writer,alphaPrev[k]);
}
}
// ------------------ Public class methods --------------------