https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SobolSampler.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 "SobolSampler.h"
11#include "Distribution.h"
12
13registerMooseObjectAliased("StochasticToolsApp", SobolSampler, "Sobol");
14registerMooseObjectReplaced("StochasticToolsApp", SobolSampler, "07/01/2020 00:00", Sobol);
15
18{
20 params.addClassDescription("Sobol variance-based sensitivity analysis Sampler.");
21 params.addParam<bool>("resample", true, "Create the re-sample matrix for second-order indices.");
22 params.addRequiredParam<SamplerName>("sampler_a", "The 'sample' matrix.");
23 params.addRequiredParam<SamplerName>("sampler_b", "The 're-sample' matrix.");
24 return params;
25}
26
28 : Sampler(parameters),
29 _sampler_a(getSampler("sampler_a")),
30 _sampler_b(getSampler("sampler_b")),
31 _resample(getParam<bool>("resample")),
32 _num_matrices(_resample ? 2 * _sampler_a.getNumberOfCols() + 2
33 : _sampler_a.getNumberOfCols() + 2)
34{
36 paramError("sampler_a", "The supplied Sampler objects must have the same number of columns.");
37
39 paramError("sampler_a", "The supplied Sampler objects must have the same number of rows.");
40
41 if (_sampler_a.name() == _sampler_b.name())
42 paramError("sampler_a", "The supplied sampler matrices must not be the same.");
43
44 // Initialize this object
47}
48
49Real
50SobolSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
51{
52 const dof_id_type matrix_index = row_index % _num_matrices;
53
54 if (matrix_index == 0 && col_index == 0)
55 {
58 if (std::equal(_row_a.begin(), _row_a.end(), _row_b.begin()))
59 paramError("sampler_a", "The supplied sampler matrices must not be the same.");
60 }
61
62 // M2 Matrix
63 if (matrix_index == 0)
64 return _row_b[col_index];
65
66 // M1 Matrix
67 else if (matrix_index == _num_matrices - 1)
68 return _row_a[col_index];
69
70 // N_-i Matrices
71 else if (matrix_index > getNumberOfCols())
72 {
73 if (col_index == (matrix_index - getNumberOfCols() - 1))
74 return _row_b[col_index];
75 else
76 return _row_a[col_index];
77 }
78
79 // N_i Matrices
80 else if (matrix_index <= getNumberOfCols())
81 {
82 if (col_index == matrix_index - 1)
83 return _row_a[col_index];
84 else
85 return _row_b[col_index];
86 }
87
88 mooseError("Invalid row and column index, if you are seeing this Zach messed up because this "
89 "should be impossible to reach.");
90 return 0;
91}
92
95{
96 std::vector<LocalRankConfig> all_rc(processor_id() + 1);
97 for (processor_id_type r = 0; r <= processor_id(); ++r)
98 all_rc[r] = rankConfig(r,
103 batch_mode);
104 LocalRankConfig & rc = all_rc.back();
105
107 bool found_first = false;
108 for (auto it = all_rc.rbegin(); it != all_rc.rend(); ++it)
109 if (it->is_first_local_rank)
110 {
111 if (found_first)
112 rc.first_local_sim_index += it->num_local_sims * (_num_matrices - 1);
113 else
114 found_first = true;
115 }
116
117 if (!batch_mode)
118 {
121 }
122
123 return rc;
124}
LocalRankConfig rankConfig(processor_id_type rank, processor_id_type nprocs, dof_id_type napps, processor_id_type min_app_procs, processor_id_type max_app_procs, bool batch_mode=false)
registerMooseObjectAliased("StochasticToolsApp", SobolSampler, "Sobol")
registerMooseObjectReplaced("StochasticToolsApp", SobolSampler, "07/01/2020 00:00", Sobol)
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)
const std::string & name() const
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
void setNumberOfCols(dof_id_type n_cols)
std::vector< Real > getNextLocalRow()
const dof_id_type _max_procs_per_row
dof_id_type getNumberOfRows() const
static InputParameters validParams()
dof_id_type getNumberOfCols() const
void setNumberOfRows(dof_id_type n_rows)
const dof_id_type _min_procs_per_row
A class used to perform Monte Carlo sampling for performing Sobol sensitivity analysis.
SobolSampler(const InputParameters &parameters)
Sampler & _sampler_a
Sampler matrix.
virtual LocalRankConfig constructRankConfig(bool batch_mode) const override
Sobol sampling should have a slightly different partitioning in order to keep the sample and resample...
std::vector< Real > _row_b
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
std::vector< Real > _row_a
Sampler & _sampler_b
Re-sample matrix.
const dof_id_type _num_matrices
Number of matrices.
static InputParameters validParams()
processor_id_type processor_id() const
processor_id_type n_processors() const
dof_id_type num_local_sims
dof_id_type first_local_sim_index
dof_id_type num_local_apps
dof_id_type first_local_app_index