condition_action_sequence_2.cpp
1 #include "condition_action_sequence_2.hpp"
3 #include "../../misc/utils.hpp"
4 #ifdef RICH_MPI
5 #include <mpi.h>
6 #endif
7 
9 (const vector<pair<const ConditionActionSequence::Condition*, const ConditionActionSequence::Action*> >& sequence,
10  const vector<pair<const ConditionActionSequence::Condition*, const Action2*> >& sequence2,
11  SpatialReconstruction const& interp):
12  sequence_(sequence),sequence2_(sequence2),interp_(interp),edge_values_(vector<pair<ComputationalCell,
13  ComputationalCell> >()){}
14 
15 ConditionActionSequence2::~ConditionActionSequence2(void)
16 {}
17 
18 namespace
19 {
20  pair<Vector2D, Vector2D> calc_parallel_normal(const Tessellation& tess, const Edge& edge)
21  {
22  const Vector2D p = normalize(Parallel(edge));
23  if (edge.neighbors.first >= 0 && edge.neighbors.first < tess.GetPointNo())
24  return pair<Vector2D, Vector2D>(p,normalize(remove_parallel_component(edge.vertices.first -
25  tess.GetMeshPoint(edge.neighbors.first), p)));
26  if (edge.neighbors.second >= 0 && edge.neighbors.second < tess.GetPointNo())
27  return pair<Vector2D, Vector2D>(p, normalize(remove_parallel_component(tess.GetMeshPoint(edge.neighbors.second) -
28  edge.vertices.first, p)));
29  assert(false);
30  return pair<Vector2D, Vector2D>();
31  }
32 
33  Extensive convert_conserved_to_extensive(const Conserved& conserved, const pair<ComputationalCell, ComputationalCell>& cells)
34  {
35  Extensive res;
36  res.mass = conserved.Mass;
37  res.momentum = conserved.Momentum;
38  res.energy = conserved.Energy;
39  res.tracers.resize(cells.first.tracers.size());
40  size_t N = res.tracers.size();
41  for (size_t i = 0; i < N; ++i)
42  res.tracers[i] = conserved.Mass*(conserved.Mass>0 ? cells.first.tracers[i] : cells.second.tracers[i]);
43  return res;
44  }
45 
46  void choose_action
47  (const Edge& edge,
48  const Tessellation& tess,
49  const vector<ComputationalCell>& cells,
50  const EquationOfState& eos,
51  const Vector2D& edge_velocity,
52  const vector<pair<const ConditionActionSequence::Condition*, const ConditionActionSequence::Action*> >& sequence,
53  const vector<pair<const ConditionActionSequence::Condition*, const ConditionActionSequence2::Action2*> >& sequence2,
54  pair<ComputationalCell,ComputationalCell> const& edge_values,Extensive &res,double time,
55  TracerStickerNames const& tracerstickernames,size_t index)
56  {
57  for (size_t i = 0; i<sequence.size(); ++i)
58  {
59  const pair<bool, bool> flag_aux = (*sequence[i].first)
60  (edge, tess, cells,tracerstickernames);
61  if (flag_aux.first)
62  {
63  (*sequence[i].second)(edge, tess, edge_velocity, cells, eos, flag_aux.second,res,time,tracerstickernames);
64  return;
65  }
66  }
67  for (size_t i = 0; i<sequence2.size(); ++i)
68  {
69  const pair<bool, bool> flag_aux = (*sequence2[i].first)
70  (edge, tess, cells,tracerstickernames);
71  if (flag_aux.first)
72  {
73  (*sequence2[i].second)(edge,index, tess, edge_velocity, cells, eos, flag_aux.second, edge_values,res,time,
74  tracerstickernames);
75  return;
76  }
77  }
78  throw UniversalError("Error in ConditionActionSequence");
79  }
80 }
81 
82 vector<Extensive> ConditionActionSequence2::operator()
83 (const Tessellation& tess,
84  const vector<Vector2D>& edge_velocities,
85  const vector<ComputationalCell>& cells,
86  const vector<Extensive>& extensives,
87  const CacheData& cd,
88  const EquationOfState& eos,
89  const double time,
90  const double /*dt*/,
91  TracerStickerNames const& tracerstickernames) const
92 {
93  for (size_t i = 0; i < sequence2_.size(); ++i)
94  sequence2_[i].second->Reset();
95  edge_values_.resize(static_cast<size_t>(tess.GetTotalSidesNumber()),
96  pair<ComputationalCell, ComputationalCell>(cells[0], cells[0]));
97  interp_.operator()(tess, cells, time,edge_values_,tracerstickernames,cd);
98  vector<Extensive> res(tess.getAllEdges().size(), extensives[0]);
99  for (size_t i = 0; i < tess.getAllEdges().size(); ++i)
100  {
101  try {
102  choose_action
103  (tess.getAllEdges()[i],
104  tess,
105  cells,
106  eos,
107  edge_velocities[i],
108  sequence_,
109  sequence2_,
110  edge_values_[i], res[i], time, tracerstickernames, i);
111  }
112  catch (UniversalError & eo)
113  {
114  size_t N0 = static_cast<size_t>(tess.GetEdge(static_cast<int>(i)).neighbors.first);
115  size_t N1 = static_cast<size_t>(tess.GetEdge(static_cast<int>(i)).neighbors.second);
116  eo.AddEntry("Error in conditionactionseq2, edge", static_cast<double>(i));
117  eo.AddEntry("density left", edge_values_[i].first.density);
118  eo.AddEntry("pressure left", edge_values_[i].first.pressure);
119  eo.AddEntry("original density left", cells[N0].density);
120  eo.AddEntry("original pressure left", cells[N0].pressure);
121  eo.AddEntry("density right", edge_values_[i].second.density);
122  eo.AddEntry("pressure right", edge_values_[i].second.pressure);
123  eo.AddEntry("original density right", cells[N1].density);
124  eo.AddEntry("original pressure right", cells[N1].pressure);
125  eo.AddEntry("Left neighbor", static_cast<double>(N0));
126  eo.AddEntry("Right neighbor", static_cast<double>(N1));
127  eo.AddEntry("Total number of cells", static_cast<double>(tess.GetPointNo()));
128 #ifdef RICH_MPI
129  int rank = 0;
130  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
131  eo.AddEntry("Rank", static_cast<double>(rank));
132 #endif
133  throw;
134  }
135  }
136  return res;
137 }
138 
139 
140 ConditionActionSequence2::Action2::~Action2(void) {}
141 
143  rs_(rs) {}
144 
145 namespace
146 {
147  void conserved_to_extensive
148  (const Conserved& c, const ComputationalCell& cell,Extensive &res)
149  {
150  res.mass = c.Mass;
151  res.momentum = c.Momentum;
152  res.energy = c.Energy;
153  res.tracers.resize(cell.tracers.size());
154  size_t N = cell.tracers.size();
155  for (size_t i = 0; i < N; ++i)
156  res.tracers[i] = cell.tracers[i] * c.Mass;
157  }
158 }
159 
160 void RegularFlux2::operator()
161 (const Edge& edge,
162  const size_t /*index*/,
163  const Tessellation& tess,
164  const Vector2D& edge_velocity,
165  const vector<ComputationalCell>& /*cells*/,
166  const EquationOfState& eos,
167  const bool /*aux*/,
168  pair<ComputationalCell,ComputationalCell> const& edge_values,
169  Extensive &res,double /*time*/,
170  TracerStickerNames const& tracerstickernames) const
171 {
172  const Vector2D n = normalize
173  (tess.GetMeshPoint(edge.neighbors.second) -
174  tess.GetMeshPoint(edge.neighbors.first));
175  const Vector2D p(n.y, -n.x);
176  const double v =
177  ScalarProd(n, edge_velocity);
179  (rs_,
181  (edge_values.first, eos,tracerstickernames),
183  (edge_values.second, eos,tracerstickernames),
184  v, n, p);
185  conserved_to_extensive(c,c.Mass>0 ? edge_values.first : edge_values.second,res);
186 }
187 
189 (const RiemannSolver& rs) :
190  rs_(rs) {}
191 
192 namespace
193 {
194  pair<Primitive, Primitive> rigid_wall_states
195  (const Primitive& state,
196  const Vector2D& p,
197  const bool aux)
198  {
199  if (aux) {
200  const Primitive left = state;
201  const Primitive right = reflect(left, p);
202  return pair<Primitive, Primitive>(left, right);
203  }
204  else {
205  const Primitive right = state;
206  const Primitive left = reflect(right, p);
207  return pair<Primitive, Primitive>(left, right);
208  }
209  }
210 }
211 
212 void RigidWallFlux2::operator()
213 (const Edge& edge,
214  const size_t /*index*/,
215  const Tessellation& tess,
216  const Vector2D& /*edge_velocity*/,
217  const vector<ComputationalCell>& /*cells*/,
218  const EquationOfState& eos,
219  const bool aux,
220  pair<ComputationalCell,ComputationalCell> const& edge_values,
221  Extensive &res,double /*time*/,
222  TracerStickerNames const& tracerstickernames) const
223 {
224 #ifndef RICH_MPI
225  if (aux)
226  assert(edge.neighbors.first >= 0 && edge.neighbors.first<tess.GetPointNo());
227  else
228  assert(edge.neighbors.second >= 0 && edge.neighbors.second<tess.GetPointNo());
229 #endif
230  const Vector2D p = normalize
231  (edge.vertices.second - edge.vertices.first);
232  const Vector2D n =
233  normalize
235  (aux ?
236  edge.vertices.first - tess.GetMeshPoint(edge.neighbors.first) :
237  tess.GetMeshPoint(edge.neighbors.second) - edge.vertices.first,
238  p));
239  const double v = 0;
240  const pair<Primitive, Primitive> left_right =
241  rigid_wall_states
243  (aux ? edge_values.first :edge_values.second,eos,tracerstickernames),
244  p, aux);
246  (rs_,
247  left_right.first,
248  left_right.second,
249  v, n, p);
250  conserved_to_extensive(c,aux ? edge_values.first : edge_values.second,res);
251 }
252 
253 Ratchet::Ratchet(const RiemannSolver& rs,bool in) :
254  in_(in),
255  // wall_(RigidWallFlux2(rs)),
256  wall_(rs),
257  //free_(FreeFlowFlux(rs)) {}
258  free_(rs) {}
259 
260 
261 void Ratchet::operator()
262 (const Edge& edge,
263  const size_t index,
264  const Tessellation& tess,
265  const Vector2D& edge_velocity,
266  const vector<ComputationalCell>& cells,
267  const EquationOfState& eos,
268  const bool aux,
269  const pair<ComputationalCell, ComputationalCell> & edge_values,
270  Extensive &res,double time,
271  TracerStickerNames const& tracerstickernames) const
272 {
273  Vector2D n = aux ? tess.GetMeshPoint(edge.neighbors.second) - tess.GetMeshPoint(edge.neighbors.first) :
274  tess.GetMeshPoint(edge.neighbors.first) - tess.GetMeshPoint(edge.neighbors.second);
275  if (ScalarProd(n, cells[static_cast<size_t>(aux ? edge.neighbors.first : edge.neighbors.second)].velocity)*(2*static_cast<double>(in_)-1) < 0)
276  free_.operator()(edge,tess, edge_velocity, cells, eos, aux,res,time,tracerstickernames);
277  else
278  wall_.operator()(edge,index, tess, edge_velocity, cells, eos, aux,edge_values,res,time,tracerstickernames);
279 }
280 
282 (const LagrangianHLLC& rs,
283  const LagrangianHLLC& rs2,
284  const LagrangianFlux::LagrangianCriteria& criteria):
285  ws_(vector<double>()),
286  edge_vel_(vector<double>()),
287  Lag_calc_(vector<bool>()),
288  rs_(rs),
289  rs2_(rs2),
290  criteria_(criteria) {}
291 
292 void LagrangianFlux::Reset(void)const
293 {
294  ws_.assign(ws_.size(), 0);
295  edge_vel_.assign(edge_vel_.size(), 0);
296  Lag_calc_.assign(Lag_calc_.size(), false);
297 }
298 
299 void LagrangianFlux::operator()(const Edge& edge,const size_t index,const Tessellation& tess,const Vector2D& edge_velocity,const vector<ComputationalCell>& cells,
300  const EquationOfState& eos,const bool aux,const pair<ComputationalCell, ComputationalCell> & edge_values,Extensive &res, double time,
301  TracerStickerNames const& tracerstickernames) const
302 {
303  size_t N = static_cast<size_t>(tess.GetTotalSidesNumber());
304  ws_.resize(N,0.0);
305  edge_vel_.resize(N,0.0);
306  Lag_calc_.resize(N, false);
307  const pair<Vector2D, Vector2D> p_n = calc_parallel_normal(tess, edge);
308  const double speed = ScalarProd(p_n.second, edge_velocity) / abs(p_n.second);
309  const Primitive p_left =convert_to_primitive(edge_values.first, eos, tracerstickernames);
310  const Primitive p_right =convert_to_primitive(edge_values.second, eos, tracerstickernames);
311  if (criteria_(edge, index, tess, edge_velocity, cells, eos, aux, edge_values, time, tracerstickernames))
312  {
313  res = convert_conserved_to_extensive(rotate_solve_rotate_back(rs_, p_left, p_right, speed, p_n.second, p_n.first), edge_values);
314  ws_[index] = rs_.energy;
315  Lag_calc_[index] = true;
316  }
317  else
318  {
319  res = convert_conserved_to_extensive(rotate_solve_rotate_back(rs2_, p_left, p_right,speed, p_n.second, p_n.first), edge_values);
320  ws_[index] = 0;
321  }
322  edge_vel_[index] = speed;
323 }
324 
325 LagrangianFlux::LagrangianCriteria::~LagrangianCriteria() {}
326 
327 WallsMassFlux::WallsMassFlux() {}
328 
330  const size_t /*index*/,
331  const Tessellation& tess,
332  const Vector2D& /*edge_velocity*/,
333  const vector<ComputationalCell>& /*cells*/,
334  const EquationOfState& /*eos*/,
335  const bool /*aux*/,
336  const pair<ComputationalCell, ComputationalCell> & /*edge_values*/,
337  double /*time*/,
338  TracerStickerNames const& /*tracerstickernames*/) const
339 {
340  return !(tess.GetOriginalIndex(edge.neighbors.first) == tess.GetOriginalIndex(edge.neighbors.second));
341 }
342 
343 
Set of conserved variables (extensive)
Extensive variables.
Definition: extensive.hpp:18
Vector2D Momentum
Momentum.
LagrangianHLLC Riemann solver for an Eulerian grid.
Vector2D remove_parallel_component(const Vector2D &v, const Vector2D &p)
Remove parallel component of a vector.
Vector2D Parallel(Edge const &edge)
Calculates a unit vector parallel to an edge.
Definition: Edge.cpp:83
Spatial reconstruction of the primitive functions.
Abstract class for tessellation.
Condition on when to apply mass transfer fix.
virtual int GetPointNo(void) const =0
Get Total number of mesh generating points.
virtual int GetOriginalIndex(int point) const
Returns the original index of the duplicated point.
Definition: tessellation.cpp:5
Vector2D momentum
momentum, in relativity it is = rho*h*gamma*v
Definition: extensive.hpp:31
Container for error reports.
Primitive convert_to_primitive(const ComputationalCell &cell, const EquationOfState &eos, TracerStickerNames const &tracerstickernames)
Converts computational cell to primitive variables.
RegularFlux2(const RiemannSolver &rs)
Class constructor.
double mass
rest mass times gamma
Definition: extensive.hpp:25
Interface between two cells.
Definition: Edge.hpp:13
virtual Vector2D GetMeshPoint(int index) const =0
Returns Position of mesh generating point.
double Energy
Total energy (kinetic + thermal)
double energy
energy, in relativity it is = rho*h*gamma^2-P-rho
Definition: extensive.hpp:28
double y
Component in the y direction.
Definition: geometry.hpp:48
tvector tracers
Tracers (can transfer from one cell to another)
Simple flux calculator.
virtual Edge const & GetEdge(int index) const =0
Returns edge (interface between cells)
Base class for Riemann solver.
Ratchet(const RiemannSolver &rs, const bool in)
Class constructor.
tvector tracers
tracers
Definition: extensive.hpp:34
Conserved rotate_solve_rotate_back(const RiemannSolver &rs, const Primitive &left, const Primitive &right, const double velocity, const Vector2D &n, const Vector2D &p)
Rotates, solve riemann problem and rotates results back.
RigidWallFlux2(const RiemannSolver &rs)
Class constructor.
double ScalarProd(Vector3D const &v1, Vector3D const &v2)
Scalar product of two vectors.
Definition: Vector3D.cpp:185
virtual const vector< Edge > & getAllEdges(void) const =0
Returns reference to the list of all edges.
Base class for equation of state.
std::pair< Vector2D, Vector2D > vertices
Points at the ends of the edge.
Definition: Edge.hpp:18
Primitive reflect(const Primitive &p, const Vector2D &axis)
Reflects velocity about axis.
Container for cache data.
Definition: cache_data.hpp:14
Vector2D normalize(const Vector2D &v)
Normalized a vector.
Definition: geometry.cpp:158
Class for keeping the names of the tracers and stickers.
virtual int GetTotalSidesNumber(void) const =0
Returns the total number of faces.
std::pair< int, int > neighbors
Neighboring cells.
Definition: Edge.hpp:21
LagrangianFlux(const LagrangianHLLC &rs, const LagrangianHLLC &rs2, LagrangianCriteria const &criteria)
Class constructor.
double abs(Vector3D const &v)
Norm of a vector.
Definition: Vector3D.cpp:44
void AddEntry(std::string const &field, double value)
Adds an entry to the list.
Primitive hydrodynamic variables.
ConditionActionSequence2(const vector< pair< const ConditionActionSequence::Condition *, const ConditionActionSequence::Action *> > &sequence, const vector< pair< const ConditionActionSequence::Condition *, const ConditionActionSequence2::Action2 *> > &sequence2, SpatialReconstruction const &interp)
Class constructor.
2D Mathematical vector
Definition: geometry.hpp:15
void Reset(void) const
Resets the internal variables.
bool operator()(const Edge &edge, const size_t index, const Tessellation &tess, const Vector2D &edge_velocity, const vector< ComputationalCell > &cells, const EquationOfState &eos, const bool aux, const pair< ComputationalCell, ComputationalCell > &edge_values, double time, TracerStickerNames const &tracerstickernames) const
Criteria for calculating mass flux or not.
double x
Component in the x direction.
Definition: geometry.hpp:45
void operator()(const Edge &edge, const size_t index, const Tessellation &tess, const Vector2D &edge_velocity, const vector< ComputationalCell > &cells, const EquationOfState &eos, const bool aux, const pair< ComputationalCell, ComputationalCell > &edge_values, Extensive &res, double time, TracerStickerNames const &tracerstickernames) const
Calculates flux.
Computational cell.