https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
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.
 
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.
 
virtual void initialize ()
 Initialize the datastructures.
 
virtual void finalize ()
 Finalize the values.
 
virtual void threadJoin (const SamplerBase &y)
 Join the values.
 

Protected Attributes

const InputParameters_sampler_params
 The child params.
 
VectorPostprocessor_vpp
 The child VectorPostprocessor.
 
const libMesh::Parallel::Communicator_comm
 The communicator of the child.
 
const TransientBase *const _sampler_transient
 Transient executioner used to determine if the last solve converged.
 
std::vector< std::string > _variable_names
 The variable names.
 
unsigned int _sort_by_index
 The index for what to sort by: x=0, y=1, z=2, then sampled variables in ordered specified in the parameter.
 
VectorPostprocessorValue_x
 x coordinate of the points
 
VectorPostprocessorValue_y
 y coordinate of the points
 
VectorPostprocessorValue_z
 x coordinate of the points
 
VectorPostprocessorValue_id
 The node ID of each point.
 
std::vector< VectorPostprocessorValue * > _values
 

Private Attributes

const std::string _sort_by
 What to sort by.
 
std::size_t _curr_num_samples = 0
 The number of samples added in the last execution.
 
std::set< std::size_t, std::greater< std::size_t > > _curr_indices
 The indices of the samples in the last execution.
 
std::size_t _curr_total_samples = 0
 The full size of the vector since the last execution.
 

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

43 : _sampler_params(parameters),
44 _vpp(vpp),
45 _comm(comm),
46 _sampler_transient(dynamic_cast<TransientBase *>(
47 parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")
48 ->getMooseApp()
49 .getExecutioner())),
51 _x(vpp->declareVector("x")),
52 _y(vpp->declareVector("y")),
53 _z(vpp->declareVector("z")),
54 _id(vpp->declareVector("id")),
55 _sort_by(parameters.get<std::string>("sort_by"))
56{
57 if (_sort_by == "x")
59 else if (_sort_by == "y")
61 else if (_sort_by == "z")
63 else if (_sort_by == "id")
65 // Sort by index for variables determined in setupVariables()
66}
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
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.
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.
Executioner * getExecutioner() const
Retrieve the Executioner for this App.
Definition MooseApp.C:2015
MooseApp & getMooseApp() const
Get the MooseApp this class is associated with.
Definition MooseBase.h:87
const InputParameters & _sampler_params
The child params.
unsigned int _sort_by_index
The index for what to sort by: x=0, y=1, z=2, then sampled variables in ordered specified in the para...
const std::string _sort_by
What to sort by.
VectorPostprocessorValue & _x
x coordinate of the points
VectorPostprocessorValue & _y
y coordinate of the points
const TransientBase *const _sampler_transient
Transient executioner used to determine if the last solve converged.
VectorPostprocessor * _vpp
The child VectorPostprocessor.
VectorPostprocessorValue & _id
The node ID of each point.
VectorPostprocessorValue & _z
x coordinate of the points
const libMesh::Parallel::Communicator & _comm
The communicator of the child.
Base class for transient executioners that use a FixedPointSolve solve object for multiapp-main app i...
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
const unsigned int invalid_uint

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

92{
93 _x.push_back(p(0));
94 _y.push_back(p(1));
95 _z.push_back(p(2));
96
97 _id.push_back(id);
98
99 mooseAssert(values.size() == _variable_names.size(), "Mismatch of variable names to vector size");
100 for (MooseIndex(values) i = 0; i < values.size(); ++i)
101 _values[i]->emplace_back(values[i]);
102
104}
std::array< Real, 2 > values
Definition MortarUtils.C:52
std::vector< std::string > _variable_names
The variable names.
std::vector< VectorPostprocessorValue * > _values
std::size_t _curr_num_samples
The number of samples added in the last execution.

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

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

155{
156 // A pointer to a MooseVariableFieldBase should never be SCALAR
157 mooseAssert(var_ptr->feType().family != SCALAR,
158 "Scalar variable '" + var_ptr->name() + "' cannot be sampled.");
159 mooseAssert(dynamic_cast<const MooseObject *>(_vpp), "Should have succeeded");
160 if (var_ptr->isVector())
161 dynamic_cast<const MooseObject *>(_vpp)->paramError(
162 var_param_name,
163 "The variable '",
164 var_ptr->name(),
165 "' is a vector variable. Sampling those is not currently supported in the "
166 "framework. It may be supported using a dedicated object in your application. Use "
167 "'VectorVariableComponentAux' auxkernel to copy those values into a regular field "
168 "variable");
169 if (var_ptr->isArray())
170 dynamic_cast<const MooseObject *>(_vpp)->paramError(
171 var_param_name,
172 "The variable '",
173 var_ptr->name(),
174 "' is an array variable. Sampling those is not currently supported in the "
175 "framework. It may be supported using a dedicated object in your application. Use "
176 "'ArrayVariableComponent' auxkernel to copy those values into a regular field variable");
177}
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
const libMesh::FEType & feType() const
Get the type of finite element object.
virtual bool isArray() const
virtual bool isVector() const =0

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

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

Definition at line 180 of file SamplerBase.C.

