LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - SamplerData.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #31405 (292dce) with base fef103 Lines: 51 53 96.2 %
Date: 2025-09-04 07:57:52 Functions: 5 6 83.3 %
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             : // Stocastic Tools Includes
      11             : #include "SamplerData.h"
      12             : 
      13             : // MOOSE includes
      14             : #include "Sampler.h"
      15             : 
      16             : registerMooseObject("StochasticToolsApp", SamplerData);
      17             : 
      18             : InputParameters
      19        2534 : SamplerData::validParams()
      20             : {
      21        2534 :   InputParameters params = GeneralVectorPostprocessor::validParams();
      22        2534 :   params.addClassDescription(
      23             :       "Tool for extracting Sampler object data and storing in VectorPostprocessor vectors.");
      24        5068 :   params.addRequiredParam<SamplerName>("sampler",
      25             :                                        "The sample from which to extract distribution data.");
      26             : 
      27             :   // Do not broadcast, this tools is mainly for testing of sampler data so the data is only needed
      28             :   // on the root process, which is handled in finalize
      29        2534 :   params.set<bool>("_auto_broadcast") = false;
      30             : 
      31        5068 :   MooseEnum method("get_global_samples get_local_samples get_next_local_row", "get_next_local_row");
      32        5068 :   params.addParam<MooseEnum>(
      33             :       "sampler_method",
      34             :       method,
      35             :       "Control the method of data retrieval from the Sampler object; this is mainly for testing.");
      36             : 
      37        2534 :   return params;
      38        2534 : }
      39             : 
      40        1243 : SamplerData::SamplerData(const InputParameters & parameters)
      41             :   : GeneralVectorPostprocessor(parameters),
      42        1243 :     _sampler(getSampler("sampler")),
      43        3729 :     _sampler_method(getParam<MooseEnum>("sampler_method"))
      44             : {
      45        1243 :   const int padding = MooseUtils::numDigits(_sampler.getNumberOfCols());
      46        4789 :   for (dof_id_type j = 0; j < _sampler.getNumberOfCols(); ++j)
      47             :   {
      48        3546 :     std::stringstream nm;
      49        7092 :     nm << getParam<SamplerName>("sampler") << "_" << std::setw(padding) << std::setfill('0') << j;
      50        3546 :     _sample_vectors.push_back(&declareVector(nm.str()));
      51        3546 :   }
      52        1243 : }
      53             : 
      54             : void
      55        1956 : SamplerData::initialize()
      56             : {
      57        1956 :   dof_id_type n = (_sampler_method == "get_global_samples") ? _sampler.getNumberOfRows()
      58        1698 :                                                             : _sampler.getNumberOfLocalRows();
      59        7183 :   for (auto & ppv_ptr : _sample_vectors)
      60        5227 :     ppv_ptr->resize(n, 0);
      61        1956 : }
      62             : 
      63             : void
      64        1956 : SamplerData::execute()
      65             : {
      66        1956 :   if (_sampler_method == "get_global_samples")
      67             :   {
      68         258 :     if (processor_id() == 0)
      69             :     {
      70         132 :       DenseMatrix<Real> data = _sampler.getGlobalSamples();
      71         429 :       for (unsigned int j = 0; j < data.n(); ++j)
      72        2123 :         for (unsigned int i = 0; i < data.m(); ++i)
      73        1826 :           (*_sample_vectors[j])[i] = data(i, j);
      74         132 :     }
      75             :   }
      76             : 
      77        1698 :   else if (_sampler_method == "get_local_samples")
      78             :   {
      79         172 :     DenseMatrix<Real> data = _sampler.getLocalSamples();
      80         466 :     for (unsigned int j = 0; j < data.n(); ++j)
      81        1042 :       for (unsigned int i = 0; i < data.m(); ++i)
      82         748 :         (*_sample_vectors[j])[i] = data(i, j);
      83         172 :   }
      84             : 
      85        1526 :   else if (_sampler_method == "get_next_local_row")
      86             :   {
      87       24043 :     for (dof_id_type i = _sampler.getLocalRowBegin(); i < _sampler.getLocalRowEnd(); ++i)
      88             :     {
      89       22517 :       std::vector<Real> data = _sampler.getNextLocalRow();
      90       92763 :       for (std::size_t j = 0; j < data.size(); ++j)
      91       70246 :         (*_sample_vectors[j])[i - _sampler.getLocalRowBegin()] = data[j];
      92       22517 :     }
      93             :   }
      94        1956 : }
      95             : 
      96             : void
      97        1956 : SamplerData::finalize()
      98             : {
      99        1956 :   if (!isDistributed() && _sampler_method != "get_global_samples")
     100             :   {
     101        4530 :     for (auto & ppv_ptr : _sample_vectors)
     102        3311 :       _communicator.gather(0, *ppv_ptr);
     103             :   }
     104        1956 : }
     105             : 
     106             : void
     107           0 : SamplerData::threadJoin(const UserObject & /*uo*/)
     108             : {
     109             :   /// TODO: Use this when the Sampler objects become threaded
     110             :   /*
     111             :   if (_use_local_samples)
     112             :   {
     113             :     const SamplerData & obj = static_cast<const SamplerData &>(uo);
     114             :     for (std::size_t i = 0; i < _sample_vectors.size(); ++i)
     115             :       (*_sample_vectors[i]).insert(_sample_vectors[i]->end(), obj._sample_vectors[i]->begin(),
     116             :                                    obj._sample_vectors[i]->end());
     117             :   }
     118             :   */
     119           0 : }

Generated by: LCOV version 1.14