https://mooseframework.inl.gov
ConstantReporter.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 {
16 public:
19  virtual void initialize() override {}
20  virtual void finalize() override {}
21  virtual void execute() override {}
22 
23 protected:
25  template <typename T>
26  static InputParameters addReporterTypeParams(const std::string & prefix, bool add_vector = true);
27 
30  template <typename T>
31  std::vector<T *> declareConstantReporterValues(const std::string & prefix);
32  template <typename T>
33  std::vector<std::vector<T> *> declareConstantVectorReporterValues(const std::string & prefix);
34  template <typename T>
35  std::vector<std::vector<std::vector<T>> *>
36  declareConstantVectorVectorReporterValues(const std::string & prefix);
38 };
39 
40 template <typename T>
42 ConstantReporter::addReporterTypeParams(const std::string & prefix, bool add_vector)
43 {
45 
46  params.addParam<std::vector<ReporterValueName>>(prefix + "_names",
47  "Names for each " + prefix + " value.");
48  params.addParam<std::vector<T>>(prefix + "_values", "Values for " + prefix + "s.");
49  if (add_vector)
50  {
51  params.addParam<std::vector<ReporterValueName>>(
52  prefix + "_vector_names", "Names for each vector of " + prefix + "s value.");
53  params.addParam<std::vector<std::vector<T>>>(prefix + "_vector_values",
54  "Values for vectors of " + prefix + "s.");
55 
56  params.addParam<std::vector<ReporterValueName>>(prefix + "_vector_vector_names",
57  "Names for each vector of vectors of " +
58  prefix + "s value.");
59  params.addParam<std::vector<std::vector<std::vector<T>>>>(
60  prefix + "_vector_vector_values", "Values for vectors of vectors of " + prefix + "s.");
61  }
62 
63  return params;
64 }
65 
66 template <typename T>
67 std::vector<T *>
69 {
70  std::string names_param(prefix + "_names");
71  std::string values_param(prefix + "_values");
72  std::vector<T *> data;
73 
74  if (isParamValid(names_param) && !isParamValid(values_param))
75  paramError(names_param, "Must specify values using ", values_param);
76  else if (!isParamValid(names_param) && isParamValid(values_param))
77  paramError(values_param, "Use ", names_param, " to specify reporter names.");
78  else if (!isParamValid(names_param) && !isParamValid(values_param))
79  return data;
80 
81  auto & names = getParam<std::vector<ReporterValueName>>(names_param);
82  auto & values = this->getParam<std::vector<T>>(values_param);
83  if (names.size() != values.size())
84  paramError(values_param,
85  "Number of names specified in ",
86  names_param,
87  " must match number of values specified in ",
88  values_param);
89 
90  for (unsigned int i = 0; i < names.size(); ++i)
91  data.push_back(&this->declareValueByName<T>(names[i], values[i]));
92 
93  return data;
94 }
95 
96 template <typename T>
97 std::vector<std::vector<T> *>
99 {
100  return this->declareConstantReporterValues<std::vector<T>>(prefix + "_vector");
101 }
102 
103 template <typename T>
104 std::vector<std::vector<std::vector<T>> *>
106 {
107  return this->declareConstantReporterValues<std::vector<std::vector<T>>>(prefix +
108  "_vector_vector");
109 }
virtual void execute() override
Execute method.
Reporter object that has a single execution of the "execute" method for for each execute flag...
ConstantReporter(const InputParameters &parameters)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< std::vector< std::vector< T > > * > declareConstantVectorVectorReporterValues(const std::string &prefix)
InputParameters emptyInputParameters()
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
static InputParameters validParams()
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 ...
static InputParameters addReporterTypeParams(const std::string &prefix, bool add_vector=true)
This will add another type of reporter to the params.
virtual void finalize() override
Finalize.
const InputParameters & parameters() const
Get the parameters of the object.
std::vector< T * > declareConstantReporterValues(const std::string &prefix)
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...
std::vector< std::vector< T > * > declareConstantVectorReporterValues(const std::string &prefix)
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.