https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes | Friends | List of all members
FormattedTable Class Reference

This class is used for building, formatting, and outputting tables of numbers. More...

#include <FormattedTable.h>

Public Member Functions

 FormattedTable ()
 Default constructor - The default constructor takes an optional parameter to turn off stateful printing.
 
 FormattedTable (const FormattedTable &o)
 Copy constructor - The copy constructor will duplicate the data structures but is not designed to work with FormattedTables with open streams (e.g.
 
 ~FormattedTable ()
 The destructor is used to close the file handle.
 
bool empty () const
 Returns a boolean value based on whether the FormattedTable contains data or not.
 
void append (bool append_existing_file)
 Sets append mode which means an existing file is not truncated on opening.
 
void addRow (Real time)
 Force a new row in the table with the passed in time.
 
template<typename T = Real>
void addData (const std::string &name, const T &value)
 Method for adding data to the output table.
 
template<typename T = Real>
void addData (const std::string &name, const T &value, Real time)
 Method for adding data to the output table.
 
template<typename T = Real>
void addData (const std::string &name, const std::vector< T > &vector)
 Method for adding an entire vector to a table at a time.
 
Real getLastTime () const
 Retrieve the last time (or independent variable) value.
 
template<typename T = Real>
T & getLastData (const std::string &name) const
 Retrieve Data for last value of given name.
 
void clear ()
 
void outputTimeColumn (bool output_time)
 Set whether or not to output time column.
 
void printTable (std::ostream &out, unsigned int last_n_entries=0)
 Methods for dumping the table to the stream - either by filename or by stream handle.
 
void printTable (std::ostream &out, unsigned int last_n_entries, const MooseEnum &suggested_term_width)
 
void printTable (const std::string &file_name)
 
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.
 
void printEnsight (const std::string &file_name)
 
void writeExodus (libMesh::ExodusII_IO *ex_out, Real time)
 
void makeGnuplot (const std::string &base_file, const std::string &format)
 
void setDelimiter (std::string delimiter)
 By default printCSV places "," between each entry, this allows this to be changed.
 
void setPrecision (unsigned int precision)
 By default printCSV prints output to a precision of 14, this allows this to be changed.
 
void setUseScientificNotation (bool use_scientific_notation)
 Set whether printCSV uses scientific notation for floating point values.
 
void sortColumns ()
 Sorts columns alphabetically.
 

Static Public Member Functions

static MooseEnum getWidthModes ()
 

Protected Member Functions

void 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)
 
void 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
 
void 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
 
void 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
 

Protected Attributes

std::vector< std::pair< Real, std::map< std::string, std::shared_ptr< TableValueBase > > > > _data
 Data structure for the console table: The first part of the pair tracks the independent variable (normally time) and is associated with the second part of the table which is the map of dependent variables and their associated values.
 
std::map< std::string, unsigned int_align_widths
 Alignment widths (only used if asked to print aligned to CSV output)
 
std::vector< std::string > _column_names
 The set of column names updated when data is inserted through the setter methods.
 

Static Protected Attributes

static const unsigned short _column_width = 15
 The single cell width used for all columns in the table.
 
static const unsigned short _min_pps_width = 40
 The absolute minimum PPS table width.
 

Private Member Functions

void close ()
 Close the underlying output file stream if any. This is idempotent.
 
void open (const std::string &file_name)
 Open or switch the underlying file stream to point to file_name. This is idempotent.
 
void printRow (std::pair< Real, std::map< std::string, std::shared_ptr< TableValueBase > > > &row_data, bool align)
 
void fillEmptyValues (unsigned int last_n_entries=0)
 Fill any values that are not defined (usually when there are mismatched column lengths)
 

Private Attributes

std::string _output_file_name
 The optional output file stream.
 
std::ofstream _output_file
 The stream handle (corresponds to _output_file_name)
 
std::size_t _output_row_index
 Keeps track of the index indicating which vector elements have been output.
 
bool _headers_output
 Keeps track of whether the header has been output.
 
bool _append
 Keeps track of whether we want to open an existing file for appending or overwriting.
 
bool _output_time
 Whether or not to output the Time column.
 
std::string _csv_delimiter
 *.csv file delimiter, defaults to ","
 
unsigned int _csv_precision
 *.csv file precision, defaults to 14
 
