https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ReporterVectorContext< T > Class Template Reference

This context is specific for vector types of reporters, mainly for declaring a vector of the type from another context. More...

#include <ReporterContext.h>

Inheritance diagram for ReporterVectorContext< T >:
[legend]

Public Types

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

Public Member Functions

 ReporterVectorContext (const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< std::vector< T > > &state)
 
 ReporterVectorContext (const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< std::vector< T > > &state, const std::vector< T > &default_value)
 
virtual void declareClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const final
 This simply throws an error to avoid infinite instantiations.
 
virtual void declareVectorClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const final
 This simply throws an error to avoid infinite instantiations.
 
virtual void resize (dof_id_type local_size) override
 Since we know that the _state value is a vector type, we can resize it based on.
 
virtual void clear () override
 Since we know that the _state value is a vector type, we can clear it.
 
virtual void vectorSum () override
 Helper for summing reporter value.
 
virtual std::string contextType () const override
 
const ReporterName & name () const override final
 Return the name of the Reporter value.
 
const ReporterState< std::vector< T > > & state () const
 Return a reference to the ReporterState object that is storing the Reporter value.
 
virtual std::string type () const override
 Return the type being stored by the associated ReporterState object.
 
virtual void finalize () override
 Perform automatic parallel communication based on the producer/consumer modes.
 
virtual void transfer (ReporterData &r_data, const ReporterName &r_name, unsigned int time_index=0) const override
 Perform type specific transfer.
 
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.
 
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.
 
virtual void broadcast (processor_id_type root=0) override
 Helper for broadcasting reporter value from processor root.
 
void init (const ReporterMode &mode)
 Initialize the producer mode.
 
const MooseObject & getProducer () const
 Return the MooseObject that produces this Reporter.
 
const ReporterProducerEnum & getProducerModeEnum () const
 Return the Reporter value produced mode.
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Protected Member Functions

virtual void storeInfo (nlohmann::json &json) const override
 Output meta data to JSON, see JSONOutput.
 
virtual void store (nlohmann::json &json) const override
 Output data to JSON, see JSONOutput.
 
virtual void copyValuesBack () override
 Called by FEProblemBase::advanceState via ReporterData.
 
virtual bool restoreState () override
 Restore state to its old values.
 
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.
 

Protected Attributes

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

Detailed Description

template<typename T>
class ReporterVectorContext< T >

This context is specific for vector types of reporters, mainly for declaring a vector of the type from another context.

As well as resizing the vector of data.

See also
ReporterGeneralContext::declareVectorClone and ReporterTransferInterface

Definition at line 740 of file ReporterContext.h.

Member Enumeration Documentation

◆ AutoOperation

enum ReporterContext::AutoOperation
stronginherited

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

Definition at line 239 of file ReporterContext.h.

240 {
241 NONE,
242 BROADCAST
243 };

Constructor & Destructor Documentation

◆ ReporterVectorContext() [1/2]

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

Definition at line 833 of file ReporterContext.h.

836 : ReporterContext<std::vector<T>>(other, producer, state)
837{
838}
General context that is called by all Reporter values to manage the old values.
const ReporterState< std::vector< T > > & state() const
Return a reference to the ReporterState object that is storing the Reporter value.

◆ ReporterVectorContext() [2/2]

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

Definition at line 841 of file ReporterContext.h.

845 : ReporterContext<std::vector<T>>(other, producer, state, default_value)
846{
847}

Member Function Documentation

◆ broadcast()

virtual void ReporterContext< std::vector< T > >::broadcast ( processor_id_type  root = 0)
inlineoverridevirtualinherited

Helper for broadcasting reporter value from processor root.

Implements ReporterContextBase.

Definition at line 307 of file ReporterContext.h.

