https://mooseframework.inl.gov
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ReporterState< T > Class Template Reference

A special version of RestartableData to aid in storing Reporter values. More...

#include <ReporterState.h>

Inheritance diagram for ReporterState< T >:
[legend]

Public Member Functions

 ReporterState (const ReporterName &name)
 
void copyValuesBack ()
 Copy stored values back in time to old/older etc. More...
 
bool restoreState ()
 Restore values to their old values, i.e. More...
 
std::string valueType () const override final
 
const ReporterNamegetReporterName () const
 Return the ReporterName that this state is associated with. More...
 
void addConsumer (ReporterMode mode, const MooseObject &moose_object)
 Add a consumer for this ReporterState. More...
 
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 consumed by, and the object consuming it. More...
 
void setIsPostprocessor ()
 Sets the special Reporter type to a Postprocessor. More...
 
void setIsVectorPostprocessor ()
 Sets the special Reporter type to a VectorPostprocessor. More...
 
const std::list< T > & get () const
 
std::list< T > & set ()
 
void reset ()
 Resets (destructs) the underlying data. More...
 
virtual std::string type () const override final
 String identifying the type of parameter stored. More...
 
virtual const std::type_info & typeId () const override final
 The type ID of the underlying data. More...
 
virtual bool hasStoreJSON () const override final
 
const std::string & name () const
 The full (unique) name of this particular piece of data. More...
 
voidcontext ()
 A context pointer for helping with load / store. More...
 
bool hasContext () const
 
bool declared () const
 Whether or not this data has been declared. More...
 
void setDeclared (const SetDeclaredKey)
 Sets that this restartable value has been declared. More...
 
bool loaded () const
 Whether or not this data has been loaded. More...
 
void setNotLoaded (const SetNotLoadedKey)
 Sets that this restartable value has been loaded. More...
 
bool stored () const
 Whether or not this data has been loaded. More...
 
void setNotStored (const SetNotStoredKey)
 Sets that this restartable value has been loaded. More...
 
void store (std::ostream &stream)
 Stores the value into the stream stream and sets it as stored. More...
 
void store (nlohmann::json &json, const StoreJSONParams &params=StoreJSONParams{}) const
 Stores this restartable data in the JSON entry json, with the options set by params (optional; defaults to just the type and underlying value) More...
 
void load (std::istream &stream)
 Loads the value from the stream stream and sets it as loaded. More...
 
T & value (const std::size_t time_index=0)
 Return a reference to the current value or one of the old values. More...
 
const T & value (const std::size_t time_index=0) const
 
void storeInternal (std::ostream &stream) override final
 Loads and stores the data from/to a stream for restart. More...
 
void loadInternal (std::istream &stream) override final
 Load the RestartableData from a binary stream. More...
 

Static Public Attributes

static constexpr bool has_store_json
 Whether or not this type has a JSON store method implemented. More...
 

Protected Member Functions

virtual void storeJSONValue (nlohmann::json &json) const override final
 Internal method for storing the underlying JSON value. More...
 

Protected Attributes

const std::string _name
 The full (unique) name of this particular piece of data. More...
 
void *const _context
 A context pointer for helping with load and store. More...
 

Detailed Description

template<typename T>
class ReporterState< T >

A special version of RestartableData to aid in storing Reporter values.

This object is used by the ReporterData object. The objects provides a convenient method to define Reporter data that has a value as well as some number of old data values. Please refer to ReporterData.h for more information regarding the use of this class.

Parameters
nameThe name of the Reporter value

This class stores the current/old/older/... data using a std::list to allow values to be inserted into the data structure without corrupting references to the other values. This allows for arbitrary time data to be stored.

NOTE: Access to the data should be through the value() method to ensure the data is allocated correctly

Definition at line 110 of file ReporterState.h.

Constructor & Destructor Documentation

◆ ReporterState()

template<typename T >
ReporterState< T >::ReporterState ( const ReporterName name)

Definition at line 162 of file ReporterState.h.

163  : ReporterStateBase(name), RestartableData<std::list<T>>(name.getRestartableName(), nullptr)
164 {
165 }
ReporterStateBase(const ReporterName &name)
Definition: ReporterState.C:14
Concrete definition of a parameter value for a specified type.
const std::string & name() const
The full (unique) name of this particular piece of data.

