https://mooseframework.inl.gov
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
ReporterData Class Reference

This is a helper class for managing the storage of declared Reporter object values. More...

#include <ReporterData.h>

Classes

class  WriteKey
 

Public Member Functions

 ReporterData (MooseApp &moose_app)
 
template<typename T >
bool hasReporterValue (const ReporterName &reporter_name) const
 Return True if a Reporter value with the given type and name have been created. More...
 
bool hasReporterValue (const ReporterName &reporter_name) const
 Return True if a Reporter value with any type exists with the given name. More...
 
template<typename T >
bool hasReporterState (const ReporterName &reporter_name) const
 
bool hasReporterState (const ReporterName &reporter_name) const
 
std::set< ReporterNamegetReporterNames () const
 Return a list of all reporter names. More...
 
std::set< std::string > getPostprocessorNames () const
 Return a list of all postprocessor names. More...
 
DenseVector< RealgetAllRealReporterValues () const
 Get all real reporter values including postprocessor and vector postprocessor values into a dense vector. More...
 
std::vector< std::string > getAllRealReporterFullNames () const
 Get full names of all real reporter values Note: For a postprocessor, the full name is the postprocessor name plus '/value'. More...
 
template<typename T >
const T & getReporterValue (const ReporterName &reporter_name, const MooseObject &consumer, const ReporterMode &mode, const std::size_t time_index=0) const
 Method for returning read only references to Reporter values. More...
 
template<typename T >
const T & getReporterValue (const ReporterName &reporter_name, const std::size_t time_index=0) const
 Method for returning a read-only reference to Reporter values that already exist. More...
 
template<typename T >
void setReporterValue (const ReporterName &reporter_name, const T &value, const std::size_t time_index=0)
 Method for setting Reporter values that already exist. More...
 
template<typename T >
void needReporterTimeIndex (const ReporterName &reporter_name, const std::size_t time_index)
 Method for setting that a specific time index is requested for a Reporter value. More...
 
const ReporterProducerEnumgetReporterMode (const ReporterName &reporter_name) const
 Return the ReporterProducerEnum for an existing ReporterValue. More...
 
std::string getReporterInfo (const ReporterName &reporter_name) const
 Gets information pertaining to the Reporter with name reporter_name. More...
 
std::string getReporterInfo () const
 Gets information about all declared/requested Reporters. More...
 
template<typename T , typename S , typename... Args>
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. More...
 
void finalize (const std::string &object_name)
 Helper function for performing post calculation actions via the ReporterContext objects. More...
 
void copyValuesBack ()
 At the end of a timestep this method is called to copy the values back in time in preparation for the next timestep. More...
 
void restoreState (bool verbose=false)
 When a time step fails, this method is called to revert the current reporter values to their old state. More...
 
void check () const
 Perform integrity check for get/declare calls. More...
 
bool hasReporterWithMode (const std::string &obj_name, const ReporterMode &mode) const
 Return true if the supplied mode exists in the produced Reporter values. More...
 
const ReporterContextBasegetReporterContextBase (const ReporterName &reporter_name) const
 
ReporterContextBasegetReporterContextBase (const ReporterName &reporter_name)
 
const ReporterStateBasegetReporterStateBase (const ReporterName &reporter_name) const
 The ReporterStateBase associated with the Reporter with name reporter_name. More...
 
ReporterStateBasegetReporterStateBase (const ReporterName &reporter_name)
 

Static Public Member Functions

static std::string getReporterInfo (const ReporterStateBase &state, const ReporterContextBase *context)
 Gets information pertaining to the Reporter with state state and possibly context context. More...
 

Private Member Functions

template<typename T >
ReporterState< T > & getReporterStateHelper (const ReporterName &reporter_name, bool declare, const MooseObject *moose_object=nullptr) const
 Helper method for creating the necessary RestartableData for Reporter values. More...
 
RestartableDataValuegetRestartableDataHelper (std::unique_ptr< RestartableDataValue > data_ptr, bool declare) const
 Helper for registering data with the MooseApp to avoid cyclic includes. More...
 

Private Attributes

MooseApp_app
 For accessing the restart/recover system, which is where Reporter values are stored. More...
 
std::map< ReporterName, ReporterStateBase * > _states
 Map from ReporterName -> Reporter state. More...
 
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
 The ReporterContext objects are created when a value is declared. More...
 

Detailed Description

This is a helper class for managing the storage of declared Reporter object values.

