LCOV - code coverage report
Current view: top level - src/samplers - LatinHypercubeSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #31405 (292dce) with base fef103 Lines: 41 42 97.6 %
Date: 2025-09-04 07:57:52 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        1852 : LatinHypercubeSampler::validParams()
      16             : {
      17        1852 :   InputParameters params = Sampler::validParams();
      18        1852 :   params.addClassDescription("Latin Hypercube Sampler.");
      19        3704 :   params.addRequiredParam<dof_id_type>("num_rows", "The size of the square matrix to generate.");
      20        3704 :   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        1852 :   return params;
      25           0 : }
      26             : 
      27        1068 : LatinHypercubeSampler::LatinHypercubeSampler(const InputParameters & parameters)
      28        1068 :   : Sampler(parameters)
      29             : {
      30        2136 :   const auto & distribution_names = getParam<std::vector<DistributionName>>("distributions");
      31        4437 :   for (const DistributionName & name : distribution_names)
      32        3369 :     _distributions.push_back(&getDistributionByName(name));
      33             : 
      34        3204 :   setNumberOfRows(getParam<dof_id_type>("num_rows"));
      35        1068 :   setNumberOfCols(distribution_names.size());
      36        1068 :   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        1068 :   setAutoAdvanceGenerators(false);
      44        1068 : }
      45             : 
      46             : void
      47        2296 : 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        2296 :   _is_local = mode == Sampler::SampleMode::LOCAL;
      54             : 
      55        2296 :   const Real bin_size = 1. / getNumberOfRows();
      56        2296 :   _probabilities.resize(getNumberOfCols());
      57        2296 :   if (mode == Sampler::SampleMode::GLOBAL)
      58             :   {
      59         132 :     for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
      60             :     {
      61             :       std::vector<Real> & local = _probabilities[col];
      62          88 :       local.resize(getNumberOfRows());
      63         616 :       for (dof_id_type row = 0; row < getNumberOfRows(); ++row)
      64             :       {
      65         528 :         const auto lower = row * bin_size;
      66         528 :         const auto upper = (row + 1) * bin_size;
      67         528 :         local[row] = getRand(col) * (upper - lower) + lower;
      68             :       }
      69          88 :       shuffle(local, col + getNumberOfCols(), CommMethod::NONE);
      70             :     }
      71             :   }
      72             : 
      73             :   else
      74             :   {
      75        9875 :     for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
      76             :     {
      77             :       std::vector<Real> & local = _probabilities[col];
      78        7623 :       local.resize(getNumberOfLocalRows());
      79        7623 :       advanceGenerator(col, getLocalRowBegin());
      80       50761 :       for (dof_id_type row = getLocalRowBegin(); row < getLocalRowEnd(); ++row)
      81             :       {
      82       43138 :         const auto lower = row * bin_size;
      83       43138 :         const auto upper = (row + 1) * bin_size;
      84       43138 :         local[row - getLocalRowBegin()] = getRand(col) * (upper - lower) + lower;
      85             :       }
      86        7623 :       advanceGenerator(col, getNumberOfRows() - getLocalRowEnd());
      87             : 
      88             :       // Do not advance generator for shuffle, the shuffle handles it
      89        7623 :       shuffle(local, col + getNumberOfCols(), CommMethod::SEMI_LOCAL);
      90             :     }
      91             :   }
      92        2296 : }
      93             : 
      94             : Real
      95       43666 : 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       43666 :   auto row = _is_local ? row_index - getLocalRowBegin() : row_index;
      99       43666 :   return _distributions[col_index]->quantile(_probabilities[col_index][row]);
     100             : }

Generated by: LCOV version 1.14