Member Function Documentation

◆ addConsumer()

void ReporterStateBase::addConsumer ( ReporterMode  mode,
const MooseObject moose_object 
)
inherited

Add a consumer for this ReporterState.

Parameters
modeThe mode that the object will consume the Reporter value
moose_objectThe MooseObject doing the consuming (for error reporting)
See also
ReporterData

Definition at line 17 of file ReporterState.C.

Referenced by ReporterTransferInterface::addReporterTransferMode().

18 {
19  _consumers.emplace(mode, &moose_object);
20 }
std::set< std::pair< ReporterMode, const MooseObject * > > _consumers
The consumers for this state; we store the MooseObject for detailed error reporting.
Definition: ReporterState.h:84

◆ context()

void* RestartableDataValue::context ( )
inlineinherited

A context pointer for helping with load / store.

Definition at line 65 of file RestartableData.h.

65 { return _context; }
void *const _context
A context pointer for helping with load and store.

◆ copyValuesBack()

template<typename T >
void ReporterState< T >::copyValuesBack ( )

Copy stored values back in time to old/older etc.

Definition at line 209 of file ReporterState.h.

210 {
211  std::list<T> & values = this->set();
212  for (typename std::list<T>::reverse_iterator iter = values.rbegin();
213  std::next(iter) != values.rend();
214  ++iter)
215  (*iter) = (*std::next(iter));
216 }

◆ declared()

bool RestartableDataValue::declared ( ) const
inlineinherited

Whether or not this data has been declared.

Definition at line 85 of file RestartableData.h.

Referenced by RestartableDataValue::store().

85 { return _declared; }
bool _declared
Whether or not this data has been declared (true) or only retreived (false)

◆ get()

const std::list< T > & RestartableData< std::list< T > >::get ( ) const
inlineinherited
Returns
a read-only reference to the parameter value.

Definition at line 280 of file RestartableData.h.

281 {
282  mooseAssert(_value, "Not valid");
283  return *_value;
284 }
std::unique_ptr< std::list< T > > _value
Stored value.

◆ getConsumers()

const std::set<std::pair<ReporterMode, const MooseObject *> >& ReporterStateBase::getConsumers ( ) const
inlineinherited

Returns the consumers for this state; a pair that consists of the mode that the state is being consumed by, and the object consuming it.

See also
ReporterContext

Definition at line 56 of file ReporterState.h.

Referenced by ReporterData::getReporterInfo(), and ReporterContextBase::requiresConsumerModes().

57  {
58  return _consumers;
59  }
std::set< std::pair< ReporterMode, const MooseObject * > > _consumers
The consumers for this state; we store the MooseObject for detailed error reporting.
Definition: ReporterState.h:84

◆ getReporterName()

const ReporterName& ReporterStateBase::getReporterName ( ) const
inlineinherited

Return the ReporterName that this state is associated with.

Definition at line 41 of file ReporterState.h.

Referenced by ReporterData::getReporterInfo().

41 { return _reporter_name; }
ReporterName _reporter_name
Name of data that state is associated.
Definition: ReporterState.h:81

◆ hasContext()

bool RestartableDataValue::hasContext ( ) const
inlineinherited
Returns
Whether or not the data has context set.

Definition at line 70 of file RestartableData.h.

Referenced by RestartableDataValue::store().

70 { return _context != nullptr; }
void *const _context
A context pointer for helping with load and store.

◆ hasStoreJSON()

virtual bool RestartableData< std::list< T > >::hasStoreJSON ( ) const
inlinefinaloverridevirtualinherited
Returns
Whether or not this value supports storing JSON via store

Implements RestartableDataValue.

Definition at line 256 of file RestartableData.h.

256 { return has_store_json; }
static constexpr bool has_store_json
Whether or not this type has a JSON store method implemented.

◆ load()

void RestartableDataValue::load ( std::istream &  stream)
inherited

Loads the value from the stream stream and sets it as loaded.

Definition at line 32 of file RestartableData.C.