bool _csv_use_scientific_notation
 Whether to print floating point CSV values in scientific notation.
 
bool _column_names_unsorted = true
 Flag indicating that sorting is necessary (used by sortColumns method).
 

Friends

void dataStore (std::ostream &stream, FormattedTable &table, void *context)
 
void dataLoad (std::istream &stream, FormattedTable &v, void *context)
 

Detailed Description

This class is used for building, formatting, and outputting tables of numbers.

Definition at line 109 of file FormattedTable.h.

Constructor & Destructor Documentation

◆ FormattedTable() [1/2]

FormattedTable::FormattedTable ( )

Default constructor - The default constructor takes an optional parameter to turn off stateful printing.

This means that each time you ask the FormattedTable to print to a file, it'll, print the entire table. The default is to only print the part of the table that hasn't already been printed.

Definition at line 143 of file FormattedTable.C.

145 _headers_output(false),
146 _append(false),
147 _output_time(true),
151{
152}
const std::string DEFAULT_CSV_DELIMITER
const unsigned short DEFAULT_CSV_PRECISION
bool _headers_output
Keeps track of whether the header has been output.
unsigned int _csv_precision
*.csv file precision, defaults to 14
bool _output_time
Whether or not to output the Time column.
bool _append
Keeps track of whether we want to open an existing file for appending or overwriting.
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
std::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
bool _csv_use_scientific_notation
Whether to print floating point CSV values in scientific notation.

◆ FormattedTable() [2/2]

FormattedTable::FormattedTable ( const FormattedTable o)

Copy constructor - The copy constructor will duplicate the data structures but is not designed to work with FormattedTables with open streams (e.g.

CSV Output mode).

Definition at line 154 of file FormattedTable.C.

159 _append(o._append),
165{
166 if (_output_file.is_open())
167 mooseError("Copying a FormattedTable with an open stream is not supported");
168
169 for (const auto & it : o._data)
170 _data.emplace_back(it.first, it.second);
171}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
std::vector< std::pair< Real, std::map< std::string, std::shared_ptr< TableValueBase > > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
std::string _output_file_name
The optional output file stream.
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods.

◆ ~FormattedTable()

FormattedTable::~FormattedTable ( )

The destructor is used to close the file handle.

Definition at line 173 of file FormattedTable.C.

173{ close(); }
void close()
Close the underlying output file stream if any. This is idempotent.

Member Function Documentation

◆ addData() [1/3]

template<typename T >
void FormattedTable::addData ( const std::string &  name,
const std::vector< T > &  vector 
)

Method for adding an entire vector to a table at a time.

Checks are made to ensure that the dependent variable index lines up with the vector indices.

Definition at line 389 of file FormattedTable.h.

390{
391 for (MooseIndex(vector) i = 0; i < vector.size(); ++i)
392 {
393 if (i == _data.size())
394 _data.emplace_back(i, std::map<std::string, std::shared_ptr<TableValueBase>>());
395
396 mooseAssert(MooseUtils::absoluteFuzzyEqual(_data[i].first, i),
397 "Inconsistent indexing in VPP vector");
398
399 auto & curr_entry = _data[i];
400 curr_entry.second[name] =
401 std::dynamic_pointer_cast<TableValueBase>(std::make_shared<TableValue<T>>(vector[i]));
402 }
403
404 if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
405 {
406 _column_names.push_back(name);
408 }
409}
std::string name(const ElemQuality q)

◆ addData() [2/3]

template<typename T >
void FormattedTable::addData ( const std::string &  name,
const T &  value 
)

Method for adding data to the output table.

Data is added to the last row. Method will error if called on an empty table.

Definition at line 343 of file FormattedTable.h.

344{
345 if (empty())
346 mooseError("No Data stored in the the FormattedTable");
347
348 auto back_it = _data.rbegin();
349 back_it->second[name] =
350 std::dynamic_pointer_cast<TableValueBase>(std::make_shared<TableValue<T>>(value));
351
352 if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
353 {
354 _column_names.push_back(name);
356 }
357}
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not.

Referenced by AutomaticMortarGeneration::msmStatistics(), TableOutput::outputPostprocessorsRow(), TableOutput::outputReporter(), TableOutput::outputScalarVariables(), and TableOutput::outputVectorPostprocessors().

