www.mooseframework.org
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. More...
 
 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. More...
 
 ~FormattedTable ()
 The destructor is used to close the file handle. More...
 
bool empty () const
 Returns a boolean value based on whether the FormattedTable contains data or not. More...
 
void append (bool append_existing_file)
 Sets append mode which means an existing file is not truncated on opening. More...
 
void addRow (Real time)
 Force a new row in the table with the passed in time. More...
 
void addData (const std::string &name, Real value)
 Method for adding data to the output table. More...
 
void addData (const std::string &name, Real value, Real time)
 Method for adding data to the output table. More...
 
void addData (const std::string &name, const std::vector< Real > &vector)
 Method for adding an entire vector to a table at a time. More...
 
Real getLastTime ()
 Retrieve the last time (or independent variable) value. More...
 
Real & getLastData (const std::string &name)
 Retrieve Data for last value of given name. More...
 
void clear ()
 
void outputTimeColumn (bool output_time)
 Set whether or not to output time column. More...
 
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. More...
 
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. More...
 
void printEnsight (const std::string &file_name)
 
void writeExodus (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. More...
 
void setPrecision (unsigned int precision)
 By default printCSV prints output to a precision of 14, this allows this to be changed. More...
 
void sortColumns ()
 Sorts columns alphabetically. More...
 

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
 
unsigned short getTermWidth (bool use_environment) const
 Returns the width of the terminal using sys/ioctl. More...
 

Protected Attributes

std::vector< std::pair< Real, std::map< std::string, Real > > > _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. More...
 
std::map< std::string, unsigned int > _align_widths
 Alignment widths (only used if asked to print aligned to CSV output) More...
 
std::vector< std::string > _column_names
 The set of column names updated when data is inserted through the setter methods. More...
 

Static Protected Attributes

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

Private Member Functions

void close ()
 Close the underlying output file stream if any. This is idempotent. More...
 
void open (const std::string &file_name)
 Open or switch the underlying file stream to point to file_name. This is idempotent. More...
 
void printRow (std::pair< Real, std::map< std::string, Real >> &row_data, bool align)
 

Private Attributes

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

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 36 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 85 of file FormattedTable.C.

86  : _output_row_index(0),
87  _headers_output(false),
88  _append(false),
89  _output_time(true),
92 {
93 }
std::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
bool _output_time
Whether or not to output the Time column.
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
const unsigned short DEFAULT_CSV_PRECISION
unsigned int _csv_precision
*.csv file precision, defaults to 14
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.
const std::string DEFAULT_CSV_DELIMITER

◆ 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 95 of file FormattedTable.C.

100  _append(o._append),
105 {
106  if (_output_file.is_open())
107  mooseError("Copying a FormattedTable with an open stream is not supported");
108 
109  for (const auto & it : o._data)
110  _data.emplace_back(it.first, it.second);
111 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _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::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
bool _output_time
Whether or not to output the Time column.
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
unsigned int _csv_precision
*.csv file precision, defaults to 14
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...
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.

◆ ~FormattedTable()

FormattedTable::~FormattedTable ( )

The destructor is used to close the file handle.

Definition at line 113 of file FormattedTable.C.

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

Member Function Documentation

◆ addData() [1/3]

void FormattedTable::addData ( const std::string &  name,
Real  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 134 of file FormattedTable.C.

Referenced by TableOutput::outputPostprocessors(), TableOutput::outputScalarVariables(), and TableOutput::outputVectorPostprocessors().

135 {
136  if (empty())
137  mooseError("No Data stored in the the FormattedTable");
138 
139  auto back_it = _data.rbegin();
140  back_it->second[name] = value;
141 
142  if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
143  {
144  _column_names.push_back(name);
145  _column_names_unsorted = true;
146  }
147 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ addData() [2/3]

void FormattedTable::addData ( const std::string &  name,
Real  value,
Real  time 
)

Method for adding data to the output table.

The dependent variable is named "time"

Definition at line 150 of file FormattedTable.C.

151 {
152  auto back_it = _data.rbegin();
153 
154  mooseAssert(back_it == _data.rend() || !MooseUtils::absoluteFuzzyLessThan(time, back_it->first),
155  "Attempting to add data to FormattedTable with the dependent variable in a "
156  "non-increasing order.\nDid you mean to use addData(std::string &, const "
157  "std::vector<Real> &)?");
158 
159  // See if the current "row" is already in the table
160  if (back_it == _data.rend() || !MooseUtils::absoluteFuzzyEqual(time, back_it->first))
161  {
162  _data.emplace_back(time, std::map<std::string, Real>());
163  back_it = _data.rbegin();
164  }
165  // Insert or update value
166  back_it->second[name] = value;
167 
168  if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
169  {
170  _column_names.push_back(name);
171  _column_names_unsorted = true;
172  }
173 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:196
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
bool absoluteFuzzyLessThan(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within an absolute tolerance...
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ addData() [3/3]

void FormattedTable::addData ( const std::string &  name,
const std::vector< Real > &  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 176 of file FormattedTable.C.

177 {
178  for (auto i = beginIndex(vector); i < vector.size(); ++i)
179  {
180  if (i == _data.size())
181  _data.emplace_back(i, std::map<std::string, Real>());
182 
183  mooseAssert(MooseUtils::absoluteFuzzyEqual(_data[i].first, i),
184  "Inconsistent indexing in VPP vector");
185 
186  auto & curr_entry = _data[i];
187  curr_entry.second[name] = vector[i];
188  }
189 
190  if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
191  {
192  _column_names.push_back(name);
193  _column_names_unsorted = true;
194  }
195 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:196
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ addRow()

void FormattedTable::addRow ( Real  time)

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

Definition at line 128 of file FormattedTable.C.

Referenced by TableOutput::outputPostprocessors().

129 {
130  _data.emplace_back(time, std::map<std::string, Real>());
131 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...

◆ 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 122 of file FormattedTable.C.

Referenced by CSV::initialSetup().

123 {
124  _append = append_existing_file;
125 }
bool _append
Keeps track of whether we want to open an existing file for appending or overwriting.

◆ clear()

void FormattedTable::clear ( )

Definition at line 576 of file FormattedTable.C.

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

577 {
578  _data.clear();
579 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...

◆ close()

void FormattedTable::close ( )
private

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

Definition at line 53 of file FormattedTable.C.

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

54 {
55  if (!_output_file.is_open())
56  return;
57  _output_file.flush();
58  _output_file.close();
59  _output_file_name = "";
60 }
std::string _output_file_name
The optional output file stream.
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)

◆ empty()

bool FormattedTable::empty ( ) const

Returns a boolean value based on whether the FormattedTable contains data or not.

Definition at line 116 of file FormattedTable.C.

Referenced by addData(), getLastData(), getLastTime(), Gnuplot::output(), CSV::output(), TableOutput::outputPostprocessors(), Console::outputPostprocessors(), and Console::outputScalarVariables().

117 {
118  return _data.empty();
119 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...

◆ getLastData()

Real & FormattedTable::getLastData ( const std::string &  name)

Retrieve Data for last value of given name.

Definition at line 205 of file FormattedTable.C.

206 {
207  mooseAssert(!empty(), "No Data stored in the FormattedTable");
208 
209  auto & last_data_map = _data.rbegin()->second;
210  auto it = last_data_map.find(name);
211  if (it == last_data_map.end())
212  mooseError("No Data found for name: " + name);
213 
214  return it->second;
215 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...

◆ getLastTime()

Real FormattedTable::getLastTime ( )

Retrieve the last time (or independent variable) value.

Definition at line 198 of file FormattedTable.C.

Referenced by TableOutput::outputPostprocessors().

199 {
200  mooseAssert(!empty(), "No Data stored in the FormattedTable");
201  return _data.rbegin()->first;
202 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...

◆ getTermWidth()

unsigned short FormattedTable::getTermWidth ( bool  use_environment) const
protected

Returns the width of the terminal using sys/ioctl.

Initialize the value we intend to populate just in case the system call fails

Definition at line 582 of file FormattedTable.C.

Referenced by printTable().

583 {
584  struct winsize w;
589  w.ws_col = std::numeric_limits<unsigned short>::max();
590 
591  if (use_environment)
592  {
593  char * pps_width = std::getenv("MOOSE_PPS_WIDTH");
594  if (pps_width != NULL)
595  {
596  std::stringstream ss(pps_width);
597  ss >> w.ws_col;
598  }
599  }
600  else
601  {
602  try
603  {
604  ioctl(0, TIOCGWINSZ, &w);
605  }
606  catch (...)
607  {
608  // Something bad happened, make sure we have a sane value
609  w.ws_col = std::numeric_limits<unsigned short>::max();
610  }
611  }
612 
613  return w.ws_col;
614 }

◆ getWidthModes()

MooseEnum FormattedTable::getWidthModes ( )
static

Definition at line 617 of file FormattedTable.C.

Referenced by validParams< Console >(), and validParams< TableOutput >().

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

◆ makeGnuplot()

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

Definition at line 484 of file FormattedTable.C.

Referenced by Gnuplot::output().

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

◆ 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 63 of file FormattedTable.C.

Referenced by printCSV(), and printTable().

64 {
65  if (_output_file.is_open() && _output_file_name == file_name)
66  return;
67  close();
68  _output_file_name = file_name;
69 
70  std::ios_base::openmode open_flags = std::ios::out;
71  if (_append)
72  open_flags |= std::ios::app;
73  else
74  {
75  open_flags |= std::ios::trunc;
77  _headers_output = false;
78  }
79 
80  _output_file.open(file_name.c_str(), open_flags);
81  if (_output_file.fail())
82  mooseError("Unable to open file ", file_name);
83 }
std::string _output_file_name
The optional output file stream.
std::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void close()
Close the underlying output file stream if any. This is idempotent.
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
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.

◆ outputTimeColumn()

void FormattedTable::outputTimeColumn ( bool  output_time)
inline

Set whether or not to output time column.

Definition at line 106 of file FormattedTable.h.

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

106 { _output_time = output_time; }
bool _output_time
Whether or not to output the Time column.

◆ 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 363 of file FormattedTable.C.

Referenced by CSV::output().

364 {
365  open(file_name);
366 
367  if (_output_row_index == 0)
368  {
375  if (align)
376  {
377  // Set the initial width to the names of the columns
378  _align_widths["time"] = 4;
379 
380  for (const auto & col_name : _column_names)
381  _align_widths[col_name] = col_name.size();
382 
383  // Loop through the various times
384  for (const auto & it : _data)
385  {
386  // Update the time _align_width
387  {
388  std::ostringstream oss;
389  oss << std::setprecision(_csv_precision) << it.first;
390  unsigned int w = oss.str().size();
391  _align_widths["time"] = std::max(_align_widths["time"], w);
392  }
393 
394  // Loop through the data for the current time and update the _align_widths
395  for (const auto & jt : it.second)
396  {
397  std::ostringstream oss;
398  oss << std::setprecision(_csv_precision) << jt.second;
399  unsigned int w = oss.str().size();
400  _align_widths[jt.first] = std::max(_align_widths[jt.first], w);
401  }
402  }
403  }
404 
405  // Output Header
406  if (!_headers_output)
407  {
408  if (_output_time)
409  {
410  if (align)
411  _output_file << std::setw(_align_widths["time"]) << "time";
412  else
413  _output_file << "time";
414  _headers_output = true;
415  }
416 
417  for (const auto & col_name : _column_names)
418  {
419  if (_headers_output)
421 
422  if (align)
423  _output_file << std::right << std::setw(_align_widths[col_name]) << col_name;
424  else
425  _output_file << col_name;
426  _headers_output = true;
427  }
428  _output_file << "\n";
429  }
430  }
431 
432  for (; _output_row_index < _data.size(); ++_output_row_index)
433  {
434  if (_output_row_index % interval == 0)
436  }
437 
438  _output_file.flush();
439 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
std::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
void open(const std::string &file_name)
Open or switch the underlying file stream to point to file_name. This is idempotent.
bool _output_time
Whether or not to output the Time column.
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
void printRow(std::pair< Real, std::map< std::string, Real >> &row_data, bool align)
std::map< std::string, unsigned int > _align_widths
Alignment widths (only used if asked to print aligned to CSV output)
unsigned int _csv_precision
*.csv file precision, defaults to 14
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...
bool _headers_output
Keeps track of whether the header has been 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 236 of file FormattedTable.C.

Referenced by printOmittedRow(), and printRowDivider().

242 {
243  out.fill(fill_char);
244  out << std::right << intersect_char << std::setw(_column_width + 2) << intersect_char;
245  for (auto header_it = col_begin; header_it != col_end; ++header_it)
246  out << std::setw(col_widths[*header_it] + 2) << intersect_char;
247  out << "\n";
248 
249  // Clear the fill character
250  out.fill(' ');
251 }
static const unsigned short _column_width
The single cell width used for all columns in the table.

◆ 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 218 of file FormattedTable.C.

Referenced by printTablePiece().

222 {
223  printNoDataRow(':', ' ', out, col_widths, col_begin, col_end);
224 }
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

◆ printRow()

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

Definition at line 442 of file FormattedTable.C.

Referenced by printCSV().

443 {
444  bool first = true;
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, Real> & 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 }
bool _output_time
Whether or not to output the Time column.
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
std::map< std::string, unsigned int > _align_widths
Alignment widths (only used if asked to print aligned to CSV output)
unsigned int _csv_precision
*.csv file precision, defaults to 14
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ 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 227 of file FormattedTable.C.

Referenced by printTablePiece().

231 {
232  printNoDataRow('+', '-', out, col_widths, col_begin, col_end);
233 }
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

◆ printTable() [1/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 261 of file FormattedTable.C.

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

262 {
263  printTable(out, last_n_entries, MooseEnum("ENVIRONMENT=-1", "ENVIRONMENT"));
264 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:32
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 267 of file FormattedTable.C.

270 {
271  unsigned short term_width;
272 
273  if (suggested_term_width == "ENVIRONMENT")
274  term_width = getTermWidth(true);
275  else if (suggested_term_width == "AUTO")
276  term_width = getTermWidth(false);
277  else
278  term_width = MooseUtils::stringToInteger(suggested_term_width);
279 
280  if (term_width < _min_pps_width)
281  term_width = _min_pps_width;
282 
283  std::vector<std::string>::iterator col_it = _column_names.begin();
284  std::vector<std::string>::iterator col_end = _column_names.end();
285 
286  std::vector<std::string>::iterator curr_begin = col_it;
287  std::vector<std::string>::iterator curr_end;
288  while (col_it != col_end)
289  {
290  std::map<std::string, unsigned short> col_widths;
291  unsigned int curr_width = _column_width + 4;
292  unsigned int cols_in_group = 0;
293  while (curr_width < term_width && col_it != col_end)
294  {
295  curr_end = col_it;
296  col_widths[*col_it] = col_it->length() > _column_width ? col_it->length() + 1 : _column_width;
297 
298  curr_width += col_widths[*col_it] + 3;
299  ++col_it;
300  ++cols_in_group;
301  }
302  if (col_it != col_end && cols_in_group >= 2)
303  {
304  // curr_width -= col_widths[*curr_end];
305  col_widths.erase(*curr_end);
306  col_it = curr_end;
307  }
308  else
309  curr_end = col_it;
310 
311  printTablePiece(out, last_n_entries, col_widths, curr_begin, curr_end);
312  curr_begin = curr_end;
313  }
314 }
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=false)
Robust string to integer conversion that fails for cases such at "1foo".
Definition: MooseUtils.C:727
static const unsigned short _min_pps_width
The absolute minimum PPS table width.
unsigned short getTermWidth(bool use_environment) const
Returns the width of the terminal using sys/ioctl.
static const unsigned short _column_width
The single cell width used for all columns in the table.
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ printTable() [3/3]

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

Definition at line 254 of file FormattedTable.C.

255 {
256  open(file_name);
258 }
void open(const std::string &file_name)
Open or switch the underlying file stream to point to file_name. This is idempotent.
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
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.

◆ 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 317 of file FormattedTable.C.

Referenced by printTable().

322 {
326  printRowDivider(out, col_widths, col_begin, col_end);
327  out << "|" << std::setw(_column_width) << std::left << " time"
328  << " |";
329  for (auto header_it = col_begin; header_it != col_end; ++header_it)
330  out << " " << std::setw(col_widths[*header_it]) << *header_it << "|";
331  out << "\n";
332  printRowDivider(out, col_widths, col_begin, col_end);
333 
334  auto data_it = _data.begin();
335  if (last_n_entries)
336  {
337  if (_data.size() > last_n_entries)
338  {
339  // Print a blank row to indicate that values have been ommited
340  printOmittedRow(out, col_widths, col_begin, col_end);
341 
342  // Jump to the right place in the vector
343  data_it += _data.size() - last_n_entries;
344  }
345  }
346  // Now print the remaining data rows
347  for (; data_it != _data.end(); ++data_it)
348  {
349  out << "|" << std::right << std::setw(_column_width) << std::scientific << data_it->first
350  << " |";
351  for (auto header_it = col_begin; header_it != col_end; ++header_it)
352  {
353  auto & tmp = data_it->second;
354  out << std::setw(col_widths[*header_it]) << tmp[*header_it] << " |";
355  }
356  out << "\n";
357  }
358 
359  printRowDivider(out, col_widths, col_begin, col_end);
360 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
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
static const unsigned short _column_width
The single cell width used for all columns in the table.

◆ setDelimiter()

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

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

Definition at line 140 of file FormattedTable.h.

Referenced by CSV::initialSetup().

140 { _csv_delimiter = delimiter; }
std::string _csv_delimiter
*.csv file delimiter, defaults to ","

◆ 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 145 of file FormattedTable.h.

Referenced by CSV::initialSetup().

145 { _csv_precision = precision; }
unsigned int _csv_precision
*.csv file precision, defaults to 14

◆ sortColumns()

void FormattedTable::sortColumns ( )

Sorts columns alphabetically.

Definition at line 623 of file FormattedTable.C.

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

624 {
626  {
627  std::sort(_column_names.begin(), _column_names.end());
628  _column_names_unsorted = false;
629  }
630 }
bool _column_names_unsorted
Flag indicating that sorting is necessary (used by sortColumns method).
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...

◆ writeExodus()

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

Friends And Related Function Documentation

◆ dataLoad

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

Definition at line 43 of file FormattedTable.C.

44 {
45  loadHelper(stream, table._data, context);
46  loadHelper(stream, table._align_widths, context);
47  loadHelper(stream, table._column_names, context);
48  loadHelper(stream, table._output_row_index, context);
49  loadHelper(stream, table._headers_output, context);
50 }
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:777

◆ dataStore

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

Definition at line 32 of file FormattedTable.C.

33 {
34  storeHelper(stream, table._data, context);
35  storeHelper(stream, table._align_widths, context);
36  storeHelper(stream, table._column_names, context);
37  storeHelper(stream, table._output_row_index, context);
38  storeHelper(stream, table._headers_output, context);
39 }
std::vector< std::pair< Real, std::map< std::string, Real > > > _data
Data structure for the console table: The first part of the pair tracks the independent variable (nor...
std::size_t _output_row_index
Keeps track of the index indicating which vector elements have been output.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:713
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. ...
bool _headers_output
Keeps track of whether the header has been output.

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 189 of file FormattedTable.h.

Referenced by dataLoad(), dataStore(), 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 230 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 192 of file FormattedTable.h.

Referenced by addData(), dataLoad(), dataStore(), 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 242 of file FormattedTable.h.

Referenced by 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 195 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 236 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 239 of file FormattedTable.h.

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

◆ _data

std::vector<std::pair<Real, std::map<std::string, Real> > > 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 186 of file FormattedTable.h.

Referenced by addData(), addRow(), clear(), dataLoad(), dataStore(), empty(), 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 227 of file FormattedTable.h.

Referenced by dataLoad(), dataStore(), open(), and printCSV().

◆ _min_pps_width

const unsigned short FormattedTable::_min_pps_width = 40
staticprotected

The absolute minimum PPS table width.

Definition at line 198 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 213 of file FormattedTable.h.

Referenced by 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 210 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 219 of file FormattedTable.h.

Referenced by dataLoad(), dataStore(), open(), and printCSV().

◆ _output_time

bool FormattedTable::_output_time
private

Whether or not to output the Time column.

Definition at line 233 of file FormattedTable.h.

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


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