www.mooseframework.org
SamplerBase.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "SamplerBase.h"
16 
17 // MOOSE includes
18 #include "IndirectSort.h"
19 #include "InputParameters.h"
20 #include "MooseEnum.h"
21 #include "MooseError.h"
22 #include "VectorPostprocessor.h"
23 
24 template <>
27 {
29 
30  MooseEnum sort_options("x y z id");
31  params.addRequiredParam<MooseEnum>("sort_by", sort_options, "What to sort the samples by");
32 
33  return params;
34 }
35 
37  VectorPostprocessor * vpp,
38  const libMesh::Parallel::Communicator & comm)
39  : _sampler_params(parameters),
40  _vpp(vpp),
41  _comm(comm),
42  _sort_by(parameters.get<MooseEnum>("sort_by")),
43  _x(vpp->declareVector("x")),
44  _y(vpp->declareVector("y")),
45  _z(vpp->declareVector("z")),
46  _id(vpp->declareVector("id"))
47 {
48 }
49 
50 void
51 SamplerBase::setupVariables(const std::vector<std::string> & variable_names)
52 {
53  _variable_names = variable_names;
54  _values.reserve(variable_names.size());
55 
56  for (const auto & variable_name : variable_names)
57  _values.push_back(&_vpp->declareVector(variable_name));
58 }
59 
60 void
61 SamplerBase::addSample(const Point & p, const Real & id, const std::vector<Real> & values)
62 {
63  _x.push_back(p(0));
64  _y.push_back(p(1));
65  _z.push_back(p(2));
66 
67  _id.push_back(id);
68 
69  mooseAssert(values.size() == _variable_names.size(), "Mismatch of variable names to vector size");
70  for (auto i = beginIndex(values); i < values.size(); ++i)
71  _values[i]->emplace_back(values[i]);
72 }
73 
74 void
76 {
77  _x.clear();
78  _y.clear();
79  _z.clear();
80  _id.clear();
81 
82  std::for_each(
83  _values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
84 }
85 
86 void
88 {
94  constexpr auto NUM_ID_VECTORS = 4;
95 
96  std::vector<VectorPostprocessorValue *> vec_ptrs;
97  vec_ptrs.reserve(_values.size() + NUM_ID_VECTORS);
98  // Initialize the pointer vector with the position and ID vectors
99  vec_ptrs = {{&_x, &_y, &_z, &_id}};
100  // Now extend the vector by all the remaining values vector before processing
101  vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
102 
103  // Gather up each of the partial vectors
104  for (auto vec_ptr : vec_ptrs)
105  _comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);
106 
107  // Now create an index vector by using an indirect sort
108  std::vector<std::size_t> sorted_indices;
109  Moose::indirectSort(vec_ptrs[_sort_by]->begin(), vec_ptrs[_sort_by]->end(), sorted_indices);
110 
118  // This vector is used as temp storage to sort each of the remaining vectors according to the
119  // first
120  auto vector_length = sorted_indices.size();
121  VectorPostprocessorValue tmp_vector(vector_length);
122 
123 #ifndef NDEBUG
124  for (const auto vec_ptr : vec_ptrs)
125  if (vec_ptr->size() != vector_length)
126  mooseError("Vector length mismatch");
127 #endif
128 
129  // Sort each of the vectors using the same sorted indices
130  for (auto i = beginIndex(vec_ptrs); i < vec_ptrs.size(); ++i)
131  {
132  for (auto j = beginIndex(sorted_indices); j < sorted_indices.size(); ++j)
133  tmp_vector[j] = (*vec_ptrs[i])[sorted_indices[j]];
134 
135  // Swap vector storage with sorted vector
136  vec_ptrs[i]->swap(tmp_vector);
137  }
138 }
139 
140 void
142 {
143  _x.insert(_x.end(), y._x.begin(), y._x.end());
144  _y.insert(_y.end(), y._y.begin(), y._y.end());
145  _z.insert(_z.end(), y._z.begin(), y._z.end());
146 
147  _id.insert(_id.end(), y._id.begin(), y._id.end());
148 
149  for (unsigned int i = 0; i < _variable_names.size(); i++)
150  _values[i]->insert(_values[i]->end(), y._values[i]->begin(), y._values[i]->end());
151 }
Base class for VectorPostprocessors that need to do "sampling" of values in the domain.
Definition: SamplerBase.h:46
virtual void initialize()
Initialize the datastructures.
Definition: SamplerBase.C:75
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:74
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:111
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
InputParameters emptyInputParameters()
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:119
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:114
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:126
void setupVariables(const std::vector< std::string > &variable_names)
You MUST call this in the constructor of the child class and pass down the name of the variables...
Definition: SamplerBase.C:51
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:105
virtual void threadJoin(const SamplerBase &y)
Join the values.
Definition: SamplerBase.C:141
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:117
SamplerBase(const InputParameters &parameters, VectorPostprocessor *vpp, const libMesh::Parallel::Communicator &comm)
Definition: SamplerBase.C:36
virtual void finalize()
Finalize the values.
Definition: SamplerBase.C:87
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:74
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:124
virtual void addSample(const Point &p, const Real &id, const std::vector< Real > &values)
Call this with the value of every variable at each point you want to sample at.
Definition: SamplerBase.C:61
MPI_Comm comm
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:121
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:108
Base class for Postprocessors that produce a vector of values.
InputParameters validParams< SamplerBase >()
Definition: SamplerBase.C:26