LCOV - code coverage report
Current view: top level - src/samplers - LatinHypercubeSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 41 42 97.6 %
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 "LatinHypercubeSampler.h"
      11             : #include "Distribution.h"
      12             : registerMooseObjectAliased("StochasticToolsApp", LatinHypercubeSampler, "LatinHypercube");
      13             : 
      14             : InputParameters
      15        1756 : LatinHypercubeSampler::validParams()
      16             : {
      17        1756 :   InputParameters params = Sampler::validParams();
      18        1756 :   params.addClassDescription("Latin Hypercube Sampler.");
      19        3512 :   params.addRequiredParam<dof_id_type>("num_rows", "The size of the square matrix to generate.");
      20        3512 :   params.addRequiredParam<std::vector<DistributionName>>(
      21             :       "distributions",
      22             :       "The distribution names to be sampled, the number of distributions provided defines the "
      23             :       "number of columns per matrix.");
      24        1756 :   return params;
      25           0 : }
      26             : 
      27        1020 : LatinHypercubeSampler::LatinHypercubeSampler(const InputParameters & parameters)
      28        1020 :   : Sampler(parameters)
      29             : {
      30        2040 :   const auto & distribution_names = getParam<std::vector<DistributionName>>("distributions");
      31        4242 :   for (const DistributionName & name : distribution_names)
      32        3222 :     _distributions.push_back(&getDistributionByName(name));
      33             : 
      34        3060 :   setNumberOfRows(getParam<dof_id_type>("num_rows"));
      35        1020 :   setNumberOfCols(distribution_names.size());
      36        1020 :   setNumberOfRandomSeeds(2 * distribution_names.size());
      37             : 
      38             :   // The use of MooseRandom in this Sampler is fairly complex. There are two sets of random
      39             :   // generators. The first set (n = number columns) is used to generate the random probability
      40             :   // within each bin of the Latin hypercube sample. The second set (n) is used to shuffle the
      41             :   // probability values. Mainly due to how the shuffle operates, it is necessary for the management
      42             :   // of advancement of the generators to be handled manually.
      43        1020 :   setAutoAdvanceGenerators(false);
      44        1020 : }
      45             : 
      46             : void
      47        2162 : LatinHypercubeSampler::sampleSetUp(const Sampler::SampleMode mode)
      48             : {
      49             :   // All calls to the generators occur in here. Calls to the random number generators
      50             :   // (i.e., getRand) are complete by the end of this function.
      51             : 
      52             :   // Flag to indicate what vector index to use in computeSample method
      53        2162 :   _is_local = mode == Sampler::SampleMode::LOCAL;
      54             : 
      55        2162 :   const Real bin_size = 1. / getNumberOfRows();
      56        2162 :   _probabilities.resize(getNumberOfCols());
      57        2162 :   if (mode == Sampler::SampleMode::GLOBAL)
      58             :   {
      59         120 :     for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
      60             :     {
      61             :       std::vector<Real> & local = _probabilities[col];
      62          80 :       local.resize(getNumberOfRows());
      63         560 :       for (dof_id_type row = 0; row < getNumberOfRows(); ++row)
      64             :       {
      65         480 :         const auto lower = row * bin_size;
      66         480 :         const auto upper = (row + 1) * bin_size;
      67         480 :         local[row] = getRand(col) * (upper - lower) + lower;
      68             :       }
      69          80 :       shuffle(local, col + getNumberOfCols(), CommMethod::NONE);
      70             :     }
      71             :   }
      72             : 
      73             :   else
      74             :   {
      75        9314 :     for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
      76             :     {
      77             :       std::vector<Real> & local = _probabilities[col];
      78        7192 :       local.resize(getNumberOfLocalRows());
      79        7192 :       advanceGenerator(col, getLocalRowBegin());
      80       46648 :       for (dof_id_type row = getLocalRowBegin(); row < getLocalRowEnd(); ++row)
      81             :       {
      82       39456 :         const auto lower = row * bin_size;
      83       39456 :         const auto upper = (row + 1) * bin_size;
      84       39456 :         local[row - getLocalRowBegin()] = getRand(col) * (upper - lower) + lower;
      85             :       }
      86        7192 :       advanceGenerator(col, getNumberOfRows() - getLocalRowEnd());
      87             : 
      88             :       // Do not advance generator for shuffle, the shuffle handles it
      89        7192 :       shuffle(local, col + getNumberOfCols(), CommMethod::SEMI_LOCAL);
      90             :     }
      91             :   }
      92        2162 : }
      93             : 
      94             : Real
      95       39936 : LatinHypercubeSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
      96             : {
      97             :   // NOTE: All calls to generators (getRand, etc.) occur in sampleSetup
      98       39936 :   auto row = _is_local ? row_index - getLocalRowBegin() : row_index;
      99       39936 :   return _distributions[col_index]->quantile(_probabilities[col_index][row]);
     100             : }

Generated by: LCOV version 1.14