https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
SamplerBase Class Reference

Base class for VectorPostprocessors that need to do "sampling" of values in the domain. More...

#include <SamplerBase.h>

Inheritance diagram for SamplerBase:
[legend]

Public Member Functions

 SamplerBase (const InputParameters &parameters, VectorPostprocessor *vpp, const libMesh::Parallel::Communicator &comm)
 
virtual ~SamplerBase ()=default
 

Static Public Member Functions

static InputParameters validParams ()
 

Protected Member Functions

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. More...
 
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. More...
 
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. More...
 
virtual void initialize ()
 Initialize the datastructures. More...
 
virtual void finalize ()
 Finalize the values. More...
 
virtual void threadJoin (const SamplerBase &y)
 Join the values. More...
 

Protected Attributes

const InputParameters_sampler_params
 The child params. More...
 
VectorPostprocessor_vpp
 The child VectorPostprocessor. More...
 
const libMesh::Parallel::Communicator_comm
 The communicator of the child. More...
 
const TransientBase *const _sampler_transient
 Transient executioner used to determine if the last solve converged. More...
 
std::vector< std::string > _variable_names
 The variable names. More...
 
const unsigned int _sort_by
 What to sort by. More...
 
VectorPostprocessorValue_x
 x coordinate of the points More...
 
VectorPostprocessorValue_y
 y coordinate of the points More...
 
VectorPostprocessorValue_z
 x coordinate of the points More...
 
VectorPostprocessorValue_id
 The node ID of each point. More...
 
std::vector< VectorPostprocessorValue * > _values
 

Private Attributes

std::size_t _curr_num_samples = 0
 The number of samples added in the last execution. More...
 
std::set< std::size_t, std::greater< std::size_t > > _curr_indices
 The indices of the samples in the last execution. More...
 
std::size_t _curr_total_samples = 0
 The full size of the vector since the last execution. More...
 

Detailed Description

Base class for VectorPostprocessors that need to do "sampling" of values in the domain.

Definition at line 37 of file SamplerBase.h.

Constructor & Destructor Documentation

◆ SamplerBase()

SamplerBase::SamplerBase ( const InputParameters parameters,
VectorPostprocessor vpp,
const libMesh::Parallel::Communicator comm 
)
Parameters
parametersThe parameters for the object
vppA pointer to the child object
commThe communicator of the child

Definition at line 41 of file SamplerBase.C.

44  : _sampler_params(parameters),
45  _vpp(vpp),
46  _comm(comm),
47  _sampler_transient(dynamic_cast<TransientBase *>(
48  parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")
49  ->getMooseApp()
50  .getExecutioner())),
51  _sort_by(parameters.get<MooseEnum>("sort_by")),
52  _x(vpp->declareVector("x")),
53  _y(vpp->declareVector("y")),
54  _z(vpp->declareVector("z")),
55  _id(vpp->declareVector("id"))
56 {
57 }
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
const TransientBase *const _sampler_transient
Transient executioner used to determine if the last solve converged.
Definition: SamplerBase.h:113
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Verifies that the requested parameter exists and is not NULL and returns it to the caller...
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
MooseApp & getMooseApp() const
Get the MooseApp this class is associated with.
Definition: MooseBase.h:45
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:124
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:119
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:107
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:122
Executioner * getExecutioner() const
Retrieve the Executioner for this App.
Definition: MooseApp.C:2142
const InputParameters & _sampler_params
The child params.
Definition: SamplerBase.h:104
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:129
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:126
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:110

◆ ~SamplerBase()

virtual SamplerBase::~SamplerBase ( )
virtualdefault

Member Function Documentation

◆ addSample()

void SamplerBase::addSample ( const Point &  p,
const Real id,
const std::vector< Real > &  values 
)
protectedvirtual

Call this with the value of every variable at each point you want to sample at.

Parameters
pThe point where you took the sample
idThis can either be an actual ID or a distance or anything else you want
valuesThe value of each variable

Definition at line 70 of file SamplerBase.C.

Referenced by SideValueSampler::execute(), LineFunctionSampler::execute(), ElementValueSampler::execute(), NodalValueSampler::execute(), and PointSamplerBase::finalize().

