https://mooseframework.inl.gov
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ReporterContext< T > Class Template Referenceabstract

General context that is called by all Reporter values to manage the old values. More...

#include <ReporterContext.h>

Inheritance diagram for ReporterContext< T >:
[legend]

Public Types

enum  AutoOperation { AutoOperation::NONE, AutoOperation::BROADCAST }
 Options for automatic parallel operations to perform by the default context. More...
 

Public Member Functions

 ReporterContext (const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< T > &state)
 
 ReporterContext (const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< T > &state, const T &default_value)
 
const ReporterNamename () const override final
 Return the name of the Reporter value. More...
 
const ReporterState< T > & state () const
 Return a reference to the ReporterState object that is storing the Reporter value. More...
 
virtual std::string type () const override
 Return the type being stored by the associated ReporterState object. More...
 
virtual void finalize () override
 Perform automatic parallel communication based on the producer/consumer modes. More...
 
virtual void transfer (ReporterData &r_data, const ReporterName &r_name, unsigned int time_index=0) const override
 Perform type specific transfer. More...
 
virtual void transferToVector (ReporterData &r_data, const ReporterName &r_name, dof_id_type index, unsigned int time_index=0) const override
 Perform type specific transfer to a vector. More...
 
virtual void transferFromVector (ReporterData &r_data, const ReporterName &r_name, dof_id_type index, unsigned int time_index=0) const override
 Perform type specific transfer from a vector. More...
 
void init (const ReporterMode &mode)
 Initialize the producer mode. More...
 
const MooseObjectgetProducer () const
 Return the MooseObject that produces this Reporter. More...
 
const ReporterProducerEnumgetProducerModeEnum () const
 Return the Reporter value produced mode. More...
 
virtual void declareClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const =0
 Helper for declaring new reporter values based on this context. More...
 
virtual void declareVectorClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const =0
 Helper for declaring new vector reporter values based on this context. More...
 
virtual void resize (dof_id_type local_size)=0
 Helper for resizing vector data. More...
 
virtual void clear ()=0
 Helper for clearing vector data. More...
 
virtual void vectorSum ()=0
 Helper for summing reporter value. More...
 
const Parallel::Communicatorcomm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Protected Member Functions

void broadcast ()
 
virtual void storeInfo (nlohmann::json &json) const override
 Output meta data to JSON, see JSONOutput. More...
 
virtual void store (nlohmann::json &json) const override
 Output data to JSON, see JSONOutput. More...
 
virtual std::string contextType () const override=0
 
virtual void copyValuesBack () override
 Called by FEProblemBase::advanceState via ReporterData. More...
 
virtual bool restoreState () override
 Restore state to its old values. More...
 
void requiresConsumerModes (const ReporterStateBase &state, const std::set< ReporterMode > &modes) const
 Helper for checking whether or not the state state has only the modes modes. More...
 

Protected Attributes

ReporterState< T > & _state
 The state on which this context object operates. More...
 
const MooseObject_producer
 The MooseObject that is producing this Reporter. More...
 
ReporterProducerEnum _producer_enum
 Defines how the Reporter value can be produced and how it is being produced. More...
 
const Parallel::Communicator_communicator
 

Detailed Description

template<typename T>
class ReporterContext< T >

General context that is called by all Reporter values to manage the old values.

This is the default context object, the communication aspect includes inspecting the produced mode against the consumed mode to be sure that the data is compatible or if automatic communication can be done to make them compatible.

Definition at line 228 of file ReporterContext.h.

Member Enumeration Documentation

◆ AutoOperation

template<typename T>
enum ReporterContext::AutoOperation
strong

Options for automatic parallel operations to perform by the default context.

Enumerator
NONE 
BROADCAST 

Definition at line 234 of file ReporterContext.h.

235  {
236  NONE,
237  BROADCAST
238  };

Constructor & Destructor Documentation

◆ ReporterContext() [1/2]

template<typename T>
ReporterContext< T >::ReporterContext ( const libMesh::ParallelObject other,
const MooseObject producer,
ReporterState< T > &  state 
)

