https://mooseframework.inl.gov
Loading...
Searching...
No Matches
TimeWindowedStatisticsPostProcessor.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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#include <cmath>
14
16
19{
22 "Windowed statistics (mean, std, drift) and a running time average of a postprocessor, "
23 "using only samples from start_time onwards.");
24
25 params.addRequiredParam<PostprocessorName>("postprocessor", "The postprocessor to monitor.");
26 params.addRequiredParam<Real>("start_time",
27 "Simulation time from which samples are used for all statistics.");
28 params.addRangeCheckedParam<unsigned int>(
29 "window", 100, "window > 1", "Number of time steps in one window.");
30
31 MooseEnum stats(getStatisticOptions(), "mean");
32 params.addParam<MooseEnum>("statistic", stats, "Which statistic to report.");
33
34 params.addParam<Real>("reference_value",
35 0.0,
36 "Scale used to normalize relative_drift. "
37 "If 0, the magnitude of the window mean is used.");
38 return params;
39}
40
42 const InputParameters & parameters)
43 : GeneralPostprocessor(parameters),
44 _pp(getPostprocessorValue("postprocessor")),
45 _window(getParam<unsigned int>("window")),
46 _statistic(getParam<MooseEnum>("statistic").getEnum<Statistic>()),
47 _start_time(getParam<Real>("start_time")),
48 _reference(getParam<Real>("reference_value")),
49 _history(declareRestartableData<std::vector<Real>>("history")),
50 _last_step(declareRestartableData<int>("last_step", -1)),
51 _averaging(declareRestartableData<bool>("averaging", false)),
52 _sum(declareRestartableData<Real>("sum", 0.0)),
53 _avg_time(declareRestartableData<Real>("avg_time", 0.0)),
54 _value(0.0)
55{
56}
57
58Real
59TimeWindowedStatisticsPostProcessor::windowMean(std::size_t begin, std::size_t end) const
60{
61 Real s = 0.0;
62 for (std::size_t i = begin; i < end; ++i)
63 s += _history[i];
64 return end > begin ? s / (end - begin) : 0.0;
65}
66
67void
69{
70 if (!_averaging && _t >= _start_time)
71 _averaging = true;
72
73 // Before start_time: no statistics yet
74 if (!_averaging)
75 {
76 if (_statistic == Statistic::MEAN || _statistic == Statistic::TIME_INTEGRATED_MEAN)
77 _value = _pp;
78 else // STD, STATISTICS_TIME
79 _value = 0.0;
80 return;
81 }
82
83 // Record at most one sample per time step (guards against repeated execution)
84 if (_t_step != _last_step)
85 {
87
88 _history.push_back(_pp);
89 if (_history.size() > 2 * _window)
90 _history.erase(_history.begin());
91
92 // Time integral for time_integrated_mean and statistics_time only. The time step is clipped
93 // to start at start_time, so that no time before start_time is included.
94 const Real dt = _t - std::max(_t - _dt, _start_time);
95 _sum += _pp * dt;
96 _avg_time += dt;
97 }
98
99 const std::size_t n = _history.size();
100 const std::size_t nw = std::min<std::size_t>(n, _window);
101
102 switch (_statistic)
103 {
104 case Statistic::MEAN:
105 _value = windowMean(n - nw, n);
106 break;
107
108 case Statistic::STD:
109 {
110 const Real m = windowMean(n - nw, n);
111 Real s = 0.0;
112 for (std::size_t i = n - nw; i < n; ++i)
113 s += (_history[i] - m) * (_history[i] - m);
114 _value = nw > 1 ? std::sqrt(s / (nw - 1)) : 0.0;
115 break;
116 }
117
118 case Statistic::TIME_INTEGRATED_MEAN:
119 // The integration interval is empty at t = start_time
120 _value = _avg_time > 0 ? _sum / _avg_time : _pp;
121 break;
122
123 case Statistic::STATISTICS_TIME:
125 break;
126 }
127}
registerMooseObject("MooseApp", TimeWindowedStatisticsPostProcessor)
void ErrorVector unsigned int
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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 addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Time statistics of another postprocessor, using only samples with t >= start_time.
Real _value
Value of the selected statistic computed in the latest execute()
Real & _sum
Time integral of the monitored value since start_time, with the rectangle rule.
Real windowMean(std::size_t begin, std::size_t end) const
Arithmetic mean of the stored samples in the half-open index range [begin, end)
Real & _avg_time
Length of the time integration interval, the sum of the time steps clipped to start_time.
TimeWindowedStatisticsPostProcessor(const InputParameters &parameters)
const PostprocessorValue & _pp
Value of the monitored postprocessor.
bool & _averaging
Whether averaging has started, i.e. a time step with t >= start_time has been reached.
const Statistic _statistic
Statistic reported by getValue()
const Real _start_time
Simulation time from which samples are recorded.
int & _last_step
Time step index of the last recorded sample, used to record at most one sample per step.
std::vector< Real > & _history
Recorded samples, one per time step, most recent last; capped at 2 * _window entries.
virtual void execute() override
Execute method.
const unsigned int _window
Number of most recent time steps used by the windowed statistics (mean, std)
Real & _dt
Time step size.
int & _t_step
The number of the time step.