https://mooseframework.inl.gov
CSV.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 // 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 
25  addMultiAppFixedPointIterationEndExecFlag(params, "execute_on");
26  addMultiAppFixedPointIterationEndExecFlag(params, "execute_postprocessors_on");
27  addMultiAppFixedPointIterationEndExecFlag(params, "execute_scalars_on");
28 
29  params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
30 
31  // Options for aligning csv output with whitespace padding
32  params.addParam<bool>(
33  "align",
34  false,
35  "Align the outputted csv data by padding the numbers with trailing whitespace");
36  params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
37  params.addParam<unsigned int>("precision", 14, "Set the output precision");
38  params.addParam<bool>("create_final_symlink",
39  false,
40  "Enable/disable the creation of a _FINAL symlink for vector postprocessor "
41  "data with 'execute_on' includes 'FINAL'.");
42  params.addParam<bool>(
43  "create_latest_symlink",
44  false,
45  "Enable/disable the creation of a _LATEST symlink for vector postprocessor data.");
46 
47  params.addParamNamesToGroup("sort_columns align delimiter precision", "Table formatting");
48  params.addParamNamesToGroup("create_latest_symlink create_final_symlink", "Symbolic links");
49  // Suppress unused parameters
50  params.suppressParameter<unsigned int>("padding");
51 
52  // Done
53  return params;
54 }
55 
56 CSV::CSV(const InputParameters & parameters)
57  : TableOutput(parameters),
58  _align(getParam<bool>("align")),
59  _precision(getParam<unsigned int>("precision")),
60  _delimiter(getParam<std::string>("delimiter")),
61  _write_all_table(false),
62  _write_vector_table(false),
63  _sort_columns(getParam<bool>("sort_columns")),
64  _recovering(_app.isRecovering()),
65  _create_final_symlink(getParam<bool>("create_final_symlink")),
66  _create_latest_symlink(getParam<bool>("create_latest_symlink"))
67 {
68 }
69 
70 void
72 {
73  // Call the base class method
75 
76  // Set the delimiter
78 
79  // Set the precision
81 
82  if (_recovering)
83  _all_data_table.append(true);
84 
85  // Clear any existing symbolic links to LATEST and/or FINAL
86  if (processor_id() == 0)
87  {
88  const std::set<std::string> & out = getVectorPostprocessorOutput();
89  for (const auto & vpp_name : out)
90  {
91  std::string short_name = MooseUtils::shortName(vpp_name);
92  std::string out_latest = _file_base + "_" + short_name + "_LATEST.csv";
93  std::string out_final = _file_base + "_" + short_name + "_FINAL.csv";
94  MooseUtils::clearSymlink(out_latest);
95  MooseUtils::clearSymlink(out_final);
96  }
97  }
98 
99  // See https://github.com/idaholab/moose/issues/25211.
100  mooseAssert(advancedExecuteOn().contains("postprocessors"),
101  "Missing expected postprocessors key");
102  mooseAssert(advancedExecuteOn().contains("scalars"), "Missing expected scalars key");
103  mooseAssert(advancedExecuteOn().contains("reporters"), "Missing expected reporters key");
104  const auto pp_execute_on = advancedExecuteOn().find("postprocessors")->second;
105  const auto scalar_execute_on = advancedExecuteOn().find("scalars")->second;
106  const auto reporter_execute_on = advancedExecuteOn().find("reporters")->second;
107  const auto n_pps = getPostprocessorOutput().size();
108  const auto n_scalars = getScalarOutput().size();
109  const auto n_reporters = getReporterOutput().size();
110  const bool pp_active = n_pps > 0 && !pp_execute_on.isValueSet(EXEC_NONE);
111  const bool scalar_active = n_scalars > 0 && !scalar_execute_on.isValueSet(EXEC_NONE);
112  const bool reporter_active = n_reporters > 0 && !reporter_execute_on.isValueSet(EXEC_NONE);
113  if ((pp_execute_on != scalar_execute_on && pp_active && scalar_active) ||
114  (pp_execute_on != reporter_execute_on && pp_active && reporter_active))
115  mooseError("The parameters 'execute_postprocessors_on', 'execute_scalars_on', and "
116  "'execute_reporters_on' must be the same for CSV output.");
117 }
118 
119 std::string
121 {
122  return _file_base + ".csv";
123 }
124 
125 void
127 {
129  _write_all_table = true;
130 }
131 
132 void
134 {
136  _write_all_table = true;
137 }
138 
139 void
141 {
143  _write_vector_table = true;
144 }
145 
146 void
148 {
150  _write_all_table = true;
151  _write_vector_table = true;
152 }
153 
154 std::string
155 CSV::getVectorPostprocessorFileName(const std::string & vpp_name,
156  bool include_time_step,
157  bool is_distributed)
158 {
159  std::ostringstream file_name;
160  file_name << _file_base;
161 
162  auto short_name = MooseUtils::shortName(vpp_name);
163  if (short_name.size())
164  file_name << '_' << short_name;
165 
166  if (include_time_step)
167  {
168  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
169  << std::right << timeStep();
170 
172  {
173  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
174  << std::right << _nonlinear_iter;
175  }
177  {
178  file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
179  << std::right << _linear_iter;
180  }
181  }
182 
183  file_name << ".csv";
184 
185  if (is_distributed)
186  {
187  int digits = MooseUtils::numDigits(n_processors());
188  file_name << "." << std::setw(digits) << std::setfill('0') << processor_id();
189  }
190  return file_name.str();
191 }
192 
193 void
195 {
196  // Call the base class output (populates tables)
198 
199  // Print the table containing all the data to a file
201  {
202  if (_sort_columns)
205  }
206 
207  // Output each VectorPostprocessor's data to a file
209  {
210  // The VPP table will not write the same data twice, so to get the symlinks correct
211  // for EXEC_FINAL (when other flags exist) whenever files are written the names must
212  // be stored. These stored names are then used outside of this loop when the EXEC_FINAL call is
213  // made.
214  _latest_vpp_filenames.clear();
215 
216  for (auto & it : _vector_postprocessor_tables)
217  {
218  const auto & vpp_name = it.first;
219  it.second.setDelimiter(_delimiter);
220  it.second.setPrecision(_precision);
221  if (_sort_columns)
222  it.second.sortColumns();
223 
224  bool include_time_suffix = true;
225  bool is_distributed = _reporter_data.hasReporterWithMode(vpp_name, REPORTER_MODE_DISTRIBUTED);
226  if (hasVectorPostprocessorByName(vpp_name))
227  {
228  const VectorPostprocessor & vpp_obj =
230  include_time_suffix = !vpp_obj.containsCompleteHistory();
231  }
232 
233  if (is_distributed || processor_id() == 0)
234  {
235  std::string fname =
236  getVectorPostprocessorFileName(vpp_name, include_time_suffix, is_distributed);
237  std::string fprefix = getVectorPostprocessorFilePrefix(vpp_name);
238 
239  _latest_vpp_filenames.emplace_back(fname, fprefix, is_distributed);
240 
241  it.second.printCSV(fname, 1, _align);
242 
244  {
245  std::ostringstream out_latest;
246  out_latest << fprefix << "_LATEST.csv";
247  if (is_distributed)
248  {
249  int digits = MooseUtils::numDigits(n_processors());
250  out_latest << "." << std::setw(digits) << std::setfill('0') << processor_id();
251  }
252  MooseUtils::createSymlink(fname, out_latest.str());
253  }
254 
255  if (_time_data)
256  _vector_postprocessor_time_tables[vpp_name].printCSV(fprefix + "_time.csv");
257  }
258  }
259  }
260 
262  {
263  for (const auto & name_tuple : _latest_vpp_filenames)
264  {
265  std::ostringstream out_final;
266  out_final << std::get<1>(name_tuple) << "_FINAL.csv";
267  if (std::get<2>(name_tuple))
268  {
269  int digits = MooseUtils::numDigits(n_processors());
270  out_final << "." << std::setw(digits) << std::setfill('0') << processor_id();
271  MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
272  }
273  else if (processor_id() == 0)
274  MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
275  }
276  }
277 
278  // Re-set write flags
279  _write_all_table = false;
280  _write_vector_table = false;
281 }
282 
283 std::string
284 CSV::getVectorPostprocessorFilePrefix(const std::string & vpp_name)
285 {
286  return _file_base + "_" + MooseUtils::shortName(vpp_name);
287 }
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:120
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. ...
static void addMultiAppFixedPointIterationEndExecFlag(InputParameters &params, const std::string &param)
Adds the exec flag MULTIAPP_FIXED_POINT_ITERATION_END to a parameter.
Definition: TableOutput.C:71
virtual void outputReporters() override
Sets the write flag and calls TableOutput::outputVectorPostprocessors()
Definition: CSV.C:147
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:608
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:191
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:133
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:211
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:185
virtual void outputReporters() override
Populates the tables with Reporter values.
Definition: TableOutput.C:160
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:140
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:113
virtual void outputScalarVariables() override
Sets the write flag and calls TableOutput::outputScalarVariables()
Definition: CSV.C:126
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:109
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:104
void clearSymlink(const std::string &link)
Remove a symbolic link, if the given filename is a link.
Definition: MooseUtils.C:1179
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:31
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:1166
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:948
std::string getVectorPostprocessorFilePrefix(const std::string &vpp_name)
Returns the filename without the time/timestep information.
Definition: CSV.C:284
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:155
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 optional parameter and a documentation string to the InputParameters object...
virtual void output() override
Output the table to a *.csv file.
Definition: CSV.C:194
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:181
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:389
CSV(const InputParameters &parameters)
Class constructor.
Definition: CSV.C:56
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:44
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:92
void ErrorVector unsigned int
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:222
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:71
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:95
static InputParameters validParams()
Definition: TableOutput.C:26