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
10 // STL includes
11 #include <fstream>
13 // MOOSE includes
14 #include "CSVReader.h"
15 #include "MooseUtils.h"
17 registerMooseObject("MooseApp", CSVReader);
19 template <>
22 {
24  params.addClassDescription(
25  "Converts columns of a CSV file into vectors of a VectorPostprocessor.");
26  params.addRequiredParam<FileName>("csv_file",
27  "The name of the CSV file to read. Currently, with "
28  "the exception of the header row, only numeric "
29  "values are supported.");
30  params.addParam<bool>("header",
31  "When true it is assumed that the first row contains column headers, these "
32  "headers are used as the VectorPostprocessor vector names. If false the "
33  "file is assumed to contain only numbers and the vectors are named "
34  "automatically based on the column number (e.g., 'column_0000', "
35  "'column_0001'). If not supplied the reader attempts to auto detect the "
36  "headers.");
37  params.addParam<std::string>("delimiter",
38  "The column delimiter. Despite the name this can read files "
39  "separated by delimiter other than a comma. If this options is "
40  "omitted it will read comma or space separated files.");
41  params.addParam<bool>(
42  "ignore_empty_lines", true, "When true new empty lines in the file are ignored.");
43  params.set<ExecFlagEnum>("execute_on", true) = EXEC_INITIAL;
45  // The value from this VPP is naturally already on every processor
46  // TODO: Make this not the case! See #11415
47  params.set<bool>("_is_broadcast") = true;
49  return params;
50 }
53  : GeneralVectorPostprocessor(params), _csv_reader(getParam<FileName>("csv_file"), &_communicator)
54 {
55  _csv_reader.setIgnoreEmptyLines(getParam<bool>("ignore_empty_lines"));
56  if (isParamValid("header"))
57  _csv_reader.setHeaderFlag(getParam<bool>("header")
60  if (isParamValid("delimiter"))
61  _csv_reader.setDelimiter(getParam<std::string>("delimiter"));
62 }
64 void
66 {
67  _csv_reader.read();
68  for (auto & name : _csv_reader.getNames())
69  if (_column_data.find(name) == _column_data.end())
71 }
73 void
75 {
76  const std::vector<std::string> & names = _csv_reader.getNames();
77  const std::vector<std::vector<double>> & data = _csv_reader.getData();
78  for (std::size_t i = 0; i < _column_data.size(); ++i)
79  _column_data[names[i]]->assign(data[i].begin(), data[i].end());
80 }
void setHeaderFlag(HeaderFlag value)
void read()
Perform the actual data reading.
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
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.
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:35
InputParameters validParams< CSVReader >()
Definition: CSVReader.C:21
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:52
void setDelimiter(const std::string &value)
InputParameters validParams< GeneralVectorPostprocessor >()
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
const std::vector< std::string > & getNames() const
Return the column/row names.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
Definition: CSVReader.C:65
registerMooseObject("MooseApp", CSVReader)
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
MooseUtils::DelimitedFileReader _csv_reader
The MOOSE delimited file reader.
Definition: CSVReader.h:31
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
virtual void execute() override
Execute method.
Definition: CSVReader.C:74
const ExecFlagType EXEC_INITIAL