www.mooseframework.org
SphericalAverage.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "SphericalAverage.h"
16 
17 // MOOSE includes
18 #include "MooseVariable.h"
19 
20 #include "libmesh/quadrature.h"
21 
22 template <>
25 {
27  params.addParam<unsigned int>("bin_number", 50, "Number of histogram bins");
28  params.addCoupledVar("variable", "Variables to average radially");
29  params.addRequiredParam<Real>("radius", "Radius to average out to");
30  params.addParam<Real>(
31  "empty_bin_value", 0.0, "Value to assign to bins into which no datapoints fall");
32  return params;
33 }
34 
36  : ElementVectorPostprocessor(parameters),
37  _nbins(getParam<unsigned int>("bin_number")),
38  _radius(getParam<Real>("radius")),
39  _deltaR(_radius / _nbins),
40  _nvals(coupledComponents("variable")),
41  _values(_nvals),
42  _empty_bin_value(getParam<Real>("empty_bin_value")),
43  _bin_center(declareVector("radius")),
44  _counts(_nbins),
45  _average(_nvals)
46 {
47  if (coupledComponents("variable") != 1)
48  mooseError("SphericalAverage works on exactly one coupled variable");
49 
50  // Note: We associate the local variable "i" with nbins and "j" with nvals throughout.
51 
52  // couple variables initialize vectors
53  for (auto j = beginIndex(_average); j < _nvals; ++j)
54  {
55  _values[j] = &coupledValue("variable", j);
56  _average[j] = &declareVector(getVar("variable", j)->name());
57  }
58 
59  // initialize the bin center value vector
60  _bin_center.resize(_nbins);
61  for (auto i = beginIndex(_counts); i < _nbins; ++i)
62  _bin_center[i] = (i + 0.5) * _deltaR;
63 }
64 
65 void
67 {
68  // reset the histogram
69  for (auto vec_ptr : _average)
70  vec_ptr->assign(_nbins, 0.0);
71 
72  // reset bin counts
73  _counts.assign(_nbins, 0.0);
74 }
75 
76 void
78 {
79  // loop over quadrature points
80  for (_qp = 0; _qp < _qrule->n_points(); ++_qp)
81  {
82  // compute target bin
83  auto bin = computeDistance() / _deltaR;
84 
85  // add the volume contributed by the current quadrature point
86  if (bin >= 0 && bin < static_cast<int>(_nbins))
87  {
88  for (auto j = decltype(_nvals)(0); j < _nvals; ++j)
89  (*_average[j])[bin] += (*_values[j])[_qp];
90 
91  _counts[bin]++;
92  }
93  }
94 }
95 
96 void
98 {
100 
101  for (auto j = beginIndex(_average); j < _nvals; ++j)
102  {
103  gatherSum(*_average[j]);
104 
105  for (auto i = beginIndex(_counts); i < _nbins; ++i)
106  (*_average[j])[i] =
107  _counts[i] > 0 ? (*_average[j])[i] / static_cast<Real>(_counts[i]) : _empty_bin_value;
108  }
109 }
110 
111 void
113 {
114  const SphericalAverage & uo = static_cast<const SphericalAverage &>(y);
115 
116  for (auto i = beginIndex(_counts); i < _nbins; ++i)
117  {
118  _counts[i] += uo._counts[i];
119 
120  for (auto j = beginIndex(_average); j < _nvals; ++j)
121  (*_average[j])[i] += (*uo._average[j])[i];
122  }
123 }
124 
125 Real
127 {
128  // overwrite this method to implement cylindrical averages etc.
129  return _q_point[_qp].norm();
130 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
virtual void threadJoin(const UserObject &y) override
Must override.
virtual void execute() override
Execute method.
unsigned int coupledComponents(const std::string &var_name)
Number of coupled components.
Definition: Coupleable.C:111
const MooseArray< Point > & _q_point
std::vector< unsigned int > _counts
sample count per bin
const unsigned int _nbins
number of histogram bins
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
MooseVariable * getVar(const std::string &var_name, unsigned int comp)
Extract pointer to a coupled variable.
Definition: Coupleable.C:117
virtual Real computeDistance()
compute the distance of the current quadarature point for binning
VectorPostprocessorValue & _bin_center
value mid point of the bin
std::vector< const VariableValue * > _values
coupled variable that is being binned
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...
virtual void finalize() override
Finalize.
SphericalAverage(const InputParameters &parameters)
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:119
unsigned int _qp
current quadrature point - used in computeVolume()
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0)
Returns value of a coupled variable.
Definition: Coupleable.C:167
std::vector< VectorPostprocessorValue * > _average
aggregated global average vectors
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
const Real _empty_bin_value
value to assign to empty bins
const Real _deltaR
bin width
InputParameters validParams< SphericalAverage >()
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
Compute a spherical average of a variableas a function of radius throughout the simulation domain...
Base class for user-specific data.
Definition: UserObject.h:42
InputParameters validParams< ElementVectorPostprocessor >()
const unsigned int _nvals
number of coupled variables