www.mooseframework.org
CSVReader.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // STL includes
11 #include <fstream>
12 
13 // MOOSE includes
14 #include "CSVReader.h"
15 #include "MooseUtils.h"
16 
17 registerMooseObject("MooseApp", CSVReader);
18 
20 
23 {
25  params.addClassDescription(
26  "Converts columns of a CSV file into vectors of a VectorPostprocessor.");
27  params.addRequiredParam<FileName>("csv_file",
28  "The name of the CSV file to read. Currently, with "
29  "the exception of the header row, only numeric "
30  "values are supported.");
31  params.addParam<bool>("header",
32  "When true it is assumed that the first row contains column headers, these "
33  "headers are used as the VectorPostprocessor vector names. If false the "
34  "file is assumed to contain only numbers and the vectors are named "
35  "automatically based on the column number (e.g., 'column_0000', "
36  "'column_0001'). If not supplied the reader attempts to auto detect the "
37  "headers.");
38  params.addParam<std::string>("delimiter",
39  "The column delimiter. Despite the name this can read files "
40  "separated by delimiter other than a comma. If this options is "
41  "omitted it will read comma or space separated files.");
42  params.addParam<bool>(
43  "ignore_empty_lines", true, "When true new empty lines in the file are ignored.");
44  params.set<ExecFlagEnum>("execute_on", true) = EXEC_INITIAL;
45 
46  // The value from this VPP is naturally already on every processor
47  // TODO: Make this not the case! See #11415
48  params.set<bool>("_is_broadcast") = true;
49 
50  return params;
51 }
52 
54  : GeneralVectorPostprocessor(params), _csv_reader(getParam<FileName>("csv_file"), &_communicator)
55 {
56  _csv_reader.setIgnoreEmptyLines(getParam<bool>("ignore_empty_lines"));
57  if (isParamValid("header"))
58  _csv_reader.setHeaderFlag(getParam<bool>("header")
61  if (isParamValid("delimiter"))
62  _csv_reader.setDelimiter(getParam<std::string>("delimiter"));
63 }
64 
65 void
67 {
68  _csv_reader.read();
69  for (auto & name : _csv_reader.getNames())
70  if (_column_data.find(name) == _column_data.end())
72 }
73 
74 void
76 {
77  const std::vector<std::string> & names = _csv_reader.getNames();
78  const std::vector<std::vector<double>> & data = _csv_reader.getData();
79  for (std::size_t i = 0; i < _column_data.size(); ++i)
80  _column_data[names[i]]->assign(data[i].begin(), data[i].end());
81 }
MooseUtils::DelimitedFileReader::getNames
const std::vector< std::string > & getNames() const
Return the column/row names.
Definition: DelimitedFileReader.C:124
MooseObject::isParamValid
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:100
CSVReader::CSVReader
CSVReader(const InputParameters &parameters)
Definition: CSVReader.C:53
VectorPostprocessor::declareVector
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
Definition: VectorPostprocessor.C:58
CSVReader::initialize
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
Definition: CSVReader.C:66
MooseUtils::DelimitedFileReader::read
void read()
Perform the actual data reading.
Definition: DelimitedFileReader.C:35
registerMooseObject
registerMooseObject("MooseApp", CSVReader)
InputParameters::addParam
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object.
Definition: InputParameters.h:1198
CSVReader
Definition: CSVReader.h:22
MooseUtils::DelimitedFileReader::setDelimiter
void setDelimiter(const std::string &value)
Definition: DelimitedFileReader.h:78
CSVReader::validParams
static InputParameters validParams()
Definition: CSVReader.C:22
InputParameters
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Definition: InputParameters.h:53
InputParameters::set
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Definition: InputParameters.h:987
defineLegacyParams
defineLegacyParams(CSVReader)
GeneralVectorPostprocessor::validParams
static InputParameters validParams()
Definition: GeneralVectorPostprocessor.C:15
InputParameters::addClassDescription
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
Definition: InputParameters.C:70
MooseUtils::DelimitedFileReader::setHeaderFlag
void setHeaderFlag(HeaderFlag value)
Definition: DelimitedFileReader.h:81
EXEC_INITIAL
const ExecFlagType EXEC_INITIAL
MooseUtils::DelimitedFileReader::getData
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
Definition: DelimitedFileReader.C:130
CSVReader.h
CSVReader::_column_data
std::map< std::string, VectorPostprocessorValue * > _column_data
Data vectors, which are stored in a map to allow for late declarations to occur, i....
Definition: CSVReader.h:37
GeneralVectorPostprocessor
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
Definition: GeneralVectorPostprocessor.h:27
ExecFlagEnum
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
MooseUtils::DelimitedFileReader::HeaderFlag::ON
MooseUtils.h
CSVReader::_csv_reader
MooseUtils::DelimitedFileReader _csv_reader
The MOOSE delimited file reader.
Definition: CSVReader.h:33
InputParameters::addRequiredParam
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
Definition: InputParameters.h:1176
MooseUtils::DelimitedFileReader::setIgnoreEmptyLines
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
Definition: DelimitedFileReader.h:72
CSVReader::execute
virtual void execute() override
Execute method.
Definition: CSVReader.C:75
MooseObject::name
virtual const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:70
MooseUtils::DelimitedFileReader::HeaderFlag::OFF