https://mooseframework.inl.gov
PODSurrogateTester.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 // Stocastic Tools Includes
11 #include "PODSurrogateTester.h"
12 
13 #include "Sampler.h"
14 
15 registerMooseObject("StochasticToolsTestApp", PODSurrogateTester);
16 
19 {
22  params.addClassDescription("Tool for sampling POD surrogate model.");
23  params.addRequiredParam<std::vector<UserObjectName>>("model", "Name of POD surrogate models.");
25  params.addRequiredParam<SamplerName>("sampler",
26  "Sampler to use for evaluating surrogate models.");
27  params.addParam<bool>(
28  "output_samples",
29  false,
30  "True to output value of parameter values from samples (this may be VERY large).");
31  params.addRequiredParam<std::string>(
32  "variable_name", "The name of the variable this prostprocessor is supposed to operate on.");
33  MultiMooseEnum pptype("nodal_max=0 nodal_min=1 nodal_l1=2 nodal_l2=3 nodal_linf=4");
34  params.addRequiredParam<MultiMooseEnum>(
35  "to_compute", pptype, "The global data the postprocessor should compute.");
36  return params;
37 }
38 
40  : GeneralVectorPostprocessor(parameters),
42  _sampler(getSampler("sampler")),
43  _output_samples(getParam<bool>("output_samples")),
44  _variable_name(getParam<std::string>("variable_name")),
45  _to_compute(getParam<MultiMooseEnum>("to_compute"))
46 {
47  const auto & model_names = getParam<std::vector<UserObjectName>>("model");
48  _model.reserve(model_names.size());
49  _value_vector.reserve(model_names.size());
50 
51  for (unsigned int model_i = 0; model_i < model_names.size(); ++model_i)
52  {
53  // Adding surrogate models first
54  _model.push_back(&getSurrogateModelByName<PODReducedBasisSurrogate>(model_names[model_i]));
55 
56  // Creating given vector postprocessors for every item in to_compute
57  for (unsigned int pp_i = 0; pp_i < _to_compute.size(); ++pp_i)
58  {
59  std::string name = model_names[model_i] + ":" + _to_compute[pp_i];
60  _value_vector.push_back(&declareVector(name));
61  }
62  }
63 
64  if (_output_samples)
65  for (unsigned int d = 0; d < _sampler.getNumberOfCols(); ++d)
66  _sample_vector.push_back(&declareVector("sample_p" + std::to_string(d)));
67 }
68 
69 void
71 {
72  for (auto & vec : _value_vector)
73  vec->resize(_sampler.getNumberOfLocalRows(), 0);
74 
75  if (_output_samples)
76  for (unsigned int d = 0; d < _sampler.getNumberOfCols(); ++d)
78 }
79 
80 void
82 {
83  unsigned int n_models = _model.size();
84  unsigned int n_pp = _to_compute.size();
85 
86  // Loop over samples
88  {
89  std::vector<Real> data = _sampler.getNextLocalRow();
90 
91  for (unsigned int m = 0; m < n_models; ++m)
92  {
93  _model[m]->evaluateSolution(data);
94  for (unsigned int ppi = 0; ppi < n_pp; ++ppi)
95  {
96  unsigned int idx = m * n_pp + ppi;
98  _model[m]->getNodalQoI(_variable_name, _to_compute.get(ppi));
99  }
100  }
101 
102  if (_output_samples)
103  for (unsigned int d = 0; d < _sampler.getNumberOfCols(); ++d)
104  {
105  (*_sample_vector[d])[p - _sampler.getLocalRowBegin()] = data[d];
106  }
107  }
108 }
109 
110 void
112 {
113  for (auto & vec : _value_vector)
114  _communicator.gather(0, *vec);
115  if (_output_samples)
116  for (auto & ppv_ptr : _sample_vector)
117  _communicator.gather(0, *ppv_ptr);
118 }
std::vector< VectorPostprocessorValue * > _value_vector
Vectors containing results of sampling model.
static InputParameters validParams()
Sampler & _sampler
Sampler for evaluating surrogate model.
A tool for output Sampler data.
static InputParameters validParams()
void gather(const unsigned int root_id, const T &send_data, std::vector< T, A > &recv) const
virtual void initialize() override
std::vector< VectorPostprocessorValue * > _sample_vector
Vector containing all the sample points for each parameter.
std::vector< Real > getNextLocalRow()
unsigned int size() const
dof_id_type getLocalRowBegin() const
std::string _variable_name
Name of the variable this tester operates on.
const Parallel::Communicator & _communicator
dof_id_type getNumberOfLocalRows() const
virtual const std::string & name() const
virtual void execute() override
void addRequiredParam(const std::string &name, const std::string &doc_string)
registerMooseObject("StochasticToolsTestApp", PODSurrogateTester)
PODSurrogateTester(const InputParameters &parameters)
static InputParameters validParams()
const bool _output_samples
Where or not to output all the samples used.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
dof_id_type getLocalRowEnd() const
MultiMooseEnum _to_compute
The type of the post-processor value which needs to be extracted.
unsigned int get(unsigned int i) const
Interface for objects that need to use samplers.
virtual void finalize() override
void addClassDescription(const std::string &doc_string)
std::vector< PODReducedBasisSurrogate * > _model
Pointers to surrogate model.
static InputParameters validParams()
dof_id_type getNumberOfCols() const
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
uint8_t dof_id_type