https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Cartesian1DSampler.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
10#include "Cartesian1DSampler.h"
11
12registerMooseObjectAliased("StochasticToolsApp", Cartesian1DSampler, "Cartesian1D");
13
16{
18 params.addClassDescription("Provides complete Cartesian product for the supplied variables.");
19 params.addRequiredParam<std::vector<Real>>(
20 "linear_space_items",
21 "A list of triplets, each item should include the min, step size, and number of steps.");
22 params.addRequiredParam<std::vector<Real>>("nominal_values", "Nominal values for each column.");
23 params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;
24 return params;
25}
26
28 : Sampler(parameters), _nominal_values(getParam<std::vector<Real>>("nominal_values"))
29{
30 const std::vector<Real> & items = getParam<std::vector<Real>>("linear_space_items");
31 if (items.size() % 3 != 0)
32 paramError("linear_space_items",
33 "The number of numeric items must be divisible by 3; min, max, divisions for each "
34 "item are required.");
35 const dof_id_type num_cols = items.size() / 3;
36 if (_nominal_values.size() != num_cols)
37 paramError("nominal_values",
38 "The number of values specified must match the number of triplets in "
39 "'linear_space_items'.");
40
41 _grid_range.resize(num_cols + 1, 0);
42 _grid_items.resize(num_cols);
43 dof_id_type num_rows = 0;
44 for (std::size_t i = 0; i < items.size(); i += 3)
45 {
46 if (items[i + 2] != std::floor(items[i + 2]))
47 paramError("linear_space_items",
48 "The third entry for each item must be an integer; it provides the number of "
49 "entries in the resulting item vector.");
50
51 if (items[i + 2] < 0)
52 paramError("linear_space_items",
53 "The third entry for each item must be positive; it provides the number of "
54 "entries in the resulting item vector.");
55
56 const dof_id_type col = i / 3;
57 const dof_id_type ng = static_cast<dof_id_type>(items[i + 2]);
58
59 num_rows += ng;
60 _grid_range[col + 1] = num_rows;
61 _grid_items[col].resize(ng);
62 for (const auto & j : make_range(ng))
63 _grid_items[col][j] = items[i] + j * items[i + 1];
64 }
65
66 setNumberOfRows(num_rows);
67 setNumberOfCols(num_cols);
68}
69
70Real
71Cartesian1DSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
72{
73 if (row_index >= _grid_range[col_index] && row_index < _grid_range[col_index + 1])
74 return _grid_items[col_index][row_index - _grid_range[col_index]];
75 else
76 return _nominal_values[col_index];
77}
registerMooseObjectAliased("StochasticToolsApp", Cartesian1DSampler, "Cartesian1D")
const ExecFlagType EXEC_INITIAL
Similar to CartesianProduct, this object creates a sampling scheme that produces a grid of samples,...
Cartesian1DSampler(const InputParameters &parameters)
std::vector< dof_id_type > _grid_range
The range of rows in which to apply the grid for each column.
static InputParameters validParams()
std::vector< std::vector< Real > > _grid_items
The values to use when sampling from a column's grid.
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
const std::vector< Real > & _nominal_values
The values to use when not sampling from a column's grid.
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
void paramError(const std::string &param, Args... args) const
void setNumberOfCols(dof_id_type n_cols)
static InputParameters validParams()
void setNumberOfRows(dof_id_type n_rows)