33 {
34  loadInternal(stream);
35  _loaded = true;
36 }
bool _loaded
Whether or not this has value has been loaded.
virtual void loadInternal(std::istream &stream)=0
Internal method that loads the value from the stream stream in the specialized class.

◆ loaded()

bool RestartableDataValue::loaded ( ) const
inlineinherited

Whether or not this data has been loaded.

This is typically reset on a call to RestartableDataReader::restore()

Definition at line 98 of file RestartableData.h.

Referenced by RestartableDataValue::store().

98 { return _loaded; }
bool _loaded
Whether or not this has value has been loaded.

◆ loadInternal()

template<typename T >
void ReporterState< T >::loadInternal ( std::istream &  stream)
finaloverridevirtual

Load the RestartableData from a binary stream.

Reimplemented from RestartableData< std::list< T > >.

Definition at line 244 of file ReporterState.h.

245 {
246  // Read the container size
247  std::size_t size = 0;
248  dataLoad(stream, size, nullptr);
249 
250  auto & values = this->set();
251 
252  // If the current container is undersized, expand it to fit the loaded data
253  if (values.size() < size)
254  values.resize(size);
255 
256  // Load each entry of the list directly into the storage
257  // Because we don't shrink the container if the stored size is smaller than
258  // our declared size, we have the odd iterator combo you see below
259  for (auto & val : as_range(values.begin(), std::next(values.begin(), size)))
260  loadHelper(stream, val, nullptr);
261 }
void dataLoad(std::istream &stream, PenetrationInfo *&pinfo, void *context)
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:985

◆ name()

const std::string& RestartableDataValue::name ( ) const
inlineinherited

The full (unique) name of this particular piece of data.

Definition at line 60 of file RestartableData.h.

Referenced by RestartableDataMap::addData(), RestartableDataMap::findData(), and RestartableDataValue::store().

60 { return _name; }
const std::string _name
The full (unique) name of this particular piece of data.

◆ reset()

void RestartableData< std::list< T > >::reset ( )
inlineinherited

Resets (destructs) the underlying data.

Definition at line 296 of file RestartableData.h.

297 {
298  mooseAssert(_value, "Not valid"); // shouldn't really call this twice
299  _value.reset();
300 }
std::unique_ptr< std::list< T > > _value
Stored value.

◆ restoreState()

template<typename T >
bool ReporterState< T >::restoreState ( )

Restore values to their old values, i.e.

value(0) = value(1). This only occurs if old values have been declared, which happens automatically for postprocessors.

Returns
true State was restored
false State was NOT restored

Definition at line 220 of file ReporterState.h.

221 {
222  if (this->get().size() <= 1)
223  return false;
224 
225  this->set().front() = *std::next(this->get().begin());
226  return true;
227 }

◆ set()

std::list< T > & RestartableData< std::list< T > >::set ( )
inlineinherited
Returns
a writable reference to the parameter value.

Definition at line 288 of file RestartableData.h.

289 {
290  mooseAssert(_value, "Not valid");
291  return *_value;
292 }
std::unique_ptr< std::list< T > > _value
Stored value.

◆ setDeclared()

void RestartableDataValue::setDeclared ( const SetDeclaredKey  )
inherited

Sets that this restartable value has been declared.

Definition at line 18 of file RestartableData.C.

Referenced by MooseApp::registerRestartableData().

19 {
20  mooseAssert(!_declared, "Already declared");
21  _declared = true;
22 }
bool _declared
Whether or not this data has been declared (true) or only retreived (false)

◆ setIsPostprocessor()

void ReporterStateBase::setIsPostprocessor ( )
inlineinherited

Sets the special Reporter type to a Postprocessor.

See ReporterData::declareReporterValue.

Definition at line 71 of file ReporterState.h.

void setIsPostprocessor()
Sets the special type to a Postprocessor.
Definition: ReporterName.h:96
ReporterName _reporter_name
Name of data that state is associated.
Definition: ReporterState.h:81

◆ setIsVectorPostprocessor()

void ReporterStateBase::setIsVectorPostprocessor ( )
inlineinherited

Sets the special Reporter type to a VectorPostprocessor.

See ReporterData::declareReporterValue.

Definition at line 77 of file ReporterState.h.

