www.mooseframework.org
StochasticResults.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // Stocastic Tools Includes
11 #include "StochasticResults.h"
12 
13 // MOOSE includes
14 #include "Sampler.h"
15 
16 registerMooseObject("StochasticToolsApp", StochasticResults);
17 
19 
20 StochasticResultsData::StochasticResultsData(const VectorPostprocessorName & name,
21  VectorPostprocessorValue * vpp)
22  : name(name), vector(vpp)
23 {
24 }
25 
26 InputParameters
28 {
29  InputParameters params = GeneralVectorPostprocessor::validParams();
30  params.addClassDescription(
31  "Storage container for stochastic simulation results coming from a Postprocessor.");
33 
34  params.addParam<std::vector<SamplerName>>("samplers",
35  "A list of sampler names of associated data.");
36 
37  MooseEnum parallel_type("REPLICATED DISTRIBUTED", "REPLICATED");
38  params.addParam<MooseEnum>(
39  "parallel_type",
40  parallel_type,
41  "Specify if the stored data vector is replicated or distributed across processors.");
42 
43  params.addParam<processor_id_type>(
44  "output_distributed_rank",
45  Moose::INVALID_PROCESSOR_ID,
46  "When 'parallel_type = DISTRIBUTED' set this to copy the data from the specified processor "
47  "for output. This is mainly for testing since the data from that rank will override the data "
48  "on the root process.");
49 
50  params.set<bool>("_is_broadcast") = false;
51  return params;
52 }
53 
54 StochasticResults::StochasticResults(const InputParameters & parameters)
55  : GeneralVectorPostprocessor(parameters),
56  SamplerInterface(this),
57  _parallel_type(getParam<MooseEnum>("parallel_type")),
58  _output_distributed_rank(getParam<processor_id_type>("output_distributed_rank"))
59 {
60 
61  if (_output_distributed_rank != Moose::INVALID_PROCESSOR_ID)
62  {
63  if (_parallel_type == "replicated")
64  paramError("output_distributed_rank",
65  "The output rank cannot be used with 'parallel_type' set to replicated.");
66  else if (_output_distributed_rank >= n_processors())
67  paramError("output_distributed_rank",
68  "The supplied value is greater than the number of available processors: ",
70  }
71  else
72  {
73  if ((_parallel_type == "DISTRIBUTED") && (getOutputs().count("none") == 0) &&
74  (n_processors() > 1))
75  paramWarning("parallel_type",
76  "The parallel_type was set to DISTRIBUTED and output is enabled for the object, "
77  "when running in parallel the results output will only contain the data on the "
78  "root processor. Output can be disabled by setting 'outputs = none' in the "
79  "input block. If output is desired the 'output_distributed_rank' can be set.");
80  }
81 
82  if (isParamValid("samplers"))
83  for (const SamplerName & name : getParam<std::vector<SamplerName>>("samplers"))
84  {
85  Sampler & sampler = getSamplerByName(name);
86  _sample_vectors.emplace_back(sampler.name(), &declareVector(sampler.name()));
87  }
88 }
89 
90 void
92 {
93  // Clear any existing data, unless the complete history is desired
94  if (!containsCompleteHistory())
95  for (auto & data : _sample_vectors)
96  data.vector->clear();
97 }
98 
99 void
101 {
102  if (_parallel_type == "REPLICATED")
103  {
104  for (auto & data : _sample_vectors)
105  _communicator.gather(0, data.current);
106  }
107 
108  else if (_output_distributed_rank != 0 && _output_distributed_rank != Moose::INVALID_PROCESSOR_ID)
109  {
110  if (processor_id() == _output_distributed_rank)
111  for (auto & data : _sample_vectors)
112  _communicator.send(0, data.current);
113 
114  else if (processor_id() == 0)
115  for (auto & data : _sample_vectors)
116  _communicator.receive(_output_distributed_rank, data.current);
117  }
118 
119  for (auto & data : _sample_vectors)
120  {
121  data.vector->insert(data.vector->end(), data.current.begin(), data.current.end());
122  data.current.clear();
123  }
124 }
125 
126 void
128  const std::string & name, const VectorPostprocessorValue && current)
129 {
130  mooseAssert(!hasVectorPostprocessorByName(name),
131  "The supplied name must be a valid vector postprocessor name.");
132  auto data_ptr = std::find_if(_sample_vectors.begin(),
133  _sample_vectors.end(),
134  [&name](StochasticResultsData & data) { return data.name == name; });
135 
136  data_ptr->current = current;
137 }
138 
139 // DEPRECATED
140 void
141 StochasticResults::init(Sampler & sampler)
142 {
143  if (!isParamValid("samplers"))
144  {
145  paramWarning("samplers",
146  "Support for the 'StochasticResults' objects without the 'samplers' input "
147  "parameter is being removed, please update your input file(s).");
148  _sample_vectors.emplace_back(sampler.name(), &declareVector(sampler.name()));
149  }
150 }
StochasticResults::StochasticResults
StochasticResults(const InputParameters &parameters)
Definition: StochasticResults.C:54
StochasticResults::_sample_vectors
std::vector< StochasticResultsData > _sample_vectors
Storage for declared vectors.
Definition: StochasticResults.h:76
defineLegacyParams
defineLegacyParams(StochasticResults)
StochasticResults::finalize
virtual void finalize() override
Definition: StochasticResults.C:100
StochasticResultsData
Storage helper for managing data being assigned to this VPP by a Transfer object.
Definition: StochasticResults.h:24
StochasticResults
A tool for output Sampler data.
Definition: StochasticResults.h:35
StochasticResults.h
StochasticResultsData::StochasticResultsData
StochasticResultsData(const VectorPostprocessorName &name, VectorPostprocessorValue *)
Definition: StochasticResults.C:20
StochasticResults::initialize
virtual void initialize() override
Definition: StochasticResults.C:91
validParams
InputParameters validParams()
name
const std::string name
Definition: Setup.h:21
StochasticResults::setCurrentLocalVectorPostprocessorValue
void setCurrentLocalVectorPostprocessorValue(const std::string &vector_name, const VectorPostprocessorValue &&current)
Definition: StochasticResults.C:127
registerMooseObject
registerMooseObject("StochasticToolsApp", StochasticResults)
StochasticResults::init
void init(Sampler &_sampler)
DEPRECATED Initialize storage based on the Sampler returned by the SamplerTransientMultiApp or Sample...
Definition: StochasticResults.C:141
StochasticResults::_parallel_type
const MooseEnum _parallel_type
Parallel operation mode.
Definition: StochasticResults.h:79
StochasticResults::validParams
static InputParameters validParams()
Definition: StochasticResults.C:27
StochasticResults::_output_distributed_rank
const processor_id_type _output_distributed_rank
The rank data to output if parallel type is distributed.
Definition: StochasticResults.h:82