◆ addData() [3/3]

template<typename T >
void FormattedTable::addData ( const std::string &  name,
const T &  value,
Real  time 
)

Method for adding data to the output table.

The dependent variable is named "time"

Definition at line 361 of file FormattedTable.h.

362{
363 auto back_it = _data.rbegin();
364
365 mooseAssert(back_it == _data.rend() || !MooseUtils::absoluteFuzzyLessThan(time, back_it->first),
366 "Attempting to add data to FormattedTable with the dependent variable in a "
367 "non-increasing order.\nDid you mean to use addData(std::string &, const "
368 "std::vector<Real> &)?");
369
370 // See if the current "row" is already in the table
371 if (back_it == _data.rend() || !MooseUtils::absoluteFuzzyEqual(time, back_it->first))
372 {
373 _data.emplace_back(time, std::map<std::string, std::shared_ptr<TableValueBase>>());
374 back_it = _data.rbegin();
375 }
376 // Insert or update value
377 back_it->second[name] =
378 std::dynamic_pointer_cast<TableValueBase>(std::make_shared<TableValue<T>>(value));
379
380 if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
381 {
382 _column_names.push_back(name);
384 }
385}

◆ addRow()

void FormattedTable::addRow ( Real  time)

Force a new row in the table with the passed in time.

Definition at line 188 of file FormattedTable.C.

189{
190 _data.emplace_back(time, std::map<std::string, std::shared_ptr<TableValueBase>>());
191}

Referenced by AutomaticMortarGeneration::msmStatistics(), TableOutput::outputPostprocessorsRow(), and TableOutput::outputReporter().

◆ append()

void FormattedTable::append ( bool  append_existing_file)

Sets append mode which means an existing file is not truncated on opening.

This mode is typically used for recovery.

Definition at line 182 of file FormattedTable.C.

183{
184 _append = append_existing_file;
185}

Referenced by CSV::initialSetup().

◆ clear()

void FormattedTable::clear ( )

Definition at line 578 of file FormattedTable.C.

579{
580 _data.clear();
581 _output_file.close();
583}

Referenced by TableOutput::clear(), and AutomaticMortarGeneration::msmStatistics().

◆ close()

void FormattedTable::close ( )
private

Close the underlying output file stream if any. This is idempotent.

Definition at line 111 of file FormattedTable.C.

112{
113 if (!_output_file.is_open())
114 return;
115 _output_file.flush();
116 _output_file.close();
118}

Referenced by open(), printCSV(), and ~FormattedTable().

◆ empty()

bool FormattedTable::empty ( ) const

◆ fillEmptyValues()

void FormattedTable::fillEmptyValues ( unsigned int  last_n_entries = 0)
private

Fill any values that are not defined (usually when there are mismatched column lengths)

If last_n_entries is non-zero, only values in that many final rows will be examined and filled.

Definition at line 586 of file FormattedTable.C.

587{
588 auto begin = _data.begin();
589 auto end = _data.end();
590 if (last_n_entries && (last_n_entries < _data.size()))
591 begin = end - last_n_entries;
592
593 for (auto it = begin; it != end; ++it)
594 {
595 auto & datamap = it->second;
596 if (datamap.size() != _column_names.size())
597 {
598 for (const auto & col_name : _column_names)
599 if (!datamap[col_name])
600 datamap[col_name] =
601 std::dynamic_pointer_cast<TableValueBase>(std::make_shared<TableValue<char>>('0'));
602 }
603 else
604 {
605 for (auto & [key, val] : datamap)
606 if (!val)
607 val = std::dynamic_pointer_cast<TableValueBase>(std::make_shared<TableValue<char>>('0'));
608 }
609 }
610}
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD

Referenced by makeGnuplot(), printCSV(), and printTablePiece().

◆ getLastData()

template<typename T >
T & FormattedTable::getLastData ( const std::string &  name) const

Retrieve Data for last value of given name.

Definition at line 413 of file FormattedTable.h.

414{
415 mooseAssert(!empty(), "No Data stored in the FormattedTable");
416
417 auto & last_data_map = _data.rbegin()->second;
418 auto it = last_data_map.find(name);
419 if (it == last_data_map.end())
420 mooseError("No Data found for name: " + name);
421
422 auto value = std::dynamic_pointer_cast<TableValue<T>>(it->second);
423 if (!value)
424 mooseError("Data for ", name, " is not of the requested type.");
425 return value->set();
426}
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)