This design of the system is a generalization of the VectorPostprocessor system that the Reporter objects replaced.

Foremost, this object doesn't store the data. It simply acts as helper for using the restart system of the MooseApp. This object automatically handles the old, older, ... data. All declarations create std::pair<T, std::vector<T>> restartable data on the MooseApp, where the first value is the current value and the data in the vector are the older data.

The ReporterState object is a RestartableData object that serves as a helper for managing the time history. A "context" object also exists that uses the ReporterState for performing special operations. Refer to ReporterState.h/C for more information.

It is important to note that the Reporter values are not threaded. However, the Reporter objects are UserObject based, so the calculation of the values can be threaded.

This object also relies on ReporterName objects, which are simply a combination of the Reporter object name and the data name. If you recall the VectorPostprocessor system on which this is based required an object name and a vector name. The ReporterName class simply provides a convenient way to provide that information in a single object. Special Parser syntax was also defined so that application developers do not have to have input parameters for both the object and data names (see Parser.C/h).

Definition at line 48 of file ReporterData.h.

Constructor & Destructor Documentation

◆ ReporterData()

ReporterData::ReporterData ( MooseApp moose_app)

Definition at line 14 of file ReporterData.C.

14 : _app(moose_app) {}
MooseApp & _app
For accessing the restart/recover system, which is where Reporter values are stored.
Definition: ReporterData.h:293

Member Function Documentation

◆ check()

void ReporterData::check ( ) const

Perform integrity check for get/declare calls.

Definition at line 162 of file ReporterData.C.

Referenced by FEProblemBase::initialSetup().

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 }
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
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

◆ copyValuesBack()

void ReporterData::copyValuesBack ( )

At the end of a timestep this method is called to copy the values back in time in preparation for the next timestep.

See FEProblemBase::advanceState

Definition at line 17 of file ReporterData.C.

Referenced by FEProblemBase::advanceState().

18 {
19  for (const auto & name_context_pair : _context_ptrs)
20  name_context_pair.second->copyValuesBack();
21 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ declareReporterValue()

template<typename T , typename S , typename... Args>
T & ReporterData::declareReporterValue ( const ReporterName reporter_name,
const ReporterMode mode,
const MooseObject producer,
Args &&...  args 
)

Method for returning a writable reference to the current Reporter value.

This method is used by the Reporter class to produce values.

Template Parameters
TThe Reporter value C++ type.
S(optional) The ReporterContext for performing specialized actions after the values have been computed. For example, ReporterBroadcastContext automatically broadcasts the computed value. See ReporterState.C/h for more information.
Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.
modeThe mode that the produced value will be computed by the Reporter object
producerThe MooseObject that produces this value
args(optional) Any number of optional arguments passed into the Context type given by the S template parameter. If S = ReporterContext then the first argument can be used as the default value (see ReporterContext.h).

The ReporterContext objects allow for custom handling of data (e.g., broadcasting the value). The get/declare methods can be called in any order thus an the underlying RestartableData object is often created by the get method before it is declared. Therefore the custom functionality cannot be handled by specializing the RestartableData/ReporterState object directly because the state is often created prior to the declaration that dictates how the produced value shall be computed. Thus, the reason for the separate ReporterContext objects.

Definition at line 400 of file ReporterData.h.

Referenced by ReporterTransferInterface::declareClone(), ReporterGeneralContext< T >::declareClone(), VectorPostprocessor::declareVector(), ReporterTransferInterface::declareVectorClone(), and ReporterGeneralContext< T >::declareVectorClone().

404 {
405  // Get/create the ReporterState
406  auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ true, &producer);
407 
408  // They key in _states (ReporterName) is not unique by special type. This is done on purpose
409  // because we want to store reporter names a single name regardless of special type.
410  // Because of this, we have the case where someone could request a reporter value
411  // that is later declared as a pp or a vpp value. In this case, when it is first
412  // requested, the _state entry will have a key and name with a special type of ANY.
413  // When it's declared here (later), we will still find the correct entry because
414  // we don't check the special type in the key/name. But... we want the actual
415  // key and name to represent a pp or a vpp. Therefore, we'll set it properly,
416  // remove the entry in the map (which has the ANY key), and re-add it so that it
417  // has the pp/vpp key. This allows us to identify Reporters that really represent
418  // pps/vpps in output and in error reporting.
419  if (reporter_name.isPostprocessor() && !state.getReporterName().isPostprocessor())
420  {
421  state.setIsPostprocessor();
422  _states.erase(reporter_name);
423  _states.emplace(reporter_name, &state);
424  }
425  else if (reporter_name.isVectorPostprocessor() &&
426  !state.getReporterName().isVectorPostprocessor())
427  {
428  state.setIsVectorPostprocessor();
429  _states.erase(reporter_name);
430  _states.emplace(reporter_name, &state);
431  }
432 
433  mooseAssert(!_context_ptrs.count(reporter_name), "Context already exists");
434 
435  // Create the ReporterContext
436  auto context_ptr = std::make_unique<S>(_app, producer, state, args...);
437  context_ptr->init(mode); // initialize the mode, see ContextReporter
438  _context_ptrs.emplace(reporter_name, std::move(context_ptr));
439 
440  return state.value();
441 }
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
MooseApp & _app
For accessing the restart/recover system, which is where Reporter values are stored.
Definition: ReporterData.h:293

