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 700 : SobolSampler::validParams() 18 : { 19 700 : InputParameters params = Sampler::validParams(); 20 700 : params.addClassDescription("Sobol variance-based sensitivity analysis Sampler."); 21 1400 : params.addParam<bool>("resample", true, "Create the re-sample matrix for second-order indices."); 22 1400 : params.addRequiredParam<SamplerName>("sampler_a", "The 'sample' matrix."); 23 1400 : params.addRequiredParam<SamplerName>("sampler_b", "The 're-sample' matrix."); 24 700 : return params; 25 0 : } 26 : 27 408 : SobolSampler::SobolSampler(const InputParameters & parameters) 28 : : Sampler(parameters), 29 408 : _sampler_a(getSampler("sampler_a")), 30 408 : _sampler_b(getSampler("sampler_b")), 31 816 : _resample(getParam<bool>("resample")), 32 454 : _num_matrices(_resample ? 2 * _sampler_a.getNumberOfCols() + 2 33 454 : : _sampler_a.getNumberOfCols() + 2) 34 : { 35 408 : if (_sampler_a.getNumberOfCols() != _sampler_b.getNumberOfCols()) 36 4 : paramError("sampler_a", "The supplied Sampler objects must have the same number of columns."); 37 : 38 404 : if (_sampler_a.getNumberOfRows() != _sampler_b.getNumberOfRows()) 39 4 : paramError("sampler_a", "The supplied Sampler objects must have the same number of rows."); 40 : 41 400 : if (_sampler_a.name() == _sampler_b.name()) 42 4 : paramError("sampler_a", "The supplied sampler matrices must not be the same."); 43 : 44 : // Initialize this object 45 396 : setNumberOfCols(_sampler_a.getNumberOfCols()); 46 396 : setNumberOfRows(_sampler_a.getNumberOfRows() * _num_matrices); 47 396 : } 48 : 49 : Real 50 2862332 : SobolSampler::computeSample(dof_id_type row_index, dof_id_type col_index) 51 : { 52 2862332 : const dof_id_type matrix_index = row_index % _num_matrices; 53 : 54 2862332 : if (matrix_index == 0 && col_index == 0) 55 : { 56 34474 : _row_a = _sampler_a.getNextLocalRow(); 57 34474 : _row_b = _sampler_b.getNextLocalRow(); 58 34474 : 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 2862332 : if (matrix_index == 0) 64 205304 : return _row_b[col_index]; 65 : 66 : // M1 Matrix 67 2657028 : else if (matrix_index == _num_matrices - 1) 68 205304 : return _row_a[col_index]; 69 : 70 : // N_-i Matrices 71 2451724 : else if (matrix_index > getNumberOfCols()) 72 : { 73 1223684 : if (col_index == (matrix_index - getNumberOfCols() - 1)) 74 204512 : return _row_b[col_index]; 75 : else 76 1019172 : return _row_a[col_index]; 77 : } 78 : 79 : // N_i Matrices 80 1228040 : else if (matrix_index <= getNumberOfCols()) 81 : { 82 1228040 : if (col_index == matrix_index - 1) 83 205304 : return _row_a[col_index]; 84 : else 85 1022736 : 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 792 : SobolSampler::constructRankConfig(bool batch_mode) const 95 : { 96 792 : std::vector<LocalRankConfig> all_rc(processor_id() + 1); 97 1914 : for (processor_id_type r = 0; r <= processor_id(); ++r) 98 1122 : all_rc[r] = rankConfig(r, 99 : n_processors(), 100 1122 : _sampler_a.getNumberOfRows(), 101 1122 : _min_procs_per_row, 102 1122 : _max_procs_per_row, 103 : batch_mode); 104 : LocalRankConfig & rc = all_rc.back(); 105 : 106 792 : rc.num_local_sims *= _num_matrices; 107 : bool found_first = false; 108 1914 : for (auto it = all_rc.rbegin(); it != all_rc.rend(); ++it) 109 1122 : if (it->is_first_local_rank) 110 : { 111 1122 : if (found_first) 112 330 : rc.first_local_sim_index += it->num_local_sims * (_num_matrices - 1); 113 : else 114 : found_first = true; 115 : } 116 : 117 792 : if (!batch_mode) 118 : { 119 396 : rc.num_local_apps = rc.num_local_sims; 120 396 : rc.first_local_app_index = rc.first_local_sim_index; 121 : } 122 : 123 792 : return rc; 124 792 : }