www.mooseframework.org
SpatialAverageBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "SpatialAverageBase.h"
11 
12 // MOOSE includes
13 #include "MooseVariableFE.h"
14 
15 #include "libmesh/quadrature.h"
16 
19 {
21  params.addParam<unsigned int>("bin_number", 50, "Number of histogram bins");
22  params.addCoupledVar("variable", "Variables to average radially");
23  params.addRequiredParam<Real>("radius", "Radius to average out to");
24  params.addParam<Point>("origin", Point(), "Origin of the cylinder");
25  params.addParam<Real>(
26  "empty_bin_value", 0.0, "Value to assign to bins into which no datapoints fall");
27  return params;
28 }
29 
31  : ElementVectorPostprocessor(parameters),
32  _nbins(getParam<unsigned int>("bin_number")),
33  _radius(getParam<Real>("radius")),
34  _origin(getParam<Point>("origin")),
35  _deltaR(_radius / _nbins),
36  _nvals(coupledComponents("variable")),
37  _values(coupledValues("variable")),
38  _empty_bin_value(getParam<Real>("empty_bin_value")),
39  _bin_center(declareVector("radius")),
40  _counts(_nbins),
41  _average(_nvals)
42 {
43  if (coupledComponents("variable") != 1)
44  mooseError("SpatialAverageBase works on exactly one coupled variable");
45 
46  // Note: We associate the local variable "i" with nbins and "j" with nvals throughout.
47 
48  // couple variables initialize vectors
49  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
50  _average[j] = &declareVector(coupledName("variable", j));
51 
52  // initialize the bin center value vector
53  _bin_center.resize(_nbins);
54  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
55  _bin_center[i] = (i + 0.5) * _deltaR;
56 }
57 
58 void
60 {
61  // reset the histogram
62  for (auto vec_ptr : _average)
63  vec_ptr->assign(_nbins, 0.0);
64 
65  // reset bin counts
66  _counts.assign(_nbins, 0.0);
67 }
68 
69 void
71 {
72  // loop over quadrature points
73  for (_qp = 0; _qp < _qrule->n_points(); ++_qp)
74  {
75  // compute target bin
76  auto bin = computeDistance() / _deltaR;
77 
78  // add the volume contributed by the current quadrature point
79  if (bin >= 0 && bin < static_cast<int>(_nbins))
80  {
81  for (MooseIndex(_nvals) j = 0; j < _nvals; ++j)
82  (*_average[j])[bin] += (*_values[j])[_qp];
83 
84  _counts[bin]++;
85  }
86  }
87 }
88 
89 void
91 {
93 
94  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
95  {
96  gatherSum(*_average[j]);
97 
98  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
99  (*_average[j])[i] =
100  _counts[i] > 0 ? (*_average[j])[i] / static_cast<Real>(_counts[i]) : _empty_bin_value;
101  }
102 }
103 
104 void
106 {
107  const auto & uo = static_cast<const SpatialAverageBase &>(y);
108 
109  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
110  {
111  _counts[i] += uo._counts[i];
112 
113  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
114  (*_average[j])[i] += (*uo._average[j])[i];
115  }
116 }
virtual Real computeDistance()=0
compute the distance of the current quadarature point for binning
static InputParameters validParams()
const unsigned int _nvals
number of coupled variables
virtual void threadJoin(const UserObject &y) override
Must override.
std::vector< unsigned int > _counts
sample count per bin
VariableName coupledName(const std::string &var_name, unsigned int comp=0) const
Names of the variable in the Coupleable interface.
Definition: Coupleable.C:2370
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< VectorPostprocessorValue * > _average
aggregated global average vectors
SpatialAverageBase(const InputParameters &parameters)
const Real _deltaR
bin width
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...
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:125
virtual void finalize() override
Finalize.
unsigned int _qp
current quadrature point - used in computeVolume()
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
unsigned int coupledComponents(const std::string &var_name) const
Number of coupled components.
Definition: Coupleable.C:153
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const QBase *const & _qrule
const unsigned int _nbins
number of histogram bins
const std::vector< const VariableValue * > _values
coupled variable that is being binned
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
static InputParameters validParams()
VectorPostprocessorValue & _bin_center
value mid point of the bin
const Real _empty_bin_value
value to assign to empty bins
void ErrorVector unsigned int
Base class for user-specific data.
Definition: UserObject.h:39
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
Base clase for computing spatial average of a variable over simple spatial regions of the computation...
virtual void execute() override
Execute method.