◆ finalize()

void ReporterData::finalize ( const std::string &  object_name)

Helper function for performing post calculation actions via the ReporterContext objects.

If you recall, the original VectorPostprocessor system included the ability to perform some scatter and broadcast actions via the special call on the storage helper object. This is a replacement for that method that leverages the ReporterContext objects to perform value specific actions, including some automatic operations depending how the data is produced and consumed.

See FEProblemBase::joinAndFinalize

Definition at line 48 of file ReporterData.C.

Referenced by FEProblemBase::joinAndFinalize().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ getAllRealReporterFullNames()

std::vector< std::string > ReporterData::getAllRealReporterFullNames ( ) const

Get full names of all real reporter values Note: For a postprocessor, the full name is the postprocessor name plus '/value'.

For a vector postprocessor, the full name is the vector postprocessor name plus the vector name followed by '/#' where '#' is the index of the vector.

Definition at line 106 of file ReporterData.C.

Referenced by FEProblemBase::execute().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
const std::string getCombinedName() const
Return the name of the object and data as object_name/data_name.
Definition: ReporterName.C:47
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
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30

◆ getAllRealReporterValues()

DenseVector< Real > ReporterData::getAllRealReporterValues ( ) const

Get all real reporter values including postprocessor and vector postprocessor values into a dense vector.

Definition at line 81 of file ReporterData.C.

Referenced by FEProblemBase::execute().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
const std::string getCombinedName() const
Return the name of the object and data as object_name/data_name.
Definition: ReporterName.C:47
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
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30

◆ getPostprocessorNames()

std::set< std::string > ReporterData::getPostprocessorNames ( ) const

Return a list of all postprocessor names.

Definition at line 71 of file ReporterData.C.

Referenced by FEProblemBase::checkDuplicatePostprocessorVariableNames().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ getReporterContextBase() [1/2]

const ReporterContextBase & ReporterData::getReporterContextBase ( const ReporterName reporter_name) const
Returns
The ReporterContextBase associated with the Reporter with name reporter_name.

Definition at line 130 of file ReporterData.C.

Referenced by ReporterTransferInterface::clearVectorReporter(), ReporterTransferInterface::declareClone(), AccumulateReporter::declareLateValues(), ReporterTransferInterface::declareVectorClone(), getReporterInfo(), getReporterMode(), VectorPostprocessorInterface::getVectorPostprocessorContextByNameHelper(), hasReporterValue(), JSONOutput::outputReporters(), ReporterTransferInterface::resizeReporter(), ReporterTransferInterface::sumVectorReporter(), ReporterTransferInterface::transferFromVectorReporter(), ReporterTransferInterface::transferReporter(), and ReporterTransferInterface::transferToVectorReporter().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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

◆ getReporterContextBase() [2/2]

ReporterContextBase & ReporterData::getReporterContextBase ( const ReporterName reporter_name)

Definition at line 138 of file ReporterData.C.

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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

◆ getReporterInfo() [1/3]

std::string ReporterData::getReporterInfo ( const ReporterStateBase state,
const ReporterContextBase context 
)
static

Gets information pertaining to the Reporter with state state and possibly context context.

Definition at line 203 of file ReporterData.C.

Referenced by ReporterDebugOutput::output().

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 }
std::string name(const ElemQuality q)
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
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
Definition: ReporterState.h:41
virtual std::string contextType() const =0
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
virtual std::string valueType() const =0
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:27
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

