www.mooseframework.org
SlopeLimitingBase.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "SlopeLimitingBase.h"
9 #include <unistd.h>
10 #include "libmesh/parallel.h"
11 #include "libmesh/parallel_algebra.h"
12 
13 // Static mutex definition
14 Threads::spin_mutex SlopeLimitingBase::_mutex;
15 
16 template <>
17 InputParameters
19 {
20  InputParameters params = validParams<ElementLoopUserObject>();
21  params.addClassDescription(
22  "Base class for slope limiting to limit the slopes of cell average variables.");
23 
24  params.addParam<bool>("include_bc", true, "Indicate whether to include bc, default = true");
25 
26  params.addRequiredParam<UserObjectName>("slope_reconstruction",
27  "Name for slope reconstruction user object");
28 
29  return params;
30 }
31 
32 SlopeLimitingBase::SlopeLimitingBase(const InputParameters & parameters)
33  : ElementLoopUserObject(parameters),
34  _include_bc(getParam<bool>("include_bc")),
35  _rslope(getUserObject<SlopeReconstructionBase>("slope_reconstruction")),
36  _q_point_face(_assembly.qPointsFace()),
37  _qrule_face(_assembly.qRuleFace()),
38  _JxW_face(_assembly.JxWFace()),
39  _normals_face(_assembly.normals()),
40  _side(_assembly.side()),
41  _side_elem(_assembly.sideElem()),
42  _side_volume(_assembly.sideElemVolume()),
43  _neighbor_elem(_assembly.neighbor())
44 {
45 }
46 
47 void
49 {
51 
52  _lslope.clear();
53 }
54 
55 const std::vector<RealGradient> &
56 SlopeLimitingBase::getElementSlope(dof_id_type elementid) const
57 {
58  Threads::spin_mutex::scoped_lock lock(_mutex);
59  std::map<dof_id_type, std::vector<RealGradient>>::const_iterator pos = _lslope.find(elementid);
60 
61  if (pos == _lslope.end())
62  mooseError("Limited slope is not cached for element id '", elementid, "' in ", __FUNCTION__);
63 
64  return pos->second;
65 }
66 
67 void
69 {
70  dof_id_type _elementID = _current_elem->id();
71 
72  _lslope[_elementID] = limitElementSlope();
73 }
74 
75 void
76 SlopeLimitingBase::serialize(std::string & serialized_buffer)
77 {
78  std::ostringstream oss;
79 
80  // First store the number of elements to send
81  unsigned int size = _interface_elem_ids.size();
82  oss.write((char *)&size, sizeof(size));
83 
84  for (auto it = _interface_elem_ids.begin(); it != _interface_elem_ids.end(); ++it)
85  {
86  storeHelper(oss, *it, this);
87  storeHelper(oss, _lslope[*it], this);
88  }
89 
90  // Populate the passed in string pointer with the string stream's buffer contents
91  serialized_buffer.assign(oss.str());
92 }
93 
94 void
95 SlopeLimitingBase::deserialize(std::vector<std::string> & serialized_buffers)
96 {
97  // The input string stream used for deserialization
98  std::istringstream iss;
99 
100  mooseAssert(serialized_buffers.size() == _app.n_processors(),
101  "Unexpected size of serialized_buffers: " << serialized_buffers.size());
102 
103  for (auto rank = decltype(_app.n_processors())(0); rank < serialized_buffers.size(); ++rank)
104  {
105  if (rank == processor_id())
106  continue;
107 
108  iss.str(serialized_buffers[rank]); // populate the stream with a new buffer
109  iss.clear(); // reset the string stream state
110 
111  // Load the communicated data into all of the other processors' slots
112 
113  unsigned int size = 0;
114  iss.read((char *)&size, sizeof(size));
115 
116  for (unsigned int i = 0; i < size; i++)
117  {
118  dof_id_type key;
119  loadHelper(iss, key, this);
120 
121  std::vector<RealGradient> value;
122  loadHelper(iss, value, this);
123 
124  // merge the data we received from other procs
125  _lslope.insert(std::pair<dof_id_type, std::vector<RealGradient>>(key, value));
126  }
127  }
128 }
129 
130 void
132 {
134 
135  if (_app.n_processors() > 1)
136  {
137  std::vector<std::string> send_buffers(1);
138  std::vector<std::string> recv_buffers;
139 
140  recv_buffers.reserve(_app.n_processors());
141  serialize(send_buffers[0]);
142  comm().allgather_packed_range((void *)(nullptr),
143  send_buffers.begin(),
144  send_buffers.end(),
145  std::back_inserter(recv_buffers));
146  deserialize(recv_buffers);
147  }
148 }
virtual void serialize(std::string &serialized_buffer)
A base class that loops over elements and do things.
virtual void finalize()
InputParameters validParams< SlopeLimitingBase >()
InputParameters validParams< ElementLoopUserObject >()
virtual void computeElement()
virtual const std::vector< RealGradient > & getElementSlope(dof_id_type elementid) const
accessor function call
static Threads::spin_mutex _mutex
SlopeLimitingBase(const InputParameters &parameters)
Base class for piecewise linear slope reconstruction to get the slopes of element average variables...
virtual void deserialize(std::vector< std::string > &serialized_buffers)
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 void initialize()
virtual std::vector< RealGradient > limitElementSlope() const =0
compute the slope of the cell
std::map< dof_id_type, std::vector< RealGradient > > _lslope
store the updated slopes into this map indexed by element ID