Definition at line 330 of file ReporterContext.h.

333  : ReporterContextBase(other, producer), _state(state)
334 {
336 }
const ReporterMode REPORTER_MODE_ROOT
const ReporterState< T > & state() const
Return a reference to the ReporterState object that is storing the Reporter value.
ReporterState< T > & _state
The state on which this context object operates.
ReporterContextBase(const libMesh::ParallelObject &other, const MooseObject &producer)
const ReporterMode REPORTER_MODE_DISTRIBUTED
void insert(const ReporterMode &mode, Args... modes)
Definition: ReporterMode.h:79
const ReporterMode REPORTER_MODE_REPLICATED
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.

◆ ReporterContext() [2/2]

template<typename T>
ReporterContext< T >::ReporterContext ( const libMesh::ParallelObject other,
const MooseObject producer,
ReporterState< T > &  state,
const T &  default_value 
)

Definition at line 339 of file ReporterContext.h.

343  : ReporterContext(other, producer, state)
344 {
345  _state.value() = default_value;
346 }
const ReporterState< T > & state() const
Return a reference to the ReporterState object that is storing the Reporter value.
ReporterState< T > & _state
The state on which this context object operates.
ReporterContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< T > &state)

Member Function Documentation

◆ broadcast()

template<typename T>
void ReporterContext< T >::broadcast ( )
inlineprotected

Definition at line 303 of file ReporterContext.h.

304  {
306  this->comm().broadcast(this->_state.value());
307  else
308  mooseError("Cannot broadcast Reporter type '", MooseUtils::prettyCppType<T>(), "'");
309  }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
ReporterState< T > & _state
The state on which this context object operates.
const Parallel::Communicator & comm() const
Custom type trait that has a value of true for types that can be broadcasted.
Definition: MooseUtils.h:1015
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const

◆ clear()

virtual void ReporterContextBase::clear ( )
pure virtualinherited

Helper for clearing vector data.

Implemented in ReporterVectorContext< T >, and ReporterGeneralContext< T >.

Referenced by ReporterTransferInterface::clearVectorReporter().

◆ contextType()

template<typename T>
virtual std::string ReporterContext< T >::contextType ( ) const
overrideprotectedpure virtual
Returns
The derived context type for this context.

This must be overridden in every class.

Implements ReporterContextBase.

Implemented in ReporterVectorContext< T >, ReporterGatherContext< T >, ReporterScatterContext< T >, ReporterBroadcastContext< T >, and ReporterGeneralContext< T >.

◆ copyValuesBack()

template<typename T >
void ReporterContext< T >::copyValuesBack ( )
overrideprotectedvirtual

Called by FEProblemBase::advanceState via ReporterData.

Implements ReporterContextBase.

Reimplemented in VectorPostprocessorContext< T >.

Definition at line 408 of file ReporterContext.h.

Referenced by VectorPostprocessorContext< T >::copyValuesBack().

409 {
410  _state.copyValuesBack();
411 }
ReporterState< T > & _state
The state on which this context object operates.

◆ declareClone()

virtual void ReporterContextBase::declareClone ( ReporterData r_data,
const ReporterName r_name,
const ReporterMode mode,
const MooseObject producer 
) const
pure virtualinherited

Helper for declaring new reporter values based on this context.

Parameters
r_dataThe ReporterData on the app that this value is being declared
r_nameThe name of the Reporter value being declared
modeReporter mode to declare with
See also
ReporterTransferInterface

Implemented in ReporterVectorContext< T >, and ReporterGeneralContext< T >.

Referenced by ReporterTransferInterface::declareClone().

◆ declareVectorClone()

virtual void ReporterContextBase::declareVectorClone ( ReporterData r_data,
const ReporterName r_name,
const ReporterMode mode,
const MooseObject producer 
) const
pure virtualinherited

Helper for declaring new vector reporter values based on this context.

Parameters
r_dataThe ReporterData on the app that this value is being declared
r_nameThe name of the Reporter value being declared
modeReporter mode to declare with
See also
ReporterTransferInterface

Implemented in ReporterVectorContext< T >, and ReporterGeneralContext< T >.