◆ getReporterInfo() [2/3]

std::string ReporterData::getReporterInfo ( const ReporterName reporter_name) const

Gets information pertaining to the Reporter with name reporter_name.

Definition at line 239 of file ReporterData.C.

240 {
241  return getReporterInfo(getReporterStateBase(reporter_name),
242  hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name)
243  : nullptr);
244 }
const ReporterStateBase & getReporterStateBase(const ReporterName &reporter_name) const
The ReporterStateBase associated with the Reporter with name reporter_name.
Definition: ReporterData.C:146
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
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
const ReporterContextBase & getReporterContextBase(const ReporterName &reporter_name) const
Definition: ReporterData.C:130

◆ getReporterInfo() [3/3]

std::string ReporterData::getReporterInfo ( ) const

Gets information about all declared/requested Reporters.

Definition at line 247 of file ReporterData.C.

Referenced by check(), getReporterInfo(), getReporterStateHelper(), and ReporterContextBase::requiresConsumerModes().

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::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
std::set< ReporterName > getReporterNames() const
Return a list of all reporter names.
Definition: ReporterData.C:62
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
OStreamProxy out

◆ getReporterMode()

const ReporterProducerEnum & ReporterData::getReporterMode ( const ReporterName reporter_name) const

Return the ReporterProducerEnum for an existing ReporterValue.

Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.

Definition at line 191 of file ReporterData.C.

Referenced by AccumulateReporter::declareAccumulateHelper().

192 {
193  return getReporterContextBase(reporter_name).getProducerModeEnum();
194 }
const ReporterProducerEnum & getProducerModeEnum() const
Return the Reporter value produced mode.
const ReporterContextBase & getReporterContextBase(const ReporterName &reporter_name) const
Definition: ReporterData.C:130

◆ getReporterNames()

std::set< ReporterName > ReporterData::getReporterNames ( ) const

Return a list of all reporter names.

Definition at line 62 of file ReporterData.C.

Referenced by getReporterInfo(), AdvancedOutput::initAvailableLists(), XMLOutput::outputVectorPostprocessors(), and TableOutput::outputVectorPostprocessors().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ getReporterStateBase() [1/2]

const ReporterStateBase & ReporterData::getReporterStateBase ( const ReporterName reporter_name) const

The ReporterStateBase associated with the Reporter with name reporter_name.

Definition at line 146 of file ReporterData.C.

Referenced by ReporterTransferInterface::addReporterTransferMode(), getReporterInfo(), and hasReporterState().

147 {
148  if (!hasReporterState(reporter_name))
149  mooseError("Unable to locate Reporter state with name: ", reporter_name);
150  return *_states.at(reporter_name);
151 }
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool hasReporterState(const ReporterName &reporter_name) const
Definition: ReporterData.h:454

◆ getReporterStateBase() [2/2]

ReporterStateBase & ReporterData::getReporterStateBase ( const ReporterName reporter_name)

Definition at line 154 of file ReporterData.C.

155 {
156  if (!hasReporterState(reporter_name))
157  mooseError("Unable to locate Reporter state with name: ", reporter_name);
158  return *_states.at(reporter_name);
159 }
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool hasReporterState(const ReporterName &reporter_name) const
Definition: ReporterData.h:454

◆ getReporterStateHelper()

template<typename T >
ReporterState< T > & ReporterData::getReporterStateHelper ( const ReporterName reporter_name,
bool  declare,
const MooseObject moose_object = nullptr 
) const
private

Helper method for creating the necessary RestartableData for Reporter values.

Template Parameters
TThe desired C++ type for the Reporter value
Parameters
reporter_nameObject/data name for the Reporter value
declareFlag indicating if the ReporterValue is being declared or read. This flag is passed to the existing MooseApp restart/recover system that errors if a value is declared multiple times.
moose_objectThe object requesting/declaring the state, used in error handling.

Definition at line 330 of file ReporterData.h.

