forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV.C
233 lines (195 loc) · 6.73 KB
/
CSV.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
// Moose includes
#include "CSV.h"
#include "FEProblem.h"
#include "MooseApp.h"
registerMooseObject("MooseApp", CSV);
defineLegacyParams(CSV);
InputParameters
CSV::validParams()
{
// Get the parameters from the parent object
InputParameters params = TableOutput::validParams();
params.addClassDescription("Output for postprocessors, vector postprocessors, and scalar "
"variables using comma seperated values (CSV).");
params.addParam<bool>("sort_columns", false, "Toggle the sorting of columns alphabetically.");
// Options for aligning csv output with whitespace padding
params.addParam<bool>(
"align",
false,
"Align the outputted csv data by padding the numbers with trailing whitespace");
params.addParam<std::string>("delimiter", ",", "Assign the delimiter (default is ','");
params.addParam<unsigned int>("precision", 14, "Set the output precision");
params.addParam<bool>("create_final_symlink",
false,
"Enable/disable the creation of a _FINAL symlink for vector postprocessor "
"data with 'execute_on' includes 'FINAL'.");
params.addParam<bool>(
"create_latest_symlink",
false,
"Enable/disable the creation of a _LATEST symlink for vector postprocessor data.");
// Suppress unused parameters
params.suppressParameter<unsigned int>("padding");
// Done
return params;
}
CSV::CSV(const InputParameters & parameters)
: TableOutput(parameters),
_align(getParam<bool>("align")),
_precision(getParam<unsigned int>("precision")),
_delimiter(getParam<std::string>("delimiter")),
_write_all_table(false),
_write_vector_table(false),
_sort_columns(getParam<bool>("sort_columns")),
_recovering(_app.isRecovering()),
_create_final_symlink(getParam<bool>("create_final_symlink")),
_create_latest_symlink(getParam<bool>("create_latest_symlink"))
{
}
void
CSV::initialSetup()
{
// Call the base class method
TableOutput::initialSetup();
// Set the delimiter
_all_data_table.setDelimiter(_delimiter);
// Set the precision
_all_data_table.setPrecision(_precision);
if (_recovering)
_all_data_table.append(true);
// Clear any existing symbolic links to LATEST and/or FINAL
if (processor_id() == 0)
{
const std::set<std::string> & out = getVectorPostprocessorOutput();
for (const auto & vpp_name : out)
{
std::string short_name = MooseUtils::shortName(vpp_name);
std::string out_latest = _file_base + "_" + short_name + "_LATEST.csv";
std::string out_final = _file_base + "_" + short_name + "_FINAL.csv";
MooseUtils::clearSymlink(out_latest);
MooseUtils::clearSymlink(out_final);
}
}
}
std::string
CSV::filename()
{
return _file_base + ".csv";
}
void
CSV::outputScalarVariables()
{
TableOutput::outputScalarVariables();
_write_all_table = true;
}
void
CSV::outputPostprocessors()
{
TableOutput::outputPostprocessors();
_write_all_table = true;
}
void
CSV::outputVectorPostprocessors()
{
TableOutput::outputVectorPostprocessors();
_write_vector_table = true;
}
std::string
CSV::getVectorPostprocessorFileName(const std::string & vpp_name,
bool include_time_step,
bool is_distributed)
{
std::ostringstream file_name;
file_name << _file_base;
auto short_name = MooseUtils::shortName(vpp_name);
if (short_name.size())
file_name << '_' << short_name;
if (include_time_step)
file_name << '_' << std::setw(_padding) << std::setprecision(0) << std::setfill('0')
<< std::right << timeStep();
file_name << ".csv";
if (is_distributed)
file_name << "." << processor_id();
return file_name.str();
}
void
CSV::output(const ExecFlagType & type)
{
// Call the base class output (populates tables)
TableOutput::output(type);
// Print the table containing all the data to a file
if (_write_all_table && !_all_data_table.empty() && processor_id() == 0)
{
if (_sort_columns)
_all_data_table.sortColumns();
_all_data_table.printCSV(filename(), 1, _align);
}
const auto & vpp_data = _problem_ptr->getVectorPostprocessorData();
// Output each VectorPostprocessor's data to a file
if (_write_vector_table)
{
// The VPP table will not write the same data twice, so to get the symlinks correct
// for EXEC_FINAL (when other flags exist) whenever files are written the names must
// be stored. These stored names are then used outside of this loop when the EXEC_FINAL call is
// made.
_latest_vpp_filenames.clear();
for (auto & it : _vector_postprocessor_tables)
{
const auto & vpp_name = it.first;
it.second.setDelimiter(_delimiter);
it.second.setPrecision(_precision);
if (_sort_columns)
it.second.sortColumns();
auto include_time_suffix = !vpp_data.containsCompleteHistory(vpp_name);
auto is_distributed = vpp_data.isDistributed(vpp_name);
if (is_distributed || processor_id() == 0)
{
std::string fname =
getVectorPostprocessorFileName(vpp_name, include_time_suffix, is_distributed);
std::string fprefix = getVectorPostprocessorFilePrefix(vpp_name);
_latest_vpp_filenames.emplace_back(fname, fprefix, is_distributed);
it.second.printCSV(fname, 1, _align);
if (_create_latest_symlink)
{
std::ostringstream out_latest;
out_latest << fprefix << "_LATEST.csv";
if (is_distributed)
out_latest << "." << processor_id();
MooseUtils::createSymlink(fname, out_latest.str());
}
if (_time_data)
_vector_postprocessor_time_tables[vpp_name].printCSV(fprefix + "_time.csv");
}
}
}
if (type == EXEC_FINAL && _create_final_symlink)
{
for (const auto & name_tuple : _latest_vpp_filenames)
{
std::ostringstream out_final;
out_final << std::get<1>(name_tuple) << "_FINAL.csv";
if (std::get<2>(name_tuple))
{
out_final << "." << processor_id();
MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
}
else if (processor_id() == 0)
MooseUtils::createSymlink(std::get<0>(name_tuple), out_final.str());
}
}
// Re-set write flags
_write_all_table = false;
_write_vector_table = false;
}
std::string
CSV::getVectorPostprocessorFilePrefix(const std::string & vpp_name)
{
return _file_base + "_" + MooseUtils::shortName(vpp_name);
}