www.mooseframework.org
CSV.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 // Moose includes
11 #include "CSV.h"
12 #include "FEProblem.h"
13 #include "MooseApp.h"
14 
15 registerMooseObject("MooseApp", CSV);
16 
17 template <>
20 {
21  // Get the parameters from the parent object
23  params.addClassDescription("Output for postprocessors, vector postprocessors, and scalar "
24  "variables using comma seperated values (CSV).");
25  params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
26 
27  // Options for aligning csv output with whitespace padding
28  params.addParam<bool>(
29  "align",
30  false,
31  "Align the outputted csv data by padding the numbers with trailing whitespace");
32  params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
33  params.addParam<unsigned int>("precision", 14, "Set the output precision");
34  params.addParam<bool>("create_final_symlink",
35  false,
36  "Enable/disable the creation of a _FINAL symlink for vector postprocessor "
37  "data with 'execute_on' includes 'FINAL'.");
38  params.addParam<bool>(
39  "create_latest_symlink",
40  false,
41  "Enable/disable the creation of a _LATEST symlink for vector postprocessor data.");
42 
43  // Suppress unused parameters
44  params.suppressParameter<unsigned int>("padding");
45 
46  // Done
47  return params;
48 }
49 
50 CSV::CSV(const InputParameters & parameters)
51  : TableOutput(parameters),
52  _align(getParam<bool>("align")),
53  _precision(getParam<unsigned int>("precision")),
54  _delimiter(getParam<std::string>("delimiter")),
55  _write_all_table(false),
56  _write_vector_table(false),
57  _sort_columns(getParam<bool>("sort_columns")),
58  _recovering(_app.isRecovering()),
59  _create_final_symlink(getParam<bool>("create_final_symlink")),
60  _create_latest_symlink(getParam<bool>("create_latest_symlink"))
61 {
62 }
63 
64 void
66 {
67  // Call the base class method
69 
70  // Set the delimiter
72 
73  // Set the precision
75 
76  if (_recovering)
77  _all_data_table.append(true);
78 
79  // Clear any existing symbolic links to LATEST and/or FINAL
80  if (processor_id() == 0)
81  {
82  const std::set<std::string> & out = getVectorPostprocessorOutput();
83  for (const auto & vpp_name : out)
84  {
85  std::string short_name = MooseUtils::shortName(vpp_name);
86  std::string out_latest = _file_base + "_" + short_name + "_LATEST.csv";
87  std::string out_final = _file_base + "_" + short_name + "_FINAL.csv";
88  MooseUtils::clearSymlink(out_latest);
89  MooseUtils::clearSymlink(out_final);
90  }
91  }
92 }
93 
94 std::string
96 {
97  return _file_base + ".csv";
98 }
99 
100 void
102 {
104  _write_all_table = true;
105 }
106 
107 void
109 {
111  _write_all_table = true;
112 }
113 
114 void
116 {
118  _write_vector_table = true;
119 }
120 
121 std::string
122 CSV::getVectorPostprocessorFileName(const std::string & vpp_name, bool include_time_step)
123 {
124  std::ostringstream file_name;
125  file_name << _file_base;
126 
127  auto short_name = MooseUtils::shortName(vpp_name);
128  if (short_name.size())
129  file_name << '_' << short_name;
130 
131  if (include_time_step)
132  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
133  << std::right << timeStep();
134  file_name << ".csv";
135 
136  return file_name.str();
137 }
138 
139 void
141 {
142  // Call the base class output (populates tables)
144 
145  // Print the table containing all the data to a file
146  if (_write_all_table && !_all_data_table.empty() && processor_id() == 0)
147  {
148  if (_sort_columns)
151  }
152 
153  const auto & vpp_data = _problem_ptr->getVectorPostprocessorData();
154 
155  // Output each VectorPostprocessor's data to a file
156  if (_write_vector_table && processor_id() == 0)
157  {
158 
159  // The VPP table will not write the same data twice, so to get the symlinks correct
160  // for EXEC_FINAL (when other flags exist) whenever files are written the names must
161  // be stored. These stored names are then used outside of this loop when the EXEC_FINAL call is
162  // made.
163  _latest_vpp_filenames.clear();
164 
165  for (auto & it : _vector_postprocessor_tables)
166  {
167  const auto & vpp_name = it.first;
168  it.second.setDelimiter(_delimiter);
169  it.second.setPrecision(_precision);
170  if (_sort_columns)
171  it.second.sortColumns();
172 
173  auto include_time_suffix = !vpp_data.containsCompleteHistory(vpp_name);
174  std::string fname = getVectorPostprocessorFileName(vpp_name, include_time_suffix);
175  std::string fprefix = getVectorPostprocessorFilePrefix(vpp_name);
176  _latest_vpp_filenames.emplace_back(fname, fprefix);
177  it.second.printCSV(fname, 1, _align);
178 
180  {
181  std::string out_latest = fprefix + "_LATEST.csv";
182  MooseUtils::createSymlink(fname, out_latest);
183  }
184 
185  if (_time_data)
186  _vector_postprocessor_time_tables[vpp_name].printCSV(fprefix + "_time.csv");
187  }
188  }
189 
190  if (type == EXEC_FINAL && _create_final_symlink && processor_id() == 0)
191  {
192  for (const auto & name_pair : _latest_vpp_filenames)
193  {
194  std::string out_final = name_pair.second + "_FINAL.csv";
195  MooseUtils::createSymlink(name_pair.first, out_final);
196  }
197  }
198 
199  // Re-set write flags
200  _write_all_table = false;
201  _write_vector_table = false;
202 }
203 
204 std::string
205 CSV::getVectorPostprocessorFilePrefix(const std::string & vpp_name)
206 {
207  return _file_base + "_" + MooseUtils::shortName(vpp_name);
208 }
const VectorPostprocessorData & getVectorPostprocessorData() const
InputParameters validParams< CSV >()
Definition: CSV.C:19
unsigned int _precision
Decimal digits per number in the CSV file.
Definition: CSV.h:83
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:140
bool _write_all_table
Flag for writing 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:95
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:81
void setPrecision(unsigned int precision)
By default printCSV prints output to a precision of 14, this allows this to be changed.
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
const std::set< std::string > & getVectorPostprocessorOutput()
The list of VectorPostprocessor names that are set for output.
std::vector< std::pair< std::string, std::string > > _latest_vpp_filenames
Current list of VPP filenames for creating _LATEST/_FINAL symlinks.
Definition: CSV.h:109
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:363
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:110
const std::string & type() const
Get the type of this object.
Definition: MooseObject.h:53
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:87
bool _write_vector_table
Flag for writing 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:108
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.
Based class for adding basic filename support to output base class.
Definition: CSV.h:26
bool _recovering
Flag indicating MOOSE is recovering via –recover command-line option.
Definition: CSV.h:98
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
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:115
const bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:84
virtual void outputScalarVariables() override
Sets the write flag and calls TableOutput::outputScalarVariables()
Definition: CSV.C:101
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:86
FormattedTable & _all_data_table
Table containing postprocessor values and scalar aux variables.
Definition: TableOutput.h:78
void clearSymlink(const std::string &link)
Remove a symbolic link, if the given filename is a link.
Definition: MooseUtils.C:777
void createSymlink(const std::string &target, const std::string &link)
Create a symbolic link, if the link already exists it is replaced.
Definition: MooseUtils.C:768
MatType type
virtual void initialSetup()
Initialization method.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
std::string getVectorPostprocessorFilePrefix(const std::string &vpp_name)
Returns the filename without the time/timestep information.
Definition: CSV.C:205
std::string getVectorPostprocessorFileName(const std::string &vpp_name, bool include_time_step)
Generates a filename pattern for Vectorpostprocessors filebase + VPP name + time step + "...
Definition: CSV.C:122
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:33
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...
registerMooseObject("MooseApp", CSV)
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...
void sortColumns()
Sorts columns alphabetically.
bool _create_latest_symlink
Flag for creating a _LATEST symlink.
Definition: CSV.h:104
bool _create_final_symlink
Flag for creating a _FINAL symlink.
Definition: CSV.h:101
virtual int timeStep()
Get the current time step.
Definition: Output.C:259
CSV(const InputParameters &parameters)
Class constructor.
Definition: CSV.C:50
InputParameters validParams< TableOutput >()
Definition: TableOutput.C:27
void setDelimiter(std::string delimiter)
By default printCSV places "," between each entry, this allows this to be changed.
const ExecFlagType EXEC_FINAL
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:69
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:147
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:65
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:72