333 {
334  if (hasReporterState(reporter_name))
335  {
336  const auto error_helper =
337  [this, &reporter_name, &moose_object, &declare](const std::string & suffix)
338  {
339  std::stringstream oss;
340  oss << "While " << (declare ? "declaring" : "requesting") << " a "
341  << reporter_name.specialTypeToName() << " value with the name \""
342  << reporter_name.getValueName() << "\"";
343  if (!reporter_name.isPostprocessor() && !reporter_name.isVectorPostprocessor())
344  oss << " and type \"" << MooseUtils::prettyCppType<T>() << "\"";
345  oss << ",\na Reporter with the same name " << suffix << ".\n\n";
346  oss << getReporterInfo(reporter_name);
347 
348  if (moose_object)
349  moose_object->mooseError(oss.str());
350  else
351  mooseError(oss.str());
352  };
353 
354  if (declare && hasReporterValue(reporter_name))
355  error_helper("has already been declared");
356  if (!hasReporterState<T>(reporter_name))
357  {
358  std::stringstream oss;
359  oss << "has been " << (declare || !hasReporterValue(reporter_name) ? "requested" : "declared")
360  << " with a different type";
361  error_helper(oss.str());
362  }
363  }
364 
365  // Reporter states are stored as restartable data. The act of registering restartable data
366  // may be done multiple times with the same name, which will happen when more than one
367  // get value is done, or a get value and a declare is done. With this, we create a new
368  // state every time, but said created state may not be the actual state if this state
369  // is already registered as restartable data. Therefore, we create a state, and then
370  // cast the restartable data received back to a state (which may be different than
371  // the one we created, but that's okay)
372  auto state_unique_ptr = std::make_unique<ReporterState<T>>(reporter_name);
373  auto & restartable_value = getRestartableDataHelper(std::move(state_unique_ptr), declare);
374 
375  auto * state = dynamic_cast<ReporterState<T> *>(&restartable_value);
376  mooseAssert(state, "Cast failed. The check above must be broken!");
377 
378  // See declareReporterValue for a comment on what happens if a state for the same
379  // name is requested but with different special types. TLDR: ReporterNames with
380  // different special types are not unique so they'll be the same entry
381  _states.emplace(reporter_name, state);
382 
383  return *state;
384 }
A special version of RestartableData to aid in storing Reporter values.
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320
bool isVectorPostprocessor() const
Definition: ReporterName.h:89
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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
std::string specialTypeToName() const
Converts the special type to a usable name for error reporting.
Definition: ReporterName.C:83
bool hasReporterState(const ReporterName &reporter_name) const
Definition: ReporterData.h:454
bool isPostprocessor() const
Definition: ReporterName.h:85
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
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
const std::string & getValueName() const
Return the data name for the Reporter value.
Definition: ReporterName.C:41

◆ getReporterValue() [1/2]

template<typename T >
const T & ReporterData::getReporterValue ( const ReporterName reporter_name,
const MooseObject consumer,
const ReporterMode mode,
const std::size_t  time_index = 0 
) const

Method for returning read only references to Reporter values.

Template Parameters
TThe Reporter value C++ type.
Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.
consumerThe MooseObject consuming the Reporter value (for error reporting)
modeThe mode that the value will be consumed by the by the ReporterInterface object
time_index(optional) When not provided or zero is provided the current value is returned. If an index greater than zero is provided then the corresponding old data is returned (1 = old, 2 = older, etc.).

Definition at line 388 of file ReporterData.h.

Referenced by FEProblemBase::getPostprocessorValueByName(), PostprocessorInterface::getPostprocessorValueByNameInternal(), ReporterInterface::getReporterValueByName(), VectorPostprocessorInterface::getVectorPostprocessorByNameHelper(), VectorPostprocessorInterface::getVectorPostprocessorContextByNameHelper(), FEProblemBase::getVectorPostprocessorValueByName(), TableOutput::outputReporter(), Exodus::outputReporters(), XMLOutput::outputVectorPostprocessors(), TableOutput::outputVectorPostprocessors(), and ReporterContext< std::vector< T > >::transferToVector().

392 {
393  auto & state = getReporterStateHelper<T>(reporter_name, /* declare = */ false, &consumer);
394  state.addConsumer(mode, consumer);
395  return state.value(time_index);
396 }

◆ getReporterValue() [2/2]

template<typename T >
const T & ReporterData::getReporterValue ( const ReporterName reporter_name,
const std::size_t  time_index = 0 
) const

Method for returning a read-only reference to Reporter values that already exist.

Template Parameters
TThe Reporter value C++ type.
Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.
time_index(optional) When not provided or zero is provided the current value is returned. If an index greater than zero is provided then the corresponding old data is returned (1 = old, 2 = older, etc.).

Definition at line 463 of file ReporterData.h.

