https://mooseframework.inl.gov
AccumulateReporter.h
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 
10 #pragma once
11 
12 #include "GeneralReporter.h"
13 
15 template <typename T>
17 
19 {
20 public:
23  virtual void initialize() override {}
24  virtual void execute() override;
25  virtual void finalize() override {}
26  virtual void declareLateValues() override;
27 
28 protected:
33  template <typename T>
34  bool declareAccumulateHelper(const ReporterName & rname);
35 
37  std::vector<std::unique_ptr<AccumulatedValueBase>> _accumulated_values;
38 };
39 
40 template <typename T>
41 bool
43 {
44  const ReporterData & rdata = _fe_problem.getReporterData();
45 
46  if (!rdata.hasReporterValue<T>(rname))
47  return false;
48 
49  const auto & pmode = rdata.getReporterMode(rname);
51  if (pmode == REPORTER_MODE_ROOT)
52  rmode = REPORTER_MODE_ROOT;
53  else if (pmode == REPORTER_MODE_REPLICATED)
55  else if (pmode == REPORTER_MODE_DISTRIBUTED)
57  const T & val = getReporterValueByName<T>(rname);
58  std::vector<T> & acc_val =
59  declareValueByName<std::vector<T>>(rname.getObjectName() + ":" + rname.getValueName(), rmode);
60 
61  _accumulated_values.push_back(std::make_unique<AccumulatedValue<T>>(val, acc_val));
62  return true;
63 }
64 
66 {
67 public:
68  virtual ~AccumulatedValueBase() = default;
69 
70  virtual void accumulate(unsigned int index) = 0;
71 };
72 
73 template <typename T>
75 {
76 public:
77  AccumulatedValue(const T & val, std::vector<T> & acc_val)
78  : AccumulatedValueBase(), _val(val), _acc_val(acc_val)
79  {
80  }
81 
82  virtual void accumulate(unsigned int index) override
83  {
84  if (_acc_val.size() <= index)
85  _acc_val.resize(index + 1, _val);
86  else
87  _acc_val[index] = _val;
88  }
89 
90 private:
91  const T & _val;
92  std::vector<T> & _acc_val;
93 };
std::vector< T > & _acc_val
virtual void finalize() override
Finalize.
virtual void declareLateValues() override
Method that can be overriden to declare "late" Reporter values.
Reporter object that has a single execution of the "execute" method for for each execute flag...
const ReporterMode REPORTER_MODE_UNSET
const ReporterMode REPORTER_MODE_ROOT
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
AccumulatedValue(const T &val, std::vector< T > &acc_val)
const ReporterData & getReporterData() const
Provides const access the ReporterData object.
virtual void execute() override
Execute method.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
const ReporterMode REPORTER_MODE_DISTRIBUTED
const std::string & getObjectName() const
Return the object name that produces the Reporter value.
Definition: ReporterName.C:35
const ReporterProducerEnum & getReporterMode(const ReporterName &reporter_name) const
Return the ReporterProducerEnum for an existing ReporterValue.
Definition: ReporterData.C:191
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:211
virtual void accumulate(unsigned int index)=0
virtual ~AccumulatedValueBase()=default
const InputParameters & parameters() const
Get the parameters of the object.
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
Definition: ReporterData.h:445
AccumulateReporter(const InputParameters &parameters)
const ReporterMode REPORTER_MODE_REPLICATED
std::vector< std::unique_ptr< AccumulatedValueBase > > _accumulated_values
Vector of accumulated value objects.
const std::string & getValueName() const
Return the data name for the Reporter value.
Definition: ReporterName.C:41
virtual void accumulate(unsigned int index) override
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:44
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
bool declareAccumulateHelper(const ReporterName &rname)
Helper for declaring an accumulative reporter value This will fill in _accumulated_values if the repo...
This is a helper class for managing the storage of declared Reporter object values.
Definition: ReporterData.h:48