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