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 
18 {
20  params.addClassDescription("Compute a histogram for each column of a VectorPostprocessor");
21 
22  params.addRequiredParam<VectorPostprocessorName>(
23  "vpp", "The VectorPostprocessor to compute histogram of");
24 
25  params.addRequiredParam<unsigned int>("num_bins", "The number of bins for the histograms");
26 
27  return params;
28 }
29 
31  : GeneralVectorPostprocessor(parameters),
32  _vpp_name(getParam<VectorPostprocessorName>("vpp")),
33  _num_bins(getParam<unsigned int>("num_bins"))
34 {
35 }
36 
37 void
39 {
40  const VectorPostprocessor & vpp = getUserObjectByName<VectorPostprocessor>(_vpp_name);
41  for (const auto & vec_name : vpp.getVectorNames())
42  _histogram_data[vec_name] = {&declareVector(vec_name + "_lower"),
43  &declareVector(vec_name + "_upper"),
44  &declareVector(vec_name)};
45  if (_histogram_data.empty())
46  paramError("vpp", "The specified VectorPostprocessor does not have any declared vectors");
47 }
48 
49 void
51 {
52  // no need to reset, execute() writes in place
53 }
54 
55 void
57 {
58  if (processor_id() == 0) // Only compute on processor 0
59  {
60  const VectorPostprocessor & vpp = getUserObjectByName<VectorPostprocessor>(_vpp_name);
61  for (const auto & vec_name : vpp.getVectorNames())
62  {
63  const auto & values = _fe_problem.getVectorPostprocessorValueByName(_vpp_name, vec_name);
64 
65  mooseAssert(_histogram_data.count(vec_name), "Error retrieving VPP vector");
66  auto & histo_data = _histogram_data.at(vec_name);
67  computeHistogram(values, histo_data);
68  }
69  }
70 }
71 
72 void
74 {
75 }
76 
77 void
78 HistogramVectorPostprocessor::computeHistogram(const std::vector<Real> & values,
79  HistoData & histo_data)
80 {
81  if (values.empty())
82  mooseError("Cannot compute histogram without data!");
83 
84  // Grab the vectors to fill
85  auto & lower_vector = *histo_data._lower;
86  auto & upper_vector = *histo_data._upper;
87  auto & histogram = *histo_data._histogram;
88 
89  // Resize everything
90  // Note: no need to zero anything out
91  // that will automatically be done if the bin should be zero by the algorithm below
92  lower_vector.resize(_num_bins);
93  upper_vector.resize(_num_bins);
94  histogram.resize(_num_bins);
95 
96  // Create a sorted copy of the values
97  std::vector<Real> sorted_values(values.size());
98  std::partial_sort_copy(values.begin(), values.end(), sorted_values.begin(), sorted_values.end());
99 
100  // Get the min and max values
101  auto min = sorted_values.front();
102  auto max = sorted_values.back();
103 
104  // The bin stride/length
105  auto bin_stride = (max - min) / static_cast<Real>(_num_bins);
106 
107  auto current_value_iter = sorted_values.begin();
108  auto sorted_values_end = sorted_values.end();
109 
110  // Fill the bins
111  for (unsigned int bin = 0; bin < _num_bins; bin++)
112  {
113  // Compute bin edges
114  // These are computed individually on purpose so that the exact same values will match the
115  // previous and next bins
116  auto lower = (bin * bin_stride) + min;
117  auto upper = ((bin + 1) * bin_stride) + min;
118 
119  lower_vector[bin] = lower;
120  upper_vector[bin] = upper;
121 
122  // Find the number of values that fall in this bin
123  unsigned long int num_values = 0;
124  while (current_value_iter != sorted_values_end && *current_value_iter <= upper)
125  {
126  num_values++;
127  current_value_iter++;
128  }
129 
130  histogram[bin] = static_cast<Real>(num_values);
131  }
132 }
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.
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 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...
auto max(const L &left, const R &right)
virtual void execute() override
Execute method.
const VectorPostprocessorValue & getVectorPostprocessorValueByName(const std::string &object_name, const std::string &vector_name, std::size_t t_index=0) const
Get a read-only reference to the vector value associated with the VectorPostprocessor.
std::map< std::string, HistoData > _histogram_data
The VPP vectors that will hold the Histogram for each column.
static InputParameters validParams()
registerMooseObject("MooseApp", HistogramVectorPostprocessor)
const VectorPostprocessorName & _vpp_name
The name of the VPP to work on.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
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.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:210
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
HistogramVectorPostprocessor(const InputParameters &parameters)
processor_id_type processor_id() const
Base class for Postprocessors that produce a vector of values.
auto min(const L &left, const R &right)
void computeHistogram(const std::vector< Real > &values, HistoData &histo_data)
Compute the histogram of a vector and fill in the lower/upper edges.
const std::set< std::string > & getVectorNames() const
Return the names of the vectors associated with this object.
void ErrorVector unsigned int