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 "SobolSampler.h" 11 : #include "Distribution.h" 12 : 13 : registerMooseObjectAliased("StochasticToolsApp", SobolSampler, "Sobol"); 14 : registerMooseObjectReplaced("StochasticToolsApp", SobolSampler, "07/01/2020 00:00", Sobol); 15 : 16 : InputParameters 17 269 : SobolSampler::validParams() 18 : { 19 269 : InputParameters params = Sampler::validParams(); 20 269 : params.addClassDescription("Sobol variance-based sensitivity analysis Sampler."); 21 538 : params.addParam<bool>("resample", true, "Create the re-sample matrix for second-order indices."); 22 538 : params.addRequiredParam<SamplerName>("sampler_a", "The 'sample' matrix."); 23 538 : params.addRequiredParam<SamplerName>("sampler_b", "The 're-sample' matrix."); 24 269 : return params; 25 0 : } 26 : 27 147 : SobolSampler::SobolSampler(const InputParameters & parameters) 28 : : Sampler(parameters), 29 147 : _sampler_a(getSampler("sampler_a")), 30 147 : _sampler_b(getSampler("sampler_b")), 31 294 : _resample(getParam<bool>("resample")), 32 163 : _num_matrices(_resample ? 2 * _sampler_a.getNumberOfCols() + 2 33 163 : : _sampler_a.getNumberOfCols() + 2) 34 : { 35 147 : if (_sampler_a.getNumberOfCols() != _sampler_b.getNumberOfCols()) 36 2 : paramError("sampler_a", "The supplied Sampler objects must have the same number of columns."); 37 : 38 145 : if (_sampler_a.getNumberOfRows() != _sampler_b.getNumberOfRows()) 39 2 : paramError("sampler_a", "The supplied Sampler objects must have the same number of rows."); 40 : 41 143 : if (_sampler_a.name() == _sampler_b.name()) 42 2 : paramError("sampler_a", "The supplied sampler matrices must not be the same."); 43 : 44 : // Initialize this object 45 141 : setNumberOfCols(_sampler_a.getNumberOfCols()); 46 141 : setNumberOfRows(_sampler_a.getNumberOfRows() * _num_matrices); 47 141 : } 48 : 49 : Real 50 1301060 : SobolSampler::computeSample(dof_id_type row_index, dof_id_type col_index) 51 : { 52 1301060 : const dof_id_type matrix_index = row_index % _num_matrices; 53 : 54 1301060 : if (matrix_index == 0 && col_index == 0) 55 : { 56 15670 : _row_a = _sampler_a.getNextLocalRow(); 57 15670 : _row_b = _sampler_b.getNextLocalRow(); 58 15670 : if (std::equal(_row_a.begin(), _row_a.end(), _row_b.begin())) 59 0 : paramError("sampler_a", "The supplied sampler matrices must not be the same."); 60 : } 61 : 62 : // M2 Matrix 63 1301060 : if (matrix_index == 0) 64 93320 : return _row_b[col_index]; 65 : 66 : // M1 Matrix 67 1207740 : else if (matrix_index == _num_matrices - 1) 68 93320 : return _row_a[col_index]; 69 : 70 : // N_-i Matrices 71 1114420 : else if (matrix_index > getNumberOfCols()) 72 : { 73 556220 : if (col_index == (matrix_index - getNumberOfCols() - 1)) 74 92960 : return _row_b[col_index]; 75 : else 76 463260 : return _row_a[col_index]; 77 : } 78 : 79 : // N_i Matrices 80 558200 : else if (matrix_index <= getNumberOfCols()) 81 : { 82 558200 : if (col_index == matrix_index - 1) 83 93320 : return _row_a[col_index]; 84 : else 85 464880 : return _row_b[col_index]; 86 : } 87 : 88 0 : 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 : 93 : LocalRankConfig 94 282 : SobolSampler::constructRankConfig(bool batch_mode) const 95 : { 96 282 : std::vector<LocalRankConfig> all_rc(processor_id() + 1); 97 660 : for (processor_id_type r = 0; r <= processor_id(); ++r) 98 378 : all_rc[r] = rankConfig(r, 99 : n_processors(), 100 378 : _sampler_a.getNumberOfRows(), 101 378 : _min_procs_per_row, 102 378 : _max_procs_per_row, 103 : batch_mode); 104 : LocalRankConfig & rc = all_rc.back(); 105 : 106 282 : rc.num_local_sims *= _num_matrices; 107 : bool found_first = false; 108 660 : for (auto it = all_rc.rbegin(); it != all_rc.rend(); ++it) 109 378 : if (it->is_first_local_rank) 110 : { 111 378 : if (found_first) 112 96 : rc.first_local_sim_index += it->num_local_sims * (_num_matrices - 1); 113 : else 114 : found_first = true; 115 : } 116 : 117 282 : if (!batch_mode) 118 : { 119 141 : rc.num_local_apps = rc.num_local_sims; 120 141 : rc.first_local_app_index = rc.first_local_sim_index; 121 : } 122 : 123 282 : return rc; 124 282 : }