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 
17 template <>
20 {
22  params.addParam<unsigned int>("bin_number", 50, "Number of histogram bins");
23  params.addCoupledVar("variable", "Variables to average radially");
24  params.addRequiredParam<Real>("radius", "Radius to average out to");
25  params.addParam<Point>("origin", Point(), "Origin of the cylinder");
26  params.addParam<Real>(
27  "empty_bin_value", 0.0, "Value to assign to bins into which no datapoints fall");
28  return params;
29 }
30 
32  : ElementVectorPostprocessor(parameters),
33  _nbins(getParam<unsigned int>("bin_number")),
34  _radius(getParam<Real>("radius")),
35  _origin(getParam<Point>("origin")),
36  _deltaR(_radius / _nbins),
37  _nvals(coupledComponents("variable")),
38  _values(_nvals),
39  _empty_bin_value(getParam<Real>("empty_bin_value")),
40  _bin_center(declareVector("radius")),
41  _counts(_nbins),
42  _average(_nvals)
43 {
44  if (coupledComponents("variable") != 1)
45  mooseError("SpatialAverageBase works on exactly one coupled variable");
46 
47  // Note: We associate the local variable "i" with nbins and "j" with nvals throughout.
48 
49  // couple variables initialize vectors
50  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
51  {
52  _values[j] = &coupledValue("variable", j);
53  _average[j] = &declareVector(getVar("variable", j)->name());
54  }
55 
56  // initialize the bin center value vector
57  _bin_center.resize(_nbins);
58  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
59  _bin_center[i] = (i + 0.5) * _deltaR;
60 }
61 
62 void
64 {
65  // reset the histogram
66  for (auto vec_ptr : _average)
67  vec_ptr->assign(_nbins, 0.0);
68 
69  // reset bin counts
70  _counts.assign(_nbins, 0.0);
71 }
72 
73 void
75 {
76  // loop over quadrature points
77  for (_qp = 0; _qp < _qrule->n_points(); ++_qp)
78  {
79  // compute target bin
80  auto bin = computeDistance() / _deltaR;
81 
82  // add the volume contributed by the current quadrature point
83  if (bin >= 0 && bin < static_cast<int>(_nbins))
84  {
85  for (MooseIndex(_nvals) j = 0; j < _nvals; ++j)
86  (*_average[j])[bin] += (*_values[j])[_qp];
87 
88  _counts[bin]++;
89  }
90  }
91 }
92 
93 void
95 {
97 
98  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
99  {
100  gatherSum(*_average[j]);
101 
102  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
103  (*_average[j])[i] =
104  _counts[i] > 0 ? (*_average[j])[i] / static_cast<Real>(_counts[i]) : _empty_bin_value;
105  }
106 }
107 
108 void
110 {
111  const SpatialAverageBase & uo = static_cast<const SpatialAverageBase &>(y);
112 
113  for (MooseIndex(_counts) i = 0; i < _nbins; ++i)
114  {
115  _counts[i] += uo._counts[i];
116 
117  for (MooseIndex(_average) j = 0; j < _nvals; ++j)
118  (*_average[j])[i] += (*uo._average[j])[i];
119  }
120 }
virtual Real computeDistance()=0
compute the distance of the current quadarature point for binning
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
unsigned int coupledComponents(const std::string &var_name)
Number of coupled components.
Definition: Coupleable.C:166
std::vector< const VariableValue * > _values
coupled variable that is being binned
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:241
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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:103
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.
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0)
Returns value of a coupled variable.
Definition: Coupleable.C:361
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
const QBase *const & _qrule
const unsigned int _nbins
number of histogram bins
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
InputParameters validParams< SpatialAverageBase >()
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...
VectorPostprocessorValue & _bin_center
value mid point of the bin
const Real _empty_bin_value
value to assign to empty bins
Base class for user-specific data.
Definition: UserObject.h:37
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
InputParameters validParams< ElementVectorPostprocessor >()
Base clase for computing spatial average of a variable over simple spatial regions of the computation...
virtual void execute() override
Execute method.