https://mooseframework.inl.gov
SlopeReconstructionBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 
12 // Static mutex definition
13 Threads::spin_mutex SlopeReconstructionBase::_mutex;
14 
17 {
19  params.addClassDescription("Base class for piecewise linear slope reconstruction to get the "
20  "slopes of element average variables.");
21  return params;
22 }
23 
25  : ElementLoopUserObject(parameters),
26  _rslope(declareRestartableData<std::map<dof_id_type, std::vector<RealGradient>>>(
27  "reconstructed_slopes")),
28  _avars(declareRestartableData<std::map<dof_id_type, std::vector<Real>>>("avg_var_values")),
29  _bnd_avars(
30  declareRestartableData<std::map<std::pair<dof_id_type, unsigned int>, std::vector<Real>>>(
31  "avg_bnd_var_values")),
32  _side_centroid(declareRestartableData<std::map<std::pair<dof_id_type, dof_id_type>, Point>>(
33  "side_centroid")),
34  _bnd_side_centroid(
35  declareRestartableData<std::map<std::pair<dof_id_type, unsigned int>, Point>>(
36  "bnd_side_centroid")),
37  _side_area(
38  declareRestartableData<std::map<std::pair<dof_id_type, dof_id_type>, Real>>("side_area")),
39  _bnd_side_area(declareRestartableData<std::map<std::pair<dof_id_type, unsigned int>, Real>>(
40  "bnd_side_area")),
41  _side_normal(declareRestartableData<std::map<std::pair<dof_id_type, dof_id_type>, Point>>(
42  "side_normal")),
43  _bnd_side_normal(declareRestartableData<std::map<std::pair<dof_id_type, unsigned int>, Point>>(
44  "bnd_side_normal")),
45  _q_point_face(_assembly.qPointsFace()),
46  _qrule_face(_assembly.qRuleFace()),
47  _JxW_face(_assembly.JxWFace()),
48  _normals_face(_assembly.normals()),
49  _side(_assembly.side()),
50  _side_elem(_assembly.sideElem()),
51  _side_volume(_assembly.sideElemVolume()),
52  _neighbor_elem(_assembly.neighbor()),
53  _side_geoinfo_cached(false)
54 {
55 }
56 
57 void
59 {
61 
62  _rslope.clear();
63  _avars.clear();
64 }
65 
66 void
68 {
70 
71  if (_app.n_processors() > 1)
72  {
73  _side_geoinfo_cached = true;
74 
75  std::vector<std::string> send_buffers(1);
76  std::vector<std::string> recv_buffers;
77 
78  recv_buffers.reserve(_app.n_processors());
79  serialize(send_buffers[0]);
80  comm().allgather_packed_range((void *)(nullptr),
81  send_buffers.begin(),
82  send_buffers.end(),
83  std::back_inserter(recv_buffers));
84  deserialize(recv_buffers);
85  }
86 }
87 
88 void
90 {
92 
93  _side_geoinfo_cached = false;
94  _side_centroid.clear();
95  _bnd_side_centroid.clear();
96  _side_normal.clear();
97  _bnd_side_normal.clear();
98  _side_area.clear();
99  _bnd_side_area.clear();
100 }
101 
102 const std::vector<RealGradient> &
104 {
105  Threads::spin_mutex::scoped_lock lock(_mutex);
106  std::map<dof_id_type, std::vector<RealGradient>>::const_iterator pos = _rslope.find(elementid);
107 
108  if (pos == _rslope.end())
109  mooseError(
110  "Reconstructed slope is not cached for element id '", elementid, "' in ", __FUNCTION__);
111 
112  return pos->second;
113 }
114 
115 const std::vector<Real> &
117 {
118  Threads::spin_mutex::scoped_lock lock(_mutex);
119  std::map<dof_id_type, std::vector<Real>>::const_iterator pos = _avars.find(elementid);
120 
121  if (pos == _avars.end())
122  mooseError("Average variable values are not cached for element id '",
123  elementid,
124  "' in ",
125  __FUNCTION__);
126 
127  return pos->second;
128 }
129 
130 const std::vector<Real> &
132 {
133  Threads::spin_mutex::scoped_lock lock(_mutex);
134  std::map<std::pair<dof_id_type, unsigned int>, std::vector<Real>>::const_iterator pos =
135  _bnd_avars.find(std::pair<dof_id_type, unsigned int>(elementid, side));
136 
137  if (pos == _bnd_avars.end())
138  mooseError("Average variable values are not cached for element id '",
139  elementid,
140  "' and side '",
141  side,
142  "' in ",
143  __FUNCTION__);
144 
145  return pos->second;
146 }
147 
148 const Point &
150 {
151  Threads::spin_mutex::scoped_lock lock(_mutex);
152  std::map<std::pair<dof_id_type, dof_id_type>, Point>::const_iterator pos =
153  _side_centroid.find(std::pair<dof_id_type, dof_id_type>(elementid, neighborid));
154 
155  if (pos == _side_centroid.end())
156  mooseError("Side centroid values are not cached for element id '",
157  elementid,
158  "' and neighbor id '",
159  neighborid,
160  "' in ",
161  __FUNCTION__);
162 
163  return pos->second;
164 }
165 
166 const Point &
168 {
169  Threads::spin_mutex::scoped_lock lock(_mutex);
170  std::map<std::pair<dof_id_type, unsigned int>, Point>::const_iterator pos =
171  _bnd_side_centroid.find(std::pair<dof_id_type, unsigned int>(elementid, side));
172 
173  if (pos == _bnd_side_centroid.end())
174  mooseError("Boundary side centroid values are not cached for element id '",
175  elementid,
176  "' and side '",
177  side,
178  "' in ",
179  __FUNCTION__);
180 
181  return pos->second;
182 }
183 
184 const Point &
186 {
187  Threads::spin_mutex::scoped_lock lock(_mutex);
188  std::map<std::pair<dof_id_type, dof_id_type>, Point>::const_iterator pos =
189  _side_normal.find(std::pair<dof_id_type, dof_id_type>(elementid, neighborid));
190 
191  if (pos == _side_normal.end())
192  mooseError("Side normal values are not cached for element id '",
193  elementid,
194  "' and neighbor id '",
195  neighborid,
196  "' in ",
197  __FUNCTION__);
198 
199  return pos->second;
200 }
201 
202 const Point &
204 {
205  Threads::spin_mutex::scoped_lock lock(_mutex);
206  std::map<std::pair<dof_id_type, unsigned int>, Point>::const_iterator pos =
207  _bnd_side_normal.find(std::pair<dof_id_type, unsigned int>(elementid, side));
208 
209  if (pos == _bnd_side_normal.end())
210  mooseError("Boundary side normal values are not cached for element id '",
211  elementid,
212  "' and side '",
213  side,
214  "' in ",
215  __FUNCTION__);
216 
217  return pos->second;
218 }
219 
220 const Real &
222 {
223  Threads::spin_mutex::scoped_lock lock(_mutex);
224  std::map<std::pair<dof_id_type, dof_id_type>, Real>::const_iterator pos =
225  _side_area.find(std::pair<dof_id_type, dof_id_type>(elementid, neighborid));
226 
227  if (pos == _side_area.end())
228  mooseError("Side area values are not cached for element id '",
229  elementid,
230  "' and neighbor id '",
231  neighborid,
232  "' in ",
233  __FUNCTION__);
234 
235  return pos->second;
236 }
237 
238 const Real &
240 {
241  Threads::spin_mutex::scoped_lock lock(_mutex);
242  std::map<std::pair<dof_id_type, unsigned int>, Real>::const_iterator pos =
243  _bnd_side_area.find(std::pair<dof_id_type, unsigned int>(elementid, side));
244 
245  if (pos == _bnd_side_area.end())
246  mooseError("Boundary side area values are not cached for element id '",
247  elementid,
248  "' and side '",
249  side,
250  "' in ",
251  __FUNCTION__);
252 
253  return pos->second;
254 }
255 
256 void
258 {
260 }
261 
262 void
263 SlopeReconstructionBase::serialize(std::string & serialized_buffer)
264 {
265  std::ostringstream oss;
266 
267  // First store the number of elements to send
268  unsigned int size = _interface_elem_ids.size();
269  oss.write((char *)&size, sizeof(size));
270 
271  for (auto it = _interface_elem_ids.begin(); it != _interface_elem_ids.end(); ++it)
272  {
273  storeHelper(oss, *it, this);
274  storeHelper(oss, _rslope[*it], this);
275  }
276 
277  // Populate the passed in string pointer with the string stream's buffer contents
278  serialized_buffer.assign(oss.str());
279 }
280 
281 void
282 SlopeReconstructionBase::deserialize(std::vector<std::string> & serialized_buffers)
283 {
284  // The input string stream used for deserialization
285  std::istringstream iss;
286 
287  mooseAssert(serialized_buffers.size() == _app.n_processors(),
288  "Unexpected size of serialized_buffers: " << serialized_buffers.size());
289 
290  for (auto rank = decltype(_app.n_processors())(0); rank < serialized_buffers.size(); ++rank)
291  {
292  if (rank == processor_id())
293  continue;
294 
295  iss.str(serialized_buffers[rank]); // populate the stream with a new buffer
296  iss.clear(); // reset the string stream state
297 
298  // Load the communicated data into all of the other processors' slots
299 
300  unsigned int size = 0;
301  iss.read((char *)&size, sizeof(size));
302 
303  for (unsigned int i = 0; i < size; i++)
304  {
305  dof_id_type key;
306  loadHelper(iss, key, this);
307 
308  std::vector<RealGradient> value;
309  loadHelper(iss, value, this);
310 
311  // merge the data we received from other procs
312  _rslope.insert(std::pair<dof_id_type, std::vector<RealGradient>>(key, value));
313  }
314  }
315 }
std::map< dof_id_type, std::vector< Real > > & _avars
store the average variable values into this map indexed by element ID
virtual void serialize(std::string &serialized_buffer)
A base class that loops over elements and do things.
virtual const std::vector< Real > & getBoundaryAverageValue(dof_id_type elementid, unsigned int side) const
accessor function call to get boundary average variable values
virtual void reconstructElementSlope()=0
compute the slope of the cell
const Parallel::Communicator & comm() const
void allgather_packed_range(Context *context, Iter range_begin, const Iter range_end, OutputIter out, std::size_t approx_buffer_size=1000000) const
static InputParameters validParams()
std::map< std::pair< dof_id_type, unsigned int >, std::vector< Real > > & _bnd_avars
store the boundary average variable values into this map indexed by pair of element ID and local side...
static InputParameters validParams()
std::map< std::pair< dof_id_type, unsigned int >, Point > & _bnd_side_normal
store the boundary side normal into this map indexed by pair of element ID and local side ID ...
void storeHelper(std::ostream &stream, P &data, void *context)
virtual const std::vector< RealGradient > & getElementSlope(dof_id_type elementid) const
accessor function call to get element slope values
std::map< std::pair< dof_id_type, dof_id_type >, Point > & _side_centroid
store the side centroid into this map indexed by pair of element ID and neighbor ID ...
static Threads::spin_mutex _mutex
processor_id_type n_processors() const
virtual void deserialize(std::vector< std::string > &serialized_buffers)
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
virtual const std::vector< Real > & getElementAverageValue(dof_id_type elementid) const
accessor function call to get element average variable values
virtual const Real & getBoundarySideArea(dof_id_type elementid, unsigned int side) const
accessor function call to get cached boundary side area
SlopeReconstructionBase(const InputParameters &parameters)
std::map< std::pair< dof_id_type, dof_id_type >, Real > & _side_area
store the side area into this map indexed by pair of element ID and neighbor ID
virtual const Point & getSideNormal(dof_id_type elementid, dof_id_type neighborid) const
accessor function call to get cached internal side normal
virtual const Point & getBoundarySideCentroid(dof_id_type elementid, unsigned int side) const
accessor function call to get cached boundary side centroid
virtual const Real & getSideArea(dof_id_type elementid, dof_id_type neighborid) const
accessor function call to get cached internal side area
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual const Point & getBoundarySideNormal(dof_id_type elementid, unsigned int side) const
accessor function call to get cached boundary side centroid
std::set< dof_id_type > _interface_elem_ids
List of element IDs that are on the processor boundary and need to be send to other processors...
virtual const Point & getSideCentroid(dof_id_type elementid, dof_id_type neighborid) const
accessor function call to get cached internal side centroid
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
std::map< std::pair< dof_id_type, unsigned int >, Point > & _bnd_side_centroid
store the boundary side centroid into this map indexed by pair of element ID and local side ID ...
processor_id_type processor_id() const
bool _side_geoinfo_cached
flag to indicated if side geometry info is cached
void loadHelper(std::istream &stream, P &data, void *context)
void ErrorVector unsigned int
std::map< std::pair< dof_id_type, unsigned int >, Real > & _bnd_side_area
store the boundary side area into this map indexed by pair of element ID and local side ID ...
uint8_t dof_id_type
std::map< std::pair< dof_id_type, dof_id_type >, Point > & _side_normal
store the side normal into this map indexed by pair of element ID and neighbor ID ...
std::map< dof_id_type, std::vector< RealGradient > > & _rslope
store the reconstructed slopes into this map indexed by element ID