Referenced by ReporterTransferInterface::declareVectorClone().

◆ finalize()

template<typename T >
void ReporterContext< T >::finalize ( )
overridevirtual

Perform automatic parallel communication based on the producer/consumer modes.

Implements ReporterContextBase.

Reimplemented in ReporterGatherContext< T >, ReporterScatterContext< T >, ReporterBroadcastContext< T >, and VectorPostprocessorContext< T >.

Definition at line 350 of file ReporterContext.h.

Referenced by VectorPostprocessorContext< T >::finalize().

351 {
352  // Automatic parallel operation to perform
354 
355  // Set the default producer mode to ROOT
356  if (!_producer_enum.isValid())
358 
359  // Determine auto parallel operation to perform
360  const auto & producer = _producer_enum; // convenience
361  for (const auto & pair : _state.getConsumers())
362  {
363  const ReporterMode consumer = pair.first;
364  const MooseObject * moose_object = pair.second;
365 
366  // The following sets up the automatic operations and performs error checking for the various
367  // modes for the producer and consumer
368  //
369  // The data is correct and requires no operation for the following conditions (PRODUCER ->
370  // CONSUMER)
371  // ROOT -> ROOT
372  // REPLICATED -> REPLICATED
373  // DISTRIBUTED -> DISTRIBUTED
374  // REPLICATED -> ROOT
375 
376  // Perform broadcast in the case
377  // ROOT -> REPLICATED
378  if (producer == REPORTER_MODE_ROOT && consumer == REPORTER_MODE_REPLICATED)
380 
381  // The following are not support and create an error
382  // ROOT -> DISTRIBUTED
383  // REPLICATED -> DISTRIBUTED
384  // DISTRIBUTED -> ROOT
385  // DISTRIBUTED -> REPLICATED
386  else if ((producer == REPORTER_MODE_ROOT && consumer == REPORTER_MODE_DISTRIBUTED) ||
387  (producer == REPORTER_MODE_REPLICATED && consumer == REPORTER_MODE_DISTRIBUTED) ||
388  (producer == REPORTER_MODE_DISTRIBUTED && consumer == REPORTER_MODE_ROOT) ||
389  (producer == REPORTER_MODE_DISTRIBUTED && consumer == REPORTER_MODE_REPLICATED))
390  mooseError("The Reporter value \"",
391  name(),
392  "\" is being produced in ",
393  producer,
394  " mode, but the ",
395  moose_object->typeAndName(),
396  " is requesting to consume it in ",
397  consumer,
398  " mode, which is not supported.");
399  }
400 
401  // Perform desired auto parallel operation
402  if (auto_operation == ReporterContext::AutoOperation::BROADCAST)
403  this->broadcast();
404 }
const ReporterName & name() const override final
Return the name of the Reporter value.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const ReporterMode REPORTER_MODE_ROOT
ReporterState< T > & _state
The state on which this context object operates.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
AutoOperation
Options for automatic parallel operations to perform by the default context.
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:27
const ReporterMode REPORTER_MODE_DISTRIBUTED
void assign(const std::string &name, const std::optional< std::string > &context={})
Definition: MooseEnum.C:60
const ReporterMode REPORTER_MODE_REPLICATED
virtual bool isValid() const override
IsValid.
Definition: MooseEnum.h:118
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:44

◆ getProducer()

const MooseObject& ReporterContextBase::getProducer ( ) const
inlineinherited

Return the MooseObject that produces this Reporter.

Definition at line 118 of file ReporterContext.h.

Referenced by ReporterData::getReporterInfo().

118 { return _producer; }
const MooseObject & _producer
The MooseObject that is producing this Reporter.

◆ getProducerModeEnum()

const ReporterProducerEnum & ReporterContextBase::getProducerModeEnum ( ) const
inherited

Return the Reporter value produced mode.

Definition at line 28 of file ReporterContext.C.

Referenced by ReporterData::getReporterMode().

29 {
30  return _producer_enum;
31 }
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.

◆ init()

void ReporterContextBase::init ( const ReporterMode mode)
inherited

