https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SurrogateModelAuxKernel.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
12#include "Function.h"
13
14template class AuxKernelTempl<Real>;
16
19
20template <typename ComputeValueType>
23{
26 params.addClassDescription("Sets a value of a variable based on a surrogate model.");
27 params.addRequiredParam<UserObjectName>("model", "Name of surrogate models.");
28 params.addRequiredParam<std::vector<std::string>>(
29 "parameters",
30 "Parameter values at which the surrogate is evaluated. These can be post-processors, "
31 "functions, variables, or constant numbers.");
32 params.addParam<std::vector<std::string>>(
33 "scalar_parameters",
34 std::vector<std::string>(),
35 "Parameters in 'parameters' that are post-processors or functions.");
36 params.addCoupledVar("coupled_variables",
37 "Parameters in 'parameters' that are standard variables.");
38 params.addCoupledVar("coupled_array_variables",
39 "Parameters in 'parameters' that are array variables.");
40 if constexpr (!std::is_same<ComputeValueType, RealEigenVector>::value)
41 params.suppressParameter<std::vector<VariableName>>("coupled_array_variables");
42 return params;
43}
44
45template <typename ComputeValueType>
47 const InputParameters & parameters)
48 : AuxKernelTempl<ComputeValueType>(parameters),
50 _model(getSurrogateModel("model")),
51 _n_params(this->template getParam<std::vector<std::string>>("parameters").size())
52{
53 const auto & params = this->template getParam<std::vector<std::string>>("parameters");
54 const auto & scalar_params =
55 this->template getParam<std::vector<std::string>>("scalar_parameters");
56 const auto var_params = this->coupledNames("coupled_variables");
57 const auto array_var_params = this->coupledNames("coupled_array_variables");
58
59 for (const auto & p : make_range(_n_params))
60 {
61 const auto & param = params[p];
62
63 // Iterators if the name was found
64 auto sit = std::find(scalar_params.begin(), scalar_params.end(), param);
65 auto vit = std::find(var_params.begin(), var_params.end(), param);
66 auto ait = std::find(array_var_params.begin(), array_var_params.end(), param);
67
68 // Scalar parameters
69 if (sit != scalar_params.end())
70 {
71 // Postprocessor
72 if (this->hasPostprocessorByName(param))
74 // Function
75 else if (this->hasFunctionByName(param))
76 _function_params[p] = &this->getFunctionByName(param);
77 else
78 this->paramError(
79 "scalar_parameters", "'", param, "' is not a postprocessor or a function.");
80 }
81 // Standard variable
82 else if (vit != var_params.end())
83 {
84 auto index = std::distance(var_params.begin(), vit);
85 _var_params[p] = &this->coupledValue("coupled_variables", index);
86 }
87 // Array variable
88 else if (ait != array_var_params.end())
89 {
90 auto index = std::distance(array_var_params.begin(), ait);
91 _array_var_params[p] = &this->coupledArrayValue("coupled_array_variables", index);
92 const auto & cvar = *this->getArrayVar("coupled_array_variables", index);
93 if (cvar.count() != this->_var.count())
94 this->paramError("coupled_array_variables",
95 "The number of components in '",
96 cvar.name(),
97 "' (",
98 cvar.count(),
99 ") does not match the number of components in '",
100 this->_var.name(),
101 "' (",
102 this->_var.count(),
103 ").");
104 }
105 else
106 {
107 Real & val = _constant_params[p];
108 try
109 {
110 val = MooseUtils::convert<Real>(param, true);
111 }
112 catch (const std::invalid_argument &)
113 {
114 this->paramError("parameters",
115 "'",
116 param,
117 "' is not listed in 'scalar_parameters', 'coupled_variables', or "
118 "'coupled_array_variables'.");
119 }
120 }
121 }
122}
123
124template <typename ComputeValueType>
125ComputeValueType
127{
128 std::vector<Real> x(_n_params);
129 // insert postprocessors
130 for (const auto & [p, pp_ptr] : _pp_params)
131 x[p] = *pp_ptr;
132 // insert functions
133 for (const auto & [p, fun_ptr] : _function_params)
134 x[p] = fun_ptr->value(this->_t, this->_q_point[this->_qp]);
135 // insert variables
136 for (const auto & [p, var_ptr] : _var_params)
137 x[p] = (*var_ptr)[this->_qp];
138 // insert constant values
139 for (const auto & [p, v] : _constant_params)
140 x[p] = v;
141
142 return _model.evaluate(x);
143}
144
145template <>
146RealEigenVector
148{
149 std::vector<Real> x(_n_params);
150
151 // insert postprocessors
152 for (const auto & [p, pp_ptr] : _pp_params)
153 x[p] = *pp_ptr;
154 // insert functions
155 for (const auto & [p, fun_ptr] : _function_params)
156 x[p] = fun_ptr->value(this->_t, this->_q_point[this->_qp]);
157 // insert variables
158 for (const auto & [p, var_ptr] : _var_params)
159 x[p] = (*var_ptr)[this->_qp];
160 // insert constant values
161 for (const auto & [p, v] : _constant_params)
162 x[p] = v;
163
164 RealEigenVector val(_var.count());
165 for (const auto & c : make_range(_var.count()))
166 {
167 for (const auto & [p, avar_ptr] : _array_var_params)
168 x[p] = (*avar_ptr)[this->_qp](c);
169 val(c) = _model.evaluate(x);
170 }
171
172 return val;
173}
const std::vector< double > x
const Real p
const double v
registerMooseObject("StochasticToolsApp", SurrogateModelAuxKernel)
static InputParameters validParams()
virtual const ArrayVariableValue & coupledArrayValue(const std::string &var_name, unsigned int comp=0) const
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0) const
ArrayMooseVariable * getArrayVar(const std::string &var_name, unsigned int comp)
std::vector< VariableName > coupledNames(const std::string &var_name) const
const Function & getFunctionByName(const FunctionName &name) const
bool hasFunctionByName(const FunctionName &name) const
void suppressParameter(const std::string &name)
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 addCoupledVar(const std::string &name, const std::string &doc_string)
void paramError(const std::string &param, Args... args) const
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
bool hasPostprocessorByName(const PostprocessorName &name) const
Sets a value of auxiliary variables based on a surrogate model.
const unsigned int _n_params
number of parameters
virtual ComputeValueType computeValue() override
std::map< unsigned int, const ArrayVariableValue * > _array_var_params
The array variable parameters that _model is evaluated at.
static InputParameters validParams()
std::map< unsigned int, const VariableValue * > _var_params
The standard variable parameters that _model is evaluated at.
SurrogateModelAuxKernelTempl(const InputParameters &parameters)
std::map< unsigned int, const PostprocessorValue * > _pp_params
The pp parameters that _model is evaluated at.
std::map< unsigned int, Real > _constant_params
Constant parameters that _model is evaluated at.
std::map< unsigned int, const Function * > _function_params
The function parameters that _model is evaluated at.
Interface for objects that need to use samplers.
static InputParameters validParams()