https://mooseframework.inl.gov
ReporterState.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 #pragma once
10 
11 #include <iostream>
12 
13 #include "libmesh/parallel.h"
14 #include "libmesh/parallel_object.h"
15 #include "libmesh/simple_range.h"
16 
17 #include "ReporterName.h"
18 #include "RestartableData.h"
19 #include "ReporterMode.h"
20 
21 // Forward declarations
22 class MooseObject;
24 
33 {
34 public:
36  virtual ~ReporterStateBase() = default;
37 
41  const ReporterName & getReporterName() const { return _reporter_name; }
42 
49  void addConsumer(ReporterMode mode, const MooseObject & moose_object);
50 
56  const std::set<std::pair<ReporterMode, const MooseObject *>> & getConsumers() const
57  {
58  return _consumers;
59  }
60 
64  virtual std::string valueType() const = 0;
65 
78 
79 private:
82 
84  std::set<std::pair<ReporterMode, const MooseObject *>> _consumers;
85 };
86 
91 bool operator<(const std::pair<ReporterMode, const MooseObject *> & a,
92  const std::pair<ReporterMode, const MooseObject *> & b);
93 
109 template <typename T>
110 class ReporterState : public ReporterStateBase, public RestartableData<std::list<T>>
111 {
112 public:
114 
116 
121  T & value(const std::size_t time_index = 0);
122  const T & value(const std::size_t time_index = 0) const;
124 
128  void copyValuesBack();
129 
137  bool restoreState();
138 
139  std::string valueType() const override final { return MooseUtils::prettyCppType<T>(); }
140 
155  void storeInternal(std::ostream & stream) override final;
157  void loadInternal(std::istream & stream) override final;
159 };
160 
161 template <typename T>
163  : ReporterStateBase(name), RestartableData<std::list<T>>(name.getRestartableName(), nullptr)
164 {
165 }
166 
167 template <typename T>
168 T &
169 ReporterState<T>::value(const std::size_t time_index)
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 }
190 
191 template <typename T>
192 const T &
193 ReporterState<T>::value(const std::size_t time_index) const
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 }
206 
207 template <typename T>
208 void
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 }
217 
218 template <typename T>
219 bool
221 {
222  if (this->get().size() <= 1)
223  return false;
224 
225  this->set().front() = *std::next(this->get().begin());
226  return true;
227 }
228 
229 template <typename T>
230 void
231 ReporterState<T>::storeInternal(std::ostream & stream)
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 }
241 
242 template <typename T>
243 void
244 ReporterState<T>::loadInternal(std::istream & stream)
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 }
std::string name(const ElemQuality q)
A special version of RestartableData to aid in storing Reporter values.
std::string valueType() const override final
ReporterStateBase(const ReporterName &name)
Definition: ReporterState.C:14
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
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
ReporterState(const ReporterName &name)
void dataLoad(std::istream &stream, PenetrationInfo *&pinfo, void *context)
void addConsumer(ReporterMode mode, const MooseObject &moose_object)
Add a consumer for this ReporterState.
Definition: ReporterState.C:17
void storeInternal(std::ostream &stream) override final
Loads and stores the data from/to a stream for restart.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:893
void setIsVectorPostprocessor()
Sets the special Reporter type to a VectorPostprocessor.
Definition: ReporterState.h:77
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.
Definition: ReporterState.h:41
This is a helper class to aid with parallel communication of compute Reporter values as well as provi...
void setIsPostprocessor()
Sets the special Reporter type to a Postprocessor.
Definition: ReporterState.h:71
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
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
void loadInternal(std::istream &stream) override final
Load the RestartableData from a binary stream.
virtual std::string valueType() const =0
The base class for storing a Reporter&#39;s state.
Definition: ReporterState.h:32
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
void setIsVectorPostprocessor()
Sets the special type to a VectorPostprocessor.
Definition: ReporterName.h:102
Concrete definition of a parameter value for a specified type.
bool restoreState()
Restore values to their old values, i.e.
void copyValuesBack()
Copy stored values back in time to old/older etc.
T & value(const std::size_t time_index=0)
Return a reference to the current value or one of the old values.
void dataStore(std::ostream &stream, PenetrationInfo *&pinfo, void *context)
virtual ~ReporterStateBase()=default
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:985
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:44
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
const std::string & name() const
The full (unique) name of this particular piece of data.