Referenced by TableOutput::shouldOutputPostprocessorsRow().

◆ getLastTime()

Real FormattedTable::getLastTime ( ) const

Retrieve the last time (or independent variable) value.

Definition at line 194 of file FormattedTable.C.

195{
196 mooseAssert(!empty(), "No Data stored in the FormattedTable");
197 return _data.rbegin()->first;
198}

Referenced by TableOutput::outputReporter(), and TableOutput::shouldOutputPostprocessorsRow().

◆ getWidthModes()

MooseEnum FormattedTable::getWidthModes ( )
static

Definition at line 613 of file FormattedTable.C.

614{
615 return MooseEnum("ENVIRONMENT=-1 AUTO=0 80=80 120=120 160=160", "ENVIRONMENT", true);
616}
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55

Referenced by Console::validParams().

◆ makeGnuplot()

void FormattedTable::makeGnuplot ( const std::string &  base_file,
const std::string &  format 
)

Definition at line 484 of file FormattedTable.C.

485{
487
488 // TODO: run this once at end of simulation, right now it runs every iteration
489 // TODO: do I need to be more careful escaping column names?
490 // Note: open and close the files each time, having open files may mess with gnuplot
491
492 // supported filetypes: ps, png
493 std::string extension, terminal;
494 if (format == "png")
495 {
496 extension = ".png";
497 terminal = "png";
498 }
499
500 else if (format == "ps")
501 {
502 extension = ".ps";
503 terminal = "postscript";
504 }
505
506 else if (format == "gif")
507 {
508 extension = ".gif";
509 terminal = "gif";
510 }
511
512 else
513 mooseError("gnuplot format \"" + format + "\" is not supported.");
514
515 // Write the data to disk
516 std::string dat_name = base_file + ".dat";
517 std::ofstream datfile;
518 datfile.open(dat_name.c_str(), std::ios::trunc | std::ios::out);
519 if (datfile.fail())
520 mooseError("Unable to open file ", dat_name);
521
522 datfile << "# time";
523 for (const auto & col_name : _column_names)
524 datfile << '\t' << col_name;
525 datfile << '\n';
526
527 for (auto & data_it : _data)
528 {
529 datfile << data_it.first;
530 for (const auto & col_name : _column_names)
531 {
532 auto & tmp = data_it.second;
533 datfile << '\t' << *tmp[col_name];
534 }
535 datfile << '\n';
536 }
537 datfile.flush();
538 datfile.close();
539
540 // Write the gnuplot script
541 std::string gp_name = base_file + ".gp";
542 std::ofstream gpfile;
543 gpfile.open(gp_name.c_str(), std::ios::trunc | std::ios::out);
544 if (gpfile.fail())
545 mooseError("Unable to open file ", gp_name);
546
547 gpfile << gnuplot::before_terminal << terminal << gnuplot::before_ext << extension
549
550 // plot all postprocessors in one plot
551 int column = 2;
552 for (const auto & col_name : _column_names)
553 {
554 gpfile << " '" << dat_name << "' using 1:" << column << " title '" << col_name
555 << "' with linespoints";
556 column++;
557 if (column - 2 < static_cast<int>(_column_names.size()))
558 gpfile << ", \\\n";
559 }
560 gpfile << "\n\n";
561
562 // plot the postprocessors individually
563 column = 2;
564 for (const auto & col_name : _column_names)
565 {
566 gpfile << "set output '" << col_name << extension << "'\n";
567 gpfile << "set ylabel '" << col_name << "'\n";
568 gpfile << "plot '" << dat_name << "' using 1:" << column << " title '" << col_name
569 << "' with linespoints\n\n";
570 column++;
571 }
572
573 gpfile.flush();
574 gpfile.close();
575}
void fillEmptyValues(unsigned int last_n_entries=0)
Fill any values that are not defined (usually when there are mismatched column lengths)
const std::string before_ext
const std::string before_terminal
const std::string after_ext

Referenced by Gnuplot::output().

◆ open()

void FormattedTable::open ( const std::string &  file_name)
private

Open or switch the underlying file stream to point to file_name. This is idempotent.

