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 
54 #ifdef MOOSE_KOKKOS_ENABLED
55 
58  Reporter(const Reporter & object, const Moose::Kokkos::FunctorCopy & key);
59 #endif
60 
61  virtual void store(nlohmann::json & json) const;
62 
69  virtual bool shouldStore() const { return true; }
70 
79  virtual void declareLateValues() {}
80 
81 protected:
83 
122  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
123  T & declareValue(const std::string & param_name, Args &&... args);
124  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
125  T & declareValue(const std::string & param_name, ReporterMode mode, Args &&... args);
126  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
127  T & declareValueByName(const ReporterValueName & value_name, Args &&... args);
128  template <typename T, template <typename> class S = ReporterGeneralContext, typename... Args>
129  T & declareValueByName(const ReporterValueName & value_name, ReporterMode mode, Args &&... args);
130 
131  template <typename T, typename S, typename... Args>
132  T & declareValue(const std::string & param_name, Args &&... args);
133  template <typename T, typename S, typename... Args>
134  T & declareValue(const std::string & param_name, ReporterMode mode, Args &&... args);
135  template <typename T, typename S, typename... Args>
136  T & declareValueByName(const ReporterValueName & value_name, Args &&... args);
137  template <typename T, typename S, typename... Args>
138  T & declareValueByName(const ReporterValueName & value_name, ReporterMode mode, Args &&... args);
140 
151  template <typename T, typename... Args>
152  T & declareUnusedValue(Args &&... args);
153 
154 private:
162  {
164  virtual ~UnusedWrapperBase() {}
165  };
166 
172  template <typename T>
174  {
175  T value;
176  };
177 
183  const ReporterValueName & getReporterValueName(const std::string & param_name) const;
184 
187 
190 
192  const std::string & _reporter_name;
193 
196 
199 
201  std::vector<std::unique_ptr<UnusedWrapperBase>> _unused_values;
202 };
203 
204 template <typename T, template <typename> class S, typename... Args>
205 T &
206 Reporter::declareValue(const std::string & param_name, Args &&... args)
207 {
208  return declareValue<T, S<T>>(param_name, REPORTER_MODE_UNSET, args...);
209 }
210 
211 template <typename T, template <typename> class S, typename... Args>
212 T &
213 Reporter::declareValue(const std::string & param_name, ReporterMode mode, Args &&... args)
214 {
215  return declareValue<T, S<T>>(param_name, mode, args...);
216 }
217 
218 template <typename T, typename S, typename... Args>
219 T &
220 Reporter::declareValue(const std::string & param_name, Args &&... args)
221 {
222  return declareValue<T, S>(param_name, REPORTER_MODE_UNSET, args...);
223 }
224 
225 template <typename T, typename S, typename... Args>
226 T &
227 Reporter::declareValue(const std::string & param_name, ReporterMode mode, Args &&... args)
228 {
229  return declareValueByName<T, S>(getReporterValueName(param_name), mode, args...);
230 }
231 
232 template <typename T, template <typename> class S, typename... Args>
233 T &
234 Reporter::declareValueByName(const ReporterValueName & value_name, Args &&... args)
235 {
236  return declareValueByName<T, S<T>>(value_name, REPORTER_MODE_UNSET, args...);
237 }
238 
239 template <typename T, template <typename> class S, typename... Args>
240 T &
241 Reporter::declareValueByName(const ReporterValueName & value_name,
242  ReporterMode mode,
243  Args &&... args)
244 {
245  return declareValueByName<T, S<T>>(value_name, mode, args...);
246 }
247 
248 template <typename T, typename S, typename... Args>
249 T &
250 Reporter::declareValueByName(const ReporterValueName & value_name, Args &&... args)
251 {
252  return declareValueByName<T, S>(value_name, REPORTER_MODE_UNSET, args...);
253 }
254 
255 template <typename T, typename S, typename... Args>
256 T &
257 Reporter::declareValueByName(const ReporterValueName & value_name,
258  ReporterMode mode,
259  Args &&... args)
260 {
261  const ReporterName state_name(_reporter_name, value_name);
262 
263  buildOutputHideVariableList({state_name.getCombinedName()});
264 
265  // Only thread 0 will declare the reporter value. The rest will get a reference
266  // to an UnusedValue
269  : 0;
270  if (tid)
271  return declareUnusedValue<T>();
273  state_name, mode, _reporter_moose_object, args...);
274 }
275 
276 template <typename T, typename... Args>
277 T &
279 {
280  _unused_values.emplace_back(std::make_unique<UnusedWrapper<T>>(std::forward(args)...));
281  UnusedWrapper<T> * wrapper = dynamic_cast<UnusedWrapper<T> *>(_unused_values.back().get());
282  return wrapper->value;
283 }
T & declareValueByName(const ReporterValueName &value_name, Args &&... args)
Definition: Reporter.h:234
std::vector< std::unique_ptr< UnusedWrapperBase > > _unused_values
Storage for unused values declared with declareUnusedValue().
Definition: Reporter.h:201
virtual bool shouldStore() const
Definition: Reporter.h:69
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:406
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
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
FEProblemBase & _reporter_fe_problem
Needed for access to FEProblemBase::getReporterData.
Definition: Reporter.h:195
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:61
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:173
const std::string & _reporter_name
The name of the MooseObject, from "_object_name" param.
Definition: Reporter.h:192
virtual void store(nlohmann::json &json) const
Definition: Reporter.C:55
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:164
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:198
virtual void declareLateValues()
Method that can be overriden to declare "late" Reporter values.
Definition: Reporter.h:79
T & declareValue(const std::string &param_name, Args &&... args)
Method to define a value that the Reporter object is to produced.
Definition: Reporter.h:206
const InputParameters & _reporter_params
Ref. to MooseObject params.
Definition: Reporter.h:189
static InputParameters validParams()
Definition: Reporter.C:16
T & declareUnusedValue(Args &&... args)
Declare a unused value with type T.
Definition: Reporter.h:278
const MooseObject & _reporter_moose_object
The MooseObject creating this Reporter.
Definition: Reporter.h:186
Internal base struct for use in storing unused values.
Definition: Reporter.h:161
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:45
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:237
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