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