https://mooseframework.inl.gov
SamplerBase.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 
10 #include "SamplerBase.h"
11 
12 // MOOSE includes
13 #include "IndirectSort.h"
14 #include "InputParameters.h"
15 #include "MooseEnum.h"
16 #include "MooseError.h"
17 #include "VectorPostprocessor.h"
18 #include "MooseVariableFieldBase.h"
20 
21 #include "libmesh/point.h"
22 
25 {
27 
28  MooseEnum sort_options("x y z id");
29  params.addRequiredParam<MooseEnum>("sort_by", sort_options, "What to sort the samples by");
30 
31  // The value from this VPP is naturally already on every processor
32  // TODO: Make this not the case! See #11415
33  params.set<bool>("_auto_broadcast") = false;
34 
35  return params;
36 }
37 
39  VectorPostprocessor * vpp,
41  : _sampler_params(parameters),
42  _vpp(vpp),
43  _comm(comm),
44  _sort_by(parameters.get<MooseEnum>("sort_by")),
45  _x(vpp->declareVector("x")),
46  _y(vpp->declareVector("y")),
47  _z(vpp->declareVector("z")),
48  _id(vpp->declareVector("id"))
49 {
50 }
51 
52 void
53 SamplerBase::setupVariables(const std::vector<std::string> & variable_names)
54 {
55  _variable_names = variable_names;
56  _values.reserve(variable_names.size());
57 
58  for (const auto & variable_name : variable_names)
59  _values.push_back(&_vpp->declareVector(variable_name));
60 }
61 
62 void
63 SamplerBase::addSample(const Point & p, const Real & id, const std::vector<Real> & values)
64 {
65  _x.push_back(p(0));
66  _y.push_back(p(1));
67  _z.push_back(p(2));
68 
69  _id.push_back(id);
70 
71  mooseAssert(values.size() == _variable_names.size(), "Mismatch of variable names to vector size");
72  for (MooseIndex(values) i = 0; i < values.size(); ++i)
73  _values[i]->emplace_back(values[i]);
74 }
75 
76 void
78 {
79  // Don't reset the vectors if we want to retain history
80  if (_vpp->containsCompleteHistory() && _comm.rank() == 0)
81  return;
82 
83  _x.clear();
84  _y.clear();
85  _z.clear();
86  _id.clear();
87 
88  std::for_each(
89  _values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
90 }
91 
92 void
94  const std::string & var_param_name) const
95 {
96  // A pointer to a MooseVariableFieldBase should never be SCALAR
97  mooseAssert(var_ptr->feType().family != SCALAR,
98  "Scalar variable '" + var_ptr->name() + "' cannot be sampled.");
99  mooseAssert(dynamic_cast<const MooseObject *>(_vpp), "Should have succeeded");
100  if (var_ptr->isVector())
101  dynamic_cast<const MooseObject *>(_vpp)->paramError(
102  var_param_name,
103  "The variable '",
104  var_ptr->name(),
105  "' is a vector variable. Sampling those is not currently supported in the "
106  "framework. It may be supported using a dedicated object in your application. Use "
107  "'VectorVariableComponentAux' auxkernel to copy those values into a regular field "
108  "variable");
109  if (var_ptr->isArray())
110  dynamic_cast<const MooseObject *>(_vpp)->paramError(
111  var_param_name,
112  "The variable '",
113  var_ptr->name(),
114  "' is an array variable. Sampling those is not currently supported in the "
115  "framework. It may be supported using a dedicated object in your application. Use "
116  "'ArrayVariableComponent' auxkernel to copy those values into a regular field variable");
117 }
118 
119 void
121 {
127  constexpr auto NUM_ID_VECTORS = 4;
128 
129  std::vector<VectorPostprocessorValue *> vec_ptrs;
130  vec_ptrs.reserve(_values.size() + NUM_ID_VECTORS);
131  // Initialize the pointer vector with the position and ID vectors
132  vec_ptrs = {{&_x, &_y, &_z, &_id}};
133  // Now extend the vector by all the remaining values vector before processing
134  vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
135 
136  // Gather up each of the partial vectors
137  for (auto vec_ptr : vec_ptrs)
138  _comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);
139 
140  // Now create an index vector by using an indirect sort
141  std::vector<std::size_t> sorted_indices;
142  Moose::indirectSort(vec_ptrs[_sort_by]->begin(), vec_ptrs[_sort_by]->end(), sorted_indices);
143 
151  // This vector is used as temp storage to sort each of the remaining vectors according to the
152  // first
153  auto vector_length = sorted_indices.size();
154  VectorPostprocessorValue tmp_vector(vector_length);
155 
156 #ifndef NDEBUG
157  for (const auto vec_ptr : vec_ptrs)
158  if (vec_ptr->size() != vector_length)
159  mooseError("Vector length mismatch");
160 #endif
161 
162  // Sort each of the vectors using the same sorted indices
163  for (auto & vec_ptr : vec_ptrs)
164  {
165  for (MooseIndex(sorted_indices) j = 0; j < sorted_indices.size(); ++j)
166  tmp_vector[j] = (*vec_ptr)[sorted_indices[j]];
167 
168  // Swap vector storage with sorted vector
169  vec_ptr->swap(tmp_vector);
170  }
171 }
172 
173 void
175 {
176  _x.insert(_x.end(), y._x.begin(), y._x.end());
177  _y.insert(_y.end(), y._y.begin(), y._y.end());
178  _z.insert(_z.end(), y._z.begin(), y._z.end());
179 
180  _id.insert(_id.end(), y._id.begin(), y._id.end());
181 
182  for (MooseIndex(_variable_names) i = 0; i < _variable_names.size(); i++)
183  _values[i]->insert(_values[i]->end(), y._values[i]->begin(), y._values[i]->end());
184 }
Base class for VectorPostprocessors that need to do "sampling" of values in the domain.
Definition: SamplerBase.h:36
virtual void initialize()
Initialize the datastructures.
Definition: SamplerBase.C:77
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
const libMesh::FEType & feType() const
Get the type of finite element object.
SCALAR
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:68
const std::string & name() const override
Get the variable name.
processor_id_type rank() const
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:112
This class provides an interface for common operations on field variables of both FE and FV types wit...
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:120
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:115
virtual bool isVector() const =0
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:127
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:53
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:106
virtual void threadJoin(const SamplerBase &y)
Join the values.
Definition: SamplerBase.C:174
virtual bool isArray() const
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:118
static InputParameters validParams()
Definition: SamplerBase.C:24
SamplerBase(const InputParameters &parameters, VectorPostprocessor *vpp, const libMesh::Parallel::Communicator &comm)
Definition: SamplerBase.C:38
virtual void finalize()
Finalize the values.
Definition: SamplerBase.C:120
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:203
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:125
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:63
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:122
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:109
Base class for Postprocessors that produce a vector of values.
void checkForStandardFieldVariableType(const MooseVariableFieldBase *const var_ptr, const std::string &var_param_name="variable") const
Checks whether the passed variable pointer corresponds to a regular single-valued field variable...
Definition: SamplerBase.C:93