71 {
72  _x.push_back(p(0));
73  _y.push_back(p(1));
74  _z.push_back(p(2));
75 
76  _id.push_back(id);
77 
78  mooseAssert(values.size() == _variable_names.size(), "Mismatch of variable names to vector size");
79  for (MooseIndex(values) i = 0; i < values.size(); ++i)
80  _values[i]->emplace_back(values[i]);
81 
83 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:116
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:124
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:131
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:122
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:129
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:126
std::size_t _curr_num_samples
The number of samples added in the last execution.
Definition: SamplerBase.h:135

◆ checkForStandardFieldVariableType()

void SamplerBase::checkForStandardFieldVariableType ( const MooseVariableFieldBase *const  var_ptr,
const std::string &  var_param_name = "variable" 
) const
protected

Checks whether the passed variable pointer corresponds to a regular single-valued field variable.

Parameters
var_param_namename of the variable parameter in which the variables were passed
var_ptrpointer to the field variable

Definition at line 132 of file SamplerBase.C.

Referenced by ElementValueSampler::ElementValueSampler(), NodalValueSampler::NodalValueSampler(), PointVariableSamplerBase::PointVariableSamplerBase(), and SideValueSampler::SideValueSampler().

134 {
135  // A pointer to a MooseVariableFieldBase should never be SCALAR
136  mooseAssert(var_ptr->feType().family != SCALAR,
137  "Scalar variable '" + var_ptr->name() + "' cannot be sampled.");
138  mooseAssert(dynamic_cast<const MooseObject *>(_vpp), "Should have succeeded");
139  if (var_ptr->isVector())
140  dynamic_cast<const MooseObject *>(_vpp)->paramError(
141  var_param_name,
142  "The variable '",
143  var_ptr->name(),
144  "' is a vector variable. Sampling those is not currently supported in the "
145  "framework. It may be supported using a dedicated object in your application. Use "
146  "'VectorVariableComponentAux' auxkernel to copy those values into a regular field "
147  "variable");
148  if (var_ptr->isArray())
149  dynamic_cast<const MooseObject *>(_vpp)->paramError(
150  var_param_name,
151  "The variable '",
152  var_ptr->name(),
153  "' is an array variable. Sampling those is not currently supported in the "
154  "framework. It may be supported using a dedicated object in your application. Use "
155  "'ArrayVariableComponent' auxkernel to copy those values into a regular field variable");
156 }
const libMesh::FEType & feType() const
Get the type of finite element object.
SCALAR
const std::string & name() const override
Get the variable name.
virtual bool isVector() const =0
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:107
virtual bool isArray() const

◆ finalize()

void SamplerBase::finalize ( )
protectedvirtual

Finalize the values.

YOU MUST CALL THIS DURING finalize() in the child class!

We have several vectors that all need to be processed in the same way. Rather than enumerate them all, let's just create a vector of pointers and work on them that way.

We now have one sorted vector. The remaining vectors need to be sorted according to that vector. We'll need a temporary vector to hold values as we map them according to the sorted indices. After that, we'll swap the vector contents with the sorted vector to get the values back into the original vector.

Reimplemented in LineMaterialSamplerBase< T >, LineMaterialSamplerBase< Real >, PointSamplerBase, ElementValueSampler, NodalValueSampler, LineFunctionSampler, and SideValueSampler.

Definition at line 159 of file SamplerBase.C.

Referenced by SideValueSampler::finalize(), LineFunctionSampler::finalize(), NodalValueSampler::finalize(), ElementValueSampler::finalize(), PointSamplerBase::finalize(), and LineMaterialSamplerBase< Real >::finalize().

160 {
166  constexpr auto NUM_ID_VECTORS = 4;
167 
168  std::vector<VectorPostprocessorValue *> vec_ptrs;
169  vec_ptrs.reserve(_values.size() + NUM_ID_VECTORS);
170  // Initialize the pointer vector with the position and ID vectors
171  vec_ptrs = {{&_x, &_y, &_z, &_id}};
172  // Now extend the vector by all the remaining values vector before processing
173  vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
174 
175  // Gather up each of the partial vectors
176  for (auto vec_ptr : vec_ptrs)
177  _comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);
178 
179  // Now create an index vector by using an indirect sort
180  std::vector<std::size_t> sorted_indices;
181  Moose::indirectSort(vec_ptrs[_sort_by]->begin(), vec_ptrs[_sort_by]->end(), sorted_indices);
182 
190  // This vector is used as temp storage to sort each of the remaining vectors according to the
191  // first
192  auto vector_length = sorted_indices.size();
193  VectorPostprocessorValue tmp_vector(vector_length);
194 
195 #ifndef NDEBUG
196  for (const auto vec_ptr : vec_ptrs)
197  if (vec_ptr->size() != vector_length)
198  mooseError("Vector length mismatch");
199 #endif
200 
201  // Sort each of the vectors using the same sorted indices
202  for (auto & vec_ptr : vec_ptrs)
203  {
204  for (MooseIndex(sorted_indices) j = 0; j < sorted_indices.size(); ++j)
205  tmp_vector[j] = (*vec_ptr)[sorted_indices[j]];
206 
207  // Swap vector storage with sorted vector
208  vec_ptr->swap(tmp_vector);
209  }
210 
211  // Gather the indices of samples from the last execution
212  // Used to determine which parts of the vector need to be erased if a solve fails
214  {
216  if (_comm.rank() == 0)
217  {
218  _curr_indices.insert(sorted_indices.end() - _curr_num_samples, sorted_indices.end());
219  _curr_total_samples = vec_ptrs[0]->size();
220  }
221  }
222 }
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:68
processor_id_type rank() const
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:124
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:119
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:131
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:107
std::set< std::size_t, std::greater< std::size_t > > _curr_indices
The indices of the samples in the last execution.
Definition: SamplerBase.h:137
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:122
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:203
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:129
std::size_t _curr_total_samples
The full size of the vector since the last execution.
Definition: SamplerBase.h:139
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:126
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:110
std::size_t _curr_num_samples
The number of samples added in the last execution.
Definition: SamplerBase.h:135