Initialize the producer mode.

This done after construction to allow the constructor to define the available values in the ReporterProducerEnum.

See also
ReporterData::declareReporterValue

Definition at line 21 of file ReporterContext.C.

22 {
23  if (mode != REPORTER_MODE_UNSET)
24  _producer_enum.assign(mode);
25 }
const ReporterMode REPORTER_MODE_UNSET
void assign(const std::string &name, const std::optional< std::string > &context={})
Definition: MooseEnum.C:60
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.

◆ name()

template<typename T>
const ReporterName& ReporterContext< T >::name ( ) const
inlinefinaloverridevirtual

Return the name of the Reporter value.

Implements ReporterContextBase.

Definition at line 251 of file ReporterContext.h.

251 { return _state.getReporterName(); }
ReporterState< T > & _state
The state on which this context object operates.

◆ requiresConsumerModes()

void ReporterContextBase::requiresConsumerModes ( const ReporterStateBase state,
const std::set< ReporterMode > &  modes 
) const
protectedinherited

Helper for checking whether or not the state state has only the modes modes.

Definition at line 34 of file ReporterContext.C.

36 {
37  for (const auto & mode_object_pair : state.getConsumers())
38  if (!modes.count(mode_object_pair.first))
39  {
40  std::stringstream oss;
41  std::copy(modes.begin(), modes.end(), std::ostream_iterator<ReporterMode>(oss, " "));
42 
43  mooseError("The Reporter value '",
44  name(),
45  "' is being produced in ",
47  " mode, but ",
48  mode_object_pair.second->typeAndName(),
49  " is requesting to consume it in ",
50  mode_object_pair.first,
51  " mode, which is not supported.\n\nThe mode must be { ",
52  oss.str(),
53  " }.\n\n",
54  ReporterData::getReporterInfo(state, this));
55  }
56 }
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
virtual const ReporterName & name() const =0
Return the ReporterName that the context is associated.
std::string getReporterInfo() const
Gets information about all declared/requested Reporters.
Definition: ReporterData.C:247
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.

◆ resize()

virtual void ReporterContextBase::resize ( dof_id_type  local_size)
pure virtualinherited

Helper for resizing vector data.

Parameters
local_sizeNumber of elements to resize vector to

Implemented in ReporterVectorContext< T >, and ReporterGeneralContext< T >.

Referenced by ReporterTransferInterface::resizeReporter().

◆ restoreState()

template<typename T>
virtual bool ReporterContext< T >::restoreState ( )
inlineoverrideprotectedvirtual

Restore state to its old values.

See also
ReporterState::restoreState

Implements ReporterContextBase.

Definition at line 323 of file ReporterContext.h.

323 { return _state.restoreState(); }
ReporterState< T > & _state
The state on which this context object operates.

◆ state()

template<typename T>
const ReporterState<T>& ReporterContext< T >::state ( ) const
inline

Return a reference to the ReporterState object that is storing the Reporter value.

Definition at line 256 of file ReporterContext.h.

256 { return _state; }
ReporterState< T > & _state
The state on which this context object operates.

◆ store()

template<typename T >
void ReporterContext< T >::store ( nlohmann::json &  json) const
overrideprotectedvirtual

Output data to JSON, see JSONOutput.

Implements ReporterContextBase.

Definition at line 422 of file ReporterContext.h.

423 {
424  nlohmann::to_json(json, this->_state.value());
425 }
ReporterState< T > & _state
The state on which this context object operates.
void to_json(nlohmann::json &json, const Moose::LibtorchArtificialNeuralNet *const &network)

◆ storeInfo()

template<typename T >
void ReporterContext< T >::storeInfo ( nlohmann::json &  json) const
overrideprotectedvirtual

Output meta data to JSON, see JSONOutput.

Implements ReporterContextBase.

Definition at line 415 of file ReporterContext.h.

416 {
417  json["type"] = this->type();
418 }
virtual std::string type() const override
Return the type being stored by the associated ReporterState object.

◆ transfer()