308 {
309 if constexpr (MooseUtils::canBroadcast<T>::value)
310 this->comm().broadcast(this->_state.value(), root);
311 else
312 mooseError("Cannot broadcast Reporter type '", MooseUtils::prettyCppType<T>(), "'");
313 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
ReporterState< std::vector< T > > & _state
The state on which this context object operates.
T & value(const std::size_t time_index=0)
Return a reference to the current value or one of the old values.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
const Parallel::Communicator & comm() const

◆ clear()

template<typename T >
virtual void ReporterVectorContext< T >::clear ( )
inlineoverridevirtual

Since we know that the _state value is a vector type, we can clear it.

Implements ReporterContextBase.

Definition at line 778 of file ReporterContext.h.

778{ this->_state.value().clear(); }

◆ contextType()

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

This must be overridden in every class.

Implements ReporterContext< std::vector< T > >.

Definition at line 829 of file ReporterContext.h.

829{ return MooseUtils::prettyCppType(this); }
std::string prettyCppType(const std::string &cpp_type)

◆ copyValuesBack()

void ReporterContext< std::vector< T > >::copyValuesBack ( )
overrideprotectedvirtualinherited

Called by FEProblemBase::advanceState via ReporterData.

Implements ReporterContextBase.

Definition at line 325 of file ReporterContext.h.

418{
420}
void copyValuesBack()
Copy stored values back in time to old/older etc.

◆ declareClone()

template<typename T >
void ReporterVectorContext< T >::declareClone ( ReporterData &  r_data,
const ReporterName &  r_name,
const ReporterMode &  mode,
const MooseObject &  producer 
) const
finalvirtual

This simply throws an error to avoid infinite instantiations.

It is defined in ReporterData.h to avoid cyclic included.

Implements ReporterContextBase.

Definition at line 594 of file ReporterData.h.

598{
599 mooseError("Cannot create clone with ReporterVectorContext.");
600}

◆ declareVectorClone()

template<typename T >
void ReporterVectorContext< T >::declareVectorClone ( ReporterData &  r_data,
const ReporterName &  r_name,
const ReporterMode &  mode,
const MooseObject &  producer 
) const
finalvirtual

This simply throws an error to avoid infinite instantiations.

It is defined in ReporterData.h to avoid cyclic included.

Implements ReporterContextBase.

Definition at line 605 of file ReporterData.h.

609{
610 mooseError("Cannot create clone with ReporterVectorContext.");
611}

◆ finalize()

void ReporterContext< std::vector< T > >::finalize ( )
overridevirtualinherited

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

Implements ReporterContextBase.

Definition at line 273 of file ReporterContext.h.

356{
357 // Automatic parallel operation to perform
359
360 // Set the default producer mode to ROOT
361 if (!_producer_enum.isValid())
363
364 // Determine auto parallel operation to perform
365 const auto & producer = _producer_enum; // convenience
366 for (const auto & pair : _state.getConsumers())
367 {
368 const ReporterMode consumer = pair.first;
369 const MooseObject * moose_object = pair.second;
370
371 // The following sets up the automatic operations and performs error checking for the various
372 // modes for the producer and consumer
373 //
374 // The data is correct and requires no operation for the following conditions (PRODUCER ->
375 // CONSUMER)
376 // ROOT -> ROOT
377 // REPLICATED -> REPLICATED
378 // DISTRIBUTED -> DISTRIBUTED
379 // REPLICATED -> ROOT
380
381 // Perform broadcast in the case
382 // ROOT -> REPLICATED
383 if (static_cast<int>(producer) == REPORTER_MODE_ROOT && consumer == REPORTER_MODE_REPLICATED)
385
386 // The following are not support and create an error
387 // ROOT -> DISTRIBUTED
388 // REPLICATED -> DISTRIBUTED
389 // DISTRIBUTED -> ROOT
390 // DISTRIBUTED -> REPLICATED
391 else if ((static_cast<int>(producer) == REPORTER_MODE_ROOT &&
392 consumer == REPORTER_MODE_DISTRIBUTED) ||
393 (static_cast<int>(producer) == REPORTER_MODE_REPLICATED &&
394 consumer == REPORTER_MODE_DISTRIBUTED) ||
395 (static_cast<int>(producer) == REPORTER_MODE_DISTRIBUTED &&
396 consumer == REPORTER_MODE_ROOT) ||
397 (static_cast<int>(producer) == REPORTER_MODE_DISTRIBUTED &&
398 consumer == REPORTER_MODE_REPLICATED))
399 mooseError("The Reporter value \"",
400 name(),
401 "\" is being produced in ",
402 producer,
403 " mode, but the ",
404 moose_object->typeAndName(),
405 " is requesting to consume it in ",
406 consumer,
407 " mode, which is not supported.");
408 }
409
410 // Perform desired auto parallel operation
411 if (auto_operation == ReporterContext::AutoOperation::BROADCAST)
412 this->broadcast();
413}
const ReporterMode REPORTER_MODE_REPLICATED
const ReporterMode REPORTER_MODE_DISTRIBUTED
const ReporterMode REPORTER_MODE_ROOT
std::string typeAndName() const
Get the class's combined type and name; useful in error handling.
Definition MooseBase.C:57
virtual bool isValid() const override
IsValid.
Definition MooseEnum.h:139
void assign(const std::string &name, const std::optional< std::string > &context={})
Definition MooseEnum.C:60
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
ReporterProducerEnum _producer_enum
Defines how the Reporter value can be produced and how it is being produced.
AutoOperation
Options for automatic parallel operations to perform by the default context.
virtual void broadcast(processor_id_type root=0) override
const ReporterName & name() const override final
Return the name of the Reporter value.
MooseEnumItem that automatically creates the ID and doesn't allow the ID to be assigned.

◆ getProducer()

const MooseObject & ReporterContextBase::getProducer ( ) const
inlineinherited

Return the MooseObject that produces this Reporter.

Definition at line 118 of file ReporterContext.h.

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

Referenced by ReporterData::getReporterInfo().

◆ getProducerModeEnum()

const ReporterProducerEnum & ReporterContextBase::getProducerModeEnum ( ) const
inherited

Return the Reporter value produced mode.

Definition at line 28 of file ReporterContext.C.

29{
30 return _producer_enum;
31}

Referenced by ReporterData::getReporterMode().

◆ 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)
25}
const ReporterMode REPORTER_MODE_UNSET

