https://mooseframework.inl.gov
Loading...
Searching...
No Matches
EvaluateSurrogate.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 "EvaluateSurrogate.h"
12
13#include "Sampler.h"
14
16
19{
23 params.addClassDescription("Tool for sampling surrogate models.");
24 params.addRequiredParam<std::vector<UserObjectName>>("model", "Name of surrogate models.");
25 params.addRequiredParam<SamplerName>("sampler",
26 "Sampler to use for evaluating surrogate models.");
27 MultiMooseEnum rtypes(SurrogateModel::defaultResponseTypes().getRawNames(), "real");
29 "response_type",
30 rtypes,
31 "The type of return value expected from the surrogate models, a single entry will use it for "
32 "every model. Warning: not every model is able evaluate every response type.");
33 MultiMooseEnum estd("false=0 true=1", "false");
35 "evaluate_std",
36 estd,
37 "Whether or not to evaluate standard deviation associated with each sample, a single entry "
38 "will use it for every model. Warning: not every model can compute standard deviation.");
39 return params;
40}
41
43 : StochasticReporter(parameters),
45 _sampler(getSampler("sampler")),
46 _response_types(getParam<MultiMooseEnum>("response_type"))
47{
48 const auto & model_names = getParam<std::vector<UserObjectName>>("model");
49 _model.reserve(model_names.size());
50 for (const auto & nm : model_names)
51 _model.push_back(&getSurrogateModelByName(nm));
52
53 if (_response_types.size() != 1 && _response_types.size() != _model.size())
54 paramError("response_type",
55 "Number of entries must be 1 or equal to the number of entries in 'model'.");
56
57 const auto & estd = getParam<MultiMooseEnum>("evaluate_std");
58 if (estd.size() != 1 && estd.size() != _model.size())
59 paramError("evaluate_std",
60 "Nmber of entries must be 1 or equal to the number of entries in 'model'.");
61 _doing_std.resize(_model.size());
62 for (const auto i : index_range(_model))
63 _doing_std[i] = estd.size() == 1 ? estd[0] == "true" : estd[i] == "true";
64
65 _real_values.resize(_model.size(), nullptr);
66 _real_std.resize(_model.size(), nullptr);
67 _vector_real_values.resize(_model.size(), nullptr);
68 _vector_real_std.resize(_model.size(), nullptr);
69 for (const auto i : index_range(_model))
70 {
71 const std::string rtype = _response_types.size() == 1 ? _response_types[0] : _response_types[i];
72 if (rtype == "real")
73 {
74 _real_values[i] = &declareStochasticReporter<Real>(model_names[i], _sampler);
75 if (_doing_std[i])
76 _real_std[i] = &declareStochasticReporter<Real>(model_names[i] + "_std", _sampler);
77 }
78 else if (rtype == "vector_real")
79 {
81 &declareStochasticReporter<std::vector<Real>>(model_names[i], _sampler);
82 if (_doing_std[i])
84 &declareStochasticReporter<std::vector<Real>>(model_names[i] + "_std", _sampler);
85 }
86 else
87 paramError("response_type", "Unknown response type ", _response_types[i]);
88 }
89}
90
91void
93{
94 // Loop over samples
95 for (const auto ind : make_range(_sampler.getNumberOfLocalRows()))
96 {
97 const std::vector<Real> data = _sampler.getNextLocalRow();
98 for (const auto m : index_range(_model))
99 {
100 if (_real_values[m] && _real_std[m])
101 (*_real_values[m])[ind] = _model[m]->evaluate(data, (*_real_std[m])[ind]);
102 else if (_real_values[m])
103 (*_real_values[m])[ind] = _model[m]->evaluate(data);
104 else if (_vector_real_values[m] && _vector_real_std[m])
105 _model[m]->evaluate(data, (*_vector_real_values[m])[ind], (*_vector_real_std[m])[ind]);
106 else if (_vector_real_values[m])
107 _model[m]->evaluate(data, (*_vector_real_values[m])[ind]);
108 }
109 }
110}
registerMooseObject("StochasticToolsApp", EvaluateSurrogate)
T evaluate(Real, const Point &)
A tool for output Sampler data.
EvaluateSurrogate(const InputParameters &parameters)
Sampler & _sampler
Sampler for evaluating surrogate model.
const MultiMooseEnum _response_types
The data type for the response value.
std::vector< std::vector< Real > * > _real_values
std::vector< std::vector< std::vector< Real > > * > _vector_real_std
std::vector< const SurrogateModel * > _model
Pointers to surrogate model.
std::vector< std::vector< Real > * > _real_std
std::vector< std::vector< std::vector< Real > > * > _vector_real_values
static InputParameters validParams()
virtual void execute() override
std::vector< bool > _doing_std
Whether or not to compute standard deviation.
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)
void paramError(const std::string &param, Args... args) const
unsigned int size() const
static InputParameters validParams()
std::vector< Real > getNextLocalRow()
dof_id_type getNumberOfLocalRows() const
static InputParameters validParams()
Interface for objects that need to use samplers.
static InputParameters validParams()
T & getSurrogateModelByName(const UserObjectName &name) const
Get a sampler with a given name.
static MooseEnum defaultResponseTypes()