https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
22class MooseObject;
24
33{
34public:
35 ReporterStateBase(const ReporterName & name);
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
79private:
82
84 std::set<std::pair<ReporterMode, const MooseObject *>> _consumers;
85};
86
91bool operator<(const std::pair<ReporterMode, const MooseObject *> & a,
92 const std::pair<ReporterMode, const MooseObject *> & b);
93
109template <typename T>
110class ReporterState : public ReporterStateBase, public RestartableData<std::list<T>>
111{
112public:
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
129
138
139 std::string valueType() const override final { return MooseUtils::prettyCppType<T>(); }
140
156 void storeInternal(std::ostream & stream) override final;
157 void loadInternal(std::istream & stream) override final;
159};
160
161template <typename T>
163 : ReporterStateBase(name), RestartableData<std::list<T>>(name.getRestartableName(), nullptr)
164{
165}
166
167template <typename T>
168T &
169ReporterState<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
191template <typename T>
192const T &
193ReporterState<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
207template <typename T>
208void
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
218template <typename T>
219bool
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
229template <typename T>
230void
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
242template <typename T>
243void
244ReporterState<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}
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:989
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:1081
void dataLoad(std::istream &stream, LineSegment &l, void *context)
void dataStore(std::ostream &stream, LineSegment &l, void *context)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::array< Real, 2 > values
Definition MortarUtils.C:52
bool operator<(const std::pair< ReporterMode, const MooseObject * > &a, const std::pair< ReporterMode, const MooseObject * > &b)
Custom sort for ReporterState::_consumers so that they are sorted by object type, object name,...
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...
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.
void setIsVectorPostprocessor()
Sets the special type to a VectorPostprocessor.
void setIsPostprocessor()
Sets the special type to a Postprocessor.
The base class for storing a Reporter's state.
virtual ~ReporterStateBase()=default
void addConsumer(ReporterMode mode, const MooseObject &moose_object)
Add a consumer for this ReporterState.
void setIsPostprocessor()
Sets the special Reporter type to a Postprocessor.
void setIsVectorPostprocessor()
Sets the special Reporter type to a VectorPostprocessor.
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...
ReporterName _reporter_name
Name of data that state is associated.
std::set< std::pair< ReporterMode, const MooseObject * > > _consumers
The consumers for this state; we store the MooseObject for detailed error reporting.
A special version of RestartableData to aid in storing Reporter values.
bool restoreState()
Restore values to their old values, i.e.
void storeInternal(std::ostream &stream) override final
Loads and stores the data from/to a stream for restart.
std::string valueType() const override final
void copyValuesBack()
Copy stored values back in time to old/older etc.
void loadInternal(std::istream &stream) override final
Load the RestartableData from a binary stream.
T & value(const std::size_t time_index=0)
Return a reference to the current value or one of the old values.
const T & value(const std::size_t time_index=0) const
ReporterState(const ReporterName &name)
const std::string & name() const
The full (unique) name of this particular piece of data.
Concrete definition of a parameter value for a specified type.