◆ initialize()

void SamplerBase::initialize ( )
protectedvirtual

Initialize the datastructures.

YOU MUST CALL THIS DURING initialize() in the child class!

Reimplemented in LineMaterialSamplerBase< T >, LineMaterialSamplerBase< Real >, PointSamplerBase, PointVariableSamplerBase, PositionsFunctorValueSampler, ElementValueSampler, NodalValueSampler, LineFunctionSampler, and SideValueSampler.

Definition at line 86 of file SamplerBase.C.

Referenced by SideValueSampler::initialize(), LineFunctionSampler::initialize(), NodalValueSampler::initialize(), ElementValueSampler::initialize(), PointSamplerBase::initialize(), and LineMaterialSamplerBase< Real >::initialize().

87 {
88  // Don't reset the vectors if we want to retain history
89  if (_vpp->containsCompleteHistory() && _comm.rank() == 0)
90  {
91  // If we are repeating the timestep due to an aborted solve, we want to throw away the last
92  // values
94  {
95  // Convenient to allocate a single variable for all the vpp values
96  std::vector<VectorPostprocessorValue *> vec_ptrs = {{&_x, &_y, &_z, &_id}};
97  vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
98  // Erase the elements from the last execution
99  for (auto vec_ptr : vec_ptrs)
100  {
101  // Vector may have already been restored, if so, skip the erasure
102  if (_curr_total_samples > vec_ptr->size())
103  {
104  mooseAssert(vec_ptr->size() == (_curr_total_samples - _curr_num_samples),
105  "Number of samples is not what is expected.");
106  continue;
107  }
108  for (auto ind : _curr_indices)
109  {
110  mooseAssert(ind < vec_ptr->size(), "Trying to remove a sample that doesn't exist.");
111  vec_ptr->erase(vec_ptr->begin() + ind);
112  }
113  }
114  }
115  _curr_indices.clear();
116  }
117  else
118  {
119  _x.clear();
120  _y.clear();
121  _z.clear();
122  _id.clear();
123 
124  std::for_each(
125  _values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
126  }
127 
128  _curr_num_samples = 0;
129 }
const TransientBase *const _sampler_transient
Transient executioner used to determine if the last solve converged.
Definition: SamplerBase.h:113
processor_id_type rank() const
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:124
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:131
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:107
std::set< std::size_t, std::greater< std::size_t > > _curr_indices
The indices of the samples in the last execution.
Definition: SamplerBase.h:137
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:122
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:203
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:129
std::size_t _curr_total_samples
The full size of the vector since the last execution.
Definition: SamplerBase.h:139
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:126
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:110
virtual bool lastSolveConverged() const override
Whether or not the last solve converged.
std::size_t _curr_num_samples
The number of samples added in the last execution.
Definition: SamplerBase.h:135

◆ setupVariables()

void SamplerBase::setupVariables ( const std::vector< std::string > &  variable_names)
protected

You MUST call this in the constructor of the child class and pass down the name of the variables.

Parameters
variable_namesThe names of the variables. Note: The order of the variables sets the order of the values for addSample()

Definition at line 60 of file SamplerBase.C.

Referenced by ElementValueSampler::ElementValueSampler(), LineFunctionSampler::LineFunctionSampler(), LineMaterialSamplerBase< Real >::LineMaterialSamplerBase(), NodalValueSampler::NodalValueSampler(), PointVariableSamplerBase::PointVariableSamplerBase(), PositionsFunctorValueSampler::PositionsFunctorValueSampler(), and SideValueSampler::SideValueSampler().

61 {
62  _variable_names = variable_names;
63  _values.reserve(variable_names.size());
64 
65  for (const auto & variable_name : variable_names)
66  _values.push_back(&_vpp->declareVector(variable_name));
67 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:116
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:131
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:107
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.

◆ threadJoin()

void SamplerBase::threadJoin ( const SamplerBase y)
protectedvirtual

Join the values.

YOU MUST CALL THIS DURING threadJoin() in the child class!

Parameters
yYou must cast the UserObject to your child class type first then you can pass it in here.

Definition at line 225 of file SamplerBase.C.

Referenced by SideValueSampler::threadJoin(), ElementValueSampler::threadJoin(), and NodalValueSampler::threadJoin().

226 {
227  _x.insert(_x.end(), y._x.begin(), y._x.end());
228  _y.insert(_y.end(), y._y.begin(), y._y.end());
229  _z.insert(_z.end(), y._z.begin(), y._z.end());
230 
231  _id.insert(_id.end(), y._id.begin(), y._id.end());
232 
233  for (MooseIndex(_variable_names) i = 0; i < _variable_names.size(); i++)
234  _values[i]->insert(_values[i]->end(), y._values[i]->begin(), y._values[i]->end());
235 
237 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:116
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:124
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:131
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:122
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:129
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:126
std::size_t _curr_num_samples
The number of samples added in the last execution.
Definition: SamplerBase.h:135

◆ validParams()

InputParameters SamplerBase::validParams ( )
static

Definition at line 27 of file SamplerBase.C.

Referenced by SideValueSampler::validParams(), LineFunctionSampler::validParams(), NodalValueSampler::validParams(), ElementValueSampler::validParams(), PointVariableSamplerBase::validParams(), PointSamplerBase::validParams(), and LineMaterialSamplerBase< Real >::validParams().

28 {
30 
31  MooseEnum sort_options("x y z id");
32  params.addRequiredParam<MooseEnum>("sort_by", sort_options, "What to sort the samples by");
33 
34  // The value from this VPP is naturally already on every processor
35  // TODO: Make this not the case! See #11415
36  params.set<bool>("_auto_broadcast") = false;
37 
38  return params;
39 }
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33

Member Data Documentation

◆ _comm

const libMesh::Parallel::Communicator& SamplerBase::_comm
protected

The communicator of the child.

Definition at line 110 of file SamplerBase.h.

Referenced by PointSamplerBase::finalize(), finalize(), and initialize().

◆ _curr_indices

std::set<std::size_t, std::greater<std::size_t> > SamplerBase::_curr_indices
private

The indices of the samples in the last execution.

Definition at line 137 of file SamplerBase.h.

Referenced by finalize(), and initialize().

◆ _curr_num_samples

std::size_t SamplerBase::_curr_num_samples = 0
private

The number of samples added in the last execution.

Definition at line 135 of file SamplerBase.h.

Referenced by addSample(), finalize(), initialize(), and threadJoin().

◆ _curr_total_samples

std::size_t SamplerBase::_curr_total_samples = 0
private

The full size of the vector since the last execution.

Definition at line 139 of file SamplerBase.h.

Referenced by finalize(), and initialize().

◆ _id

VectorPostprocessorValue& SamplerBase::_id
protected

The node ID of each point.

Definition at line 129 of file SamplerBase.h.

Referenced by addSample(), finalize(), LineValueSampler::getValue(), initialize(), and threadJoin().

◆ _sampler_params

const InputParameters& SamplerBase::_sampler_params
protected

The child params.

Definition at line 104 of file SamplerBase.h.

◆ _sampler_transient

const TransientBase* const SamplerBase::_sampler_transient
protected

Transient executioner used to determine if the last solve converged.

Definition at line 113 of file SamplerBase.h.

Referenced by initialize().

◆ _sort_by

const unsigned int SamplerBase::_sort_by
protected

What to sort by.

Definition at line 119 of file SamplerBase.h.

Referenced by finalize(), and LineValueSampler::getValue().

◆ _values

std::vector<VectorPostprocessorValue *> SamplerBase::_values
protected

◆ _variable_names

std::vector<std::string> SamplerBase::_variable_names
protected

The variable names.

Definition at line 116 of file SamplerBase.h.

Referenced by addSample(), setupVariables(), and threadJoin().

◆ _vpp

VectorPostprocessor* SamplerBase::_vpp
protected

◆ _x

VectorPostprocessorValue& SamplerBase::_x
protected

x coordinate of the points

Definition at line 122 of file SamplerBase.h.

Referenced by addSample(), finalize(), initialize(), and threadJoin().

◆ _y

VectorPostprocessorValue& SamplerBase::_y
protected

y coordinate of the points

Definition at line 124 of file SamplerBase.h.

Referenced by addSample(), finalize(), initialize(), and threadJoin().

◆ _z

VectorPostprocessorValue& SamplerBase::_z
protected

x coordinate of the points

Definition at line 126 of file SamplerBase.h.

Referenced by addSample(), finalize(), initialize(), and threadJoin().


The documentation for this class was generated from the following files: