https://mooseframework.inl.gov
Loading...
Searching...
No Matches
PODReducedBasisSurrogate.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{
18 params.addClassDescription("Evaluates POD-RB surrogate model with reduced operators "
19 "computed from PODReducedBasisTrainer.");
20 params.addParam<std::vector<std::string>>("change_rank",
21 std::vector<std::string>(0),
22 "Names of variables whose rank should be changed.");
23 params.addParam<std::vector<unsigned int>>(
24 "new_ranks",
25 std::vector<unsigned int>(0),
26 "The new ranks that each variable in 'change_rank' shall have.");
27 params.addParam<Real>("penalty", 1e5, "The penalty parameter for Dirichlet BCs.");
28 return params;
29}
30
32 : SurrogateModel(parameters),
33 _change_rank(getParam<std::vector<std::string>>("change_rank")),
34 _new_ranks(getParam<std::vector<unsigned int>>("new_ranks")),
35 _var_names(getModelData<std::vector<std::string>>("_var_names")),
36 _tag_types(getModelData<std::vector<std::string>>("_tag_types")),
37 _base(getModelData<std::vector<std::vector<DenseVector<Real>>>>("_base")),
38 _red_operators(getModelData<std::vector<DenseMatrix<Real>>>("_red_operators")),
39 _penalty(getParam<Real>("penalty")),
40 _initialized(false)
41{
42 if (_change_rank.size() != _new_ranks.size())
43 paramError("new_ranks",
44 "The size of 'new_ranks' is not equal to the ",
45 "size of 'change_rank' ",
46 _new_ranks.size(),
47 " != ",
48 _change_rank.size());
49
50 for (unsigned int var_i = 0; var_i < _new_ranks.size(); ++var_i)
51 if (_new_ranks[var_i] == 0)
52 paramError("new_ranks", "The values should be greater than 0!");
53}
54
55void
56PODReducedBasisSurrogate::evaluateSolution(const std::vector<Real> & params)
57{
58 if (!isReady())
60 "POD-RB surrogate '", name(), "' does not have basis vectors and reduced operators.");
61
62 if (!_initialized)
63 {
64 // The containers are initialized (if needed).
67 _initialized = true;
68 }
69
70 // Assembling and solving the reduced equation system.
71 solveReducedSystem(params);
72
73 // Reconstructing the approximate solutions for every variable.
75}
76
77void
78PODReducedBasisSurrogate::evaluateSolution(const std::vector<Real> & params,
79 DenseVector<Real> & inp_vector,
80 std::string var_name)
81{
82 if (!isReady())
84 "POD-RB surrogate '", name(), "' does not have basis vectors and reduced operators.");
85
86 if (!_initialized)
87 {
88 // The containers are initialized (if needed).
90 _initialized = true;
91 }
92
93 // Assembling and solving the reduced equation system.
94 solveReducedSystem(params);
95
96 // Reconstructing the approximate solutions for every variable.
97 reconstructApproximateSolution(inp_vector, var_name);
98}
99
100bool
102{
103 if (_base.empty() || _red_operators.empty())
104 return false;
105
106 for (const auto & bases : _base)
107 if (bases.empty())
108 return false;
109
110 for (const auto & op : _red_operators)
111 if (op.m() == 0 || op.n() == 0)
112 return false;
113
114 return true;
115}
116
117void
119{
120 // Storing important indices for the assembly loops.
121 _final_ranks.resize(_var_names.size());
122 _comulative_ranks.resize(_var_names.size());
123 unsigned int sum_ranks = 0;
124
125 // Checking if the user wants to overwrite the original ranks for the
126 // variables.
127 for (unsigned int var_i = 0; var_i < _var_names.size(); ++var_i)
128 {
129 _final_ranks[var_i] = _base[var_i].size();
130 for (unsigned int var_j = 0; var_j < _change_rank.size(); ++var_j)
131 {
132 if (_change_rank[var_j] == _var_names[var_i])
133 {
134 if (_new_ranks[var_j] > _base[var_i].size())
135 {
136 mooseWarning("The specified new rank (",
137 _new_ranks[var_j],
138 ") for variable '",
139 _var_names[var_i],
140 "' is higher than the original rank (",
141 _base[var_i].size(),
142 ")! Switched to original rank.");
143 break;
144 }
145
146 _final_ranks[var_i] = _new_ranks[var_j];
147 }
148 }
149 sum_ranks += _final_ranks[var_i];
150 _comulative_ranks[var_i] = sum_ranks;
151 }
152
153 // Resizing containers to match the newly prescribed ranks.
154 _sys_mx = DenseMatrix<Real>(sum_ranks, sum_ranks);
155 _rhs = DenseVector<Real>(sum_ranks);
156 _coeffs = DenseVector<Real>(sum_ranks);
157}
158
159void
161{
162 _approx_solution.resize(_var_names.size());
163 for (unsigned int var_i = 0; var_i < _var_names.size(); var_i++)
164 _approx_solution[var_i] = DenseVector<Real>(_base[var_i][0].size());
165}
166
167void
168PODReducedBasisSurrogate::solveReducedSystem(const std::vector<Real> & params)
169{
170 // Cleaning the containers of the system matrix and right hand side.
171 _sys_mx.zero();
172 _rhs.zero();
173
174 // The assumption here is that the reduced operators in the trainer were
175 // assembled in the order of the parameters. Also, if the number of
176 // parameters is fewer than the number of operators, the operator will
177 // just be added without scaling.
178 for (unsigned int i = 0; i < _red_operators.size(); ++i)
179 {
180 unsigned int row_start = 0;
181
182 // Checking if the reduced operator corresponds to a Dirichlet BC, if
183 // yes introduce the penalty factor.
184 Real factor = 1.0;
185 if (_tag_types[i] == "op_dir" || _tag_types[i] == "src_dir")
186 factor = _penalty;
187
188 // If the user decreased the rank of the reduced bases manually, some parts
189 // of the initial reduced operators have to be omited.
190 for (unsigned int var_i = 0; var_i < _var_names.size(); ++var_i)
191 {
192 for (unsigned int row_i = row_start; row_i < _comulative_ranks[var_i]; row_i++)
193 {
194 if (_tag_types[i] == "op" || _tag_types[i] == "op_dir")
195 {
196
197 unsigned int col_start = 0;
198
199 for (unsigned int var_j = 0; var_j < _var_names.size(); ++var_j)
200 {
201 for (unsigned int col_i = col_start; col_i < _comulative_ranks[var_j]; col_i++)
202 {
203 if (i < params.size())
204 _sys_mx(row_i, col_i) += params[i] * factor * _red_operators[i](row_i, col_i);
205 else
206 _sys_mx(row_i, col_i) += factor * _red_operators[i](row_i, col_i);
207 }
208
209 col_start = _comulative_ranks[var_j];
210 }
211 }
212 else
213 {
214 if (i < params.size())
215 _rhs(row_i) -= params[i] * factor * _red_operators[i](row_i, 0);
216 else
217 _rhs(row_i) -= factor * _red_operators[i](row_i, 0);
218 }
219 row_start = _comulative_ranks[var_i];
220 }
221 }
222 }
223
224 // Solving the reduced system.
226}
227
228void
230{
231 unsigned int counter = 0;
232 for (unsigned int var_i = 0; var_i < _var_names.size(); var_i++)
233 {
234 _approx_solution[var_i].zero();
235
236 // This also takes into account the potential truncation of the bases by
237 // the user.
238 for (unsigned int base_i = 0; base_i < _final_ranks[var_i]; ++base_i)
239 {
240 for (unsigned int dof_i = 0; dof_i < _base[var_i][base_i].size(); ++dof_i)
241 _approx_solution[var_i](dof_i) += _coeffs(counter) * _base[var_i][base_i](dof_i);
242 counter++;
243 }
244 }
245}
246
247void
249 std::string var_name)
250{
251 auto it = std::find(_var_names.begin(), _var_names.end(), var_name);
252 if (it == _var_names.end())
253 mooseError("Variable '", var_name, "' does not exist in the POD-RB surrogate!");
254
255 unsigned int var_i = std::distance(_var_names.begin(), it);
256
257 if (inp_vector.size() != _base[var_i][0].size())
258 mooseError("The size of the input vector (",
259 inp_vector.size(),
260 ") for variable '",
261 var_name,
262 "' does not match the size of the stored base vector (",
263 _base[var_i][0].size(),
264 ") in POD-RB surrogate!");
265
266 inp_vector.zero();
267
268 unsigned int base_begin = 0;
269 if (var_i != 0)
270 base_begin = _comulative_ranks[var_i - 1];
271
272 for (unsigned int base_i = 0; base_i < _final_ranks[var_i]; ++base_i)
273 {
274 for (unsigned int dof_i = 0; dof_i < inp_vector.size(); ++dof_i)
275 inp_vector(dof_i) += _coeffs(base_i + base_begin) * _base[var_i][base_i](dof_i);
276 }
277}
278
279Real
280PODReducedBasisSurrogate::getNodalQoI(std::string var_name, unsigned int qoi_type) const
281{
282 Real val = 0.0;
283
284 auto it = std::find(_var_names.begin(), _var_names.end(), var_name);
285 if (it == _var_names.end())
286 mooseError("Variable '", var_name, "' not found!");
287
288 switch (qoi_type)
289 {
290 case 0:
291 val = _approx_solution[it - _var_names.begin()].max();
292 break;
293
294 case 1:
295 val = _approx_solution[it - _var_names.begin()].min();
296 break;
297
298 case 2:
299 val = _approx_solution[it - _var_names.begin()].l1_norm();
300 break;
301
302 case 3:
303 val = _approx_solution[it - _var_names.begin()].l2_norm();
304 break;
305
306 case 4:
307 val = _approx_solution[it - _var_names.begin()].linfty_norm();
308 break;
309 }
310
311 return (val);
312}
registerMooseObject("StochasticToolsApp", PODReducedBasisSurrogate)
void ErrorVector unsigned int
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)
const std::string & name() const
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
void mooseWarning(Args &&... args) const
PODReducedBasisSurrogate(const InputParameters &parameters)
Real getNodalQoI(std::string var_name, unsigned int qoi_type) const
Get the nodal QoI of the reconstructed solution for a given variable.
void reconstructApproximateSolution()
Reconstruct the approximate solution vector using the stored coefficients.
void solveReducedSystem(const std::vector< Real > &params)
Assemble and solve the reduced equation system.
void initializeReducedSystem()
Initialize reduced matrices, vectors and additional containers.
const Real _penalty
Penalty parameter for Dirichlet BCs.
std::vector< unsigned int > _comulative_ranks
Comulative ranks of the system. Used for indexing only.
static InputParameters validParams()
std::vector< unsigned int > _new_ranks
The new rank the variable should have.
std::vector< DenseVector< Real > > _approx_solution
Reconstructed solution for each variable.
void initializeApproximateSolution()
Initialize approximate solution vector.
bool isReady() const
Whether the basis vectors and reduced operators are ready for evaluation.
const std::vector< std::vector< DenseVector< Real > > > & _base
The basis vectors for all the variables.
std::vector< std::string > _change_rank
A vector containing the number of basis functions each variable should use.
bool _initialized
Switch that is set to see if the ROM matrices and vectors are initialized.
void evaluateSolution(const std::vector< Real > &params)
Get the reduced solution for a given parameter sample.
const std::vector< std::string > & _tag_types
Strings describing which operator is indepedent of the solution and which corresponds to a reduced Di...
const std::vector< DenseMatrix< Real > > & _red_operators
The reduced operators in the same order as given in tag_types.
DenseVector< Real > _rhs
The reduced right hand side.
DenseMatrix< Real > _sys_mx
The reduced system matrix.
std::vector< unsigned int > _final_ranks
The final rank that should be used for every variable.
DenseVector< Real > _coeffs
Coefficients of the reduced order model.
const std::vector< std::string > & _var_names
Vector containing the names of the variables we want to reconstruct.
static InputParameters validParams()
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
virtual void zero() override final