www.mooseframework.org
CSV.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 // Moose includes
16 #include "CSV.h"
17 #include "FEProblem.h"
18 #include "MooseApp.h"
19 
20 template <>
23 {
24  // Get the parameters from the parent object
26 
27  params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
28 
29  // Options for aligning csv output with whitespace padding
30  params.addParam<bool>(
31  "align",
32  false,
33  "Align the outputted csv data by padding the numbers with trailing whitespace");
34  params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
35  params.addParam<unsigned int>("precision", 14, "Set the output precision");
36 
37  // Suppress unused parameters
38  params.suppressParameter<unsigned int>("padding");
39 
40  // Done
41  return params;
42 }
43 
44 CSV::CSV(const InputParameters & parameters)
45  : TableOutput(parameters),
46  _align(getParam<bool>("align")),
47  _precision(getParam<unsigned int>("precision")),
48  _delimiter(getParam<std::string>("delimiter")),
49  _write_all_table(false),
50  _write_vector_table(false),
51  _sort_columns(getParam<bool>("sort_columns")),
52  _recovering(_app.isRecovering())
53 {
54 }
55 
56 void
58 {
59  // Call the base class method
61 
62  // Set the delimiter
64 
65  // Set the precision
67 
68  if (_recovering)
69  _all_data_table.append(true);
70 }
71 
72 std::string
74 {
75  return _file_base + ".csv";
76 }
77 
78 void
80 {
82  _write_all_table = true;
83 }
84 
85 void
87 {
89  _write_all_table = true;
90 }
91 
92 void
94 {
96  _write_vector_table = true;
97 }
98 
99 void
101 {
102  // Start the performance log
103  Moose::perf_log.push("CSV::output()", "Output");
104 
105  // Call the base class output (populates tables)
106  TableOutput::output(type);
107 
108  // Print the table containing all the data to a file
109  if (_write_all_table && !_all_data_table.empty() && processor_id() == 0)
110  {
111  if (_sort_columns)
114  }
115 
116  // Output each VectorPostprocessor's data to a file
117  if (_write_vector_table && processor_id() == 0)
118  {
119  for (auto & it : _vector_postprocessor_tables)
120  {
121  std::ostringstream output;
122  output << _file_base << "_" << MooseUtils::shortName(it.first);
123  output << "_" << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
124  << std::right << timeStep() << ".csv";
125 
126  it.second.setDelimiter(_delimiter);
127  it.second.setPrecision(_precision);
128  if (_sort_columns)
129  it.second.sortColumns();
130  it.second.printCSV(output.str(), 1, _align);
131 
132  if (_time_data)
133  {
134  std::ostringstream filename;
135  filename << _file_base << "_" << MooseUtils::shortName(it.first) << "_time.csv";
136  _vector_postprocessor_time_tables[it.first].printCSV(filename.str());
137  }
138  }
139  }
140 
141  // Re-set write flags
142  _write_all_table = false;
143  _write_vector_table = false;
144 
145  Moose::perf_log.pop("CSV::output()", "Output");
146 }
InputParameters validParams< CSV >()
Definition: CSV.C:22
unsigned int _precision
Decimal digits per number in the CSV file.
Definition: CSV.h:83
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
void printCSV(const std::string &file_name, int interval=1, bool align=false)
Method for dumping the table to a csv file - opening and closing the file handle is handled...
virtual void output(const ExecFlagType &type) override
Output the table to a *.csv file.
Definition: CSV.C:100
bool _write_all_table
Flag for writting scalar and/or postprocessor data.
Definition: CSV.h:89
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual std::string filename() override
The filename for the output file.
Definition: CSV.C:73
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:87
void setPrecision(unsigned int precision)
By default printCSV prints output to a precision of 14, this allows this to be changed.
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:325
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...
virtual void outputVectorPostprocessors() override
Populates the tables with VectorPostprocessor values.
Definition: TableOutput.C:100
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:93
bool _write_vector_table
Flag for writting vector postprocessor data.
Definition: CSV.h:92
void append(bool append_existing_file)
Sets append mode which means an existing file is not truncated on opening.
virtual void outputPostprocessors() override
Sets the write flag and calls TableOutput::outputPostprocessors()
Definition: CSV.C:86
bool _align
Flag for aligning data in .csv file.
Definition: CSV.h:80
virtual void output(const ExecFlagType &type)
A single call to this function should output all the necessary data for a single timestep.
bool _recovering
Flag indicating MOOSE is recovering via –recover command-line option.
Definition: CSV.h:98
PerfLog perf_log
Perflog to be used by applications.
std::string _delimiter
The delimiter used when writing the CSV file.
Definition: CSV.h:86
virtual void outputVectorPostprocessors() override
Sets the write flag and calls TableOutput::outputVectorPostprocessors()
Definition: CSV.C:93
bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:85
virtual void outputScalarVariables() override
Sets the write flag and calls TableOutput::outputScalarVariables()
Definition: CSV.C:79
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:81
FormattedTable & _all_data_table
Table containing postprocessor values and scalar aux variables.
Definition: TableOutput.h:82
MatType type
virtual void initialSetup()
Initialization method.
const bool _sort_columns
Flag for sorting column names.
Definition: CSV.h:95
Base class for scalar variables and postprocessors output objects.
Definition: TableOutput.h:39
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...
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
void sortColumns()
Sorts columns alphabetically.
virtual int timeStep()
Get the current time step.
Definition: Output.C:233
CSV(const InputParameters &parameters)
Class constructor.
Definition: CSV.C:44
InputParameters validParams< TableOutput >()
Definition: TableOutput.C:32
void setDelimiter(std::string delimiter)
By default printCSV places "," between each entry, this allows this to be changed.
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:73
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:137
void initialSetup() override
Setup the CSV output If restarting and the append_restart flag is false, then the output data is clea...
Definition: CSV.C:57
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:76