181{
187 constexpr auto NUM_ID_VECTORS = 4;
188
189 std::vector<VectorPostprocessorValue *> vec_ptrs;
190 vec_ptrs.reserve(_values.size() + NUM_ID_VECTORS);
191 // Initialize the pointer vector with the position and ID vectors
192 vec_ptrs = {{&_x, &_y, &_z, &_id}};
193 // Now extend the vector by all the remaining values vector before processing
194 vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
195
196 // Gather up each of the partial vectors
197 for (auto vec_ptr : vec_ptrs)
198 _comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);
199
200 // Now create an index vector by using an indirect sort
201 std::vector<std::size_t> sorted_indices;
203 vec_ptrs[_sort_by_index]->begin(), vec_ptrs[_sort_by_index]->end(), sorted_indices);
204
212 // This vector is used as temp storage to sort each of the remaining vectors according to the
213 // first
214 auto vector_length = sorted_indices.size();
215 VectorPostprocessorValue tmp_vector(vector_length);
216
217#ifndef NDEBUG
218 for (const auto vec_ptr : vec_ptrs)
219 if (vec_ptr->size() != vector_length)
220 mooseError("Vector length mismatch");
221#endif
222
223 // Sort each of the vectors using the same sorted indices
224 for (auto & vec_ptr : vec_ptrs)
225 {
226 for (MooseIndex(sorted_indices) j = 0; j < sorted_indices.size(); ++j)
227 tmp_vector[j] = (*vec_ptr)[sorted_indices[j]];
228
229 // Swap vector storage with sorted vector
230 vec_ptr->swap(tmp_vector);
231 }
232
233 // Gather the indices of samples from the last execution
234 // Used to determine which parts of the vector need to be erased if a solve fails
236 {
238 if (_comm.rank() == 0)
239 {
240 _curr_indices.insert(sorted_indices.end() - _curr_num_samples, sorted_indices.end());
241 _curr_total_samples = vec_ptrs[0]->size();
242 }
243 }
244}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::vector< Real > VectorPostprocessorValue
Definition MooseTypes.h:231
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
std::size_t _curr_total_samples
The full size of the vector since the last execution.
std::set< std::size_t, std::greater< std::size_t > > _curr_indices
The indices of the samples in the last execution.
processor_id_type rank() const
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)

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

◆ initialize()

void SamplerBase::initialize ( )
protectedvirtual

Initialize the datastructures.

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

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

Definition at line 107 of file SamplerBase.C.

108{
109 // Don't reset the vectors if we want to retain history
110 if (_vpp->containsCompleteHistory() && _comm.rank() == 0)
111 {
112 // If we are repeating the timestep due to an aborted solve, we want to throw away the last
113 // values
115 {
116 // Convenient to allocate a single variable for all the vpp values
117 std::vector<VectorPostprocessorValue *> vec_ptrs = {{&_x, &_y, &_z, &_id}};
118 vec_ptrs.insert(vec_ptrs.end(), _values.begin(), _values.end());
119 // Erase the elements from the last execution
120 for (auto vec_ptr : vec_ptrs)
121 {
122 // Vector may have already been restored, if so, skip the erasure
123 if (_curr_total_samples > vec_ptr->size())
124 {
125 mooseAssert(vec_ptr->size() == (_curr_total_samples - _curr_num_samples),
126 "Number of samples is not what is expected.");
127 continue;
128 }
129 for (auto ind : _curr_indices)
130 {
131 mooseAssert(ind < vec_ptr->size(), "Trying to remove a sample that doesn't exist.");
132 vec_ptr->erase(vec_ptr->begin() + ind);
133 }
134 }
135 }
136 _curr_indices.clear();
137 }
138 else
139 {
140 _x.clear();
141 _y.clear();
142 _z.clear();
143 _id.clear();
144
145 std::for_each(
146 _values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
147 }
148
150}
virtual bool lastSolveConverged() const override
Whether or not the last solve converged.

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

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

70{
71 _variable_names = variable_names;
72 _values.reserve(variable_names.size());
73
74 for (const auto & variable_name : variable_names)
75 _values.push_back(&_vpp->declareVector(variable_name));
76
77 // Find the index of the column to sort by for variables
79 {
80 const auto it = std::find(_variable_names.begin(), _variable_names.end(), _sort_by);
81 if (it != _variable_names.end())
82 _sort_by_index = 4 + it - _variable_names.begin();
83 else
85 "The 'sort_by' parameter must be one of x/y/z/id or one of the sampled variable names: " +
87 }
88}
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64

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

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

Reimplemented in ElementValueSampler, NodalValueSampler, and SideValueSampler.

Definition at line 247 of file SamplerBase.C.

248{
249 _x.insert(_x.end(), y._x.begin(), y._x.end());
250 _y.insert(_y.end(), y._y.begin(), y._y.end());
251 _z.insert(_z.end(), y._z.begin(), y._z.end());
252
253 _id.insert(_id.end(), y._id.begin(), y._id.end());
254
255 for (MooseIndex(_variable_names) i = 0; i < _variable_names.size(); i++)
256 _values[i]->insert(_values[i]->end(), y._values[i]->begin(), y._values[i]->end());
257
259}

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

◆ validParams()

InputParameters SamplerBase::validParams ( )
static

Definition at line 24 of file SamplerBase.C.

25{
27
28 params.addRequiredParam<std::string>(
29 "sort_by",
30 "What to sort the samples by. Options include 'x', 'y', 'z', 'id', and the name of any of "
31 "the sampled quantities (which each create a vector of the same name).");
32
33 // The value from this VPP is naturally already on every processor
34 // TODO: Make this not the case! See #11415
35 params.set<bool>("_auto_broadcast") = false;
36
37 return params;
38}
InputParameters emptyInputParameters()
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...
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.

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

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 139 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 137 of file SamplerBase.h.

Referenced by 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 141 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 std::string SamplerBase::_sort_by
private

What to sort by.

Definition at line 135 of file SamplerBase.h.

Referenced by SamplerBase(), and setupVariables().

◆ _sort_by_index

unsigned int SamplerBase::_sort_by_index
protected

The index for what to sort by: x=0, y=1, z=2, then sampled variables in ordered specified in the parameter.

Definition at line 119 of file SamplerBase.h.

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

◆ _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: