https://mooseframework.inl.gov
CSVReaderVectorPostprocessor.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 // STL includes
11 #include <fstream>
12 
13 // MOOSE includes
15 #include "MooseUtils.h"
16 
18 registerMooseObjectRenamed("MooseApp", CSVReader, "06/30/2024 24:00", CSVReaderVectorPostprocessor);
19 
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<bool>("contains_complete_history") = true;
44  params.suppressParameter<bool>("contains_complete_history");
45  params.set<ExecFlagEnum>("execute_on", true) = EXEC_NONE;
46  params.suppressParameter<ExecFlagEnum>("execute_on");
47 
48  // The value from this VPP is naturally already on every processor
49  // TODO: Make this not the case! See #11415
50  params.set<bool>("_auto_broadcast") = false;
51 
52  return params;
53 }
54 
57 {
59  MooseUtils::DelimitedFileReader csv_reader(getParam<FileName>("csv_file"), &_communicator);
60  csv_reader.setIgnoreEmptyLines(getParam<bool>("ignore_empty_lines"));
61  if (isParamValid("header"))
62  csv_reader.setHeaderFlag(getParam<bool>("header")
65  if (isParamValid("delimiter"))
66  csv_reader.setDelimiter(getParam<std::string>("delimiter"));
67 
68  csv_reader.read();
69  const std::vector<std::string> & names = csv_reader.getNames();
70  const std::vector<std::vector<double>> & data = csv_reader.getData();
71  for (std::size_t i = 0; i < data.size(); ++i)
72  {
73  _column_data[names[i]] = &declareVector(names[i]);
74  _column_data[names[i]]->assign(data[i].begin(), data[i].end());
75  }
76 }
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
void setIgnoreEmptyLines(bool value)
Set/Get methods for file format controls.
const ExecFlagType EXEC_NONE
Definition: Moose.C:27
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.
registerMooseObject("MooseApp", CSVReaderVectorPostprocessor)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & _communicator
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 suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
std::map< std::string, VectorPostprocessorValue * > _column_data
The vector variables storing the data read from the csv.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
static InputParameters validParams()
registerMooseObjectRenamed("MooseApp", CSVReader, "06/30/2024 24:00", CSVReaderVectorPostprocessor)
void read()
Perform the actual data reading.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
const std::vector< std::vector< T > > & getData() const
Return the rows/columns of data.
Utility class for reading delimited data (e.g., CSV data).
void setDelimiter(const std::string &value)
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 optional parameter and a documentation string to the InputParameters object...
const std::vector< std::string > & getNames() const
Return the column/row names.
CSVReaderVectorPostprocessor(const InputParameters &parameters)