LCOV - code coverage report
Current view: top level - src/samplers - CSVSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 33 36 91.7 %
Date: 2025-07-25 05:00:46 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         470 : CSVSampler::validParams()
      17             : {
      18         470 :   InputParameters params = Sampler::validParams();
      19         470 :   params.addClassDescription("Sampler that reads samples from CSV file.");
      20         940 :   params.addRequiredParam<FileName>("samples_file",
      21             :                                     "Name of the CSV file that contains the samples matrix.");
      22         940 :   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         940 :   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         470 :   return params;
      31           0 : }
      32             : 
      33         270 : 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         992 :   if (!isParamValid("column_indices") && !isParamValid("column_names"))
      38             :   {
      39             :     // Reading the samples file and getting data
      40         188 :     MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      41          94 :     reader.read();
      42          94 :     _data = reader.getData();
      43          94 :   }
      44             :   // Both column indices and names cannot be provided
      45         528 :   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         352 :   else if (isParamValid("column_indices"))
      52             :   {
      53         176 :     std::vector<dof_id_type> indices = getParam<std::vector<dof_id_type>>("column_indices");
      54         286 :     for (unsigned int i = 0; i < indices.size(); i++)
      55             :     {
      56             :       // Reading the samples file and getting data
      57         396 :       MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      58         198 :       reader.read();
      59         198 :       _data.push_back(reader.getData(indices[i]));
      60         198 :     }
      61             :   }
      62             :   // If column names are provided, generate the matrix accordingly and store it
      63             :   // in _data
      64             :   else
      65             :   {
      66         176 :     std::vector<std::string> names = getParam<std::vector<std::string>>("column_names");
      67         374 :     for (unsigned int i = 0; i < names.size(); i++)
      68             :     {
      69             :       // Reading the samples file and getting data
      70         572 :       MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
      71         286 :       reader.read();
      72         286 :       _data.push_back(reader.getData(names[i]));
      73         286 :     }
      74          88 :   }
      75             : 
      76         270 :   setNumberOfRows(_data[0].size());
      77         270 :   setNumberOfCols(_data.size());
      78         270 : }
      79             : 
      80             : Real
      81       20920 : 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       20920 :   return _data[col_index][row_index]; // entering samples into the matrix
      88             : }

Generated by: LCOV version 1.14