forked from idaholab/moose
/
FormattedTable.C
541 lines (464 loc) · 14.9 KB
/
FormattedTable.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/
#include "FormattedTable.h"
#include "MooseError.h"
#include "InfixIterator.h"
// libMesh includes
#include "libmesh/exodusII_io.h"
#include <iomanip>
#include <iterator>
// Used for terminal width
#include <sys/ioctl.h>
#include <cstdlib>
const unsigned short FormattedTable::_column_width = 15;
const unsigned short FormattedTable::_min_pps_width = 40;
template <>
void
dataStore(std::ostream & stream, FormattedTable & table, void * context)
{
storeHelper(stream, table._data, context);
storeHelper(stream, table._column_names, context);
// Don't store these
// _output_file
// _stream_open
storeHelper(stream, table._last_key, context);
}
template <>
void
dataLoad(std::istream & stream, FormattedTable & table, void * context)
{
loadHelper(stream, table._data, context);
loadHelper(stream, table._column_names, context);
table._stream_open = false;
// table.close();
loadHelper(stream, table._last_key, context);
}
void
FormattedTable::close()
{
if (!_stream_open)
return;
_output_file.flush();
_output_file.close();
_stream_open = false;
_output_file_name = "";
}
void
FormattedTable::open(const std::string & file_name)
{
if (!_stream_open && _output_file_name == file_name)
return;
close();
_output_file_name = file_name;
_output_file.open(file_name.c_str(), std::ios::trunc | std::ios::out);
_stream_open = true;
}
FormattedTable::FormattedTable()
: _stream_open(false), _last_key(-1), _output_time(true), _csv_delimiter(","), _csv_precision(14)
{
}
FormattedTable::FormattedTable(const FormattedTable & o)
: _column_names(o._column_names),
_output_file_name(""),
_stream_open(o._stream_open),
_last_key(o._last_key),
_output_time(o._output_time),
_csv_delimiter(","),
_csv_precision(14)
{
if (_stream_open)
mooseError("Copying a FormattedTable with an open stream is not supported");
for (const auto & it : o._data)
_data[it.first] = it.second;
}
FormattedTable::~FormattedTable() { close(); }
bool
FormattedTable::empty() const
{
return _last_key == -1;
}
void
FormattedTable::addData(const std::string & name, Real value, Real time)
{
_data[time][name] = value;
if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
_column_names.push_back(name);
_last_key = time;
}
Real &
FormattedTable::getLastData(const std::string & name)
{
mooseAssert(_last_key != -1, "No Data stored in the FormattedTable");
std::map<std::string, Real>::iterator it = (_data[_last_key]).find(name);
if (it == (_data[_last_key]).end())
mooseError("No Data found for name: " + name);
return it->second;
}
void
FormattedTable::printOmittedRow(std::ostream & out,
std::map<std::string, unsigned short> & col_widths,
std::vector<std::string>::iterator & col_begin,
std::vector<std::string>::iterator & col_end) const
{
printNoDataRow(':', ' ', out, col_widths, col_begin, col_end);
}
void
FormattedTable::printRowDivider(std::ostream & out,
std::map<std::string, unsigned short> & col_widths,
std::vector<std::string>::iterator & col_begin,
std::vector<std::string>::iterator & col_end) const
{
printNoDataRow('+', '-', out, col_widths, col_begin, col_end);
}
void
FormattedTable::printNoDataRow(char intersect_char,
char fill_char,
std::ostream & out,
std::map<std::string, unsigned short> & col_widths,
std::vector<std::string>::iterator & col_begin,
std::vector<std::string>::iterator & col_end) const
{
out.fill(fill_char);
out << std::right << intersect_char << std::setw(_column_width + 2) << intersect_char;
for (std::vector<std::string>::iterator header = col_begin; header != col_end; ++header)
out << std::setw(col_widths[*header] + 2) << intersect_char;
out << "\n";
// Clear the fill character
out.fill(' ');
}
void
FormattedTable::printTable(const std::string & file_name)
{
open(file_name);
printTable(_output_file);
}
void
FormattedTable::printTable(std::ostream & out, unsigned int last_n_entries)
{
printTable(out, last_n_entries, MooseEnum("ENVIRONMENT=-1", "ENVIRONMENT"));
}
void
FormattedTable::printTable(std::ostream & out,
unsigned int last_n_entries,
const MooseEnum & suggested_term_width)
{
unsigned short term_width;
if (suggested_term_width == "ENVIRONMENT")
term_width = getTermWidth(true);
else if (suggested_term_width == "AUTO")
term_width = getTermWidth(false);
else
term_width = suggested_term_width;
if (term_width < _min_pps_width)
term_width = _min_pps_width;
std::vector<std::string>::iterator col_it = _column_names.begin();
std::vector<std::string>::iterator col_end = _column_names.end();
std::vector<std::string>::iterator curr_begin = col_it;
std::vector<std::string>::iterator curr_end;
while (col_it != col_end)
{
std::map<std::string, unsigned short> col_widths;
unsigned int curr_width = _column_width + 4;
unsigned int cols_in_group = 0;
while (curr_width < term_width && col_it != col_end)
{
curr_end = col_it;
col_widths[*col_it] = col_it->length() > _column_width ? col_it->length() + 1 : _column_width;
curr_width += col_widths[*col_it] + 3;
++col_it;
++cols_in_group;
}
if (col_it != col_end && cols_in_group >= 2)
{
// curr_width -= col_widths[*curr_end];
col_widths.erase(*curr_end);
col_it = curr_end;
}
else
curr_end = col_it;
printTablePiece(out, last_n_entries, col_widths, curr_begin, curr_end);
curr_begin = curr_end;
}
}
void
FormattedTable::printTablePiece(std::ostream & out,
unsigned int last_n_entries,
std::map<std::string, unsigned short> & col_widths,
std::vector<std::string>::iterator & col_begin,
std::vector<std::string>::iterator & col_end)
{
std::map<Real, std::map<std::string, Real>>::iterator i;
std::vector<std::string>::iterator header;
/**
* Print out the header row
*/
printRowDivider(out, col_widths, col_begin, col_end);
out << "|" << std::setw(_column_width) << std::left << " time"
<< " |";
for (header = col_begin; header != col_end; ++header)
out << " " << std::setw(col_widths[*header]) << *header << "|";
out << "\n";
printRowDivider(out, col_widths, col_begin, col_end);
/**
* Skip over values that we don't want to see.
* This step may be able to optimized if the table gets really big. We could
* iterate backwards from the end and create a new forward iterator from there.
*/
i = _data.begin();
if (last_n_entries)
{
if (_data.size() > last_n_entries)
// Print a blank row to indicate that values have been ommited
printOmittedRow(out, col_widths, col_begin, col_end);
for (int counter = 0; counter < static_cast<int>(_data.size() - last_n_entries); ++counter)
++i;
}
// Now print the remaining data rows
for (; i != _data.end(); ++i)
{
out << "|" << std::right << std::setw(_column_width) << std::scientific << i->first << " |";
for (header = col_begin; header != col_end; ++header)
{
std::map<std::string, Real> & tmp = i->second;
out << std::setw(col_widths[*header]) << tmp[*header] << " |";
}
out << "\n";
}
printRowDivider(out, col_widths, col_begin, col_end);
}
void
FormattedTable::printCSV(const std::string & file_name, int interval, bool align)
{
open(file_name);
_output_file.seekp(0, std::ios::beg);
/* When the alignment option is set to true, the widths of the columns needs to be computed based
* on
* longest of the column name of the data supplied. This is done here by creating a map of the
* widths for each of the columns, including time */
std::map<std::string, unsigned int> width;
if (align)
{
// Set the initial width to the names of the columns
width["time"] = 4;
for (const auto & col_name : _column_names)
width[col_name] = col_name.size();
// Loop through the various times
for (const auto & it : _data)
{
// Update the time width
{
std::ostringstream oss;
oss << std::setprecision(_csv_precision) << it.first;
unsigned int w = oss.str().size();
width["time"] = std::max(width["time"], w);
}
// Loop through the data for the current time and update the widths
for (const auto & jt : it.second)
{
std::ostringstream oss;
oss << std::setprecision(_csv_precision) << jt.second;
unsigned int w = oss.str().size();
width[jt.first] = std::max(width[jt.first], w);
}
}
}
{ // Output Header
bool first = true;
if (_output_time)
{
if (align)
_output_file << std::setw(width["time"]) << "time";
else
_output_file << "time";
first = false;
}
for (const auto & col_name : _column_names)
{
if (!first)
_output_file << _csv_delimiter;
if (align)
_output_file << std::right << std::setw(width[col_name]) << col_name;
else
_output_file << col_name;
first = false;
}
}
_output_file << "\n";
int counter = 0;
for (auto & i : _data)
{
if (counter++ % interval == 0)
{
bool first = true;
if (_output_time)
{
if (align)
_output_file << std::setprecision(_csv_precision) << std::right
<< std::setw(width["time"]) << i.first;
else
_output_file << std::setprecision(_csv_precision) << i.first;
first = false;
}
for (const auto & col_name : _column_names)
{
std::map<std::string, Real> & tmp = i.second;
if (!first)
_output_file << _csv_delimiter;
else
first = false;
if (align)
_output_file << std::setprecision(_csv_precision) << std::right
<< std::setw(width[col_name]) << tmp[col_name];
else
_output_file << std::setprecision(_csv_precision) << tmp[col_name];
}
_output_file << "\n";
}
}
_output_file << "\n";
_output_file.flush();
}
// const strings that the gnuplot generator needs
namespace gnuplot
{
const std::string before_terminal = "set terminal ";
const std::string before_ext = "\nset output 'all";
const std::string after_ext =
"'\nset title 'All Postprocessors'\nset xlabel 'time'\nset ylabel 'values'\nplot";
}
void
FormattedTable::makeGnuplot(const std::string & base_file, const std::string & format)
{
// TODO: run this once at end of simulation, right now it runs every iteration
// TODO: do I need to be more careful escaping column names?
// Note: open and close the files each time, having open files may mess with gnuplot
// supported filetypes: ps, png
std::string extension, terminal;
if (format == "png")
{
extension = ".png";
terminal = "png";
}
else if (format == "ps")
{
extension = ".ps";
terminal = "postscript";
}
else if (format == "gif")
{
extension = ".gif";
terminal = "gif";
}
else
mooseError("gnuplot format \"" + format + "\" is not supported.");
// Write the data to disk
std::string dat_name = base_file + ".dat";
std::ofstream datfile;
datfile.open(dat_name.c_str(), std::ios::trunc | std::ios::out);
datfile << "# time";
for (const auto & col_name : _column_names)
datfile << '\t' << col_name;
datfile << '\n';
for (auto & i : _data)
{
datfile << i.first;
for (const auto & col_name : _column_names)
{
std::map<std::string, Real> & tmp = i.second;
datfile << '\t' << tmp[col_name];
}
datfile << '\n';
}
datfile.flush();
datfile.close();
// Write the gnuplot script
std::string gp_name = base_file + ".gp";
std::ofstream gpfile;
gpfile.open(gp_name.c_str(), std::ios::trunc | std::ios::out);
gpfile << gnuplot::before_terminal << terminal << gnuplot::before_ext << extension
<< gnuplot::after_ext;
// plot all postprocessors in one plot
int column = 2;
for (const auto & col_name : _column_names)
{
gpfile << " '" << dat_name << "' using 1:" << column << " title '" << col_name
<< "' with linespoints";
column++;
if (column - 2 < static_cast<int>(_column_names.size()))
gpfile << ", \\\n";
}
gpfile << "\n\n";
// plot the postprocessors individually
column = 2;
for (const auto & col_name : _column_names)
{
gpfile << "set output '" << col_name << extension << "'\n";
gpfile << "set ylabel '" << col_name << "'\n";
gpfile << "plot '" << dat_name << "' using 1:" << column << " title '" << col_name
<< "' with linespoints\n\n";
column++;
}
gpfile.flush();
gpfile.close();
// Run the gnuplot script
/* We aren't going to run gnuplot automatically
if (!system(NULL))
mooseError("No way to run gnuplot on this computer");
std::string command = "gnuplot " + gp_name;
if (system(command.c_str()))
mooseError("gnuplot command failed");
*/
}
void
FormattedTable::clear()
{
_data.clear();
}
unsigned short
FormattedTable::getTermWidth(bool use_environment) const
{
struct winsize w;
/**
* Initialize the value we intend to populate just in case
* the system call fails
*/
w.ws_col = std::numeric_limits<unsigned short>::max();
if (use_environment)
{
char * pps_width = std::getenv("MOOSE_PPS_WIDTH");
if (pps_width != NULL)
{
std::stringstream ss(pps_width);
ss >> w.ws_col;
}
}
else
{
try
{
ioctl(0, TIOCGWINSZ, &w);
}
catch (...)
{
// Something bad happened, make sure we have a sane value
w.ws_col = std::numeric_limits<unsigned short>::max();
}
}
return w.ws_col;
}
MooseEnum
FormattedTable::getWidthModes()
{
return MooseEnum("ENVIRONMENT=-1 AUTO=0 80=80 120=120 160=160", "ENVIRONMENT", true);
}