https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
15registerMooseObject("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");
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];
61 }
62 }
63
65 for (unsigned int d = 0; d < _sampler.getNumberOfCols(); ++d)
66 _sample_vector.push_back(&declareVector("sample_p" + std::to_string(d)));
67}
68
69void
71{
72 for (auto & vec : _value_vector)
73 vec->resize(_sampler.getNumberOfLocalRows(), 0);
74
76 for (unsigned int d = 0; d < _sampler.getNumberOfCols(); ++d)
78}
79
80void
82{
83 unsigned int n_models = _model.size();
84 unsigned int n_pp = _to_compute.size();
85
86 // Loop over samples
87 for (dof_id_type p = _sampler.getLocalRowBegin(); p < _sampler.getLocalRowEnd(); ++p)
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
110void
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}
const Real p
registerMooseObject("StochasticToolsTestApp", PODSurrogateTester)
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
const std::string & name() const
unsigned int get(unsigned int i) const
void push_back(const std::string &names)
unsigned int size() const
A tool for output Sampler data.
std::string _variable_name
Name of the variable this tester operates on.
std::vector< VectorPostprocessorValue * > _sample_vector
Vector containing all the sample points for each parameter.
Sampler & _sampler
Sampler for evaluating surrogate model.
const bool _output_samples
Where or not to output all the samples used.
MultiMooseEnum _to_compute
The type of the post-processor value which needs to be extracted.
virtual void finalize() override
virtual void execute() override
std::vector< VectorPostprocessorValue * > _value_vector
Vectors containing results of sampling model.
virtual void initialize() override
static InputParameters validParams()
PODSurrogateTester(const InputParameters &parameters)
std::vector< PODReducedBasisSurrogate * > _model
Pointers to surrogate model.
static InputParameters validParams()
std::vector< Real > getNextLocalRow()
dof_id_type getNumberOfLocalRows() const
dof_id_type getLocalRowEnd() const
dof_id_type getLocalRowBegin() const
dof_id_type getNumberOfCols() const
Interface for objects that need to use samplers.
static InputParameters validParams()
void gather(const unsigned int root_id, const T &send_data, std::vector< T, A > &recv) const
VectorPostprocessorValue & declareVector(const std::string &vector_name)
const Parallel::Communicator & _communicator