www.mooseframework.org
SobolSampler.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
13 registerMooseObjectAliased("StochasticToolsApp", SobolSampler, "Sobol");
14 registerMooseObjectReplaced("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 
49 Real
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 
94 SobolSampler::constructRankConfig(bool batch_mode) const
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,
99  n_processors(),
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 }
void setNumberOfRows(dof_id_type n_rows)
Sampler & _sampler_a
Sampler matrix.
Definition: SobolSampler.h:50
Sampler & _sampler_b
Re-sample matrix.
Definition: SobolSampler.h:53
A class used to perform Monte Carlo sampling for performing Sobol sensitivity analysis.
Definition: SobolSampler.h:23
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Definition: SobolSampler.C:50
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
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)
SobolSampler(const InputParameters &parameters)
Definition: SobolSampler.C:27
dof_id_type first_local_app_index
std::vector< Real > getNextLocalRow()
registerMooseObjectAliased("StochasticToolsApp", SobolSampler, "Sobol")
registerMooseObjectReplaced("StochasticToolsApp", SobolSampler, "07/01/2020 00:00", Sobol)
const dof_id_type _max_procs_per_row
virtual const std::string & name() const
void addRequiredParam(const std::string &name, const std::string &doc_string)
uint8_t processor_id_type
processor_id_type n_processors() const
void paramError(const std::string &param, Args... args) const
dof_id_type num_local_sims
const dof_id_type _min_procs_per_row
dof_id_type getNumberOfRows() const
const dof_id_type _num_matrices
Number of matrices.
Definition: SobolSampler.h:60
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
static InputParameters validParams()
Definition: SobolSampler.C:17
void mooseError(Args &&... args) const
virtual LocalRankConfig constructRankConfig(bool batch_mode) const override
Sobol sampling should have a slightly different partitioning in order to keep the sample and resample...
Definition: SobolSampler.C:94
void addClassDescription(const std::string &doc_string)
dof_id_type num_local_apps
dof_id_type first_local_sim_index
processor_id_type processor_id() const
std::vector< Real > _row_b
Definition: SobolSampler.h:46
std::vector< Real > _row_a
Definition: SobolSampler.h:45
dof_id_type getNumberOfCols() const
uint8_t dof_id_type