ReporterName _reporter_name
Name of data that state is associated.
Definition: ReporterState.h:81
void setIsVectorPostprocessor()
Sets the special type to a VectorPostprocessor.
Definition: ReporterName.h:102

◆ setNotLoaded()

void RestartableDataValue::setNotLoaded ( const SetNotLoadedKey  )
inlineinherited

Sets that this restartable value has been loaded.

Definition at line 113 of file RestartableData.h.

113 { _loaded = false; }
bool _loaded
Whether or not this has value has been loaded.

◆ setNotStored()

void RestartableDataValue::setNotStored ( const SetNotStoredKey  )
inlineinherited

Sets that this restartable value has been loaded.

Definition at line 136 of file RestartableData.h.

136 { _stored = false; }
bool _stored
Whether or not this has value has been stored.

◆ store() [1/2]

void RestartableDataValue::store ( std::ostream &  stream)
inherited

Stores the value into the stream stream and sets it as stored.

Definition at line 25 of file RestartableData.C.

26 {
27  storeInternal(stream);
28  _stored = true;
29 }
virtual void storeInternal(std::ostream &stream)=0
Internal method that stores the value into the stream stream in the specialized class.
bool _stored
Whether or not this has value has been stored.

◆ store() [2/2]

void RestartableDataValue::store ( nlohmann::json &  json,
const StoreJSONParams params = StoreJSONParams{} 
) const
inherited

Stores this restartable data in the JSON entry json, with the options set by params (optional; defaults to just the type and underlying value)

If the underlying type is not supported for JSON output (if hasStoreJSON() == false), and the parameters have the value output as enabled, this will error.

Definition at line 39 of file RestartableData.C.

40 {
41  if (params.value)
42  {
43  if (hasStoreJSON())
44  storeJSONValue(json["value"]);
45  else
46  mooseError("Failed to output restartable data '",
47  name(),
48  "' as JSON because a to_json method is not implemented for the type '",
49  type(),
50  "'");
51  }
52  if (params.type)
53  json["type"] = type();
54  if (params.name)
55  json["name"] = name();
56  if (params.declared)
57  json["declared"] = declared();
58  if (params.loaded)
59  json["loaded"] = loaded();
60  if (params.stored)
61  json["stored"] = stored();
62  if (params.has_context)
63  json["has_context"] = hasContext();
64 }
bool stored() const
Whether or not this data has been loaded.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
virtual bool hasStoreJSON() const =0
bool hasContext() const
virtual void storeJSONValue(nlohmann::json &json) const =0
Internal method for storing the underlying JSON value.
virtual std::string type() const =0
String identifying the type of parameter stored.
bool loaded() const
Whether or not this data has been loaded.
bool declared() const
Whether or not this data has been declared.
const std::string & name() const
The full (unique) name of this particular piece of data.

◆ stored()

bool RestartableDataValue::stored ( ) const
inlineinherited

Whether or not this data has been loaded.

This is typically reset on a call to RestartableDataWriter::write()

Definition at line 121 of file RestartableData.h.

Referenced by RestartableDataValue::store().

121 { return _stored; }
bool _stored
Whether or not this has value has been stored.

◆ storeInternal()

template<typename T >
void ReporterState< T >::storeInternal ( std::ostream &  stream)
finaloverridevirtual

Loads and stores the data from/to a stream for restart.

This is a special version that handles the fact that the calls declare/getReporterValue occur within the constructor of objects. As such, the storage list already contains data and the references to this data must remain valid.

The default dataLoad assumes the list being populated is empty and simply uses push_back. Therefore, this function loads the data directly into the container to avoid this problem and unnecessary copies.

