Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ReporterData.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 
10 #include "ReporterData.h"
11 #include "MooseApp.h"
12 #include "VariadicTable.h"
13 
14 ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {}
15 
16 void
18 {
19  for (const auto & name_context_pair : _context_ptrs)
20  name_context_pair.second->copyValuesBack();
21 }
22 
23 void
25 {
26  // Table of reporter values that were and were not restored
27  VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"});
28 
29  for (const auto & [rname, context] : _context_ptrs)
30  {
31  const bool restored = context->restoreState();
32 
33  if (verbose)
34  summary_table.addRow(
35  rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO");
36  }
37 
38  if (verbose && !_context_ptrs.empty())
39  {
40  std::stringstream oss;
41  oss << "Reporter Restoration Summary:\n";
42  summary_table.print(oss);
43  mooseInfo(oss.str());
44  }
45 }
46 
47 void
48 ReporterData::finalize(const std::string & object_name)
49 {
50  for (auto & name_context_pair : _context_ptrs)
51  if (name_context_pair.first.getObjectName() == object_name)
52  name_context_pair.second->finalize();
53 }
54 
55 bool
56 ReporterData::hasReporterValue(const ReporterName & reporter_name) const
57 {
58  return _context_ptrs.count(reporter_name);
59 }
60 
61 std::set<ReporterName>
63 {
64  std::set<ReporterName> output;
65  for (const auto & name_context_pair : _context_ptrs)
66  output.insert(name_context_pair.second->name());
67  return output;
68 }
69 
70 std::set<std::string>
72 {
73  std::set<std::string> output;
74  for (const auto & name_context_pair : _context_ptrs)
75  if (name_context_pair.first.isPostprocessor())
76  output.insert(name_context_pair.first.getObjectName());
77  return output;
78 }
79 
80 DenseVector<Real>
82 {
83  DenseVector<Real> all_values;
84 
85  std::vector<Real> & output = all_values.get_values();
86 
87  for (const auto & name_context_pair : _context_ptrs)
88  {
89  const ReporterName & rname = name_context_pair.first;
90 
91  if (hasReporterValue<Real>(rname))
92  output.push_back(getReporterValue<Real>(rname.getCombinedName()));
93 
94  if (hasReporterValue<std::vector<Real>>(rname))
95  {
96  const auto & vec = getReporterValue<std::vector<Real>>(rname.getCombinedName());
97  for (const auto & v : vec)
98  output.push_back(v);
99  }
100  }
101 
102  return all_values;
103 }
104 
105 std::vector<std::string>
107 {
108  std::vector<std::string> output;
109 
110  for (const auto & name_context_pair : _context_ptrs)
111  {
112  const ReporterName & rname = name_context_pair.first;
113 
114  if (hasReporterValue<Real>(rname))
115  output.push_back(rname.getCombinedName());
116 
117  if (hasReporterValue<std::vector<Real>>(rname))
118  {
119  auto pname = rname.getCombinedName();
120  const auto & vec = getReporterValue<std::vector<Real>>(pname);
121  for (unsigned int i = 0; i < vec.size(); ++i)
122  output.push_back(pname + "/" + std::to_string(i));
123  }
124  }
125 
126  return output;
127 }
128 
129 const ReporterContextBase &
131 {
132  if (!hasReporterValue(reporter_name))
133  mooseError("Unable to locate Reporter context with name: ", reporter_name);
134  return *_context_ptrs.at(reporter_name);
135 }
136 
139 {
140  if (!hasReporterValue(reporter_name))
141  mooseError("Unable to locate Reporter context with name: ", reporter_name);
142  return *_context_ptrs.at(reporter_name);
143 }
144 
145 const ReporterStateBase &
147 {
148  if (!hasReporterState(reporter_name))
149  mooseError("Unable to locate Reporter state with name: ", reporter_name);
150  return *_states.at(reporter_name);
151 }
152 
155 {
156  if (!hasReporterState(reporter_name))
157  mooseError("Unable to locate Reporter state with name: ", reporter_name);
158  return *_states.at(reporter_name);
159 }
160 
161 void
163 {
164  std::string missing;
165  for (const auto & name_state_pair : _states)
166  if (!hasReporterValue(name_state_pair.first))
167  missing += getReporterInfo(name_state_pair.first) + "\n";
168 
169  if (missing.size())
170  mooseError("The following Reporter(s) were not declared:\n\n", missing);
171 }
172 
174 ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr,
175  bool declare) const
176 {
177  return _app.registerRestartableData(std::move(data_ptr), 0, !declare);
178 }
179 
180 bool
181 ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const
182 {
183  for (const auto & name_context_pair : _context_ptrs)
184  if (name_context_pair.first.getObjectName() == obj_name &&
185  name_context_pair.second->getProducerModeEnum() == mode)
186  return true;
187  return false;
188 }
189 
190 const ReporterProducerEnum &
191 ReporterData::getReporterMode(const ReporterName & reporter_name) const
192 {
193  return getReporterContextBase(reporter_name).getProducerModeEnum();
194 }
195 
196 bool
197 ReporterData::hasReporterState(const ReporterName & reporter_name) const
198 {
199  return _states.count(reporter_name);
200 }
201 
202 std::string
204 {
205  std::stringstream oss;
206 
207  const auto & name = state.getReporterName();
208 
209  if (name.isPostprocessor())
210  oss << "Postprocessor \"" << name.getObjectName() << "\":\n";
211  else
212  {
213  oss << name.specialTypeToName() << " \"" << name.getCombinedName() << "\":\n Type:\n "
214  << state.valueType() << "\n";
215  }
216  oss << " Producer:\n ";
217  if (context)
218  {
219  oss << context->getProducer().type() << " \"" << context->getProducer().name() << "\"";
220  oss << "\n Context type:\n " << context->contextType();
221  }
222  else
223  oss << "None";
224  oss << "\n Consumer(s):\n";
225  if (state.getConsumers().empty())
226  oss << " None\n";
227  else
228  for (const auto & mode_object_pair : state.getConsumers())
229  {
230  const ReporterMode mode = mode_object_pair.first;
231  const MooseObject * object = mode_object_pair.second;
232  oss << " " << object->typeAndName() << " (mode: " << mode << ")\n";
233  }
234 
235  return oss.str();
236 }
237 
238 std::string
239 ReporterData::getReporterInfo(const ReporterName & reporter_name) const
240 {
241  return getReporterInfo(getReporterStateBase(reporter_name),
242  hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name)
243  : nullptr);
244 }
245 
246 std::string
248 {
249  std::string out = _states.empty() ? "No reporters were requested or declared." : "";
250  for (const auto & name : getReporterNames())
251  out += getReporterInfo(name) + "\n";
252  return out;
253 }
std::string name(const ElemQuality q)
ReporterData(MooseApp &moose_app)
Definition: ReporterData.C:14
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
const std::set< std::pair< ReporterMode, const MooseObject * > > & getConsumers() const
Returns the consumers for this state; a pair that consists of the mode that the state is being consum...
Definition: ReporterState.h:56
const ReporterStateBase & getReporterStateBase(const ReporterName &reporter_name) const
The ReporterStateBase associated with the Reporter with name reporter_name.
Definition: ReporterData.C:146
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
MooseEnum designed for the ReporterContext objects to define how a ReporterValue can and is being pro...
Definition: ReporterMode.h:61
A class for "pretty printing" a table of data.
Definition: PerfGraph.h:34
Base class for MOOSE-based applications.
Definition: MooseApp.h:85
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
const std::string getCombinedName() const
Return the name of the object and data as object_name/data_name.
Definition: ReporterName.C:47
RestartableDataValue & getRestartableDataHelper(std::unique_ptr< RestartableDataValue > data_ptr, bool declare) const
Helper for registering data with the MooseApp to avoid cyclic includes.
Definition: ReporterData.C:174
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:369
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
Definition: ReporterState.h:41
virtual std::string contextType() const =0
This is a helper class to aid with parallel communication of compute Reporter values as well as provi...
DenseVector< Real > getAllRealReporterValues() const
Get all real reporter values including postprocessor and vector postprocessor values into a dense vec...
Definition: ReporterData.C:81
const ReporterProducerEnum & getProducerModeEnum() const
Return the Reporter value produced mode.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
std::vector< std::string > getAllRealReporterFullNames() const
Get full names of all real reporter values Note: For a postprocessor, the full name is the postproces...
Definition: ReporterData.C:106
RestartableDataValue & registerRestartableData(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid, bool read_only, const RestartableDataMapName &metaname="")
Definition: MooseApp.C:2421
bool hasReporterState(const ReporterName &reporter_name) const
Definition: ReporterData.h:454
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
void finalize(const std::string &object_name)
Helper function for performing post calculation actions via the ReporterContext objects.
Definition: ReporterData.C:48
virtual std::string valueType() const =0
The base class for storing a Reporter&#39;s state.
Definition: ReporterState.h:32
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:27
void addRow(Ts... entries)
Add a row of data.
Definition: VariadicTable.h:91
std::set< ReporterName > getReporterNames() const
Return a list of all reporter names.
Definition: ReporterData.C:62
void copyValuesBack()
At the end of a timestep this method is called to copy the values back in time in preparation for the...
Definition: ReporterData.C:17
const ReporterProducerEnum & getReporterMode(const ReporterName &reporter_name) const
Return the ReporterProducerEnum for an existing ReporterValue.
Definition: ReporterData.C:191
MooseApp & _app
For accessing the restart/recover system, which is where Reporter values are stored.
Definition: ReporterData.h:293
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
OStreamProxy out
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
bool hasReporterWithMode(const std::string &obj_name, const ReporterMode &mode) const
Return true if the supplied mode exists in the produced Reporter values.
Definition: ReporterData.C:181
void check() const
Perform integrity check for get/declare calls.
Definition: ReporterData.C:162
std::set< std::string > getPostprocessorNames() const
Return a list of all postprocessor names.
Definition: ReporterData.C:71
const MooseObject & getProducer() const
Return the MooseObject that produces this Reporter.
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:44
Abstract definition of a RestartableData value.
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
void restoreState(bool verbose=false)
When a time step fails, this method is called to revert the current reporter values to their old stat...
Definition: ReporterData.C:24
const ReporterContextBase & getReporterContextBase(const ReporterName &reporter_name) const
Definition: ReporterData.C:130