www.mooseframework.org
StatisticsVectorPostprocessor.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 
11 
12 // MOOSE includes
13 #include "MooseVariable.h"
15 #include "ThreadedNodeLoop.h"
16 
17 #include "libmesh/quadrature.h"
18 
19 #include <numeric>
20 
22 
23 template <>
26 {
28  params.addClassDescription("Compute statistical values of a given VectorPostprocessor. The "
29  "statistics are computed for each column.");
30 
31  params.addRequiredParam<VectorPostprocessorName>(
32  "vpp", "The VectorPostprocessor to compute statistics for.");
33 
34  // These are directly numbered to ensure that any changes to this list will not cause a bug
35  // Do not change the numbers here without changing the corresponding code in computeStatVector()
36  MultiMooseEnum stats("min=0 max=1 sum=2 average=3 stddev=4 norm2=5");
38  "stats",
39  stats,
40  "The statistics you would like to compute for each column of the VectorPostprocessor");
41 
42  return params;
43 }
44 
46  : GeneralVectorPostprocessor(parameters),
47  _vpp_name(getParam<VectorPostprocessorName>("vpp")),
48  _stats(getParam<MultiMooseEnum>("stats")),
49  _stat_type_vector(declareVector("stat_type"))
50 {
51 }
52 
53 void
55 {
56  const auto & vpp_vectors = _fe_problem.getVectorPostprocessorVectors(_vpp_name);
57 
58  // Add vectors for each column, the reason for the extra logic here is that the columns a VPP
59  // produces can change
60  for (const auto & the_pair : vpp_vectors)
61  {
62  const auto & name = the_pair.first;
63 
64  if (_stat_vectors.find(name) == _stat_vectors.end())
66  }
67 }
68 
69 void
71 {
72  if (processor_id() == 0) // Only compute on processor 0
73  {
74  const auto & vpp_vectors = _fe_problem.getVectorPostprocessorVectors(_vpp_name);
75 
76  _stat_type_vector.clear();
77 
78  // Add the stat IDs into the first colum
79  for (const auto & stat : _stats)
80  {
81  auto stat_id = stat.id();
82 
83  _stat_type_vector.push_back(stat_id);
84  }
85 
86  // For each value vector compute the stats
87  for (auto & the_pair : vpp_vectors)
88  {
89  const auto & name = the_pair.first;
90  const auto & values = *the_pair.second.current;
91 
92  mooseAssert(_stat_vectors.count(name), "Error retrieving VPP vector");
93  auto & stat_vector = *_stat_vectors.at(name);
94 
95  stat_vector.clear();
96 
97  for (const auto & stat : _stats)
98  {
99  auto stat_id = stat.id();
100 
101  stat_vector.push_back(computeStatValue(stat_id, values));
102  }
103  }
104  }
105 }
106 
107 void
109 {
110 }
111 
112 Real
113 StatisticsVectorPostprocessor::computeStatValue(int stat_id, const std::vector<Real> & stat_vector)
114 {
115  switch (stat_id)
116  {
117  case 0: // min
118  return *std::min_element(stat_vector.begin(), stat_vector.end());
119  case 1: // max
120  return *std::max_element(stat_vector.begin(), stat_vector.end());
121  case 2: // sum
122  return std::accumulate(stat_vector.begin(), stat_vector.end(), 0.);
123  case 3: // average
124  return std::accumulate(stat_vector.begin(), stat_vector.end(), 0.) /
125  static_cast<Real>(stat_vector.size());
126  case 4: // stddev
127  {
128  auto mean = std::accumulate(stat_vector.begin(), stat_vector.end(), 0.) /
129  static_cast<Real>(stat_vector.size());
130 
131  auto the_sum = std::accumulate(stat_vector.begin(),
132  stat_vector.end(),
133  0.,
134  [&mean](Real running_value, Real current_value) {
135  return running_value + std::pow(current_value - mean, 2);
136  });
137 
138  return std::sqrt(the_sum / (stat_vector.size() - 1.));
139  }
140  case 5: // norm2
141  return std::sqrt(std::accumulate(
142  stat_vector.begin(), stat_vector.end(), 0., [](Real running_value, Real current_value) {
143  return running_value + std::pow(current_value, 2);
144 
145  }));
146  default:
147  mooseError("Unknown statistics type: ", stat_id);
148  }
149 }
virtual void finalize() override
Finalize.
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
StatisticsVectorPostprocessor(const InputParameters &parameters)
const VectorPostprocessorName & _vpp_name
The name of the VPP to work on.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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...
const std::vector< std::pair< std::string, VectorPostprocessorData::VectorPostprocessorState > > & getVectorPostprocessorVectors(const std::string &vpp_name)
Get the vectors for a specific VectorPostprocessor.
Real pow(Real x, int e)
Definition: MathUtils.C:211
InputParameters validParams< StatisticsVectorPostprocessor >()
InputParameters validParams< GeneralVectorPostprocessor >()
Real computeStatValue(int stat_id, const std::vector< Real > &stat_vector)
Compute the passed in statistic for the vector.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
VectorPostprocessorValue & _stat_type_vector
The VPP vector that will hold the statistics identifiers.
virtual void execute() override
Execute method.
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:141
registerMooseObject("MooseApp", StatisticsVectorPostprocessor)
std::map< std::string, VectorPostprocessorValue * > _stat_vectors
The VPP vectors that will hold the statistics for each column.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
Compute several metrics for each MPI process.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
MultiMooseEnum _stats
The chosen statistics to compute.