◆ name()

const ReporterName & ReporterContext< std::vector< T > >::name ( ) const
inlinefinaloverridevirtualinherited

Return the name of the Reporter value.

Implements ReporterContextBase.

Definition at line 256 of file ReporterContext.h.

256{ return _state.getReporterName(); }
const ReporterName & getReporterName() const
Return the ReporterName that this state is associated with.

◆ 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",
55 }
56}
unsigned int count
Definition MortarUtils.C:53
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
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.

◆ resize()

template<typename T >
virtual void ReporterVectorContext< T >::resize ( dof_id_type  local_size)
inlineoverridevirtual

Since we know that the _state value is a vector type, we can resize it based on.

Parameters
local_size

Implements ReporterContextBase.

Definition at line 773 of file ReporterContext.h.

773{ this->_state.value().resize(local_size); }

◆ restoreState()

virtual bool ReporterContext< std::vector< T > >::restoreState ( )
inlineoverrideprotectedvirtualinherited

Restore state to its old values.

See also
ReporterState::restoreState

Implements ReporterContextBase.

Definition at line 328 of file ReporterContext.h.

328{ return _state.restoreState(); }
bool restoreState()
Restore values to their old values, i.e.

◆ state()

const ReporterState< std::vector< T > > & ReporterContext< std::vector< T > >::state ( ) const
inlineinherited

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

Definition at line 261 of file ReporterContext.h.

261{ return _state; }

◆ store()

void ReporterContext< std::vector< T > >::store ( nlohmann::json &  json) const
overrideprotectedvirtualinherited

Output data to JSON, see JSONOutput.

Implements ReporterContextBase.

Definition at line 320 of file ReporterContext.h.

432{
433 nlohmann::to_json(json, this->_state.value());
434}

◆ storeInfo()

void ReporterContext< std::vector< T > >::storeInfo ( nlohmann::json &  json) const
overrideprotectedvirtualinherited

Output meta data to JSON, see JSONOutput.

Implements ReporterContextBase.

Definition at line 317 of file ReporterContext.h.

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

◆ transfer()

void ReporterContext< std::vector< T > >::transfer ( ReporterData &  r_data,
const ReporterName &  r_name,
unsigned int  time_index = 0 
) const
overridevirtualinherited

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 281 of file ReporterData.h.

518{
519 r_data.setReporterValue<T>(r_name, _state.value(), time_index);
520}
void setReporterValue(const ReporterName &reporter_name, const T &value, const std::size_t time_index=0)
Method for setting Reporter values that already exist.

◆ transferFromVector()

void ReporterContext< std::vector< T > >::transferFromVector ( ReporterData &  r_data,
const ReporterName &  r_name,
dof_id_type  index,
unsigned int  time_index = 0 
) const
overridevirtualinherited

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 302 of file ReporterData.h.