465 {
466  if (!hasReporterValue<T>(reporter_name))
467  mooseError("Reporter name \"",
468  reporter_name,
469  "\" with type \"",
470  MooseUtils::prettyCppType<T>(),
471  "\" is not declared.");
472 
473  // Force the const version of value, which does not allow for increasing time index
474  return static_cast<const ReporterState<T> &>(
475  getReporterStateHelper<T>(reporter_name, /* declare = */ false))
476  .value(time_index);
477 }
A special version of RestartableData to aid in storing Reporter values.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
T & value(const std::size_t time_index=0)
Return a reference to the current value or one of the old values.

◆ getRestartableDataHelper()

RestartableDataValue & ReporterData::getRestartableDataHelper ( std::unique_ptr< RestartableDataValue data_ptr,
bool  declare 
) const
private

Helper for registering data with the MooseApp to avoid cyclic includes.

Definition at line 174 of file ReporterData.C.

Referenced by getReporterStateHelper().

176 {
177  return _app.registerRestartableData(std::move(data_ptr), 0, !declare);
178 }
RestartableDataValue & registerRestartableData(std::unique_ptr< RestartableDataValue > data, THREAD_ID tid, bool read_only, const RestartableDataMapName &metaname="")
Definition: MooseApp.C:2454
MooseApp & _app
For accessing the restart/recover system, which is where Reporter values are stored.
Definition: ReporterData.h:293

◆ hasReporterState() [1/2]

template<typename T >
bool ReporterData::hasReporterState ( const ReporterName reporter_name) const
Returns
True if a ReporterState is defined for the Reporter with name reporter_name and the given type.

Definition at line 454 of file ReporterData.h.

Referenced by getReporterStateBase(), and getReporterStateHelper().

455 {
456  if (!hasReporterState(reporter_name))
457  return false;
458  return dynamic_cast<const ReporterState<T> *>(&getReporterStateBase(reporter_name));
459 }
A special version of RestartableData to aid in storing Reporter values.
const ReporterStateBase & getReporterStateBase(const ReporterName &reporter_name) const
The ReporterStateBase associated with the Reporter with name reporter_name.
Definition: ReporterData.C:146
bool hasReporterState(const ReporterName &reporter_name) const
Definition: ReporterData.h:454

◆ hasReporterState() [2/2]

bool ReporterData::hasReporterState ( const ReporterName reporter_name) const
Returns
True if a ReporterState is defined for the Reporter with name reporter_name.

Definition at line 197 of file ReporterData.C.

198 {
199  return _states.count(reporter_name);
200 }
std::map< ReporterName, ReporterStateBase * > _states
Map from ReporterName -> Reporter state.
Definition: ReporterData.h:320

◆ hasReporterValue() [1/2]

template<typename T >
bool ReporterData::hasReporterValue ( const ReporterName reporter_name) const

Return True if a Reporter value with the given type and name have been created.

Definition at line 445 of file ReporterData.h.

Referenced by check(), ReporterTransferInterface::checkHasReporterValue(), AccumulateReporter::declareAccumulateHelper(), AccumulateReporter::declareLateValues(), getAllRealReporterFullNames(), getAllRealReporterValues(), getReporterContextBase(), getReporterInfo(), getReporterStateHelper(), PostprocessorInterface::hasPostprocessorByName(), FEProblemBase::hasPostprocessorValueByName(), ReporterInterface::hasReporterValueByName(), VectorPostprocessorInterface::hasVectorPostprocessorByName(), MooseParsedFunctionWrapper::initialize(), ParsedConvergence::initializeSymbols(), JSONOutput::initialSetup(), PostprocessorInterface::isDefaultPostprocessorValueByName(), TableOutput::outputReporter(), Exodus::outputReporters(), XMLOutput::outputVectorPostprocessors(), and TableOutput::outputVectorPostprocessors().

446 {
447  if (!hasReporterValue(reporter_name))
448  return false;
449  return dynamic_cast<const ReporterContext<T> *>(&getReporterContextBase(reporter_name));
450 }
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
General context that is called by all Reporter values to manage the old values.
const ReporterContextBase & getReporterContextBase(const ReporterName &reporter_name) const
Definition: ReporterData.C:130

◆ hasReporterValue() [2/2]

bool ReporterData::hasReporterValue ( const ReporterName reporter_name) const

Return True if a Reporter value with any type exists with the given name.

Definition at line 56 of file ReporterData.C.

