https://mooseframework.inl.gov
Loading...
Searching...
No Matches
StochasticReporter.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
10#pragma once
11
12// MOOSE includes
13#include "GeneralReporter.h"
15#include "Sampler.h"
16
17template <typename T>
19{
20public:
22 const MooseObject & producer,
23 ReporterState<std::vector<T>> & state,
24 const Sampler & sampler);
25
26 virtual void copyValuesBack() override;
27 virtual void finalize() override;
28 virtual std::string contextType() const override { return MooseUtils::prettyCppType(this); }
29 virtual void storeInfo(nlohmann::json & json) const override;
30
31protected:
35};
36
37template <typename T>
39 const MooseObject & producer,
40 ReporterState<std::vector<T>> & state,
41 const Sampler & sampler)
42 : ReporterGeneralContext<std::vector<T>>(other, producer, state),
43 _sampler(sampler),
44 _has_gathered(false),
45 _has_allgathered(false)
46{
47 this->_state.value().resize(_sampler.getNumberOfLocalRows());
48}
49
50template <typename T>
51void
53{
54 this->_state.copyValuesBack();
55 if (_has_allgathered || (_has_gathered && this->processor_id() == 0))
56 {
57 auto & val = this->_state.value();
58 val.erase(val.begin(), val.begin() + _sampler.getLocalRowBegin());
59 val.erase(val.begin() + _sampler.getLocalRowEnd(), val.end());
60 }
61 _has_gathered = false;
62 _has_allgathered = false;
63}
64
65template <typename T>
66void
68{
69 bool gather_required = this->_producer_enum == REPORTER_MODE_ROOT;
70 bool allgather_required = this->_producer_enum == REPORTER_MODE_REPLICATED;
71 for (const auto & pair : this->_state.getConsumers())
72 {
73 const ReporterMode consumer = pair.first;
74 if (consumer == REPORTER_MODE_ROOT)
75 gather_required = true;
76 else if (consumer == REPORTER_MODE_REPLICATED)
77 allgather_required = true;
78 }
79
80 if (allgather_required && !_has_allgathered)
81 StochasticTools::stochasticAllGather(this->comm(), this->_state.value());
82 else if (gather_required && !_has_gathered)
83 StochasticTools::stochasticGather(this->comm(), 0, this->_state.value());
84
85 _has_gathered = gather_required || _has_gathered;
86 _has_allgathered = allgather_required || _has_allgathered;
87}
88
89template <typename T>
90void
91StochasticReporterContext<T>::storeInfo(nlohmann::json & json) const
92{
94 if (_has_allgathered || (_has_gathered && this->processor_id() == 0))
95 {
96 json["row_begin"] = 0;
97 json["row_end"] = this->_sampler.getNumberOfRows();
98 }
99 else
100 {
101 json["row_begin"] = this->_sampler.getLocalRowBegin();
102 json["row_end"] = this->_sampler.getLocalRowEnd();
103 }
104}
105
111{
112public:
113 StochasticReporterValueBase(const Sampler & sampler) : _sampler(sampler) {}
114 virtual ~StochasticReporterValueBase() = default;
115
116 virtual void initialize() {}
117
118protected:
120};
121
122template <typename T>
124{
125public:
126 StochasticReporterValue(std::vector<T> & value, const Sampler & sampler)
127 : StochasticReporterValueBase(sampler), _value(value)
128 {
129 }
130
131 virtual void initialize() { this->_value.resize(this->_sampler.getNumberOfLocalRows()); }
132
133private:
134 std::vector<T> & _value;
135};
136
138{
139public:
141
143 virtual void initialize() override final;
144 virtual void execute() override {}
145 virtual void finalize() override {}
146
147protected:
149 const ReporterData & from_data,
150 const ReporterName & from_reporter,
151 std::string prefix = "");
152 template <typename T>
153 std::vector<T> & declareStochasticReporter(std::string value_name, const Sampler & sampler);
155
156private:
157 const unsigned int _parallel_type;
159 std::deque<std::unique_ptr<StochasticReporterValueBase>> _vectors;
160};
161
162template <typename T>
163std::vector<T> &
164StochasticReporter::declareStochasticReporter(std::string value_name, const Sampler & sampler)
165{
166 const ReporterMode mode =
168 std::vector<T> & vector =
169 this->template declareValueByName<std::vector<T>, StochasticReporterContext<T>>(
170 value_name, mode, sampler);
171
172 _vectors.push_back(std::make_unique<StochasticReporterValue<T>>(vector, sampler));
173 return vector;
174}
const double T
const ReporterMode REPORTER_MODE_REPLICATED
const ReporterMode REPORTER_MODE_DISTRIBUTED
const ReporterMode REPORTER_MODE_ROOT
const InputParameters & parameters() const
const ReporterState< T > & state() const
T & value(const std::size_t time_index=0)
Transfer Reporters from sub-applications to a StochasticReporter on the main application.
dof_id_type getNumberOfLocalRows() const
virtual std::string contextType() const override
virtual void storeInfo(nlohmann::json &json) const override
virtual void copyValuesBack() override
virtual void finalize() override
StochasticReporterContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< std::vector< T > > &state, const Sampler &sampler)
This is a non-typed base class of the stochastic vector value, used to update reporter values during ...
virtual ~StochasticReporterValueBase()=default
StochasticReporterValueBase(const Sampler &sampler)
StochasticReporterValue(std::vector< T > &value, const Sampler &sampler)
static InputParameters validParams()
virtual void finalize() override
virtual void execute() override
virtual ReporterName declareStochasticReporterClone(const Sampler &sampler, const ReporterData &from_data, const ReporterName &from_reporter, std::string prefix="")
std::deque< std::unique_ptr< StochasticReporterValueBase > > _vectors
Container for declared values that we may need to resize at initialize.
std::vector< T > & declareStochasticReporter(std::string value_name, const Sampler &sampler)
virtual void initialize() override final
const unsigned int _parallel_type
std::string prettyCppType(const std::string &cpp_type)
void stochasticGather(const libMesh::Parallel::Communicator &, processor_id_type, T &)
void stochasticAllGather(const libMesh::Parallel::Communicator &, T &)