Definition at line 121 of file FormattedTable.C.

122{
123 if (_output_file.is_open() && _output_file_name == file_name)
124 return;
125 close();
126 _output_file_name = file_name;
127
128 std::ios_base::openmode open_flags = std::ios::out;
129 if (_append)
130 open_flags |= std::ios::app;
131 else
132 {
133 open_flags |= std::ios::trunc;
135 _headers_output = false;
136 }
137
138 _output_file.open(file_name.c_str(), open_flags);
139 if (_output_file.fail())
140 mooseError("Unable to open file ", file_name);
141}

Referenced by printCSV(), and printTable().

◆ outputTimeColumn()

void FormattedTable::outputTimeColumn ( bool  output_time)
inline

Set whether or not to output time column.

Definition at line 183 of file FormattedTable.h.

183{ _output_time = output_time; }

Referenced by TableOutput::outputReporter(), TableOutput::outputVectorPostprocessors(), and TableOutput::TableOutput().

◆ printCSV()

void FormattedTable::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.

Note: Only call this on processor 0!

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

Definition at line 351 of file FormattedTable.C.

352{
354
355 open(file_name);
356
357 if (_output_row_index == 0)
358 {
365 if (align)
366 {
367 // Set the initial width to the names of the columns
368 _align_widths["time"] = 4;
369
370 for (const auto & col_name : _column_names)
371 _align_widths[col_name] = col_name.size();
372
373 // Loop through the various times
374 for (const auto & it : _data)
375 {
376 // Update the time _align_width
377 {
378 std::ostringstream oss;
380 oss << std::scientific;
381 oss << std::setprecision(_csv_precision) << it.first;
382 unsigned int w = oss.str().size();
383 _align_widths["time"] = std::max(_align_widths["time"], w);
384 }
385
386 // Loop through the data for the current time and update the _align_widths
387 for (const auto & jt : it.second)
388 {
389 std::ostringstream oss;
391 oss << std::scientific;
392 oss << std::setprecision(_csv_precision) << *jt.second;
393 unsigned int w = oss.str().size();
394 _align_widths[jt.first] = std::max(_align_widths[jt.first], w);
395 }
396 }
397 }
398
399 // Output Header
400 if (!_headers_output)
401 {
402 if (_output_time)
403 {
404 if (align)
405 _output_file << std::setw(_align_widths["time"]) << "time";
406 else
407 _output_file << "time";
408 _headers_output = true;
409 }
410
411 for (const auto & col_name : _column_names)
412 {
413 if (_headers_output)
415
416 if (align)
417 _output_file << std::right << std::setw(_align_widths[col_name]) << col_name;
418 else
419 _output_file << col_name;
420 _headers_output = true;
421 }
422 _output_file << "\n";
423 }
424 }
425
426 for (; _output_row_index < _data.size(); ++_output_row_index)
427 {
428 if (_output_row_index % interval == 0)
430 }
431
432 close();
433}
void printRow(std::pair< Real, std::map< std::string, std::shared_ptr< TableValueBase > > > &row_data, bool align)
void open(const std::string &file_name)
Open or switch the underlying file stream to point to file_name. This is idempotent.
std::map< std::string, unsigned int > _align_widths
Alignment widths (only used if asked to print aligned to CSV output)

Referenced by CSV::output().

◆ printEnsight()

void FormattedTable::printEnsight ( const std::string &  file_name)

◆ printNoDataRow()

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
protected

Definition at line 219 of file FormattedTable.C.

225{
226 out.fill(fill_char);
227 out << std::right << intersect_char;
228 if (_output_time)
229 out << std::setw(_column_width + 2) << intersect_char;
230 for (auto header_it = col_begin; header_it != col_end; ++header_it)
231 out << std::setw(col_widths[*header_it] + 2) << intersect_char;
232 out << "\n";
233
234 // Clear the fill character
235 out.fill(' ');
236}
static const unsigned short _column_width
The single cell width used for all columns in the table.
OStreamProxy out(std::cout)

Referenced by printOmittedRow(), and printRowDivider().

◆ printOmittedRow()

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
protected

Definition at line 201 of file FormattedTable.C.

205{
206 printNoDataRow(':', ' ', out, col_widths, col_begin, col_end);
207}
void 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

Referenced by printTablePiece().

◆ printRow()

