LCOV - code coverage report
Current view: top level - src/samplers - SobolSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 56 59 94.9 %
Date: 2025-07-25 05:00:46 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          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         664 : SobolSampler::validParams()
      18             : {
      19         664 :   InputParameters params = Sampler::validParams();
      20         664 :   params.addClassDescription("Sobol variance-based sensitivity analysis Sampler.");
      21        1328 :   params.addParam<bool>("resample", true, "Create the re-sample matrix for second-order indices.");
      22        1328 :   params.addRequiredParam<SamplerName>("sampler_a", "The 'sample' matrix.");
      23        1328 :   params.addRequiredParam<SamplerName>("sampler_b", "The 're-sample' matrix.");
      24         664 :   return params;
      25           0 : }
      26             : 
      27         390 : SobolSampler::SobolSampler(const InputParameters & parameters)
      28             :   : Sampler(parameters),
      29         390 :     _sampler_a(getSampler("sampler_a")),
      30         390 :     _sampler_b(getSampler("sampler_b")),
      31         780 :     _resample(getParam<bool>("resample")),
      32         434 :     _num_matrices(_resample ? 2 * _sampler_a.getNumberOfCols() + 2
      33         434 :                             : _sampler_a.getNumberOfCols() + 2)
      34             : {
      35         390 :   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         386 :   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         382 :   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         378 :   setNumberOfCols(_sampler_a.getNumberOfCols());
      46         378 :   setNumberOfRows(_sampler_a.getNumberOfRows() * _num_matrices);
      47         378 : }
      48             : 
      49             : Real
      50     2602120 : SobolSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
      51             : {
      52     2602120 :   const dof_id_type matrix_index = row_index % _num_matrices;
      53             : 
      54     2602120 :   if (matrix_index == 0 && col_index == 0)
      55             :   {
      56       31340 :     _row_a = _sampler_a.getNextLocalRow();
      57       62680 :     _row_b = _sampler_b.getNextLocalRow();
      58       31340 :     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     2602120 :   if (matrix_index == 0)
      64      186640 :     return _row_b[col_index];
      65             : 
      66             :   // M1 Matrix
      67     2415480 :   else if (matrix_index == _num_matrices - 1)
      68      186640 :     return _row_a[col_index];
      69             : 
      70             :   // N_-i Matrices
      71     2228840 :   else if (matrix_index > getNumberOfCols())
      72             :   {
      73     1112440 :     if (col_index == (matrix_index - getNumberOfCols() - 1))
      74      185920 :       return _row_b[col_index];
      75             :     else
      76      926520 :       return _row_a[col_index];
      77             :   }
      78             : 
      79             :   // N_i Matrices
      80     1116400 :   else if (matrix_index <= getNumberOfCols())
      81             :   {
      82     1116400 :     if (col_index == matrix_index - 1)
      83      186640 :       return _row_a[col_index];
      84             :     else
      85      929760 :       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         756 : SobolSampler::constructRankConfig(bool batch_mode) const
      95             : {
      96         756 :   std::vector<LocalRankConfig> all_rc(processor_id() + 1);
      97        1836 :   for (processor_id_type r = 0; r <= processor_id(); ++r)
      98        1080 :     all_rc[r] = rankConfig(r,
      99             :                            n_processors(),
     100        1080 :                            _sampler_a.getNumberOfRows(),
     101        1080 :                            _min_procs_per_row,
     102        1080 :                            _max_procs_per_row,
     103             :                            batch_mode);
     104             :   LocalRankConfig & rc = all_rc.back();
     105             : 
     106         756 :   rc.num_local_sims *= _num_matrices;
     107             :   bool found_first = false;
     108        1836 :   for (auto it = all_rc.rbegin(); it != all_rc.rend(); ++it)
     109        1080 :     if (it->is_first_local_rank)
     110             :     {
     111        1080 :       if (found_first)
     112         324 :         rc.first_local_sim_index += it->num_local_sims * (_num_matrices - 1);
     113             :       else
     114             :         found_first = true;
     115             :     }
     116             : 
     117         756 :   if (!batch_mode)
     118             :   {
     119         378 :     rc.num_local_apps = rc.num_local_sims;
     120         378 :     rc.first_local_app_index = rc.first_local_sim_index;
     121             :   }
     122             : 
     123         756 :   return rc;
     124             : }

Generated by: LCOV version 1.14