LCOV - code coverage report
Current view: top level - src/samplers - SobolSampler.C (source / functions) Hit Total Coverage
Test: stochastic_tools Test Coverage Lines: 31 31 100.0 %
Date: 2017-11-20 10:22:20 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /****************************************************************/
       2             : /* MOOSE - Multiphysics Object Oriented Simulation Environment  */
       3             : /*                                                              */
       4             : /*          All contents are licensed under LGPL V2.1           */
       5             : /*             See LICENSE for full restrictions                */
       6             : /****************************************************************/
       7             : 
       8             : #include "SobolSampler.h"
       9             : 
      10             : template <>
      11             : InputParameters
      12           7 : validParams<SobolSampler>()
      13             : {
      14           7 :   InputParameters params = validParams<Sampler>();
      15          14 :   params.addClassDescription("Sobol variance-based sensitivity analysis Sampler.");
      16          21 :   params.addRequiredParam<unsigned int>(
      17           7 :       "n_samples", "Number of Monte Carlo samples to perform for each distribution.");
      18           7 :   return params;
      19             : }
      20             : 
      21           7 : SobolSampler::SobolSampler(const InputParameters & parameters)
      22             :   : Sampler(parameters),
      23          21 :     _num_samples(getParam<unsigned int>("n_samples")),
      24             :     _a_matrix(0, 0),
      25          21 :     _b_matrix(0, 0)
      26             : {
      27           7 :   setNumberOfRequiedRandomSeeds(2);
      28           7 : }
      29             : 
      30             : void
      31          18 : SobolSampler::sampleSetUp()
      32             : {
      33          18 :   _a_matrix.resize(_num_samples, _distributions.size());
      34          18 :   _b_matrix.resize(_num_samples, _distributions.size());
      35         130 :   for (std::size_t i = 0; i < _num_samples; ++i)
      36         296 :     for (auto j = beginIndex(_distributions); j < _distributions.size(); ++j)
      37             :     {
      38         240 :       _a_matrix(i, j) = _distributions[j]->quantile(this->rand(0));
      39         120 :       _b_matrix(i, j) = _distributions[j]->quantile(this->rand(1));
      40             :     }
      41          18 : }
      42             : 
      43             : void
      44          18 : SobolSampler::sampleTearDown()
      45             : {
      46          18 :   _a_matrix.resize(0, 0);
      47          18 :   _b_matrix.resize(0, 0);
      48          18 : }
      49             : 
      50             : std::vector<DenseMatrix<Real>>
      51          18 : SobolSampler::sample()
      52             : {
      53             :   // Create the output vector
      54          18 :   auto n = _distributions.size() + 2;
      55          18 :   std::vector<DenseMatrix<Real>> output(n);
      56             : 
      57             :   // Include the A and B matrices
      58             :   output[0] = _a_matrix;
      59             :   output[1] = _b_matrix;
      60             : 
      61             :   // Create the AB matrices
      62          94 :   for (auto idx = beginIndex(_distributions, 2); idx < n; ++idx)
      63             :   {
      64             :     output[idx] = _a_matrix;
      65         278 :     for (std::size_t i = 0; i < _num_samples; ++i)
      66         240 :       output[idx](i, idx - 2) = _b_matrix(i, idx - 2);
      67             :   }
      68             : 
      69          18 :   return output;
      70        1812 : }

Generated by: LCOV version 1.11