The default dataStore is very similar, but to ensure consistency (because we're re-defining the load), we implement it again here.

Reimplemented from RestartableData< std::list< T > >.

Definition at line 231 of file ReporterState.h.

232 {
233  // Store the container size
234  std::size_t size = this->get().size();
235  dataStore(stream, size, nullptr);
236 
237  // Store each entry of the list directly into the storage
238  for (auto & val : this->set())
239  storeHelper(stream, val, nullptr);
240 }
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:893
void dataStore(std::ostream &stream, PenetrationInfo *&pinfo, void *context)

◆ storeJSONValue()

void RestartableData< std::list< T > >::storeJSONValue ( nlohmann::json &  json) const
inlinefinaloverrideprotectedvirtualinherited

Internal method for storing the underlying JSON value.

Implements RestartableDataValue.

Definition at line 325 of file RestartableData.h.

326 {
328  nlohmann::to_json(json, get());
329  else
330  mooseAssert(false, "Should not be called");
331 }
Concrete definition of a parameter value for a specified type.
void to_json(nlohmann::json &json, const Moose::LibtorchArtificialNeuralNet *const &network)

◆ type()

std::string RestartableData< std::list< T > >::type ( ) const
inlinefinaloverridevirtualinherited

String identifying the type of parameter stored.

Implements RestartableDataValue.

Definition at line 304 of file RestartableData.h.

305 {
306  return MooseUtils::prettyCppType<T>();
307 }

◆ typeId()

virtual const std::type_info& RestartableData< std::list< T > >::typeId ( ) const
inlinefinaloverridevirtualinherited

The type ID of the underlying data.

Implements RestartableDataValue.

Definition at line 254 of file RestartableData.h.

254 { return typeid(T); }

◆ value() [1/2]

template<typename T >
T & ReporterState< T >::value ( const std::size_t  time_index = 0)

Return a reference to the current value or one of the old values.

The time_index of 0 returns the current value, 1 returns old, 2 returns older, etc.

Definition at line 169 of file ReporterState.h.

Referenced by ReporterVectorContext< T >::clear(), ReporterData::getReporterValue(), ReporterVectorContext< T >::resize(), and ReporterVectorContext< T >::vectorSum().

170 {
171  // Initialize the data; the first entry is the "current" data
172  if (this->get().empty())
173  this->set().resize(1);
174 
175  // If we are a postprocessor, we want to always store an old value so that we can restore the data
176  // if needed. See restoreState.
177  // Note: This can easily be extended to other states like vector-postprocessors or any other
178  // reporter value. However, these states have indeterminate sizes and could create significant,
179  // unecessary memory overhead. In the future, we can extend this to other reporter types if
180  // the need justifies the overhead.
181  if (this->getReporterName().isPostprocessor() && time_index == 0 && this->get().size() <= 1)
182  this->set().push_back(this->get().back());
183 
184  // Initialize old, older, ... data
185  if (this->get().size() <= time_index)
186  this->set().resize(time_index + 1, this->get().back());
187 
188  return *(std::next(this->set().begin(), time_index));
189 }
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
Definition: ReporterState.h:41
bool isPostprocessor() const
Definition: ReporterName.h:85

◆ value() [2/2]

template<typename T >
const T & ReporterState< T >::value ( const std::size_t  time_index = 0) const

Definition at line 193 of file ReporterState.h.

194 {
195  if (this->get().size() <= time_index)
196  mooseError("The desired time index ",
197  time_index,
198  " does not exists for the '",
199  getReporterName(),
200  "' Reporter value, which contains ",
201  this->get().size(),
202  " old value(s). The getReporterValue method must be called with the desired time "
203  "index to be able to access data.");
204  return *(std::next(this->get().begin(), time_index));
205 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
Definition: ReporterState.h:41

◆ valueType()

template<typename T>
std::string ReporterState< T >::valueType ( ) const
inlinefinaloverridevirtual
Returns
The type associated with this state

Implements ReporterStateBase.

Definition at line 139 of file ReporterState.h.

139 { return MooseUtils::prettyCppType<T>(); }

Member Data Documentation

◆ _context

void* const RestartableDataValue::_context
protectedinherited

A context pointer for helping with load and store.

Definition at line 197 of file RestartableData.h.

Referenced by RestartableDataValue::context(), and RestartableDataValue::hasContext().

◆ _name

const std::string RestartableDataValue::_name
protectedinherited

The full (unique) name of this particular piece of data.

Definition at line 194 of file RestartableData.h.

Referenced by RestartableDataValue::name().

◆ has_store_json

constexpr bool RestartableData< std::list< T > >::has_store_json
staticinherited

Whether or not this type has a JSON store method implemented.

Definition at line 219 of file RestartableData.h.


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