Memosa-FVM  0.2
ShockTube.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _SHOCKTUBE_H_
6 #define _SHOCKTUBE_H_
7 
8 #include "Array.h"
9 #include "Vector.h"
10 
11 template<class T>
12 class ShockTube
13 {
14 public:
15 
16  typedef Vector<T,3> Vec3;
17 
18  ShockTube(const int nCells) :
19  pL(10.0),
20  pR(1.0),
21  rhoL(8.0),
22  rhoR(1.0),
23  uL(0),
24  uR(0),
25  _nCells(nCells),
26  _gmm(0.4),
27  _nStages(5),
28  _stageCoeffs(5),
29  q(_nCells),
31  rho(nCells),
32  u(nCells),
33  M(nCells)
34  {
35  _stageCoeffs[0] = 0.25;
36  _stageCoeffs[1] = 1.0/6.0;
37  _stageCoeffs[2] = 0.375;
38  _stageCoeffs[3] = 0.5;
39  _stageCoeffs[4] = 1;
40  }
41 
42  T getPressure(const Vec3& q)
43  {
44  const T u = q[1]/q[0];
45  return (q[2] - 0.5*(q[0]*u*u))*_gmm;
46  }
47 
48  Vec3 getConservedVars(const T rho, const T p, const T u)
49  {
50  Vec3 q;
51  q[0] = rho;
52  q[1] = rho*u;
53  q[2] = p/_gmm + 0.5*rho*u*u;
54  return q;
55  }
56 
57  Vec3 getFlux(const Vec3& q)
58  {
59  const T u = q[1]/q[0];
60  const T p = getPressure(q);
61  Vec3 flux;
62  flux[0] = q[1];
63  flux[1] = q[0]*u*u + p;
64  flux[2] = (q[2]+p)*u;
65  return flux;
66  }
67 
68  Vec3 getRoeFlux_Frink(const Vec3& q0, const Vec3& q1)
69  {
70  const T half(0.5);
71 
72  const T sr0 = sqrt(q0[0]);
73  const T sr1 = sqrt(q1[0]);
74 
75  const T sr0psr1 = sr0 + sr1;
76  const T u0 = q0[1]/q0[0];
77  const T u1 = q1[1]/q1[0];
78 
79  const T p0 = getPressure(q0);
80  const T p1 = getPressure(q1);
81 
82  const T h0 = (q0[2]+p0) / q0[0];
83  const T h1 = (q1[2]+p1) / q1[0];
84 
85  const T rho_f = sr0 * sr1;
86  const T u_f = (sr0*u0 + sr1*u1) / sr0psr1;
87  const T h_f = (sr0*h0 + sr1*h1) / sr0psr1;
88 
89  const T c_f = sqrt( _gmm * (h_f - half*u_f*u_f));
90 
91  const T abs_uf = fabs(u_f);
92  const T abs_upc = fabs(u_f + c_f);
93  const T abs_umc = fabs(u_f - c_f);
94 
95 
96  const Vec3 flux0 = getFlux(q0);
97  const Vec3 flux1 = getFlux(q1);
98 
99  const T dr = q1[0]-q0[0];
100  const T rdu = rho_f*(u1-u0);
101 
102  const T dp = p1-p0;
103 
104  const T dpc2 = dp*c_f*c_f;
105 
106  const T a0 = half*(dpc2 + rdu*c_f)*abs_upc;
107  const T a4 = half*(dpc2 - rdu*c_f)*abs_umc;
108 
109  const T drmdpc2 = dr-dpc2;
110  Vec3 dFlux;
111 
112  dFlux[0] = abs_uf*drmdpc2 + a0 + a4;
113  dFlux[1] = abs_uf*drmdpc2*u_f + a0*(u_f+c_f) + a4*(u_f-c_f);
114  dFlux[2] = abs_uf*(half*drmdpc2*u_f*u_f) + a0*(h_f+u_f*c_f) + a4*(h_f-u_f*c_f);
115 
116  Vec3 flux(flux0+flux1-dFlux);
117  return half*flux;
118  }
119 
120  Vec3 getRoeFlux(const Vec3& q0, const Vec3& q1)
121  {
122  const T half(0.5);
123 
124  const T sr0 = sqrt(q0[0]);
125  const T sr1 = sqrt(q1[0]);
126 
127  const T sr0psr1 = sr0 + sr1;
128  const T u0 = q0[1]/q0[0];
129  const T u1 = q1[1]/q1[0];
130 
131  const T p0 = getPressure(q0);
132  const T p1 = getPressure(q1);
133 
134  const T h0 = (q0[2]+p0) / q0[0];
135  const T h1 = (q1[2]+p1) / q1[0];
136 
137  const T rho_f = sr0 * sr1;
138  const T u_f = (sr0*u0 + sr1*u1) / sr0psr1;
139  const T h_f = (sr0*h0 + sr1*h1) / sr0psr1;
140 
141  const T c_f = sqrt( _gmm * (h_f - half*u_f*u_f));
142 
143  const T abs_uf = fabs(u_f);
144  const T abs_upc = fabs(u_f + c_f);
145  const T abs_umc = fabs(u_f - c_f);
146 
147 
148  const Vec3 flux0 = getFlux(q0);
149  const Vec3 flux1 = getFlux(q1);
150 
151  const T dr = q1[0]-q0[0];
152  const T rdu = rho_f*(u1-u0);
153 
154  const T dp = p1-p0;
155 
156  const T dpbyc2 = dp/(c_f*c_f);
157 
158  const T a0 = half*(dpbyc2 + rdu/c_f)*abs_upc;
159  const T a4 = half*(dpbyc2 - rdu/c_f)*abs_umc;
160 
161  const T drmdpbyc2 = dr-dpbyc2;
162  Vec3 dFlux;
163 
164  dFlux[0] = abs_uf*drmdpbyc2 + a0 + a4;
165  dFlux[1] = abs_uf*drmdpbyc2*u_f + a0*(u_f+c_f) + a4*(u_f-c_f);
166  dFlux[2] = abs_uf*(half*drmdpbyc2*u_f*u_f) + a0*(h_f+u_f*c_f)
167  + a4*(h_f-u_f*c_f);
168 
169  Vec3 flux(flux0+flux1-dFlux);
170  return half*flux;
171  }
172 
173 
174  void init()
175  {
176  Vec3 qL(getConservedVars(rhoL, pL, uL));
177  Vec3 qR(getConservedVars(rhoR, pR, uR));
178 
179  for(int i=0; i<_nCells/2; i++)
180  q[i] = qL;
181 
182  for(int i=_nCells/2; i<_nCells; i++)
183  q[i] = qR;
184  }
185 
186  void solve(const double dt, const int nsteps)
187  {
188  const T dx = T(1.0)/_nCells;
189 
190  Array<Vec3> R(_nCells);
191 
192  Array<Vec3> qN(_nCells);
193 
194  for(int n=0; n<nsteps; n++)
195  {
196  cout << "step " << n << endl;
197  qN = q;
198  for(int s=0; s<_nStages; s++)
199  {
200  R.zero();
201 
202  for(int f=1; f<_nCells; f++)
203  {
204  int c0 = f-1;
205  int c1 = f;
206  const Vec3 flux = getRoeFlux(q[c0], q[c1]);
207  R[c0] += flux;
208  R[c1] -= flux;
209  }
210 
211  R[0] -= getRoeFlux(q[0],q[0]);
212  R[_nCells-1] += getRoeFlux(q[_nCells-1],q[_nCells-1]);
213 
214  const T sCoeff = _stageCoeffs[s]*dt/dx;
215  for(int c=0; c<_nCells; c++)
216  {
217  q[c] = qN[c] - sCoeff*R[c];
218  }
219  }
220  }
221 
222  for(int c=0; c<_nCells; c++)
223  {
224  rho[c] = q[c][0];
225  pressure[c] = getPressure(q[c]);
226  u[c] = q[c][1]/q[c][0];
227  T soundSpeed = sqrt((_gmm+1)*pressure[c]/rho[c]);
228  M[c] = u[c]/soundSpeed;
229  }
230  }
231 
232  ArrayBase& getSolution() {return q;}
234  ArrayBase& getDensity() {return rho;}
235  ArrayBase& getVelocity() {return u;}
236  ArrayBase& getMachNumber() {return M;}
237 
238 
239  T pL;
240  T pR;
241  T rhoL;
242  T rhoR;
243  T uL;
244  T uR;
245 
246  const int _nCells;
247  T _gmm;
248  const int _nStages;
255 };
256 #endif
virtual void zero()
Definition: Array.h:281
void solve(const double dt, const int nsteps)
Definition: ShockTube.h:186
Vec3 getRoeFlux_Frink(const Vec3 &q0, const Vec3 &q1)
Definition: ShockTube.h:68
Array< double > _stageCoeffs
Definition: ShockTube.h:249
Array< T > pressure
Definition: ShockTube.h:251
void init()
Definition: ShockTube.h:174
ArrayBase & getDensity()
Definition: ShockTube.h:234
Vec3 getConservedVars(const T rho, const T p, const T u)
Definition: ShockTube.h:48
ArrayBase & getPressure()
Definition: ShockTube.h:233
Array< Vec3 > q
Definition: ShockTube.h:250
Tangent sqrt(const Tangent &a)
Definition: Tangent.h:317
ShockTube(const int nCells)
Definition: ShockTube.h:18
const int _nStages
Definition: ShockTube.h:248
const int _nCells
Definition: ShockTube.h:246
Vector< T, 3 > Vec3
Definition: ShockTube.h:16
ArrayBase & getSolution()
Definition: ShockTube.h:232
Tangent fabs(const Tangent &a)
Definition: Tangent.h:312
Definition: Array.h:14
ArrayBase & getVelocity()
Definition: ShockTube.h:235
Array< T > M
Definition: ShockTube.h:254
Vec3 getFlux(const Vec3 &q)
Definition: ShockTube.h:57
Vec3 getRoeFlux(const Vec3 &q0, const Vec3 &q1)
Definition: ShockTube.h:120
ArrayBase & getMachNumber()
Definition: ShockTube.h:236
T getPressure(const Vec3 &q)
Definition: ShockTube.h:42
Array< T > rho
Definition: ShockTube.h:252
Array< T > u
Definition: ShockTube.h:253