https://mooseframework.inl.gov
Loading...
Searching...
No Matches
CSVSampler.C
Go to the documentation of this file.
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
13registerMooseObject("StochasticToolsApp", CSVSampler);
14
17{
19 params.addClassDescription("Sampler that reads samples from CSV file.");
20 params.addRequiredParam<FileName>("samples_file",
21 "Name of the CSV file that contains the samples matrix.");
22 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 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 return params;
31}
32
33CSVSampler::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 if (!isParamValid("column_indices") && !isParamValid("column_names"))
38 {
39 // Reading the samples file and getting data
40 MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
41 reader.read();
42 _data = reader.getData();
43 }
44 // Both column indices and names cannot be provided
45 else if (isParamValid("column_indices") && isParamValid("column_names"))
46 mooseError("In sampler, ",
47 _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 else if (isParamValid("column_indices"))
52 {
53 std::vector<dof_id_type> indices = getParam<std::vector<dof_id_type>>("column_indices");
54 for (unsigned int i = 0; i < indices.size(); i++)
55 {
56 // Reading the samples file and getting data
57 MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
58 reader.read();
59 _data.push_back(reader.getData(indices[i]));
60 }
61 }
62 // If column names are provided, generate the matrix accordingly and store it
63 // in _data
64 else
65 {
66 std::vector<std::string> names = getParam<std::vector<std::string>>("column_names");
67 for (unsigned int i = 0; i < names.size(); i++)
68 {
69 // Reading the samples file and getting data
70 MooseUtils::DelimitedFileReader reader(getParam<FileName>("samples_file"), &_communicator);
71 reader.read();
72 _data.push_back(reader.getData(names[i]));
73 }
74 }
75
76 setNumberOfRows(_data[0].size());
77 setNumberOfCols(_data.size());
78}
79
80Real
81CSVSampler::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 return _data[col_index][row_index]; // entering samples into the matrix
88}
registerMooseObject("StochasticToolsApp", CSVSampler)
A class used to generate samples from a CSV file.
Definition CSVSampler.h:18
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
Definition CSVSampler.C:81
CSVSampler(const InputParameters &parameters)
Definition CSVSampler.C:33
static InputParameters validParams()
Definition CSVSampler.C:16
std::vector< std::vector< Real > > _data
Data read in from the CSV file.
Definition CSVSampler.h:30
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void mooseError(Args &&... args) const
const std::string & _name
bool isParamValid(const std::string &name) const
const std::vector< std::vector< T > > & getData() const
void setNumberOfCols(dof_id_type n_cols)
static InputParameters validParams()
void setNumberOfRows(dof_id_type n_rows)
const Parallel::Communicator & _communicator