Line data Source code
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 : #include "SurrogateModelAuxKernel.h"
11 :
12 : #include "Function.h"
13 :
14 : template class AuxKernelTempl<Real>;
15 : template class AuxKernelTempl<RealEigenVector>;
16 :
17 : registerMooseObject("StochasticToolsApp", SurrogateModelAuxKernel);
18 : registerMooseObject("StochasticToolsApp", SurrogateModelArrayAuxKernel);
19 :
20 : template <typename ComputeValueType>
21 : InputParameters
22 88 : SurrogateModelAuxKernelTempl<ComputeValueType>::validParams()
23 : {
24 88 : InputParameters params = AuxKernelTempl<ComputeValueType>::validParams();
25 88 : params += SurrogateModelInterface::validParams();
26 88 : params.addClassDescription("Sets a value of a variable based on a surrogate model.");
27 176 : params.addRequiredParam<UserObjectName>("model", "Name of surrogate models.");
28 176 : 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 88 : params.addParam<std::vector<std::string>>(
33 : "scalar_parameters",
34 88 : std::vector<std::string>(),
35 : "Parameters in 'parameters' that are post-processors or functions.");
36 176 : params.addCoupledVar("coupled_variables",
37 : "Parameters in 'parameters' that are standard variables.");
38 176 : params.addCoupledVar("coupled_array_variables",
39 : "Parameters in 'parameters' that are array variables.");
40 : if constexpr (!std::is_same<ComputeValueType, RealEigenVector>::value)
41 46 : params.suppressParameter<std::vector<VariableName>>("coupled_array_variables");
42 88 : return params;
43 0 : }
44 :
45 : template <typename ComputeValueType>
46 56 : SurrogateModelAuxKernelTempl<ComputeValueType>::SurrogateModelAuxKernelTempl(
47 : const InputParameters & parameters)
48 : : AuxKernelTempl<ComputeValueType>(parameters),
49 : SurrogateModelInterface(this),
50 56 : _model(getSurrogateModel("model")),
51 168 : _n_params(this->template getParam<std::vector<std::string>>("parameters").size())
52 : {
53 56 : const auto & params = this->template getParam<std::vector<std::string>>("parameters");
54 : const auto & scalar_params =
55 56 : this->template getParam<std::vector<std::string>>("scalar_parameters");
56 56 : const auto var_params = this->coupledNames("coupled_variables");
57 56 : const auto array_var_params = this->coupledNames("coupled_array_variables");
58 :
59 264 : for (const auto & p : make_range(_n_params))
60 : {
61 220 : const auto & param = params[p];
62 :
63 : // Iterators if the name was found
64 220 : auto sit = std::find(scalar_params.begin(), scalar_params.end(), param);
65 220 : auto vit = std::find(var_params.begin(), var_params.end(), param);
66 220 : auto ait = std::find(array_var_params.begin(), array_var_params.end(), param);
67 :
68 : // Scalar parameters
69 220 : if (sit != scalar_params.end())
70 : {
71 : // Postprocessor
72 216 : if (this->hasPostprocessorByName(param))
73 52 : _pp_params[p] = &this->getPostprocessorValueByName(param);
74 : // Function
75 112 : else if (this->hasFunctionByName(param))
76 52 : _function_params[p] = &this->getFunctionByName(param);
77 : else
78 4 : this->paramError(
79 : "scalar_parameters", "'", param, "' is not a postprocessor or a function.");
80 : }
81 : // Standard variable
82 112 : else if (vit != var_params.end())
83 : {
84 : auto index = std::distance(var_params.begin(), vit);
85 52 : _var_params[p] = &this->coupledValue("coupled_variables", index);
86 : }
87 : // Array variable
88 60 : else if (ait != array_var_params.end())
89 : {
90 : auto index = std::distance(array_var_params.begin(), ait);
91 26 : _array_var_params[p] = &this->coupledArrayValue("coupled_array_variables", index);
92 52 : const auto & cvar = *this->getArrayVar("coupled_array_variables", index);
93 26 : if (cvar.count() != this->_var.count())
94 8 : this->paramError("coupled_array_variables",
95 : "The number of components in '",
96 4 : cvar.name(),
97 : "' (",
98 : cvar.count(),
99 : ") does not match the number of components in '",
100 4 : this->_var.name(),
101 : "' (",
102 : this->_var.count(),
103 : ").");
104 : }
105 : else
106 : {
107 34 : Real & val = _constant_params[p];
108 : try
109 : {
110 34 : val = MooseUtils::convert<Real>(param, true);
111 : }
112 4 : catch (const std::invalid_argument &)
113 : {
114 4 : this->paramError("parameters",
115 : "'",
116 : param,
117 : "' is not listed in 'scalar_parameters', 'coupled_variables', or "
118 : "'coupled_array_variables'.");
119 : }
120 : }
121 : }
122 44 : }
123 :
124 : template <typename ComputeValueType>
125 : ComputeValueType
126 110000 : SurrogateModelAuxKernelTempl<ComputeValueType>::computeValue()
127 : {
128 110000 : std::vector<Real> x(_n_params);
129 : // insert postprocessors
130 220000 : for (const auto & [p, pp_ptr] : _pp_params)
131 110000 : x[p] = *pp_ptr;
132 : // insert functions
133 220000 : for (const auto & [p, fun_ptr] : _function_params)
134 110000 : x[p] = fun_ptr->value(this->_t, this->_q_point[this->_qp]);
135 : // insert variables
136 220000 : for (const auto & [p, var_ptr] : _var_params)
137 110000 : x[p] = (*var_ptr)[this->_qp];
138 : // insert constant values
139 220000 : for (const auto & [p, v] : _constant_params)
140 110000 : x[p] = v;
141 :
142 220000 : return _model.evaluate(x);
143 : }
144 :
145 : template <>
146 : RealEigenVector
147 110000 : SurrogateModelAuxKernelTempl<RealEigenVector>::computeValue()
148 : {
149 110000 : std::vector<Real> x(_n_params);
150 :
151 : // insert postprocessors
152 220000 : for (const auto & [p, pp_ptr] : _pp_params)
153 110000 : x[p] = *pp_ptr;
154 : // insert functions
155 220000 : for (const auto & [p, fun_ptr] : _function_params)
156 110000 : x[p] = fun_ptr->value(this->_t, this->_q_point[this->_qp]);
157 : // insert variables
158 220000 : for (const auto & [p, var_ptr] : _var_params)
159 110000 : x[p] = (*var_ptr)[this->_qp];
160 : // insert constant values
161 110000 : for (const auto & [p, v] : _constant_params)
162 0 : x[p] = v;
163 :
164 110000 : RealEigenVector val(_var.count());
165 330000 : for (const auto & c : make_range(_var.count()))
166 : {
167 440000 : for (const auto & [p, avar_ptr] : _array_var_params)
168 220000 : x[p] = (*avar_ptr)[this->_qp](c);
169 220000 : val(c) = _model.evaluate(x);
170 : }
171 :
172 110000 : return val;
173 : }
|