void FormattedTable::printRow ( std::pair< Real, std::map< std::string, std::shared_ptr< TableValueBase > > > &  row_data,
bool  align 
)
private

Definition at line 436 of file FormattedTable.C.

438{
439 bool first = true;
440
442 _output_file << std::scientific;
443 else
444 _output_file << std::defaultfloat;
445
446 if (_output_time)
447 {
448 if (align)
449 _output_file << std::setprecision(_csv_precision) << std::right
450 << std::setw(_align_widths["time"]) << row_data.first;
451 else
452 _output_file << std::setprecision(_csv_precision) << row_data.first;
453 first = false;
454 }
455
456 for (const auto & col_name : _column_names)
457 {
458 std::map<std::string, std::shared_ptr<TableValueBase>> & tmp = row_data.second;
459
460 if (!first)
462 else
463 first = false;
464
465 if (align)
466 _output_file << std::setprecision(_csv_precision) << std::right
467 << std::setw(_align_widths[col_name]) << *tmp[col_name];
468 else
469 _output_file << std::setprecision(_csv_precision) << *tmp[col_name];
470 }
471 _output_file << "\n";
472}

Referenced by printCSV().

◆ printRowDivider()

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
protected

Definition at line 210 of file FormattedTable.C.

214{
215 printNoDataRow('+', '-', out, col_widths, col_begin, col_end);
216}

Referenced by printTablePiece().

◆ printTable() [1/3]

void FormattedTable::printTable ( const std::string &  file_name)

Definition at line 239 of file FormattedTable.C.

240{
241 open(file_name);
243}
void printTable(std::ostream &out, unsigned int last_n_entries=0)
Methods for dumping the table to the stream - either by filename or by stream handle.

◆ printTable() [2/3]

void FormattedTable::printTable ( std::ostream &  out,
unsigned int  last_n_entries,
const MooseEnum suggested_term_width 
)

Definition at line 252 of file FormattedTable.C.

255{
256 unsigned short term_width;
257
258 if (suggested_term_width == "ENVIRONMENT")
259 term_width = MooseUtils::getTermWidth(true);
260 else if (suggested_term_width == "AUTO")
261 term_width = MooseUtils::getTermWidth(false);
262 else
263 term_width = MooseUtils::stringToInteger(suggested_term_width);
264
265 if (term_width < _min_pps_width)
266 term_width = _min_pps_width;
267
268 std::vector<std::string>::iterator col_it = _column_names.begin();
269 std::vector<std::string>::iterator col_end = _column_names.end();
270
271 std::vector<std::string>::iterator curr_begin = col_it;
272 std::vector<std::string>::iterator curr_end;
273 while (col_it != col_end)
274 {
275 std::map<std::string, unsigned short> col_widths;
276 unsigned int curr_width = _column_width + 4;
277 unsigned int cols_in_group = 0;
278 while (curr_width < term_width && col_it != col_end)
279 {
280 curr_end = col_it;
281 col_widths[*col_it] = col_it->length() > _column_width ? col_it->length() + 1 : _column_width;
282
283 curr_width += col_widths[*col_it] + 3;
284 ++col_it;
285 ++cols_in_group;
286 }
287 if (col_it != col_end && cols_in_group >= 2)
288 {
289 // curr_width -= col_widths[*curr_end];
290 col_widths.erase(*curr_end);
291 col_it = curr_end;
292 }
293 else
294 curr_end = col_it;
295
296 printTablePiece(out, last_n_entries, col_widths, curr_begin, curr_end);
297 curr_begin = curr_end;
298 }
299}
static const unsigned short _min_pps_width
The absolute minimum PPS table width.
void 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)
int stringToInteger(const std::string &input, bool throw_on_failure)
Definition MooseUtils.C:978
unsigned short getTermWidth(bool use_environment)
Definition MooseUtils.C:651

◆ printTable() [3/3]

void FormattedTable::printTable ( std::ostream &  out,
unsigned int  last_n_entries = 0 
)

Methods for dumping the table to the stream - either by filename or by stream handle.

If a filename is supplied opening and closing of the file is properly handled. In the screen version of the method, an optional parameters can be passed to print only the last "n" entries. A value of zero means don't skip any rows

Note: Only call these from processor 0!

Definition at line 246 of file FormattedTable.C.

