https://mooseframework.inl.gov
ActiveLearningReporterBase.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 #include "StochasticReporter.h"
13 
14 template <typename T>
16 
23 template <typename T>
25 {
26 public:
28 
30 
35  virtual void execute() override;
36 
37 protected:
48  const ReporterData & from_data,
49  const ReporterName & from_reporter,
50  std::string prefix = "") override;
51 
57  const Sampler & sampler() const { return _sampler; }
58 
59  const std::vector<std::vector<Real>> & getGlobalInputData() const
60  {
61  _input_data_requested = true;
62  return _input_data;
63  }
64 
68  const std::vector<T> & getGlobalOutputData() const
69  {
71  return _output_data;
72  }
73 
77  virtual void preNeedSample() {}
78 
90  virtual bool needSample(const std::vector<Real> & /*row*/,
91  dof_id_type /*local_ind*/,
92  dof_id_type /*global_ind*/,
93  T & /*val*/)
94  {
95  return true;
96  }
97 
101 
102 private:
105  std::vector<bool> & _need_sample;
107  std::vector<T> * _data = nullptr;
108 
110  mutable bool _input_data_requested = false;
112  mutable bool _output_data_requested = false;
114  std::vector<std::vector<Real>> _input_data;
116  std::vector<T> _output_data;
117 };
118 
119 template <typename T>
122 {
124  params.addRequiredParam<SamplerName>("sampler", "The sampler used to produce data.");
125  return params;
126 }
127 
128 template <typename T>
130  : StochasticReporter(parameters),
131  _sampler(this->template getSampler<Sampler>("sampler")),
132  _need_sample(this->template declareStochasticReporter<bool>("need_sample", _sampler))
133 {
134 }
135 
136 template <typename T>
137 void
139 {
140  // If requesting global data, fill it in
141  if (_input_data_requested)
142  {
143  // Gather inputs for the current step
144  _input_data.assign(_sampler.getNumberOfRows(),
145  std::vector<Real>(_sampler.getNumberOfCols(), 0.0));
146  for (dof_id_type i = _sampler.getLocalRowBegin(); i < _sampler.getLocalRowEnd(); ++i)
147  _input_data[i] = _sampler.getNextLocalRow();
148  for (auto & inp : _input_data)
149  gatherSum(inp);
150  }
151  if (_output_data_requested)
152  {
153  if (!_data)
154  mooseError("Output data has been requested, but none was declared in this object.");
155  _output_data = *_data;
156  _communicator.allgather(_output_data);
157  }
158 
159  // Optional call for before sampler loop
160  preNeedSample();
161 
162  // Dummy value in case _data has not been declared yet
163  T dummy;
164  // Loop over samples to determine if sample is needed. Replace value in _data
165  // (typically only if a sample is not needed). We insert a dummy value in case
166  // _data has not been declared.
167  for (const auto & i : make_range(_sampler.getNumberOfLocalRows()))
168  _need_sample[i] = needSample(_sampler.getNextLocalRow(),
169  i,
170  i + _sampler.getLocalRowBegin(),
171  (_data ? (*_data)[i] : dummy));
172 }
173 
174 template <typename T>
177  const ReporterData & from_data,
178  const ReporterName & from_reporter,
179  std::string prefix)
180 {
181  // Only one value is allowed to be declared
182  if (_data)
183  this->mooseError(type(), " can only declare a single reporter value.");
184  // Make sure the inputted sampler is the same one in the parameters
185  else if (sampler.name() != _sampler.name())
186  this->paramError("sampler",
187  "Inputted sampler, ",
188  _sampler.name(),
189  ", is not the same as the one producing data, ",
190  sampler.name(),
191  ".");
192  // Make sure reporter value exists
193  else if (!from_data.hasReporterValue(from_reporter))
194  this->mooseError("Reporter value ", from_reporter, " has not been declared.");
195  // Make sure the reporter value is the right type
196  else if (!from_data.hasReporterValue<T>(from_reporter))
197  this->mooseError(
198  type(), " can only use reporter values of type ", MooseUtils::prettyCppType<T>(), ".");
199 
200  std::string value_name = (prefix.empty() ? "" : prefix + ":") + from_reporter.getObjectName() +
201  ":" + from_reporter.getValueName();
202  _data = &this->declareStochasticReporter<T>(value_name, sampler);
203  return {name(), value_name};
204 }
const Sampler & sampler() const
Get a const reference to the sampler from the parameters.
virtual ReporterName declareStochasticReporterClone(const Sampler &sampler, const ReporterData &from_data, const ReporterName &from_reporter, std::string prefix="") override
This is overriden for the following reasons: 1) Only one vector can be declared and must match the ty...
const std::vector< T > & getGlobalOutputData() const
Get a const reference to the output data.
static InputParameters validParams()
void mooseError(Args &&... args)
virtual void preNeedSample()
Optional virtual function that is called before the sampler loop calling needSample.
virtual void execute() override
Here we loop through the samples and call the needSample function to determine if the sample needs to...
virtual bool needSample(const std::vector< Real > &, dof_id_type, dof_id_type, T &)
This routine is called during the sampler loop in execute() and is meant to fill in the "need_sample"...
virtual const std::string & name() const
std::vector< T > * _data
Reporter value declared with the transfer.
void addRequiredParam(const std::string &name, const std::string &doc_string)
bool _output_data_requested
Whether or not to gather global output data.
const std::vector< std::vector< Real > > & getGlobalInputData() const
std::vector< T > _output_data
Global output data from sampler.
const std::string name
Definition: Setup.h:20
std::vector< bool > & _need_sample
Reporter value determining whether we need to evaluate the sample through a multiapp or other means...
const std::string & getObjectName() const
bool _input_data_requested
Whether or not to gather global input data.
std::vector< std::vector< Real > > _input_data
Global input data from sampler.
ActiveLearningReporterTempl(const InputParameters &parameters)
static InputParameters validParams()
IntRange< T > make_range(T beg, T end)
const InputParameters & parameters() const
bool hasReporterValue(const ReporterName &reporter_name) const
const std::string & getValueName() const
Sampler & _sampler
Sampler given in the parameters, must match the one used to declare the transferred values...
This is a base class for performing active learning routines, meant to be used in conjunction with Sa...
uint8_t dof_id_type