www.mooseframework.org
HistogramVectorPostprocessor.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 #include <algorithm>
13 
15 
16 template <>
19 {
21  params.addClassDescription("Compute a histogram for each column of a VectorPostprocessor");
22 
23  params.addRequiredParam<VectorPostprocessorName>(
24  "vpp", "The VectorPostprocessor to compute histogram of");
25 
26  params.addRequiredParam<unsigned int>("num_bins", "The number of bins for the histograms");
27 
28  return params;
29 }
30 
32  : GeneralVectorPostprocessor(parameters),
33  _vpp_name(getParam<VectorPostprocessorName>("vpp")),
34  _num_bins(getParam<unsigned int>("num_bins"))
35 {
36 }
37 
38 void
40 {
41  const auto & vpp_vectors = _fe_problem.getVectorPostprocessorVectors(_vpp_name);
42 
43  // Add vectors for each column, the reason for the extra logic here is that the columns a VPP
44  // produces can change
45  for (const auto & the_pair : vpp_vectors)
46  {
47  const auto & name = the_pair.first;
48 
49  if (_histogram_data.find(name) == _histogram_data.end())
51  &declareVector(name + "_lower"), &declareVector(name + "_upper"), &declareVector(name)};
52  }
53 }
54 
55 void
57 {
58  if (processor_id() == 0) // Only compute on processor 0
59  {
60  const auto & vpp_vectors = _fe_problem.getVectorPostprocessorVectors(_vpp_name);
61 
62  // For each value vector compute the histogram
63  for (auto & the_pair : vpp_vectors)
64  {
65  const auto & name = the_pair.first;
66  const auto & values = *the_pair.second.current;
67 
68  mooseAssert(_histogram_data.count(name), "Error retrieving VPP vector");
69  auto & histo_data = _histogram_data.at(name);
70 
71  computeHistogram(values, histo_data);
72  }
73  }
74 }
75 
76 void
78 {
79 }
80 
81 void
82 HistogramVectorPostprocessor::computeHistogram(const std::vector<Real> & values,
83  HistoData & histo_data)
84 {
85  if (values.empty())
86  mooseError("Cannot compute histogram without data!");
87 
88  // Grab the vectors to fill
89  auto & lower_vector = *histo_data._lower;
90  auto & upper_vector = *histo_data._upper;
91  auto & histogram = *histo_data._histogram;
92 
93  // Resize everything
94  // Note: no need to zero anything out
95  // that will automatically be done if the bin should be zero by the algorithm below
96  lower_vector.resize(_num_bins);
97  upper_vector.resize(_num_bins);
98  histogram.resize(_num_bins);
99 
100  // Create a sorted copy of the values
101  std::vector<Real> sorted_values(values.size());
102  std::partial_sort_copy(values.begin(), values.end(), sorted_values.begin(), sorted_values.end());
103 
104  // Get the min and max values
105  auto min = sorted_values.front();
106  auto max = sorted_values.back();
107 
108  // The bin stride/length
109  auto bin_stride = (max - min) / static_cast<Real>(_num_bins);
110 
111  auto current_value_iter = sorted_values.begin();
112  auto sorted_values_end = sorted_values.end();
113 
114  // Fill the bins
115  for (unsigned int bin = 0; bin < _num_bins; bin++)
116  {
117  // Compute bin edges
118  // These are computed individually on purpose so that the exact same values will match the
119  // previous and next bins
120  auto lower = (bin * bin_stride) + min;
121  auto upper = ((bin + 1) * bin_stride) + min;
122 
123  lower_vector[bin] = lower;
124  upper_vector[bin] = upper;
125 
126  // Find the number of values that fall in this bin
127  unsigned long int num_values = 0;
128  while (current_value_iter != sorted_values_end && *current_value_iter <= upper)
129  {
130  num_values++;
131  current_value_iter++;
132  }
133 
134  histogram[bin] = static_cast<Real>(num_values);
135  }
136 }
const unsigned int & _num_bins
The number of bins.
Computes a histogram for each column in a given VectorPostprocessor.
Helper class to hold the vectors for the histogram.
InputParameters validParams< HistogramVectorPostprocessor >()
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
VectorPostprocessorValue * _upper
The upper edges for each bin.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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...
virtual void execute() override
Execute method.
const std::vector< std::pair< std::string, VectorPostprocessorData::VectorPostprocessorState > > & getVectorPostprocessorVectors(const std::string &vpp_name)
Get the vectors for a specific VectorPostprocessor.
std::map< std::string, HistoData > _histogram_data
The VPP vectors that will hold the Histogram for each column.
registerMooseObject("MooseApp", HistogramVectorPostprocessor)
InputParameters validParams< GeneralVectorPostprocessor >()
const VectorPostprocessorName & _vpp_name
The name of the VPP to work on.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
VectorPostprocessorValue * _lower
The lower edges for each bin.
virtual void finalize() override
Finalize.
VectorPostprocessorValue * _histogram
Where the data will actually be stored.
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:141
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
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...
HistogramVectorPostprocessor(const InputParameters &parameters)
void computeHistogram(const std::vector< Real > &values, HistoData &histo_data)
Compute the histogram of a vector and fill in the lower/upper edges.