247{
248 printTable(out, last_n_entries, MooseEnum("ENVIRONMENT=-1", "ENVIRONMENT"));
249}

Referenced by AutomaticMortarGeneration::msmStatistics(), Console::outputPostprocessors(), Console::outputReporters(), Console::outputScalarVariables(), printTable(), and printTable().

◆ printTablePiece()

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 
)
protected

Print out the header row

Definition at line 302 of file FormattedTable.C.

307{
308 fillEmptyValues(last_n_entries);
312 printRowDivider(out, col_widths, col_begin, col_end);
313 out << "|";
314 if (_output_time)
315 out << std::setw(_column_width) << std::left << " time" << " |";
316 for (auto header_it = col_begin; header_it != col_end; ++header_it)
317 out << " " << std::setw(col_widths[*header_it]) << *header_it << "|";
318 out << "\n";
319 printRowDivider(out, col_widths, col_begin, col_end);
320
321 auto data_it = _data.begin();
322 if (last_n_entries)
323 {
324 if (_data.size() > last_n_entries)
325 {
326 // Print a blank row to indicate that values have been ommited
327 printOmittedRow(out, col_widths, col_begin, col_end);
328
329 // Jump to the right place in the vector
330 data_it += _data.size() - last_n_entries;
331 }
332 }
333 // Now print the remaining data rows
334 for (; data_it != _data.end(); ++data_it)
335 {
336 out << "|";
337 if (_output_time)
338 out << std::right << std::setw(_column_width) << std::scientific << data_it->first << " |";
339 for (auto header_it = col_begin; header_it != col_end; ++header_it)
340 {
341 auto & tmp = data_it->second;
342 out << std::setw(col_widths[*header_it]) << *tmp[*header_it] << " |";
343 }
344 out << "\n";
345 }
346
347 printRowDivider(out, col_widths, col_begin, col_end);
348}
void 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
void 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

Referenced by printTable().

◆ setDelimiter()

void FormattedTable::setDelimiter ( std::string  delimiter)
inline

By default printCSV places "," between each entry, this allows this to be changed.

Definition at line 217 of file FormattedTable.h.

217{ _csv_delimiter = delimiter; }

Referenced by CSV::initialSetup().

◆ setPrecision()

void FormattedTable::setPrecision ( unsigned int  precision)
inline

By default printCSV prints output to a precision of 14, this allows this to be changed.

Definition at line 222 of file FormattedTable.h.

222{ _csv_precision = precision; }

Referenced by CSV::initialSetup().

◆ setUseScientificNotation()

void FormattedTable::setUseScientificNotation ( bool  use_scientific_notation)
inline

Set whether printCSV uses scientific notation for floating point values.

Definition at line 227 of file FormattedTable.h.

228 {
229 _csv_use_scientific_notation = use_scientific_notation;
230 }

Referenced by CSV::initialSetup().

◆ sortColumns()

void FormattedTable::sortColumns ( )

Sorts columns alphabetically.

Definition at line 619 of file FormattedTable.C.

620{
622 {
623 std::sort(_column_names.begin(), _column_names.end());
625 }
626}

Referenced by CSV::output(), Console::outputPostprocessors(), Console::outputReporters(), and Console::outputScalarVariables().

◆ writeExodus()

void FormattedTable::writeExodus ( libMesh::ExodusII_IO ex_out,
Real  time 
)

Friends And Related Symbol Documentation

◆ dataLoad

void dataLoad ( std::istream &  stream,
FormattedTable v,
void *  context 
)
friend

Definition at line 39 of file FormattedTable.C.

40{
41 loadHelper(stream, table._data, context);
42 loadHelper(stream, table._align_widths, context);
43 loadHelper(stream, table._column_names, context);
44 loadHelper(stream, table._output_row_index, context);
45 loadHelper(stream, table._headers_output, context);
46}
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:1081

◆ dataStore

void dataStore ( std::ostream &  stream,
FormattedTable table,
void *  context 
)
friend

Definition at line 27 of file FormattedTable.C.

28{
29 table.fillEmptyValues();
30 storeHelper(stream, table._data, context);
31 storeHelper(stream, table._align_widths, context);
32 storeHelper(stream, table._column_names, context);
33 storeHelper(stream, table._output_row_index, context);
34 storeHelper(stream, table._headers_output, context);
35}
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:989

