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