https://mooseframework.inl.gov
VectorPostprocessor.C
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 
10 // MOOSE includes
11 #include "VectorPostprocessor.h"
12 #include "SubProblem.h"
13 #include "Conversion.h"
14 #include "UserObject.h"
15 #include "FEProblem.h"
16 
19 {
21  params += OutputInterface::validParams();
23  params.addParam<bool>("contains_complete_history",
24  false,
25  "Set this flag to indicate that the values in all vectors declared by this "
26  "VPP represent a time history (e.g. with each invocation, new values are "
27  "added and old values are never removed). This changes the output so that "
28  "only a single file is output and updated with each invocation");
29 
30  // VPPs can set this to true if their resulting vectors are naturally replicated in parallel
31  // setting this to false will keep MOOSE from unnecessarily broadcasting those vectors
32  params.addPrivateParam<bool>("_auto_broadcast", true);
33 
34  // VPPs can operate in "distributed" mode, which disables the automatic broadcasting
35  // and results in an individual file per processor if CSV output is enabled
36  MooseEnum parallel_type("DISTRIBUTED REPLICATED", "REPLICATED");
37  params.addParam<MooseEnum>(
38  "parallel_type",
39  parallel_type,
40  "Set how the data is represented within the VectorPostprocessor (VPP); 'distributed' "
41  "indicates that data within the VPP is distributed and no auto communication is performed, "
42  "this setting will result in parallel output within the CSV output; 'replicated' indicates "
43  "that the data within the VPP is correct on processor 0, the data will automatically be "
44  "broadcast to all processors unless the '_auto_broadcast' param is set to false within the "
45  "validParams function.");
46 
47  params.addParamNamesToGroup("outputs", "Advanced");
48  params.registerBase("VectorPostprocessor");
49  return params;
50 }
51 
53  : OutputInterface(moose_object->parameters()),
54  NonADFunctorInterface(moose_object),
55  _vpp_name(MooseUtils::shortName(moose_object->parameters().get<std::string>("_object_name"))),
56  _vpp_fe_problem(
57  *moose_object->parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
58  _parallel_type(moose_object->parameters().get<MooseEnum>("parallel_type")),
59  _vpp_moose_object(*moose_object),
60  _vpp_tid(moose_object->parameters().isParamValid("_tid")
61  ? moose_object->parameters().get<THREAD_ID>("_tid")
62  : 0),
63  _contains_complete_history(moose_object->parameters().get<bool>("contains_complete_history")),
64  _is_distributed(_parallel_type == "DISTRIBUTED"),
65  _is_broadcast(_is_distributed || !moose_object->parameters().get<bool>("_auto_broadcast"))
66 {
67 }
68 
70 VectorPostprocessor::declareVector(const std::string & vector_name)
71 {
72  _vector_names.insert(vector_name);
73 
74  if (_vpp_tid)
75  return _thread_local_vectors.emplace(vector_name, VectorPostprocessorValue()).first->second;
76 
77  // _is_broadcast = true (_auto_broadcast = false) then data is produced in a replicated manner
79  if (_is_broadcast)
81  if (_is_distributed)
83 
88 }
89 
90 const std::set<std::string> &
92 {
93  return _vector_names;
94 }
95 
96 const ReporterMode REPORTER_MODE_VPP_SCATTER("VPP_SCATTER");
97 
98 template <typename T>
100  const MooseObject & producer,
101  ReporterState<T> & state)
102  : ReporterGeneralContext<T>(other, producer, state)
103 {
104 }
105 
106 template <typename T>
107 void
109 {
111 
112  const auto & consumer_modes = this->state().getConsumers();
113  auto func = [](const std::pair<ReporterMode, const MooseObject *> & mode_pair)
114  { return mode_pair.first == REPORTER_MODE_VPP_SCATTER; };
115  if (std::find_if(consumer_modes.begin(), consumer_modes.end(), func) != consumer_modes.end())
116  {
117  const T & value = this->state().value();
118  if (this->processor_id() == 0 && value.size() != this->n_processors())
119  mooseError("The VectorPostprocessor value to be scatter has a length of ",
120  value.size(),
121  "; it must be the same length as the number of processors (",
122  this->n_processors(),
123  ").");
124 
125  this->comm().scatter(value, _scatter_value);
126  }
127 }
128 
129 template <typename T>
130 void
132 {
134  _scatter_value_old = _scatter_value;
135 }
136 
137 template <typename T>
140 {
141  return _scatter_value;
142 }
143 
144 template <typename T>
147 {
148  return _scatter_value_old;
149 }
150 
151 // Explicit instantiation
An interface for accessing Moose::Functors for systems that do not care about automatic differentiati...
virtual void finalize() override
Perform automatic parallel communication based on the producer/consumer modes.
A special version of RestartableData to aid in storing Reporter values.
static InputParameters validParams()
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
T & declareReporterValue(const ReporterName &reporter_name, const ReporterMode &mode, const MooseObject &producer, Args &&... args)
Method for returning a writable reference to the current Reporter value.
Definition: ReporterData.h:400
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const THREAD_ID _vpp_tid
const ReporterMode REPORTER_MODE_ROOT
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155
A class to provide an common interface to objects requiring "outputs" option.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const ScatterVectorPostprocessorValue & getScatterValueOld() const
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:608
A ReporterName that represents a VectorPostprocessor.
Definition: ReporterName.h:152
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System...
const ReporterData & getReporterData() const
Provides const access the ReporterData object.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const MooseObject & _vpp_moose_object
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
const ReporterMode REPORTER_MODE_DISTRIBUTED
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
std::map< std::string, VectorPostprocessorValue > _thread_local_vectors
VectorPostprocessorContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< T > &state)
virtual void copyValuesBack() override
Called by FEProblemBase::advanceState via ReporterData.
FEProblemBase & _vpp_fe_problem
The FEProblemBase.
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:203
Real ScatterVectorPostprocessorValue
Definition: MooseTypes.h:204
virtual void copyValuesBack() override
Called by FEProblemBase::advanceState via ReporterData.
static InputParameters validParams()
const ReporterMode REPORTER_MODE_VPP_SCATTER("VPP_SCATTER")
std::set< std::string > _vector_names
static InputParameters validParams()
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
const ReporterMode REPORTER_MODE_REPLICATED
MooseEnumItem that automatically creates the ID and doesn&#39;t allow the ID to be assigned.
Definition: ReporterMode.h:44
const std::set< std::string > & getVectorNames() const
Return the names of the vectors associated with this object.
const ScatterVectorPostprocessorValue & getScatterValue() const
VectorPostprocessor(const MooseObject *moose_object)
unsigned int THREAD_ID
Definition: MooseTypes.h:209
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
static InputParameters validParams()
Definition: UserObject.C:18
const std::string _vpp_name
The name of the VectorPostprocessor.
virtual void finalize() override
Perform automatic parallel communication based on the producer/consumer modes.