LCOV - code coverage report
Current view: top level - src/samplers - CSVSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #31405 (292dce) with base fef103 Lines: 34 37 91.9 %
Date: 2025-09-04 07:57:52 Functions: 3 3 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 "CSVSampler.h"
      11             : #include "DelimitedFileReader.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", CSVSampler);
      14             : 
      15             : InputParameters
      16         496 : CSVSampler::validParams()
      17             : {
      18         496 :   InputParameters params = Sampler::validParams();
      19         496 :   params.addClassDescription("Sampler that reads samples from CSV file.");
      20         992 :   params.addRequiredParam<FileName>("samples_file",
      21             :                                     "Name of the CSV file that contains the samples matrix.");
      22         992 :   params.addParam<std::vector<dof_id_type>>(
      23             :       "column_indices",
      24             :       "Column indices in the CSV file to be sampled from. Number of indices here "
      25             :       "will be the same as the number of columns per matrix.");
      26         992 :   params.addParam<std::vector<std::string>>(
      27             :       "column_names",
      28             :       "Column names in the CSV file to be sampled from. Number of columns names "
      29             :       "here will be the same as the number of columns per matrix.");
      30         496 :   return params;
      31           0 : }
      32             : 
      33         283 : CSVSampler::CSVSampler(const InputParameters & parameters) : Sampler(parameters)
      34             : {
      35             :   // If indices or names are not provided, all of the data will be read and the
      36             :   // matrix will be the same as the contents of the data file
      37        1040 :   if (!isParamValid("column_indices") && !isParamValid("column_names"))
      38             :   {
      39             :     // Reading the samples file and getting data
      40         198 :     MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      41          99 :     reader.read();
      42          99 :     _data = reader.getData();
      43          99 :   }
      44             :   // Both column indices and names cannot be provided
      45         552 :   else if (isParamValid("column_indices") && isParamValid("column_names"))
      46           0 :     mooseError("In sampler, ",
      47           0 :                _name,
      48             :                ": Please provide either column_indices or column_names but not both.");
      49             :   // If only column indices is provided, generate the matrix accordingly and
      50             :   // store it in _data
      51         368 :   else if (isParamValid("column_indices"))
      52             :   {
      53         184 :     std::vector<dof_id_type> indices = getParam<std::vector<dof_id_type>>("column_indices");
      54         299 :     for (unsigned int i = 0; i < indices.size(); i++)
      55             :     {
      56             :       // Reading the samples file and getting data
      57         414 :       MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      58         207 :       reader.read();
      59         207 :       _data.push_back(reader.getData(indices[i]));
      60         207 :     }
      61          92 :   }
      62             :   // If column names are provided, generate the matrix accordingly and store it
      63             :   // in _data
      64             :   else
      65             :   {
      66         184 :     std::vector<std::string> names = getParam<std::vector<std::string>>("column_names");
      67         391 :     for (unsigned int i = 0; i < names.size(); i++)
      68             :     {
      69             :       // Reading the samples file and getting data
      70         598 :       MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      71         299 :       reader.read();
      72         299 :       _data.push_back(reader.getData(names[i]));
      73         299 :     }
      74          92 :   }
      75             : 
      76         283 :   setNumberOfRows(_data[0].size());
      77         283 :   setNumberOfCols(_data.size());
      78         283 : }
      79             : 
      80             : Real
      81       23012 : CSVSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
      82             : {
      83             :   // Checks to make sure that the row and column indices are not out of bounds
      84             :   mooseAssert(row_index < _data[0].size(), "row_index cannot be out of bounds of the data.");
      85             :   mooseAssert(col_index < _data.size(), "col_index cannot be out of bounds of the data.");
      86             : 
      87       23012 :   return _data[col_index][row_index]; // entering samples into the matrix
      88             : }

Generated by: LCOV version 1.14