Member Data Documentation

◆ _align_widths

std::map<std::string, unsigned int> FormattedTable::_align_widths
protected

Alignment widths (only used if asked to print aligned to CSV output)

Definition at line 269 of file FormattedTable.h.

Referenced by printCSV(), and printRow().

◆ _append

bool FormattedTable::_append
private

Keeps track of whether we want to open an existing file for appending or overwriting.

Definition at line 319 of file FormattedTable.h.

Referenced by append(), and open().

◆ _column_names

std::vector<std::string> FormattedTable::_column_names
protected

The set of column names updated when data is inserted through the setter methods.

Definition at line 272 of file FormattedTable.h.

Referenced by addData(), addData(), addData(), fillEmptyValues(), makeGnuplot(), printCSV(), printRow(), printTable(), and sortColumns().

◆ _column_names_unsorted

bool FormattedTable::_column_names_unsorted = true
private

Flag indicating that sorting is necessary (used by sortColumns method).

Definition at line 334 of file FormattedTable.h.

Referenced by addData(), addData(), addData(), and sortColumns().

◆ _column_width

const unsigned short FormattedTable::_column_width = 15
staticprotected

The single cell width used for all columns in the table.

Definition at line 275 of file FormattedTable.h.

Referenced by printNoDataRow(), printTable(), and printTablePiece().

◆ _csv_delimiter

std::string FormattedTable::_csv_delimiter
private

*.csv file delimiter, defaults to ","

Definition at line 325 of file FormattedTable.h.

Referenced by printCSV(), printRow(), and setDelimiter().

◆ _csv_precision

unsigned int FormattedTable::_csv_precision
private

*.csv file precision, defaults to 14

Definition at line 328 of file FormattedTable.h.

Referenced by printCSV(), printRow(), and setPrecision().

◆ _csv_use_scientific_notation

bool FormattedTable::_csv_use_scientific_notation
private

Whether to print floating point CSV values in scientific notation.

Definition at line 331 of file FormattedTable.h.

Referenced by printCSV(), printRow(), and setUseScientificNotation().

◆ _data

std::vector<std::pair<Real, std::map<std::string, std::shared_ptr<TableValueBase> > > > FormattedTable::_data
protected

Data structure for the console table: The first part of the pair tracks the independent variable (normally time) and is associated with the second part of the table which is the map of dependent variables and their associated values.

Definition at line 266 of file FormattedTable.h.

Referenced by addData(), addData(), addData(), addRow(), clear(), empty(), fillEmptyValues(), FormattedTable(), getLastData(), getLastTime(), makeGnuplot(), printCSV(), and printTablePiece().

◆ _headers_output

bool FormattedTable::_headers_output
private

Keeps track of whether the header has been output.

This is separate from _output_row_index because it's possible to output the header with zero rows. We don't consider this a bug, it helps users understand that they have declared vectors properly but maybe haven't populated them correctly.

Definition at line 316 of file FormattedTable.h.

Referenced by open(), and printCSV().

◆ _min_pps_width

const unsigned short FormattedTable::_min_pps_width = 40
staticprotected

The absolute minimum PPS table width.

Definition at line 278 of file FormattedTable.h.

Referenced by printTable().

◆ _output_file

std::ofstream FormattedTable::_output_file
private

The stream handle (corresponds to _output_file_name)

Definition at line 302 of file FormattedTable.h.

Referenced by clear(), close(), FormattedTable(), open(), printCSV(), printRow(), and printTable().

◆ _output_file_name

std::string FormattedTable::_output_file_name
private

The optional output file stream.

Definition at line 299 of file FormattedTable.h.

Referenced by close(), and open().

◆ _output_row_index

std::size_t FormattedTable::_output_row_index
private

Keeps track of the index indicating which vector elements have been output.

All items with an index less than this index have been output. Higher values have not.

Definition at line 308 of file FormattedTable.h.

Referenced by clear(), open(), and printCSV().

◆ _output_time

bool FormattedTable::_output_time
private

Whether or not to output the Time column.

Definition at line 322 of file FormattedTable.h.

Referenced by outputTimeColumn(), printCSV(), printNoDataRow(), printRow(), and printTablePiece().


The documentation for this class was generated from the following files: