LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - SamplerData.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 50 52 96.2 %
Date: 2025-07-25 05:00:46 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        2352 : SamplerData::validParams()
      20             : {
      21        2352 :   InputParameters params = GeneralVectorPostprocessor::validParams();
      22        2352 :   params.addClassDescription(
      23             :       "Tool for extracting Sampler object data and storing in VectorPostprocessor vectors.");
      24        4704 :   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        2352 :   params.set<bool>("_auto_broadcast") = false;
      30             : 
      31        4704 :   MooseEnum method("get_global_samples get_local_samples get_next_local_row", "get_next_local_row");
      32        4704 :   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        2352 :   return params;
      38        2352 : }
      39             : 
      40        1152 : SamplerData::SamplerData(const InputParameters & parameters)
      41             :   : GeneralVectorPostprocessor(parameters),
      42        1152 :     _sampler(getSampler("sampler")),
      43        3456 :     _sampler_method(getParam<MooseEnum>("sampler_method"))
      44             : {
      45        1152 :   const int padding = MooseUtils::numDigits(_sampler.getNumberOfCols());
      46        4444 :   for (dof_id_type j = 0; j < _sampler.getNumberOfCols(); ++j)
      47             :   {
      48        3292 :     std::stringstream nm;
      49        6584 :     nm << getParam<SamplerName>("sampler") << "_" << std::setw(padding) << std::setfill('0') << j;
      50        3292 :     _sample_vectors.push_back(&declareVector(nm.str()));
      51        3292 :   }
      52        1152 : }
      53             : 
      54             : void
      55        1812 : SamplerData::initialize()
      56             : {
      57        1812 :   dof_id_type n = (_sampler_method == "get_global_samples") ? _sampler.getNumberOfRows()
      58        1572 :                                                             : _sampler.getNumberOfLocalRows();
      59        6656 :   for (auto & ppv_ptr : _sample_vectors)
      60        4844 :     ppv_ptr->resize(n, 0);
      61        1812 : }
      62             : 
      63             : void
      64        1812 : SamplerData::execute()
      65             : {
      66        1812 :   if (_sampler_method == "get_global_samples")
      67             :   {
      68         240 :     if (processor_id() == 0)
      69             :     {
      70         120 :       DenseMatrix<Real> data = _sampler.getGlobalSamples();
      71         390 :       for (unsigned int j = 0; j < data.n(); ++j)
      72        1930 :         for (unsigned int i = 0; i < data.m(); ++i)
      73        1660 :           (*_sample_vectors[j])[i] = data(i, j);
      74         120 :     }
      75             :   }
      76             : 
      77        1572 :   else if (_sampler_method == "get_local_samples")
      78             :   {
      79         158 :     DenseMatrix<Real> data = _sampler.getLocalSamples();
      80         428 :     for (unsigned int j = 0; j < data.n(); ++j)
      81         950 :       for (unsigned int i = 0; i < data.m(); ++i)
      82         680 :         (*_sample_vectors[j])[i] = data(i, j);
      83         158 :   }
      84             : 
      85        1414 :   else if (_sampler_method == "get_next_local_row")
      86             :   {
      87       21884 :     for (dof_id_type i = _sampler.getLocalRowBegin(); i < _sampler.getLocalRowEnd(); ++i)
      88             :     {
      89       20470 :       std::vector<Real> data = _sampler.getNextLocalRow();
      90       84330 :       for (std::size_t j = 0; j < data.size(); ++j)
      91       63860 :         (*_sample_vectors[j])[i - _sampler.getLocalRowBegin()] = data[j];
      92             :     }
      93             :   }
      94        1812 : }
      95             : 
      96             : void
      97        1812 : SamplerData::finalize()
      98             : {
      99        1812 :   if (!isDistributed() && _sampler_method != "get_global_samples")
     100             :   {
     101        4224 :     for (auto & ppv_ptr : _sample_vectors)
     102        3088 :       _communicator.gather(0, *ppv_ptr);
     103             :   }
     104        1812 : }
     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