hydrodynamic_variables.cpp
1 #include <cmath>
3 #include "../../misc/universal_error.hpp"
4 #include "../../misc/utils.hpp"
5 
6 Primitive::Primitive(void):
7  Density(0),
8  Pressure(0),
9  Velocity(0,0),
10  Energy(0),
11  SoundSpeed(0) {}
12 
13 Primitive::Primitive(double density_i,
14  double pressure_i,
15  Vector2D const& velocity_i,
16  double energy_i,
17  double sound_speed_i):
18  Density(density_i),
19  Pressure(pressure_i),
20  Velocity(velocity_i),
21  Energy(energy_i),
22  SoundSpeed(sound_speed_i) {}
23 
24 int Primitive::GetVarNo(void) const
25 {
26  return 6;
27 }
28 
30 {
31  if (this == &other)
32  return *this;
33  Density=other.Density;
34  Energy=other.Energy;
35  Pressure=other.Pressure;
36  SoundSpeed=other.SoundSpeed;
37  Velocity=other.Velocity;
38  return *this;
39 }
40 
42 {
43  return is_nan(p.Density)||
44  is_nan(p.Pressure)||
45  is_nan(p.Velocity.x)||
46  is_nan(p.Velocity.y)||
47  is_nan(p.Energy)||
48  is_nan(p.SoundSpeed);
49 }
50 
52  Mass(other.Mass),
53  Momentum(other.Momentum),
54  Energy(other.Energy) {}
55 
57  Vector2D const& momentum,
58  double energy):
59  Mass(mass), Momentum(momentum),
60  Energy(energy) {}
61 
63 {
64  if (this == &other)
65  return *this;
66  Energy=other.Energy;
67  Mass=other.Mass;
68  Momentum=other.Momentum;
69  return *this;
70 }
71 
72 Primitive::Primitive(const Primitive& other):
73  Density(other.Density),
74  Pressure(other.Pressure),
75  Velocity(other.Velocity),
76  Energy(other.Energy),
77  SoundSpeed(other.SoundSpeed) {}
78 
80 {
81  Density += p.Density;
82  Pressure += p.Pressure;
83  Energy += p.Energy;
85  Velocity.x += p.Velocity.x;
86  Velocity.y += p.Velocity.y;
87  return *this;
88 }
89 
90 double& Primitive::operator[](int index)
91 {
92  if(index==0)
93  return Density;
94  else if(index==1)
95  return Pressure;
96  else if(index==2)
97  return Energy;
98  else if(index==3)
99  return SoundSpeed;
100  else if(index==4)
101  return Velocity.x;
102  else if(index==5)
103  return Velocity.y;
104  else
105  throw UniversalError("Invalid index in Primitive::operator[]");
106 }
107 
108 double Primitive::operator[](int index) const
109 {
110  if(index==0)
111  return Density;
112  else if(index==1)
113  return Pressure;
114  else if(index==2)
115  return Energy;
116  else if(index==3)
117  return SoundSpeed;
118  else if(index==4)
119  return Velocity.x;
120  else if(index==5)
121  return Velocity.y;
122  else
123  throw UniversalError("Invalid index in Primitive::operator[]");
124 }
125 
126 namespace{
127  Primitive binary_op_primitive(Primitive const& p1,
128  Primitive const& p2,
129  BinaryOperation<double> const& op)
130  {
131  return Primitive(op(p1.Density,p2.Density),
132  op(p1.Pressure,p2.Pressure),
133  Vector2D(op(p1.Velocity.x,p2.Velocity.x),
134  op(p1.Velocity.y,p2.Velocity.y)),
135  op(p1.Energy,p2.Energy),
136  op(p1.SoundSpeed,p2.SoundSpeed));
137  }
138 
139  Primitive primitive_scalar_op(Primitive const& p,
140  double c,
141  BinaryOperation<double> const& op)
142  {
143  return Primitive(op(p.Density,c),
144  op(p.Pressure,c),
145  Vector2D(op(p.Velocity.x,c),
146  op(p.Velocity.y,c)),
147  op(p.Energy,c),
148  op(p.SoundSpeed,c));
149  }
150 }
151 
153  Primitive const& p2)
154 {
155  class plus: public BinaryOperation<double>
156  {
157  double operator()(double const& x, double const& y) const
158  {
159  return x+y;
160  }
161  } op;
162  return binary_op_primitive(p1, p2, op);
163 }
164 
166  Primitive const& p2)
167 {
168  class minus: public BinaryOperation<double>
169  {
170  public:
171  double operator()(double const& x, double const& y) const
172  {
173  return x-y;
174  }
175  } op;
176 
177  return binary_op_primitive(p1,p2,op);
178 }
179 
181  double c)
182 {
183  class divide: public BinaryOperation<double>
184  {
185  public:
186  double operator()(double const& x,double const& y) const
187  {
188  return x/y;
189  }
190  } op;
191  return primitive_scalar_op(p,c,op);
192 }
193 
195  double s)
196 {
197  class multiply: public BinaryOperation<double>
198  {
199  public:
200  double operator()(double const& x, double const& y) const
201  {
202  return x*y;
203  }
204  } op;
205  return primitive_scalar_op(p,s,op);
206 }
207 
209  Primitive const& p)
210 {
211  return p*s;
212 }
213 
215  Mass(0), Momentum(Vector2D(0,0)), Energy(0) {}
216 
217 Conserved operator+(Conserved const& c1, Conserved const& c2)
218 {
219  return Conserved(c1.Mass+c2.Mass,
220  c1.Momentum+c2.Momentum,
221  c1.Energy+c2.Energy);
222 }
223 
224 Conserved operator-(Conserved const& c1, Conserved const& c2)
225 {
226  return Conserved(c1.Mass-c2.Mass,
227  c1.Momentum-c2.Momentum,
228  c1.Energy-c2.Energy);
229 }
230 
231 Conserved operator*(double d, Conserved const& c)
232 {
233  return Conserved(d*c.Mass,
234  d*c.Momentum,
235  d*c.Energy);
236 }
237 
238 Conserved operator/(Conserved const& c, double d)
239 {
240  return Conserved(c.Mass/d,
241  c.Momentum/d,
242  c.Energy/d);
243 }
244 
246 {
247  Mass += c.Mass;
248  Momentum += c.Momentum;
249  Energy += c.Energy;
250  return *this;
251 }
252 
254 {
255  Mass -= c.Mass;
256  Momentum -= c.Momentum;
257  Energy -= c.Energy;
258  return *this;
259 }
260 
262 {
263  return p.Density*(0.5*pow(abs(p.Velocity),2)+
264  p.Energy);
265 }
266 
268 {
269  return Conserved(p.Density,
270  p.Density*p.Velocity,
271  TotalEnergyDensity(p));
272 }
273 
275 {
276  return Conserved(vol*p.Density,
277  vol*p.Density*p.Velocity,
278  vol*TotalEnergyDensity(p));
279 }
280 
282  Vector2D const& n)
283 {
284  const Vector2D nn = n/abs(n);
285  return Conserved(p.Density*ScalarProd(p.Velocity, nn),
286  p.Pressure*nn +
287  p.Density*ScalarProd(p.Velocity, nn)*p.Velocity,
289  ScalarProd(p.Velocity, nn));
290 }
Set of conserved variables (extensive)
Vector2D Momentum
Momentum.
double Density
Density.
int GetVarNo(void) const
Returns the numbers of members.
double SoundSpeed
Speed of sound.
bool primitive_has_nan(Primitive const &p)
Checks if on of the fields of Primitive is a nan.
Hydrodynamic variables.
Conserved & operator-=(Conserved const &c)
subtraction of flux
Container for error reports.
BinaryOperation Binary operator.
Definition: utils.hpp:595
Vector2D Velocity
Velocity.
double Energy
Total energy (kinetic + thermal)
double TotalEnergyDensity(Primitive const &p)
Calculates the total energy density.
double y
Component in the y direction.
Definition: geometry.hpp:48
Primitive & operator=(Primitive const &other)
Assigmnet operator.
Conserved Primitive2Conserved(Primitive const &p)
Converts primitive variables to conserved intensive.
Vector3D operator*(double d, Vector3D const &v)
Scalar product.
Definition: Vector3D.cpp:162
Conserved & operator+=(Conserved const &f)
Addition of flux.
Conserved Primitive2Flux(Primitive const &p, Vector2D const &n)
Converts primitive variables to flux.
double ScalarProd(Vector3D const &v1, Vector3D const &v2)
Scalar product of two vectors.
Definition: Vector3D.cpp:185
Conserved & operator=(Conserved const &other)
Assigmnet operator.
Vector3D operator/(Vector3D const &v, double d)
Scalar division.
Definition: Vector3D.cpp:176
Primitive & operator+=(Primitive const &p)
Operator for adding members of two primitives.
double Pressure
Pressure.
double Energy
Thermal energy per unit mass, entahalpy in relativistic case.
bool is_nan(double x)
Checks whether a number is a nan.
Definition: utils.cpp:19
double abs(Vector3D const &v)
Norm of a vector.
Definition: Vector3D.cpp:44
Vector3D operator+(Vector3D const &v1, Vector3D const &v2)
Term by term addition.
Definition: Vector3D.cpp:143
double & operator[](int index)
Subscript operator.
Primitive hydrodynamic variables.
2D Mathematical vector
Definition: geometry.hpp:15
double x
Component in the x direction.
Definition: geometry.hpp:45
Vector3D operator-(Vector3D const &v1, Vector3D const &v2)
Term by term subtraction.
Definition: Vector3D.cpp:152
Conserved(void)
Null constructor (sets all members to zero)