https://mooseframework.inl.gov
Reporter.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 // Moose includes
13 #include "MooseTypes.h"
14 #include "OutputInterface.h"
15 #include "ReporterData.h"
16 #include "InputParameters.h"
17 
18 // System includes
19 #include <type_traits>
20 
21 // Forward declarations
22 class FEProblemBase;
23 
47 class Reporter : public OutputInterface
48 {
49 public:
51  Reporter(const MooseObject * moose_object);
52  virtual ~Reporter() = default;
53  virtual void store(nlohmann::json & json) const;
54 
61  virtual bool shouldStore() const { return true; }
62 
71  virtual void declareLateValues() {}
72 
73 protected:
75 
114  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
115  T & declareValue(const std::string & param_name, Args &&... args);
116  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
117  T & declareValue(const std::string & param_name, ReporterMode mode, Args &&... args);
118  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
119  T & declareValueByName(const ReporterValueName & value_name, Args &&... args);
120  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
121  T & declareValueByName(const ReporterValueName & value_name, ReporterMode mode, Args &&... args);
122 
123  template <typename T, typename S, typename... Args>
124  T & declareValue(const std::string & param_name, Args &&... args);
125  template <typename T, typename S, typename... Args>
126  T & declareValue(const std::string & param_name, ReporterMode mode, Args &&... args);
127  template <typename T, typename S, typename... Args>
128  T & declareValueByName(const ReporterValueName & value_name, Args &&... args);
129  template <typename T, typename S, typename... Args>
130  T & declareValueByName(const ReporterValueName & value_name, ReporterMode mode, Args &&... args);
132 
143  template <typename T, typename... Args>
144  T & declareUnusedValue(Args &&... args);
145 
146 private:
154  {
156  virtual ~UnusedWrapperBase() {}
157  };
158 
164  template <typename T>
166  {
167  T value;
168  };
169 
175  const ReporterValueName & getReporterValueName(const std::string & param_name) const;
176 
179 
182 
184  const std::string & _reporter_name;
185 
188 
191 
193  std::vector<std::unique_ptr<UnusedWrapperBase>> _unused_values;
194 };
195 
196 template <typename T, template <typename> class S, typename... Args>
197 T &
198 Reporter::declareValue(const std::string & param_name, Args &&... args)
199 {
200  return declareValue<T, S<T>>(param_name, REPORTER_MODE_UNSET, args...);
201 }
202 
203 template <typename T, template <typename> class S, typename... Args>
204 T &
205 Reporter::declareValue(const std::string & param_name, ReporterMode mode, Args &&... args)
206 {
207  return declareValue<T, S<T>>(param_name, mode, args...);
208 }
209 
210 template <typename T, typename S, typename... Args>
211 T &
212 Reporter::declareValue(const std::string & param_name, Args &&... args)
213 {
214  return declareValue<T, S>(param_name, REPORTER_MODE_UNSET, args...);
215 }
216 
217 template <typename T, typename S, typename... Args>
218 T &
219 Reporter::declareValue(const std::string & param_name, ReporterMode mode, Args &&... args)
220 {
221  return declareValueByName<T, S>(getReporterValueName(param_name), mode, args...);
222 }
223 
224 template <typename T, template <typename> class S, typename... Args>
225 T &
226 Reporter::declareValueByName(const ReporterValueName & value_name, Args &&... args)
227 {
228  return declareValueByName<T, S<T>>(value_name, REPORTER_MODE_UNSET, args...);
229 }
230 
231 template <typename T, template <typename> class S, typename... Args>
232 T &
233 Reporter::declareValueByName(const ReporterValueName & value_name,
234  ReporterMode mode,
235  Args &&... args)
236 {
237  return declareValueByName<T, S<T>>(value_name, mode, args...);
238 }
239 
240 template <typename T, typename S, typename... Args>
241 T &
242 Reporter::declareValueByName(const ReporterValueName & value_name, Args &&... args)
243 {
244  return declareValueByName<T, S>(value_name, REPORTER_MODE_UNSET, args...);
245 }
246 
247 template <typename T, typename S, typename... Args>
248 T &
249 Reporter::declareValueByName(const ReporterValueName & value_name,
250  ReporterMode mode,
251  Args &&... args)
252 {
253  const ReporterName state_name(_reporter_name, value_name);
254 
255  buildOutputHideVariableList({state_name.getCombinedName()});
256 
257  // Only thread 0 will declare the reporter value. The rest will get a reference
258  // to an UnusedValue
261  : 0;
262  if (tid)
263  return declareUnusedValue<T>();
265  state_name, mode, _reporter_moose_object, args...);
266 }
267 
268 template <typename T, typename... Args>
269 T &
271 {
272  _unused_values.emplace_back(std::make_unique<UnusedWrapper<T>>(std::forward(args)...));
273  UnusedWrapper<T> * wrapper = dynamic_cast<UnusedWrapper<T> *>(_unused_values.back().get());
274  return wrapper->value;
275 }
T & declareValueByName(const ReporterValueName &value_name, Args &&... args)
Definition: Reporter.h:226
std::vector< std::unique_ptr< UnusedWrapperBase > > _unused_values
Storage for unused values declared with declareUnusedValue().
Definition: Reporter.h:193
virtual bool shouldStore() const
Definition: Reporter.h:61
T & declareReporterValue(const ReporterName &reporter_name, const ReporterMode &mode, const MooseObject &producer, Args &&... args)
Method for returning a writable reference to the current Reporter value.
Definition: ReporterData.h:400
const ReporterMode REPORTER_MODE_UNSET
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
virtual ~Reporter()=default
FEProblemBase & _reporter_fe_problem
Needed for access to FEProblemBase::getReporterData.
Definition: Reporter.h:187
A class to provide an common interface to objects requiring "outputs" option.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const ReporterValueName & getReporterValueName(const std::string &param_name) const
Definition: Reporter.C:49
Reporter objects allow for the declaration of arbitrary data types that are aggregate values for a si...
Definition: Reporter.h:47
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Internal struct for storing a unused value.
Definition: Reporter.h:165
const std::string & _reporter_name
The name of the MooseObject, from "_object_name" param.
Definition: Reporter.h:184
virtual void store(nlohmann::json &json) const
Definition: Reporter.C:43
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
virtual ~UnusedWrapperBase()
Needed for polymorphism.
Definition: Reporter.h:156
Reporter(const MooseObject *moose_object)
Definition: Reporter.C:29
void buildOutputHideVariableList(std::set< std::string > variable_names)
Builds hide lists for output objects NOT listed in the &#39;outputs&#39; parameter.
ReporterData & _reporter_data
Data storage.
Definition: Reporter.h:190
virtual void declareLateValues()
Method that can be overriden to declare "late" Reporter values.
Definition: Reporter.h:71
T & declareValue(const std::string &param_name, Args &&... args)
Method to define a value that the Reporter object is to produced.
Definition: Reporter.h:198
const InputParameters & _reporter_params
Ref. to MooseObject params.
Definition: Reporter.h:181
static InputParameters validParams()
Definition: Reporter.C:16
T & declareUnusedValue(Args &&... args)
Declare a unused value with type T.
Definition: Reporter.h:270
const MooseObject & _reporter_moose_object
The MooseObject creating this Reporter.
Definition: Reporter.h:178
const InputParameters & parameters() const
Get the parameters of the object.
Internal base struct for use in storing unused values.
Definition: Reporter.h:153
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
unsigned int THREAD_ID
Definition: MooseTypes.h:209
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
This is a helper class for managing the storage of declared Reporter object values.
Definition: ReporterData.h:48