Line data Source code
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 :
17 : InputParameters
18 33459 : CSV::validParams()
19 : {
20 : // Get the parameters from the parent object
21 33459 : InputParameters params = TableOutput::validParams();
22 66918 : params.addClassDescription("Output for postprocessors, vector postprocessors, and scalar "
23 : "variables using comma seperated values (CSV).");
24 :
25 66918 : addMultiAppFixedPointIterationEndExecFlag(params, "execute_on");
26 66918 : addMultiAppFixedPointIterationEndExecFlag(params, "execute_postprocessors_on");
27 66918 : addMultiAppFixedPointIterationEndExecFlag(params, "execute_scalars_on");
28 :
29 133836 : params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
30 :
31 : // Options for aligning csv output with whitespace padding
32 100377 : params.addParam<bool>(
33 : "align",
34 66918 : false,
35 : "Align the outputted csv data by padding the numbers with trailing whitespace");
36 133836 : params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
37 133836 : params.addParam<unsigned int>("precision", 14, "Set the output precision");
38 100377 : params.addParam<bool>("create_final_symlink",
39 66918 : false,
40 : "Enable/disable the creation of a _FINAL symlink for vector postprocessor "
41 : "data with 'execute_on' includes 'FINAL'.");
42 100377 : params.addParam<bool>(
43 : "create_latest_symlink",
44 66918 : false,
45 : "Enable/disable the creation of a _LATEST symlink for vector postprocessor data.");
46 :
47 133836 : params.addParamNamesToGroup("sort_columns align delimiter precision", "Table formatting");
48 133836 : params.addParamNamesToGroup("create_latest_symlink create_final_symlink", "Symbolic links");
49 : // Suppress unused parameters
50 33459 : params.suppressParameter<unsigned int>("padding");
51 :
52 : // Done
53 33459 : return params;
54 0 : }
55 :
56 15120 : CSV::CSV(const InputParameters & parameters)
57 : : TableOutput(parameters),
58 15120 : _align(getParam<bool>("align")),
59 30240 : _precision(getParam<unsigned int>("precision")),
60 30240 : _delimiter(getParam<std::string>("delimiter")),
61 15120 : _write_all_table(false),
62 15120 : _write_vector_table(false),
63 30240 : _sort_columns(getParam<bool>("sort_columns")),
64 15120 : _recovering(_app.isRecovering()),
65 30240 : _create_final_symlink(getParam<bool>("create_final_symlink")),
66 60480 : _create_latest_symlink(getParam<bool>("create_latest_symlink"))
67 : {
68 15120 : }
69 :
70 : void
71 15000 : CSV::initialSetup()
72 : {
73 : // Call the base class method
74 15000 : TableOutput::initialSetup();
75 :
76 : // Set the delimiter
77 15000 : _all_data_table.setDelimiter(_delimiter);
78 :
79 : // Set the precision
80 15000 : _all_data_table.setPrecision(_precision);
81 :
82 15000 : if (_recovering)
83 970 : _all_data_table.append(true);
84 :
85 : // Clear any existing symbolic links to LATEST and/or FINAL
86 15000 : if (processor_id() == 0)
87 : {
88 11331 : const std::set<std::string> & out = getVectorPostprocessorOutput();
89 14210 : for (const auto & vpp_name : out)
90 : {
91 2879 : std::string short_name = MooseUtils::shortName(vpp_name);
92 2879 : std::string out_latest = _file_base + "_" + short_name + "_LATEST.csv";
93 2879 : std::string out_final = _file_base + "_" + short_name + "_FINAL.csv";
94 2879 : MooseUtils::clearSymlink(out_latest);
95 2879 : MooseUtils::clearSymlink(out_final);
96 2879 : }
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 30000 : const auto pp_execute_on = advancedExecuteOn().find("postprocessors")->second;
105 30000 : const auto scalar_execute_on = advancedExecuteOn().find("scalars")->second;
106 30000 : const auto reporter_execute_on = advancedExecuteOn().find("reporters")->second;
107 15000 : const auto n_pps = getPostprocessorOutput().size();
108 15000 : const auto n_scalars = getScalarOutput().size();
109 15000 : const auto n_reporters = getReporterOutput().size();
110 15000 : const bool pp_active = n_pps > 0 && !pp_execute_on.isValueSet(EXEC_NONE);
111 15000 : const bool scalar_active = n_scalars > 0 && !scalar_execute_on.isValueSet(EXEC_NONE);
112 15000 : const bool reporter_active = n_reporters > 0 && !reporter_execute_on.isValueSet(EXEC_NONE);
113 29997 : if ((pp_execute_on != scalar_execute_on && pp_active && scalar_active) ||
114 14997 : (pp_execute_on != reporter_execute_on && pp_active && reporter_active))
115 6 : mooseError("The parameters 'execute_postprocessors_on', 'execute_scalars_on', and "
116 : "'execute_reporters_on' must be the same for CSV output.");
117 14994 : }
118 :
119 : std::string
120 64587 : CSV::filename()
121 : {
122 64587 : return _file_base + ".csv";
123 : }
124 :
125 : void
126 9484 : CSV::outputScalarVariables()
127 : {
128 9484 : TableOutput::outputScalarVariables();
129 9484 : _write_all_table = true;
130 9484 : }
131 :
132 : void
133 62909 : CSV::outputPostprocessors()
134 : {
135 62909 : TableOutput::outputPostprocessors();
136 62909 : _write_all_table = true;
137 62909 : }
138 :
139 : void
140 7848 : CSV::outputVectorPostprocessors()
141 : {
142 7848 : TableOutput::outputVectorPostprocessors();
143 7848 : _write_vector_table = true;
144 7848 : }
145 :
146 : void
147 1161 : CSV::outputReporters()
148 : {
149 1161 : TableOutput::outputReporters();
150 1161 : _write_all_table = true;
151 1161 : _write_vector_table = true;
152 1161 : }
153 :
154 : std::string
155 7028 : CSV::getVectorPostprocessorFileName(const std::string & vpp_name,
156 : bool include_time_step,
157 : bool is_distributed)
158 : {
159 7028 : std::ostringstream file_name;
160 7028 : file_name << _file_base;
161 :
162 7028 : auto short_name = MooseUtils::shortName(vpp_name);
163 7028 : if (short_name.size())
164 7028 : file_name << '_' << short_name;
165 :
166 7028 : if (include_time_step)
167 : {
168 6796 : file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
169 6796 : << std::right << timeStep();
170 :
171 6796 : if (_current_execute_flag == EXEC_NONLINEAR || _current_execute_flag == EXEC_LINEAR)
172 : {
173 946 : file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
174 946 : << std::right << _nonlinear_iter;
175 : }
176 6796 : if (_current_execute_flag == EXEC_LINEAR)
177 : {
178 789 : file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
179 789 : << std::right << _linear_iter;
180 : }
181 : }
182 :
183 7028 : file_name << ".csv";
184 :
185 7028 : if (is_distributed)
186 : {
187 144 : int digits = MooseUtils::numDigits(n_processors());
188 144 : file_name << "." << std::setw(digits) << std::setfill('0') << processor_id();
189 : }
190 14056 : return file_name.str();
191 7028 : }
192 :
193 : void
194 73347 : CSV::output()
195 : {
196 : // Call the base class output (populates tables)
197 73347 : TableOutput::output();
198 :
199 : // Print the table containing all the data to a file
200 73347 : if (_write_all_table && !_all_data_table.empty() && processor_id() == 0)
201 : {
202 49587 : if (_sort_columns)
203 8 : _all_data_table.sortColumns();
204 49587 : _all_data_table.printCSV(filename(), 1, _align);
205 : }
206 :
207 : // Output each VectorPostprocessor's data to a file
208 73347 : if (_write_vector_table)
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 8795 : _latest_vpp_filenames.clear();
215 :
216 18420 : for (auto & it : _vector_postprocessor_tables)
217 : {
218 9625 : const auto & vpp_name = it.first;
219 9625 : it.second.setDelimiter(_delimiter);
220 9625 : it.second.setPrecision(_precision);
221 9625 : if (_sort_columns)
222 0 : it.second.sortColumns();
223 :
224 9625 : bool include_time_suffix = true;
225 9625 : bool is_distributed = _reporter_data.hasReporterWithMode(vpp_name, REPORTER_MODE_DISTRIBUTED);
226 9625 : if (hasVectorPostprocessorByName(vpp_name))
227 : {
228 : const VectorPostprocessor & vpp_obj =
229 8859 : _problem_ptr->getVectorPostprocessorObjectByName(vpp_name);
230 8859 : include_time_suffix = !vpp_obj.containsCompleteHistory();
231 : }
232 :
233 9625 : if (is_distributed || processor_id() == 0)
234 : {
235 : std::string fname =
236 7028 : getVectorPostprocessorFileName(vpp_name, include_time_suffix, is_distributed);
237 7028 : std::string fprefix = getVectorPostprocessorFilePrefix(vpp_name);
238 :
239 7028 : _latest_vpp_filenames.emplace_back(fname, fprefix, is_distributed);
240 :
241 7028 : it.second.printCSV(fname, 1, _align);
242 :
243 7028 : if (_create_latest_symlink)
244 : {
245 104 : std::ostringstream out_latest;
246 104 : out_latest << fprefix << "_LATEST.csv";
247 104 : if (is_distributed)
248 : {
249 24 : int digits = MooseUtils::numDigits(n_processors());
250 24 : out_latest << "." << std::setw(digits) << std::setfill('0') << processor_id();
251 : }
252 104 : MooseUtils::createSymlink(fname, out_latest.str());
253 104 : }
254 :
255 7028 : if (_time_data)
256 88 : _vector_postprocessor_time_tables[vpp_name].printCSV(fprefix + "_time.csv");
257 7028 : }
258 : }
259 : }
260 :
261 73347 : if (_current_execute_flag == EXEC_FINAL && _create_final_symlink)
262 : {
263 86 : for (const auto & name_tuple : _latest_vpp_filenames)
264 : {
265 40 : std::ostringstream out_final;
266 40 : out_final << std::get<1>(name_tuple) << "_FINAL.csv";
267 40 : if (std::get<2>(name_tuple))
268 : {
269 24 : int digits = MooseUtils::numDigits(n_processors());
270 24 : out_final << "." << std::setw(digits) << std::setfill('0') << processor_id();
271 24 : MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
272 : }
273 16 : else if (processor_id() == 0)
274 16 : MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
275 40 : }
276 : }
277 :
278 : // Re-set write flags
279 73347 : _write_all_table = false;
280 73347 : _write_vector_table = false;
281 73347 : }
282 :
283 : std::string
284 7028 : CSV::getVectorPostprocessorFilePrefix(const std::string & vpp_name)
285 : {
286 7028 : return _file_base + "_" + MooseUtils::shortName(vpp_name);
287 : }
|