forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV.C
156 lines (128 loc) · 3.89 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
//* 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);
template <>
InputParameters
validParams<CSV>()
{
// Get the parameters from the parent object
InputParameters params = validParams<TableOutput>();
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");
// 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())
{
}
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);
}
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)
{
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";
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 && processor_id() == 0)
{
for (auto & it : _vector_postprocessor_tables)
{
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);
it.second.printCSV(getVectorPostprocessorFileName(vpp_name, include_time_suffix), 1, _align);
if (_time_data)
{
std::string file_name = _file_base + '_' + MooseUtils::shortName(vpp_name) + "_time.csv";
_vector_postprocessor_time_tables[vpp_name].printCSV(file_name);
}
}
}
// Re-set write flags
_write_all_table = false;
_write_vector_table = false;
}