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