template<typename T >
void ReporterContext< T >::transfer ( ReporterData r_data,
const ReporterName r_name,
unsigned int  time_index = 0 
) const
overridevirtual

Perform type specific transfer.

NOTE: This is defined in ReporterData.h to avoid cyclic includes that would arise. I don't know of a better solution, if you have one please implement it.

Implements ReporterContextBase.

Definition at line 502 of file ReporterData.h.

505 {
506  r_data.setReporterValue<T>(r_name, _state.value(), time_index);
507 }
ReporterState< T > & _state
The state on which this context object operates.
void setReporterValue(const ReporterName &reporter_name, const T &value, const std::size_t time_index=0)
Method for setting Reporter values that already exist.
Definition: ReporterData.h:481

◆ transferFromVector()

template<typename T >
void ReporterContext< T >::transferFromVector ( ReporterData r_data,
const ReporterName r_name,
dof_id_type  index,
unsigned int  time_index = 0 
) const
overridevirtual

Perform type specific transfer from a vector.

NOTE: This is defined in ReporterData.h to avoid cyclic includes that would arise. I don't know of a better solution, if you have one please implement it.

Implements ReporterContextBase.

Definition at line 529 of file ReporterData.h.

533 {
534  if constexpr (is_std_vector<T>::value)
535  {
536  if (index >= _state.value().size())
537  mooseError("Requested index ",
538  index,
539  " is outside the bounds of the vector reporter value ",
540  r_name);
541 
542  using R = typename T::value_type;
543  r_data.setReporterValue<R>(r_name, _state.value()[index], time_index);
544  }
545  else
546  {
547  libmesh_ignore(r_data);
548  libmesh_ignore(r_name);
549  libmesh_ignore(index);
550  libmesh_ignore(time_index);
551  mooseError("transferFromVector can only be used for reporter types that are specializatons of "
552  "std::vector.");
553  }
554 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
ReporterState< T > & _state
The state on which this context object operates.
void libmesh_ignore(const Args &...)
void setReporterValue(const ReporterName &reporter_name, const T &value, const std::size_t time_index=0)
Method for setting Reporter values that already exist.
Definition: ReporterData.h:481

◆ transferToVector()

template<typename T >
void ReporterContext< T >::transferToVector ( ReporterData r_data,
const ReporterName r_name,
dof_id_type  index,
unsigned int  time_index = 0 
) const
overridevirtual

Perform type specific transfer to a vector.

NOTE: This is defined in ReporterData.h to avoid cyclic includes that would arise. I don't know of a better solution, if you have one please implement it.

Implements ReporterContextBase.

Definition at line 512 of file ReporterData.h.

516 {
517  std::vector<T> & vec =
518  const_cast<std::vector<T> &>(r_data.getReporterValue<std::vector<T>>(r_name, time_index));
519 
520  if (index >= vec.size())
521  mooseError(
522  "Requested index ", index, " is outside the bounds of the vector reporter value ", r_name);
523  vec[index] = _state.value();
524 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
ReporterState< T > & _state
The state on which this context object operates.
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.
Definition: ReporterData.h:388

◆ type()

template<typename T>
virtual std::string ReporterContext< T >::type ( ) const
inlineoverridevirtual

Return the type being stored by the associated ReporterState object.

See also
ReporterContext::storeInfo

Implements ReporterContextBase.

Definition at line 263 of file ReporterContext.h.

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

◆ vectorSum()

virtual void ReporterContextBase::vectorSum ( )
pure virtualinherited

Helper for summing reporter value.

Implemented in ReporterVectorContext< T >, and ReporterGeneralContext< T >.

Referenced by ReporterTransferInterface::sumVectorReporter().

Member Data Documentation

◆ _producer

const MooseObject& ReporterContextBase::_producer
protectedinherited

The MooseObject that is producing this Reporter.

Definition at line 214 of file ReporterContext.h.

Referenced by ReporterContextBase::getProducer().

◆ _producer_enum

ReporterProducerEnum ReporterContextBase::_producer_enum
protectedinherited

◆ _state

template<typename T>
ReporterState<T>& ReporterContext< T >::_state
protected

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