https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SurrogateModelScalarAux.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
11
13
16{
19 params.addClassDescription("Sets a value of a scalar variable based on a surrogate model.");
20 params.addRequiredParam<UserObjectName>("model", "Name of surrogate models.");
21 params.addRequiredParam<std::vector<std::string>>(
22 "parameters",
23 "Parameter values at which the surrogate is evaluated. Accepts scalar variables, "
24 "postprocessors, and real numbers.");
25 return params;
26}
27
29 : AuxScalarKernel(parameters), SurrogateModelInterface(this), _model(getSurrogateModel("model"))
30{
31}
32
33void
35{
36 const auto parameter_names = getParam<std::vector<std::string>>("parameters");
37 _n_params = parameter_names.size();
38 for (unsigned int j = 0; j < _n_params; ++j)
39 {
40 const auto name = parameter_names[j];
41
42 // name can be a postprocessor, scalar var, or real number
44 {
45 auto & scalar_val = _sc_fe_problem.getScalarVariable(_tid, name).sln()[0];
46 _scalar_var_params.push_back(&scalar_val);
47 _scalar_var_indices.push_back(j);
48 }
50 {
51 _pp_indices.push_back(j);
53 }
54 else
55 {
56 // first check if the entry is a real
57 std::stringstream ss(name);
58 Real v;
59 ss >> v;
60
61 if (ss.fail())
62 paramError("parameters",
63 "Parameter ",
64 name,
65 " is not a scalar variable, postprocessor, or real number");
66
67 _real_params.push_back(v);
68 _real_indices.push_back(j);
69 }
70 }
72 _n_pp = _pp_indices.size();
73 _n_real = _real_indices.size();
74}
75
76Real
78{
79 std::vector<Real> x(_n_params);
80 // insert scalar variables
81 for (unsigned int j = 0; j < _n_scalar; ++j)
83 // insert postprocessors
84 for (unsigned int j = 0; j < _n_pp; ++j)
85 x[_pp_indices[j]] = *_pp_params[j];
86 // insert real values
87 for (unsigned int j = 0; j < _n_real; ++j)
89 return _model.evaluate(x);
90}
const std::vector< double > x
const double v
registerMooseObject("StochasticToolsApp", SurrogateModelScalarAux)
static InputParameters validParams()
THREAD_ID _tid
virtual bool hasScalarVariable(const std::string &var_name) const override
virtual MooseVariableScalar & getScalarVariable(const THREAD_ID tid, const std::string &var_name) override
bool hasPostprocessorValueByName(const PostprocessorName &name) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
const std::string & name() const
void paramError(const std::string &param, Args... args) const
const VariableValue & sln() const
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
FEProblemBase & _sc_fe_problem
Interface for objects that need to use samplers.
static InputParameters validParams()
Sets a value of a scalar variable based on a surrogate model.
const SurrogateModel & _model
Pointers to surrogate model.
std::vector< const PostprocessorValue * > _pp_params
the pp parameters that _model is evaluated at
std::vector< unsigned int > _real_indices
static InputParameters validParams()
std::vector< unsigned int > _scalar_var_indices
std::vector< unsigned int > _pp_indices
std::vector< Real > _real_params
the real parameters that _model is evaluated at
virtual Real computeValue() override
std::vector< const Real * > _scalar_var_params
the real parameters that _model is evaluated at
unsigned int _n_params
number of parameters
SurrogateModelScalarAux(const InputParameters &parameters)
virtual Real evaluate(const std::vector< Real > &x) const
Evaluate surrogate model given a row of parameters.