57 {
58  return _context_ptrs.count(reporter_name);
59 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ hasReporterWithMode()

bool ReporterData::hasReporterWithMode ( const std::string &  obj_name,
const ReporterMode mode 
) const

Return true if the supplied mode exists in the produced Reporter values.

See also
CSV.C/h

Definition at line 181 of file ReporterData.C.

Referenced by CSV::output(), and JSONOutput::outputReporters().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325

◆ needReporterTimeIndex()

template<typename T >
void ReporterData::needReporterTimeIndex ( const ReporterName reporter_name,
const std::size_t  time_index 
)

Method for setting that a specific time index is requested for a Reporter value.

Template Parameters
TThe Reporter value C++ type.
Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.
time_indexThe time index that is needed

Definition at line 492 of file ReporterData.h.

Referenced by Receiver::Receiver().

494 {
495  getReporterValue<T>(reporter_name, 0); // for error checking that it is declared
496  getReporterStateHelper<T>(reporter_name, /* declare = */ false).value(time_index);
497 }
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

◆ restoreState()

void ReporterData::restoreState ( bool  verbose = false)

When a time step fails, this method is called to revert the current reporter values to their old state.

See also
FEProblemBase::restoreSolutions
Parameters
verboseSet true to print whether the reporters were restored or not.

Definition at line 24 of file ReporterData.C.

Referenced by FEProblemBase::restoreSolutions().

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 }
std::map< ReporterName, std::unique_ptr< ReporterContextBase > > _context_ptrs
The ReporterContext objects are created when a value is declared.
Definition: ReporterData.h:325
A class for "pretty printing" a table of data.
Definition: PerfGraph.h:34
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:369
void addRow(Ts... entries)
Add a row of data.
Definition: VariadicTable.h:91

◆ setReporterValue()

template<typename T >
void ReporterData::setReporterValue ( const ReporterName reporter_name,
const T &  value,
const std::size_t  time_index = 0 
)

Method for setting Reporter values that already exist.

Template Parameters
TThe Reporter value C++ type.
Parameters
reporter_nameThe name of the reporter value, which includes the object name and the data name.
valueThe value to which the Reporter will be changed to.
time_index(optional) When not provided or zero is provided the current value is returned. If an index greater than zero is provided then the corresponding old data is returned (1 = old, 2 = older, etc.). WARNING! This method is designed for setting values outside of the traditional interfaces such as is necessary for Transfers. This is an advanced capability that should be used with caution.
See also
FEProblemBase::setPostprocessorValueByName

Definition at line 481 of file ReporterData.h.

Referenced by ConstantPostprocessor::ConstantPostprocessor(), FEProblemBase::setPostprocessorValueByName(), FEProblemBase::setVectorPostprocessorValueByName(), ReporterContext< std::vector< T > >::transfer(), and ReporterContext< std::vector< T > >::transferFromVector().

484 {
485  // https://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func
486  const auto & me = *this;
487  const_cast<T &>(me.getReporterValue<T>(reporter_name, time_index)) = value;
488 }
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

Member Data Documentation

◆ _app

MooseApp& ReporterData::_app
private

For accessing the restart/recover system, which is where Reporter values are stored.

Definition at line 293 of file ReporterData.h.

Referenced by declareReporterValue(), and getRestartableDataHelper().

◆ _context_ptrs

std::map<ReporterName, std::unique_ptr<ReporterContextBase> > ReporterData::_context_ptrs
private

The ReporterContext objects are created when a value is declared.

The context objects include a reference to the associated ReporterState values. This container stores the context object for each Reporter value.

Definition at line 325 of file ReporterData.h.

Referenced by copyValuesBack(), declareReporterValue(), finalize(), getAllRealReporterFullNames(), getAllRealReporterValues(), getPostprocessorNames(), getReporterContextBase(), getReporterNames(), hasReporterValue(), hasReporterWithMode(), and restoreState().

◆ _states

std::map<ReporterName, ReporterStateBase *> ReporterData::_states
mutableprivate

Map from ReporterName -> Reporter state.

We need to keep track of all of the states that are created so that we can check them after Reporter declaration to make sure all states have a producer (are delcared). We cannot check _context_ptrs, because a context is only defined for Reporters that have been declared. This is mutable so that it can be inserted into when requesting Reporter values.

Definition at line 320 of file ReporterData.h.

Referenced by check(), declareReporterValue(), getReporterInfo(), getReporterStateBase(), getReporterStateHelper(), and hasReporterState().


The documentation for this class was generated from the following files: