www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected 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
 

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...
 
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...
 
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
 

Detailed Description

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

Definition at line 41 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 35 of file SamplerBase.C.

38  : _sampler_params(parameters),
39  _vpp(vpp),
40  _comm(comm),
41  _sort_by(parameters.get<MooseEnum>("sort_by")),
42  _x(vpp->declareVector("x")),
43  _y(vpp->declareVector("y")),
44  _z(vpp->declareVector("z")),
45  _id(vpp->declareVector("id"))
46 {
47 }
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:114
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:109
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:100
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:32
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:112
const InputParameters & _sampler_params
The child params.
Definition: SamplerBase.h:97
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:119
MPI_Comm comm
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:116
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:103

◆ ~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 60 of file SamplerBase.C.

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

61 {
62  _x.push_back(p(0));
63  _y.push_back(p(1));
64  _z.push_back(p(2));
65 
66  _id.push_back(id);
67 
68  mooseAssert(values.size() == _variable_names.size(), "Mismatch of variable names to vector size");
69  for (auto i = beginIndex(values); i < values.size(); ++i)
70  _values[i]->emplace_back(values[i]);
71 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:106
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:114
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:121
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:112
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:119
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:116

◆ 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, NodalValueSampler, ElementValueSampler, LineFunctionSampler, and SideValueSampler.

Definition at line 90 of file SamplerBase.C.

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

91 {
97  constexpr auto NUM_ID_VECTORS = 4;
98 
99  std::vector<VectorPostprocessorValue *> vec_ptrs;
100  vec_ptrs.reserve(_values.size() + NUM_ID_VECTORS);
101  // Initialize the pointer vector with the position and ID vectors
102  vec_ptrs = {{&_x, &_y, &_z, &_id}};
103  // Now extend the vector by all the remaining values vector before processing
104  vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
105 
106  // Gather up each of the partial vectors
107  for (auto vec_ptr : vec_ptrs)
108  _comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);
109 
110  // Now create an index vector by using an indirect sort
111  std::vector<std::size_t> sorted_indices;
112  Moose::indirectSort(vec_ptrs[_sort_by]->begin(), vec_ptrs[_sort_by]->end(), sorted_indices);
113 
121  // This vector is used as temp storage to sort each of the remaining vectors according to the
122  // first
123  auto vector_length = sorted_indices.size();
124  VectorPostprocessorValue tmp_vector(vector_length);
125 
126 #ifndef NDEBUG
127  for (const auto vec_ptr : vec_ptrs)
128  if (vec_ptr->size() != vector_length)
129  mooseError("Vector length mismatch");
130 #endif
131 
132  // Sort each of the vectors using the same sorted indices
133  for (auto i = beginIndex(vec_ptrs); i < vec_ptrs.size(); ++i)
134  {
135  for (auto j = beginIndex(sorted_indices); j < sorted_indices.size(); ++j)
136  tmp_vector[j] = (*vec_ptrs[i])[sorted_indices[j]];
137 
138  // Swap vector storage with sorted vector
139  vec_ptrs[i]->swap(tmp_vector);
140  }
141 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:69
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:114
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:109
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:121
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:112
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:91
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:119
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:116
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:103

◆ 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, NodalValueSampler, ElementValueSampler, LineFunctionSampler, and SideValueSampler.

Definition at line 74 of file SamplerBase.C.

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

75 {
76  // Don't reset the vectors if we want to retain history
77  if (_vpp->containsCompleteHistory() && _comm.rank() == 0)
78  return;
79 
80  _x.clear();
81  _y.clear();
82  _z.clear();
83  _id.clear();
84 
85  std::for_each(
86  _values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
87 }
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:114
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:121
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:100
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:112
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:91
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:119
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:116
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Definition: SamplerBase.h:103

◆ 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 50 of file SamplerBase.C.

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

51 {
52  _variable_names = variable_names;
53  _values.reserve(variable_names.size());
54 
55  for (const auto & variable_name : variable_names)
56  _values.push_back(&_vpp->declareVector(variable_name));
57 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:106
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:121
VectorPostprocessor * _vpp
The child VectorPostprocessor.
Definition: SamplerBase.h:100
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 144 of file SamplerBase.C.

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

145 {
146  _x.insert(_x.end(), y._x.begin(), y._x.end());
147  _y.insert(_y.end(), y._y.begin(), y._y.end());
148  _z.insert(_z.end(), y._z.begin(), y._z.end());
149 
150  _id.insert(_id.end(), y._id.begin(), y._id.end());
151 
152  for (unsigned int i = 0; i < _variable_names.size(); i++)
153  _values[i]->insert(_values[i]->end(), y._values[i]->begin(), y._values[i]->end());
154 }
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:106
VectorPostprocessorValue & _y
y coordinate of the points
Definition: SamplerBase.h:114
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:121
VectorPostprocessorValue & _x
x coordinate of the points
Definition: SamplerBase.h:112
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:119
VectorPostprocessorValue & _z
x coordinate of the points
Definition: SamplerBase.h:116

Member Data Documentation

◆ _comm

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

The communicator of the child.

Definition at line 103 of file SamplerBase.h.

Referenced by finalize(), and initialize().

◆ _id

VectorPostprocessorValue& SamplerBase::_id
protected

The node ID of each point.

Definition at line 119 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 97 of file SamplerBase.h.

◆ _sort_by

const unsigned int SamplerBase::_sort_by
protected

What to sort by.

Definition at line 109 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 106 of file SamplerBase.h.

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

◆ _vpp

VectorPostprocessor* SamplerBase::_vpp
protected

The child VectorPostprocessor.

Definition at line 100 of file SamplerBase.h.

Referenced by initialize(), and setupVariables().

◆ _x

VectorPostprocessorValue& SamplerBase::_x
protected

x coordinate of the points

Definition at line 112 of file SamplerBase.h.

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

◆ _y

VectorPostprocessorValue& SamplerBase::_y
protected

y coordinate of the points

Definition at line 114 of file SamplerBase.h.

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

◆ _z

VectorPostprocessorValue& SamplerBase::_z
protected

x coordinate of the points

Definition at line 116 of file SamplerBase.h.

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


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