https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
14ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {}
15
16void
18{
19 for (const auto & name_context_pair : _context_ptrs)
20 name_context_pair.second->copyValuesBack();
21}
22
23void
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
47void
48ReporterData::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
55bool
56ReporterData::hasReporterValue(const ReporterName & reporter_name) const
57{
58 return _context_ptrs.count(reporter_name);
59}
60
61std::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
70std::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
80DenseVector<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
105std::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
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
145const 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
161void
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
174ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr,
175 bool declare) const
176{
177 return _app.registerRestartableData(std::move(data_ptr), 0, !declare);
178}
179
180void
185
186bool
187ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const
188{
189 for (const auto & name_context_pair : _context_ptrs)
190 if (name_context_pair.first.getObjectName() == obj_name &&
191 name_context_pair.second->getProducerModeEnum() == mode)
192 return true;
193 return false;
194}
195
197ReporterData::getReporterMode(const ReporterName & reporter_name) const
198{
199 return getReporterContextBase(reporter_name).getProducerModeEnum();
200}
201
202bool
204{
205 return _states.count(reporter_name);
206}
207
208std::string
210{
211 std::stringstream oss;
212
213 const auto & name = state.getReporterName();
214
215 if (name.isPostprocessor())
216 oss << "Postprocessor \"" << name.getObjectName() << "\":\n";
217 else
218 {
219 oss << name.specialTypeToName() << " \"" << name.getCombinedName() << "\":\n Type:\n "
220 << state.valueType() << "\n";
221 }
222 oss << " Producer:\n ";
223 if (context)
224 {
225 oss << context->getProducer().type() << " \"" << context->getProducer().name() << "\"";
226 oss << "\n Context type:\n " << context->contextType();
227 }
228 else
229 oss << "None";
230 oss << "\n Consumer(s):\n";
231 if (state.getConsumers().empty())
232 oss << " None\n";
233 else
234 for (const auto & mode_object_pair : state.getConsumers())
235 {
236 const ReporterMode mode = mode_object_pair.first;
237 const MooseObject * object = mode_object_pair.second;
238 oss << " " << object->typeAndName() << " (mode: " << mode << ")\n";
239 }
240
241 return oss.str();
242}
243
244std::string
245ReporterData::getReporterInfo(const ReporterName & reporter_name) const
246{
247 return getReporterInfo(getReporterStateBase(reporter_name),
248 hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name)
249 : nullptr);
250}
251
252std::string
254{
255 std::string out = _states.empty() ? "No reporters were requested or declared." : "";
256 for (const auto & name : getReporterNames())
257 out += getReporterInfo(name) + "\n";
258 return out;
259}
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition MooseError.h:401
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Base class for MOOSE-based applications.
Definition MooseApp.h:110
bool restoreDataIfAvailable(RestartableDataValue &value, const THREAD_ID tid, Moose::PassKey< ReporterData >)
Restores value in place from the checkpoint reader if it is present in the checkpoint and has not yet...
Definition MooseApp.h:839
RestartableDataValue & registerRestartableData(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid, bool read_only, const RestartableDataMapName &metaname="")
Definition MooseApp.C:2449
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
std::string typeAndName() const
Get the class's combined type and name; useful in error handling.
Definition MooseBase.C:57
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
This is a helper class to aid with parallel communication of compute Reporter values as well as provi...
const MooseObject & getProducer() const
Return the MooseObject that produces this Reporter.
virtual std::string contextType() const =0
const ReporterProducerEnum & getProducerModeEnum() const
Return the Reporter value produced mode.
std::set< ReporterName > getReporterNames() const
Return a list of all reporter names.
bool hasReporterState(const ReporterName &reporter_name) const
std::vector< std::string > getAllRealReporterFullNames() const
Get full names of all real reporter values Note: For a postprocessor, the full name is the postproces...
const ReporterStateBase & getReporterStateBase(const ReporterName &reporter_name) const
@Returns The ReporterStateBase associated with the Reporter with name reporter_name.
ReporterData(MooseApp &moose_app)
MooseApp & _app
For accessing the restart/recover system, which is where Reporter values are stored.
DenseVector< Real > getAllRealReporterValues() const
Get all real reporter values including postprocessor and vector postprocessor values into a dense vec...
void check() const
Perform integrity check for get/declare calls.
void copyValuesBack()
At the end of a timestep this method is called to copy the values back in time in preparation for the...
const ReporterContextBase & getReporterContextBase(const ReporterName &reporter_name) const
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
void restoreState(bool verbose=false)
When a time step fails, this method is called to revert the current reporter values to their old stat...
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
void restoreReporterStateIfAvailable(RestartableDataValue &state) const
Helper for restoring state from the checkpoint reader if available.
RestartableDataValue & getRestartableDataHelper(std::unique_ptr< RestartableDataValue > data_ptr, bool declare) const
Helper for registering data with the MooseApp to avoid cyclic includes.
bool hasReporterWithMode(const std::string &obj_name, const ReporterMode &mode) const
Return true if the supplied mode exists in the produced Reporter values.
std::set< std::string > getPostprocessorNames() const
Return a list of all postprocessor names.
const ReporterProducerEnum & getReporterMode(const ReporterName &reporter_name) const
Return the ReporterProducerEnum for an existing ReporterValue.
void finalize(const std::string &object_name)
Helper function for performing post calculation actions via the ReporterContext objects.
MooseEnumItem that automatically creates the ID and doesn't allow the ID to be assigned.
The Reporter system is comprised of objects that can contain any number of data values.
const std::string getCombinedName() const
Return the name of the object and data as object_name/data_name.
MooseEnum designed for the ReporterContext objects to define how a ReporterValue can and is being pro...
The base class for storing a Reporter's state.
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
virtual std::string valueType() const =0
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...
Abstract definition of a RestartableData value.
A class for "pretty printing" a table of data.
void addRow(Ts... entries)
Add a row of data.