www.mooseframework.org
CSVReader.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // STL includes
16 #include <fstream>
17 
18 // MOOSE includes
19 #include "CSVReader.h"
20 #include "MooseUtils.h"
21 
22 template <>
25 {
27  params.addClassDescription(
28  "Converts columns of a CSV file into vectors of a VectorPostprocessor.");
29  params.addRequiredParam<FileName>("csv_file",
30  "The name of the CSV file to read. Currently, with "
31  "the exception of the header row, only numeric "
32  "values are supported.");
33  params.addParam<bool>("header",
34  "When true it is assumed that the first row contains column headers, these "
35  "headers are used as the VectorPostprocessor vector names. If false the "
36  "file is assumed to contain only numbers and the vectors are named "
37  "automatically based on the column number (e.g., 'column_0000', "
38  "'column_0001'). If not supplied the reader attempts to auto detect the "
39  "headers.");
40  params.addParam<std::string>("delimiter",
41  "The column delimiter. Despite the name this can read files "
42  "separated by delimiter other than a comma. If this options is "
43  "omitted it will read comma or space separated files.");
44  params.addParam<bool>(
45  "ignore_empty_lines", true, "When true new empty lines in the file are ignored.");
46  params.set<MultiMooseEnum>("execute_on") = "initial";
47  return params;
48 }
49 
51  : GeneralVectorPostprocessor(params), _csv_reader(getParam<FileName>("csv_file"), &_communicator)
52 {
53  _csv_reader.setIgnoreEmptyLines(getParam<bool>("ignore_empty_lines"));
54  if (isParamValid("header"))
55  _csv_reader.setHeaderFlag(getParam<bool>("header")
58  if (isParamValid("delimiter"))
59  _csv_reader.setDelimiter(getParam<std::string>("delimiter"));
60 }
61 
62 void
64 {
65  _csv_reader.read();
66  for (auto & name : _csv_reader.getNames())
67  if (_column_data.find(name) == _column_data.end())
69 }
70 
71 void
73 {
74  const std::vector<std::string> & names = _csv_reader.getNames();
75  const std::vector<std::vector<double>> & data = _csv_reader.getData();
76  for (std::size_t i = 0; i < _column_data.size(); ++i)
77  _column_data[names[i]]->assign(data[i].begin(), data[i].end());
78 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
void setHeaderFlag(HeaderFlag value)
void read()
Perform the actual data reading.
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
const std::vector< std::string > & getNames() const
Return the column/row names.
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::map< std::string, VectorPostprocessorValue * > _column_data
Data vectors, which are stored in a map to allow for late declarations to occur, i.e., it is possible for the file to change and add new vectors during the simulation.
Definition: CSVReader.h:38
InputParameters validParams< CSVReader >()
Definition: CSVReader.C:24
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...
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
CSVReader(const InputParameters &parameters)
Definition: CSVReader.C:50
void setDelimiter(const std::string &value)
InputParameters validParams< GeneralVectorPostprocessor >()
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
Definition: CSVReader.C:63
MooseUtils::DelimitedFileReader _csv_reader
The MOOSE delimited file reader.
Definition: CSVReader.h:34
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...
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...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
virtual void execute() override
Execute method.
Definition: CSVReader.C:72