546{
547 if constexpr (is_std_vector<T>::value)
548 {
549 if (index >= _state.value().size())
550 mooseError("Requested index ",
551 index,
552 " is outside the bounds of the vector reporter value ",
553 r_name);
554
555 using R = typename T::value_type;
556 r_data.setReporterValue<R>(r_name, _state.value()[index], time_index);
557 }
558 else
559 {
560 libmesh_ignore(r_data);
561 libmesh_ignore(r_name);
562 libmesh_ignore(index);
563 libmesh_ignore(time_index);
564 mooseError("transferFromVector can only be used for reporter types that are specializatons of "
565 "std::vector.");
566 }
567}
void libmesh_ignore(const Args &...)

◆ transferToVector()

void ReporterContext< std::vector< T > >::transferToVector ( ReporterData &  r_data,
const ReporterName &  r_name,
dof_id_type  index,
unsigned int  time_index = 0 
) const
overridevirtualinherited

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 291 of file ReporterData.h.

529{
530 std::vector<T> & vec =
531 const_cast<std::vector<T> &>(r_data.getReporterValue<std::vector<T>>(r_name, time_index));
532
533 if (index >= vec.size())
535 "Requested index ", index, " is outside the bounds of the vector reporter value ", r_name);
536 vec[index] = _state.value();
537}
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.

◆ type()

virtual std::string ReporterContext< std::vector< T > >::type ( ) const
inlineoverridevirtualinherited

Return the type being stored by the associated ReporterState object.

See also
ReporterContext::storeInfo

Implements ReporterContextBase.

Definition at line 268 of file ReporterContext.h.

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

◆ vectorSum()

template<typename T >
virtual void ReporterVectorContext< T >::vectorSum ( )
inlineoverridevirtual

Helper for summing reporter value.

Implements ReporterContextBase.

Definition at line 780 of file ReporterContext.h.

781 {
782 // Case 1: T is type that we can sum
783 if constexpr (std::is_arithmetic<T>::value &&
784 !std::is_same<T, bool>::value) // We can't sum bools.
785 {
786 // Resize vector to max size
787 dof_id_type vec_size = this->_state.value().size();
788 this->comm().max(vec_size);
789 this->_state.value().resize(vec_size);
790
791 this->comm().sum(this->_state.value());
792 return;
793 }
794 // Case 2: T is a vector
795 else if constexpr (is_std_vector<T>::value)
796 {
797 // Resize vector to max size
798 dof_id_type vec_size = this->_state.value().size();
799 this->comm().max(vec_size);
800 this->_state.value().resize(vec_size);
801
802 using ValueType = typename T::value_type;
803 // Check if the ValueType is a vector
804 if constexpr (std::is_arithmetic<ValueType>::value && !std::is_same<ValueType, bool>::value)
805 {
806#ifdef DEBUG
807 auto vec_size = this->_state.value().size();
808 this->comm().max(vec_size);
809 // Assert only passes on all ranks if they are all the same size.
810 mooseAssert(this->_state.value().size() == vec_size,
811 "Reporter vector have different sizes on different ranks.");
812#endif
813 for (auto & val_vec : this->_state.value())
814 {
815 // Resize vector to max size
816 dof_id_type val_vec_size = val_vec.size();
817 this->comm().max(val_vec_size);
818 val_vec.resize(val_vec_size);
819
820 this->comm().sum(val_vec);
821 }
822 return;
823 }
824 }
825 // If we don't perform a summing operation, error out.
826 mooseError("Cannot perform sum operation on non-numeric or unsupported vector types.");
827 }
void max(const T &r, T &o, Request &req) const
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
uint8_t dof_id_type

Member Data Documentation

◆ _producer

const MooseObject& ReporterContextBase::_producer
protectedinherited

The MooseObject that is producing this Reporter.

Definition at line 219 of file ReporterContext.h.

Referenced by ReporterContextBase::getProducer().

◆ _producer_enum

ReporterProducerEnum ReporterContextBase::_producer_enum
protectedinherited

◆ _state

ReporterState<std::vector< T > >& ReporterContext< std::vector< T > >::_state
protectedinherited

The state on which this context object operates.

Definition at line 331 of file ReporterContext.h.


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