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 
19 {
20  // Get the parameters from the parent object
22  params.addClassDescription("Output for postprocessors, vector postprocessors, and scalar "
23  "variables using comma seperated values (CSV).");
24  params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
25 
26  // Options for aligning csv output with whitespace padding
27  params.addParam<bool>(
28  "align",
29  false,
30  "Align the outputted csv data by padding the numbers with trailing whitespace");
31  params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
32  params.addParam<unsigned int>("precision", 14, "Set the output precision");
33  params.addParam<bool>("create_final_symlink",
34  false,
35  "Enable/disable the creation of a _FINAL symlink for vector postprocessor "
36  "data with 'execute_on' includes 'FINAL'.");
37  params.addParam<bool>(
38  "create_latest_symlink",
39  false,
40  "Enable/disable the creation of a _LATEST symlink for vector postprocessor data.");
41 
42  params.addParamNamesToGroup("sort_columns align delimiter precision", "Table formatting");
43  params.addParamNamesToGroup("create_latest_symlink create_final_symlink", "Symbolic links");
44  // Suppress unused parameters
45  params.suppressParameter<unsigned int>("padding");
46 
47  // Done
48  return params;
49 }
50 
51 CSV::CSV(const InputParameters & parameters)
52  : TableOutput(parameters),
53  _align(getParam<bool>("align")),
54  _precision(getParam<unsigned int>("precision")),
55  _delimiter(getParam<std::string>("delimiter")),
56  _write_all_table(false),
57  _write_vector_table(false),
58  _sort_columns(getParam<bool>("sort_columns")),
59  _recovering(_app.isRecovering()),
60  _create_final_symlink(getParam<bool>("create_final_symlink")),
61  _create_latest_symlink(getParam<bool>("create_latest_symlink"))
62 {
63 }
64 
65 void
67 {
68  // Call the base class method
70 
71  // Set the delimiter
73 
74  // Set the precision
76 
77  if (_recovering)
78  _all_data_table.append(true);
79 
80  // Clear any existing symbolic links to LATEST and/or FINAL
81  if (processor_id() == 0)
82  {
83  const std::set<std::string> & out = getVectorPostprocessorOutput();
84  for (const auto & vpp_name : out)
85  {
86  std::string short_name = MooseUtils::shortName(vpp_name);
87  std::string out_latest = _file_base + "_" + short_name + "_LATEST.csv";
88  std::string out_final = _file_base + "_" + short_name + "_FINAL.csv";
89  MooseUtils::clearSymlink(out_latest);
90  MooseUtils::clearSymlink(out_final);
91  }
92  }
93 
94  // See https://github.com/idaholab/moose/issues/25211.
95  mooseAssert(advancedExecuteOn().contains("postprocessors"),
96  "Missing expected postprocessors key");
97  mooseAssert(advancedExecuteOn().contains("scalars"), "Missing expected scalars key");
98  mooseAssert(advancedExecuteOn().contains("reporters"), "Missing expected reporters key");
99  const auto pp_execute_on = advancedExecuteOn().find("postprocessors")->second;
100  const auto scalar_execute_on = advancedExecuteOn().find("scalars")->second;
101  const auto reporter_execute_on = advancedExecuteOn().find("reporters")->second;
102  const auto n_pps = getPostprocessorOutput().size();
103  const auto n_scalars = getScalarOutput().size();
104  const auto n_reporters = getReporterOutput().size();
105  const bool pp_active = n_pps > 0 && !pp_execute_on.contains(EXEC_NONE);
106  const bool scalar_active = n_scalars > 0 && !scalar_execute_on.contains(EXEC_NONE);
107  const bool reporter_active = n_reporters > 0 && !reporter_execute_on.contains(EXEC_NONE);
108  if ((pp_execute_on != scalar_execute_on && pp_active && scalar_active) ||
109  (pp_execute_on != reporter_execute_on && pp_active && reporter_active))
110  mooseError("The parameters 'execute_postprocessors_on', 'execute_scalars_on', and "
111  "'execute_reporters_on' must be the same for CSV output.");
112 }
113 
114 std::string
116 {
117  return _file_base + ".csv";
118 }
119 
120 void
122 {
124  _write_all_table = true;
125 }
126 
127 void
129 {
131  _write_all_table = true;
132 }
133 
134 void
136 {
138  _write_vector_table = true;
139 }
140 
141 void
143 {
145  _write_all_table = true;
146  _write_vector_table = true;
147 }
148 
149 std::string
150 CSV::getVectorPostprocessorFileName(const std::string & vpp_name,
151  bool include_time_step,
152  bool is_distributed)
153 {
154  std::ostringstream file_name;
155  file_name << _file_base;
156 
157  auto short_name = MooseUtils::shortName(vpp_name);
158  if (short_name.size())
159  file_name << '_' << short_name;
160 
161  if (include_time_step)
162  {
163  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
164  << std::right << timeStep();
165 
167  {
168  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
169  << std::right << _nonlinear_iter;
170  }
172  {
173  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
174  << std::right << _linear_iter;
175  }
176  }
177 
178  file_name << ".csv";
179 
180  if (is_distributed)
181  {
182  int digits = MooseUtils::numDigits(n_processors());
183  file_name << "." << std::setw(digits) << std::setfill('0') << processor_id();
184  }
185  return file_name.str();
186 }
187 
188 void
190 {
191  // Call the base class output (populates tables)
193 
194  // Print the table containing all the data to a file
196  {
197  if (_sort_columns)
200  }
201 
202  // Output each VectorPostprocessor's data to a file
204  {
205  // The VPP table will not write the same data twice, so to get the symlinks correct
206  // for EXEC_FINAL (when other flags exist) whenever files are written the names must
207  // be stored. These stored names are then used outside of this loop when the EXEC_FINAL call is
208  // made.
209  _latest_vpp_filenames.clear();
210 
211  for (auto & it : _vector_postprocessor_tables)
212  {
213  const auto & vpp_name = it.first;
214  it.second.setDelimiter(_delimiter);
215  it.second.setPrecision(_precision);
216  if (_sort_columns)
217  it.second.sortColumns();
218 
219  bool include_time_suffix = true;
220  bool is_distributed = _reporter_data.hasReporterWithMode(vpp_name, REPORTER_MODE_DISTRIBUTED);
221  if (hasVectorPostprocessorByName(vpp_name))
222  {
223  const VectorPostprocessor & vpp_obj =
225  include_time_suffix = !vpp_obj.containsCompleteHistory();
226  }
227 
228  if (is_distributed || processor_id() == 0)
229  {
230  std::string fname =
231  getVectorPostprocessorFileName(vpp_name, include_time_suffix, is_distributed);
232  std::string fprefix = getVectorPostprocessorFilePrefix(vpp_name);
233 
234  _latest_vpp_filenames.emplace_back(fname, fprefix, is_distributed);
235 
236  it.second.printCSV(fname, 1, _align);
237 
239  {
240  std::ostringstream out_latest;
241  out_latest << fprefix << "_LATEST.csv";
242  if (is_distributed)
243  {
244  int digits = MooseUtils::numDigits(n_processors());
245  out_latest << "." << std::setw(digits) << std::setfill('0') << processor_id();
246  }
247  MooseUtils::createSymlink(fname, out_latest.str());
248  }
249 
250  if (_time_data)
251  _vector_postprocessor_time_tables[vpp_name].printCSV(fprefix + "_time.csv");
252  }
253  }
254  }
255 
257  {
258  for (const auto & name_tuple : _latest_vpp_filenames)
259  {
260  std::ostringstream out_final;
261  out_final << std::get<1>(name_tuple) << "_FINAL.csv";
262  if (std::get<2>(name_tuple))
263  {
264  int digits = MooseUtils::numDigits(n_processors());
265  out_final << "." << std::setw(digits) << std::setfill('0') << processor_id();
266  MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
267  }
268  else if (processor_id() == 0)
269  MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
270  }
271  }
272 
273  // Re-set write flags
274  _write_all_table = false;
275  _write_vector_table = false;
276 }
277 
278 std::string
279 CSV::getVectorPostprocessorFilePrefix(const std::string & vpp_name)
280 {
281  return _file_base + "_" + MooseUtils::shortName(vpp_name);
282 }
const std::set< std::string > & getPostprocessorOutput()
The list of postprocessor names that are set for output.
unsigned int _precision
Decimal digits per number in the CSV file.
Definition: CSV.h:88
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...
const ExecFlagType EXEC_NONE
Definition: Moose.C:27
bool _write_all_table
Flag for writing scalar and/or postprocessor data.
Definition: CSV.h:94
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:115
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:89
void setPrecision(unsigned int precision)
By default printCSV prints output to a precision of 14, this allows this to be changed.
virtual void output()
A single call to this function should output all the necessary data for a single timestep.
std::vector< std::tuple< std::string, std::string, bool > > _latest_vpp_filenames
Current list of VPP filenames for creating _LATEST/_FINAL symlinks.
Definition: CSV.h:114
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
virtual void outputReporters() override
Sets the write flag and calls TableOutput::outputVectorPostprocessors()
Definition: CSV.C:142
const std::set< std::string > & getVectorPostprocessorOutput()
The list of VectorPostprocessor names that are set for output.
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:595
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:142
const OutputOnWarehouse & advancedExecuteOn() const
Get the current advanced &#39;execute_on&#39; selections for display.
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:83
const std::set< std::string > & getReporterOutput()
The list of Reporter names that are set for output.
bool _write_vector_table
Flag for writing vector postprocessor data.
Definition: CSV.h:97
processor_id_type n_processors() const
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:128
PetscInt _linear_iter
Current linear iteration returned from PETSc.
Definition: PetscOutput.h:87
bool containsCompleteHistory() const
Return whether or not this VectorPostprocessor contains complete history.
static InputParameters validParams()
Definition: CSV.C:18
const std::set< std::string > & getScalarOutput()
The list of scalar variables names that are set for output.
bool _align
Flag for aligning data in .csv file.
Definition: CSV.h:85
Based class for adding basic filename support to output base class.
Definition: CSV.h:20
ExecFlagType _current_execute_flag
Current execute on flag.
Definition: Output.h:205
bool _recovering
Flag indicating MOOSE is recovering via –recover command-line option.
Definition: CSV.h:103
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:179
virtual void outputReporters() override
Populates the tables with Reporter values.
Definition: TableOutput.C:111
std::string _delimiter
The delimiter used when writing the CSV file.
Definition: CSV.h:91
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
Return the VPP object given the name.
virtual void outputVectorPostprocessors() override
Sets the write flag and calls TableOutput::outputVectorPostprocessors()
Definition: CSV.C:135
const ReporterMode REPORTER_MODE_DISTRIBUTED
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
const bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:91
virtual void outputScalarVariables() override
Sets the write flag and calls TableOutput::outputScalarVariables()
Definition: CSV.C:121
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:86
const ReporterData & _reporter_data
Storage for Reporter values.
FormattedTable & _all_data_table
Table containing postprocessor values, scalar aux variables, and Real Reporters.
Definition: TableOutput.h:85
void clearSymlink(const std::string &link)
Remove a symbolic link, if the given filename is a link.
Definition: MooseUtils.C:1165
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:30
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:1152
bool hasVectorPostprocessorByName(const VectorPostprocessorName &name, const std::string &vector_name) const
Determine if the VectorPostprocessor data exists by name.
virtual void initialSetup()
Call init() method on setup.
int numDigits(const T &num)
Return the number of digits for a number.
Definition: MooseUtils.h:940
std::string getVectorPostprocessorFilePrefix(const std::string &vpp_name)
Returns the filename without the time/timestep information.
Definition: CSV.C:279
OStreamProxy out
const bool _sort_columns
Flag for sorting column names.
Definition: CSV.h:100
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
Base class for scalar variables and postprocessors output objects.
Definition: TableOutput.h:28
std::string getVectorPostprocessorFileName(const std::string &vpp_name, bool include_time_step, bool is_distributed)
Generates a filename pattern for Vectorpostprocessors filebase + VPP name + time step + "...
Definition: CSV.C:150
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...
virtual void output() override
Output the table to a *.csv file.
Definition: CSV.C:189
void sortColumns()
Sorts columns alphabetically.
bool hasReporterWithMode(const std::string &obj_name, const ReporterMode &mode) const
Return true if the supplied mode exists in the produced Reporter values.
Definition: ReporterData.C:156
bool _create_latest_symlink
Flag for creating a _LATEST symlink.
Definition: CSV.h:109
bool _create_final_symlink
Flag for creating a _FINAL symlink.
Definition: CSV.h:106
virtual int timeStep()
Get the current time step.
Definition: Output.C:387
CSV(const InputParameters &parameters)
Class constructor.
Definition: CSV.C:51
processor_id_type processor_id() const
Base class for Postprocessors that produce a vector of values.
std::map< std::string, T >::iterator find(const std::string &name)
void setDelimiter(std::string delimiter)
By default printCSV places "," between each entry, this allows this to be changed.
const ExecFlagType EXEC_FINAL
Definition: Moose.C:38
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:73
void ErrorVector unsigned int
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:173
PetscInt _nonlinear_iter
Current non-linear iteration returned from PETSc.
Definition: PetscOutput.h:84
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
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:66
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:76
static InputParameters validParams()
Definition: TableOutput.C:26