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 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 & 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 _stream_open
 Keeps track of whether the current stream is open or not. 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 41 of file FormattedTable.h.

Constructor & Destructor Documentation

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

91  : _output_row_index(0),
92  _stream_open(false),
93  _append(false),
94  _output_time(true),
97 {
98 }
bool _stream_open
Keeps track of whether the current stream is open or not.
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 _append
Keeps track of whether we want to open an existing file for appending or overwriting.
const std::string DEFAULT_CSV_DELIMITER
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 100 of file FormattedTable.C.

102  _output_file_name(""),
105  _append(o._append),
110 {
111  if (_stream_open)
112  mooseError("Copying a FormattedTable with an open stream is not supported");
113 
114  for (const auto & it : o._data)
115  _data.emplace_back(it.first, it.second);
116 }
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 _stream_open
Keeps track of whether the current stream is open or not.
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:182
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 ","
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 _append
Keeps track of whether we want to open an existing file for appending or overwriting.
FormattedTable::~FormattedTable ( )

The destructor is used to close the file handle.

Definition at line 118 of file FormattedTable.C.

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

Member Function Documentation

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

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

134 {
135  auto back_it = _data.rbegin();
136 
137  mooseAssert(back_it == _data.rend() || !MooseUtils::absoluteFuzzyLessThan(time, back_it->first),
138  "Attempting to add data to FormattedTable with the dependent variable in a "
139  "non-increasing order.\nDid you mean to use addData(std::string &, const "
140  "std::vector<Real> &)?");
141 
142  // See if the current "row" is already in the table
143  if (back_it == _data.rend() || !MooseUtils::absoluteFuzzyEqual(time, back_it->first))
144  {
145  _data.emplace_back(time, std::map<std::string, Real>());
146  back_it = _data.rbegin();
147  }
148  // Insert or update value
149  back_it->second[name] = value;
150 
151  if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
152  _column_names.push_back(name);
153  _column_names_unsorted = true;
154 }
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 _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. ...
bool absoluteFuzzyEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
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 157 of file FormattedTable.C.

158 {
159  for (auto i = beginIndex(vector); i < vector.size(); ++i)
160  {
161  if (i == _data.size())
162  _data.emplace_back(i, std::map<std::string, Real>());
163 
164  mooseAssert(MooseUtils::absoluteFuzzyEqual(_data[i].first, i),
165  "Inconsistent indexing in VPP vector");
166 
167  auto & curr_entry = _data[i];
168  curr_entry.second[name] = vector[i];
169  }
170 
171  if (std::find(_column_names.begin(), _column_names.end(), name) == _column_names.end())
172  _column_names.push_back(name);
173  _column_names_unsorted = true;
174 }
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 _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. ...
bool absoluteFuzzyEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
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 127 of file FormattedTable.C.

Referenced by CSV::initialSetup().

128 {
129  _append = append_existing_file;
130 }
bool _append
Keeps track of whether we want to open an existing file for appending or overwriting.
void FormattedTable::clear ( )

Definition at line 556 of file FormattedTable.C.

Referenced by TableOutput::outputVectorPostprocessors().

557 {
558  _data.clear();
559 }
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 FormattedTable::close ( )
private

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

Definition at line 59 of file FormattedTable.C.

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

60 {
61  if (!_stream_open)
62  return;
63  _output_file.flush();
64  _output_file.close();
65  _stream_open = false;
66  _output_file_name = "";
67 }
bool _stream_open
Keeps track of whether the current stream is open or not.
std::string _output_file_name
The optional output file stream.
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
bool FormattedTable::empty ( ) const

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

Definition at line 121 of file FormattedTable.C.

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

122 {
123  return _data.empty();
124 }
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...
Real & FormattedTable::getLastData ( const std::string &  name)

Retrieve Data for last value of given name.

Definition at line 177 of file FormattedTable.C.

178 {
179  mooseAssert(!empty(), "No Data stored in the FormattedTable");
180 
181  auto & last_data_map = _data.rbegin()->second;
182  auto it = last_data_map.find(name);
183  if (it == last_data_map.end())
184  mooseError("No Data found for name: " + name);
185 
186  return it->second;
187 }
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. ...
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
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 562 of file FormattedTable.C.

Referenced by printTable().

563 {
564  struct winsize w;
569  w.ws_col = std::numeric_limits<unsigned short>::max();
570 
571  if (use_environment)
572  {
573  char * pps_width = std::getenv("MOOSE_PPS_WIDTH");
574  if (pps_width != NULL)
575  {
576  std::stringstream ss(pps_width);
577  ss >> w.ws_col;
578  }
579  }
580  else
581  {
582  try
583  {
584  ioctl(0, TIOCGWINSZ, &w);
585  }
586  catch (...)
587  {
588  // Something bad happened, make sure we have a sane value
589  w.ws_col = std::numeric_limits<unsigned short>::max();
590  }
591  }
592 
593  return w.ws_col;
594 }
MooseEnum FormattedTable::getWidthModes ( )
static

Definition at line 597 of file FormattedTable.C.

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

598 {
599  return MooseEnum("ENVIRONMENT=-1 AUTO=0 80=80 120=120 160=160", "ENVIRONMENT", true);
600 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
void FormattedTable::makeGnuplot ( const std::string &  base_file,
const std::string &  format 
)

Definition at line 457 of file FormattedTable.C.

Referenced by Gnuplot::output().

458 {
459  // TODO: run this once at end of simulation, right now it runs every iteration
460  // TODO: do I need to be more careful escaping column names?
461  // Note: open and close the files each time, having open files may mess with gnuplot
462 
463  // supported filetypes: ps, png
464  std::string extension, terminal;
465  if (format == "png")
466  {
467  extension = ".png";
468  terminal = "png";
469  }
470 
471  else if (format == "ps")
472  {
473  extension = ".ps";
474  terminal = "postscript";
475  }
476 
477  else if (format == "gif")
478  {
479  extension = ".gif";
480  terminal = "gif";
481  }
482 
483  else
484  mooseError("gnuplot format \"" + format + "\" is not supported.");
485 
486  // Write the data to disk
487  std::string dat_name = base_file + ".dat";
488  std::ofstream datfile;
489  datfile.open(dat_name.c_str(), std::ios::trunc | std::ios::out);
490 
491  datfile << "# time";
492  for (const auto & col_name : _column_names)
493  datfile << '\t' << col_name;
494  datfile << '\n';
495 
496  for (auto & data_it : _data)
497  {
498  datfile << data_it.first;
499  for (const auto & col_name : _column_names)
500  {
501  auto & tmp = data_it.second;
502  datfile << '\t' << tmp[col_name];
503  }
504  datfile << '\n';
505  }
506  datfile.flush();
507  datfile.close();
508 
509  // Write the gnuplot script
510  std::string gp_name = base_file + ".gp";
511  std::ofstream gpfile;
512  gpfile.open(gp_name.c_str(), std::ios::trunc | std::ios::out);
513 
514  gpfile << gnuplot::before_terminal << terminal << gnuplot::before_ext << extension
516 
517  // plot all postprocessors in one plot
518  int column = 2;
519  for (const auto & col_name : _column_names)
520  {
521  gpfile << " '" << dat_name << "' using 1:" << column << " title '" << col_name
522  << "' with linespoints";
523  column++;
524  if (column - 2 < static_cast<int>(_column_names.size()))
525  gpfile << ", \\\n";
526  }
527  gpfile << "\n\n";
528 
529  // plot the postprocessors individually
530  column = 2;
531  for (const auto & col_name : _column_names)
532  {
533  gpfile << "set output '" << col_name << extension << "'\n";
534  gpfile << "set ylabel '" << col_name << "'\n";
535  gpfile << "plot '" << dat_name << "' using 1:" << column << " title '" << col_name
536  << "' with linespoints\n\n";
537  column++;
538  }
539 
540  gpfile.flush();
541  gpfile.close();
542 
543  // Run the gnuplot script
544  /* We aren't going to run gnuplot automatically
545 
546  if (!system(NULL))
547  mooseError("No way to run gnuplot on this computer");
548 
549  std::string command = "gnuplot " + gp_name;
550  if (system(command.c_str()))
551  mooseError("gnuplot command failed");
552  */
553 }
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:182
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
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 70 of file FormattedTable.C.

Referenced by printCSV(), and printTable().

71 {
72  if (_stream_open && _output_file_name == file_name)
73  return;
74  close();
75  _output_file_name = file_name;
76 
77  std::ios_base::openmode open_flags = std::ios::out;
78  if (_append)
79  open_flags |= std::ios::app;
80  else
81  {
82  open_flags |= std::ios::trunc;
84  }
85 
86  _output_file.open(file_name.c_str(), open_flags);
87  _stream_open = true;
88 }
bool _stream_open
Keeps track of whether the current stream is open or not.
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 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 _append
Keeps track of whether we want to open an existing file for appending or overwriting.
void FormattedTable::outputTimeColumn ( bool  output_time)
inline

Set whether or not to output time column.

Definition at line 95 of file FormattedTable.h.

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

95 { _output_time = output_time; }
bool _output_time
Whether or not to output the Time column.
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 335 of file FormattedTable.C.

Referenced by CSV::output().

336 {
337  open(file_name);
338 
339  if (_output_row_index == 0)
340  {
347  if (align)
348  {
349  // Set the initial width to the names of the columns
350  _align_widths["time"] = 4;
351 
352  for (const auto & col_name : _column_names)
353  _align_widths[col_name] = col_name.size();
354 
355  // Loop through the various times
356  for (const auto & it : _data)
357  {
358  // Update the time _align_width
359  {
360  std::ostringstream oss;
361  oss << std::setprecision(_csv_precision) << it.first;
362  unsigned int w = oss.str().size();
363  _align_widths["time"] = std::max(_align_widths["time"], w);
364  }
365 
366  // Loop through the data for the current time and update the _align_widths
367  for (const auto & jt : it.second)
368  {
369  std::ostringstream oss;
370  oss << std::setprecision(_csv_precision) << jt.second;
371  unsigned int w = oss.str().size();
372  _align_widths[jt.first] = std::max(_align_widths[jt.first], w);
373  }
374  }
375  }
376 
377  // Output Header
378  {
379  bool first = true;
380 
381  if (_output_time)
382  {
383  if (align)
384  _output_file << std::setw(_align_widths["time"]) << "time";
385  else
386  _output_file << "time";
387  first = false;
388  }
389 
390  for (const auto & col_name : _column_names)
391  {
392  if (!first)
394 
395  if (align)
396  _output_file << std::right << std::setw(_align_widths[col_name]) << col_name;
397  else
398  _output_file << col_name;
399  first = false;
400  }
401  _output_file << "\n";
402  }
403  }
404 
405  for (; _output_row_index < _data.size(); ++_output_row_index)
406  {
407  if (_output_row_index % interval == 0)
408  printRow(_data[_output_row_index], align);
409  }
410 
411  _output_file.flush();
412 }
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. ...
void FormattedTable::printEnsight ( const std::string &  file_name)
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 208 of file FormattedTable.C.

Referenced by printOmittedRow(), and printRowDivider().

214 {
215  out.fill(fill_char);
216  out << std::right << intersect_char << std::setw(_column_width + 2) << intersect_char;
217  for (auto header_it = col_begin; header_it != col_end; ++header_it)
218  out << std::setw(col_widths[*header_it] + 2) << intersect_char;
219  out << "\n";
220 
221  // Clear the fill character
222  out.fill(' ');
223 }
static const unsigned short _column_width
The single cell width used for all columns in the table.
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 190 of file FormattedTable.C.

Referenced by printTablePiece().

194 {
195  printNoDataRow(':', ' ', out, col_widths, col_begin, col_end);
196 }
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
void FormattedTable::printRow ( std::pair< Real, std::map< std::string, Real >> &  row_data,
bool  align 
)
private

Definition at line 415 of file FormattedTable.C.

Referenced by printCSV().

416 {
417  bool first = true;
418 
419  if (_output_time)
420  {
421  if (align)
422  _output_file << std::setprecision(_csv_precision) << std::right
423  << std::setw(_align_widths["time"]) << row_data.first;
424  else
425  _output_file << std::setprecision(_csv_precision) << row_data.first;
426  first = false;
427  }
428 
429  for (const auto & col_name : _column_names)
430  {
431  std::map<std::string, Real> & tmp = row_data.second;
432 
433  if (!first)
435  else
436  first = false;
437 
438  if (align)
439  _output_file << std::setprecision(_csv_precision) << std::right
440  << std::setw(_align_widths[col_name]) << tmp[col_name];
441  else
442  _output_file << std::setprecision(_csv_precision) << tmp[col_name];
443  }
444  _output_file << "\n";
445 }
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. ...
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 199 of file FormattedTable.C.

Referenced by printTablePiece().

203 {
204  printNoDataRow('+', '-', out, col_widths, col_begin, col_end);
205 }
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
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 233 of file FormattedTable.C.

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

234 {
235  printTable(out, last_n_entries, MooseEnum("ENVIRONMENT=-1", "ENVIRONMENT"));
236 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
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 FormattedTable::printTable ( std::ostream &  out,
unsigned int  last_n_entries,
const MooseEnum suggested_term_width 
)

Definition at line 239 of file FormattedTable.C.

242 {
243  unsigned short term_width;
244 
245  if (suggested_term_width == "ENVIRONMENT")
246  term_width = getTermWidth(true);
247  else if (suggested_term_width == "AUTO")
248  term_width = getTermWidth(false);
249  else
250  term_width = suggested_term_width;
251 
252  if (term_width < _min_pps_width)
253  term_width = _min_pps_width;
254 
255  std::vector<std::string>::iterator col_it = _column_names.begin();
256  std::vector<std::string>::iterator col_end = _column_names.end();
257 
258  std::vector<std::string>::iterator curr_begin = col_it;
259  std::vector<std::string>::iterator curr_end;
260  while (col_it != col_end)
261  {
262  std::map<std::string, unsigned short> col_widths;
263  unsigned int curr_width = _column_width + 4;
264  unsigned int cols_in_group = 0;
265  while (curr_width < term_width && col_it != col_end)
266  {
267  curr_end = col_it;
268  col_widths[*col_it] = col_it->length() > _column_width ? col_it->length() + 1 : _column_width;
269 
270  curr_width += col_widths[*col_it] + 3;
271  ++col_it;
272  ++cols_in_group;
273  }
274  if (col_it != col_end && cols_in_group >= 2)
275  {
276  // curr_width -= col_widths[*curr_end];
277  col_widths.erase(*curr_end);
278  col_it = curr_end;
279  }
280  else
281  curr_end = col_it;
282 
283  printTablePiece(out, last_n_entries, col_widths, curr_begin, curr_end);
284  curr_begin = curr_end;
285  }
286 }
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)
unsigned short getTermWidth(bool use_environment) const
Returns the width of the terminal using sys/ioctl.
static const unsigned short _min_pps_width
The absolute minimum PPS table width.
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. ...
void FormattedTable::printTable ( const std::string &  file_name)

Definition at line 226 of file FormattedTable.C.

227 {
228  open(file_name);
230 }
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.
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 289 of file FormattedTable.C.

Referenced by printTable().

294 {
298  printRowDivider(out, col_widths, col_begin, col_end);
299  out << "|" << std::setw(_column_width) << std::left << " time"
300  << " |";
301  for (auto header_it = col_begin; header_it != col_end; ++header_it)
302  out << " " << std::setw(col_widths[*header_it]) << *header_it << "|";
303  out << "\n";
304  printRowDivider(out, col_widths, col_begin, col_end);
305 
306  auto data_it = _data.begin();
307  if (last_n_entries)
308  {
309  if (_data.size() > last_n_entries)
310  {
311  // Print a blank row to indicate that values have been ommited
312  printOmittedRow(out, col_widths, col_begin, col_end);
313 
314  // Jump to the right place in the vector
315  data_it += _data.size() - last_n_entries;
316  }
317  }
318  // Now print the remaining data rows
319  for (; data_it != _data.end(); ++data_it)
320  {
321  out << "|" << std::right << std::setw(_column_width) << std::scientific << data_it->first
322  << " |";
323  for (auto header_it = col_begin; header_it != col_end; ++header_it)
324  {
325  auto & tmp = data_it->second;
326  out << std::setw(col_widths[*header_it]) << tmp[*header_it] << " |";
327  }
328  out << "\n";
329  }
330 
331  printRowDivider(out, col_widths, col_begin, col_end);
332 }
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...
static const unsigned short _column_width
The single cell width used for all columns in the table.
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
void FormattedTable::setDelimiter ( std::string  delimiter)
inline

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

Definition at line 129 of file FormattedTable.h.

Referenced by CSV::initialSetup().

129 { _csv_delimiter = delimiter; }
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
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 134 of file FormattedTable.h.

Referenced by CSV::initialSetup().

134 { _csv_precision = precision; }
unsigned int _csv_precision
*.csv file precision, defaults to 14
void FormattedTable::sortColumns ( )

Sorts columns alphabetically.

Definition at line 603 of file FormattedTable.C.

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

604 {
606  {
607  std::sort(_column_names.begin(), _column_names.end());
608  _column_names_unsorted = false;
609  }
610 }
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. ...
void FormattedTable::writeExodus ( ExodusII_IO *  ex_out,
Real  time 
)

Friends And Related Function Documentation

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

Definition at line 47 of file FormattedTable.C.

48 {
49  loadHelper(stream, table._data, context);
50  loadHelper(stream, table._align_widths, context);
51  loadHelper(stream, table._column_names, context);
52  loadHelper(stream, table._output_row_index, context);
53 
54  // Don't assume that the stream is open if we've restored.
55  table._stream_open = false;
56 }
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:592
void dataStore ( std::ostream &  stream,
FormattedTable table,
void *  context 
)
friend

Definition at line 37 of file FormattedTable.C.

38 {
39  storeHelper(stream, table._data, context);
40  storeHelper(stream, table._align_widths, context);
41  storeHelper(stream, table._column_names, context);
42  storeHelper(stream, table._output_row_index, context);
43 }
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:528
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. ...

Member Data Documentation

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

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

bool FormattedTable::_append
private

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

Definition at line 214 of file FormattedTable.h.

Referenced by append(), and open().

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

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

bool FormattedTable::_column_names_unsorted = true
private

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

Definition at line 226 of file FormattedTable.h.

Referenced by addData(), and sortColumns().

const unsigned short FormattedTable::_column_width = 15
staticprotected

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

Definition at line 184 of file FormattedTable.h.

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

std::string FormattedTable::_csv_delimiter
private

*.csv file delimiter, defaults to ","

Definition at line 220 of file FormattedTable.h.

Referenced by printCSV(), and printRow().

unsigned int FormattedTable::_csv_precision
private

*.csv file precision, defaults to 14

Definition at line 223 of file FormattedTable.h.

Referenced by printCSV(), and printRow().

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

Referenced by addData(), clear(), dataLoad(), dataStore(), empty(), FormattedTable(), getLastData(), makeGnuplot(), printCSV(), and printTablePiece().

const unsigned short FormattedTable::_min_pps_width = 40
staticprotected

The absolute minimum PPS table width.

Definition at line 187 of file FormattedTable.h.

Referenced by printTable().

std::ofstream FormattedTable::_output_file
private

The stream handle (corresponds to _output_file_name)

Definition at line 202 of file FormattedTable.h.

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

std::string FormattedTable::_output_file_name
private

The optional output file stream.

Definition at line 199 of file FormattedTable.h.

Referenced by close(), and open().

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

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

bool FormattedTable::_output_time
private

Whether or not to output the Time column.

Definition at line 217 of file FormattedTable.h.

Referenced by printCSV(), and printRow().

bool FormattedTable::_stream_open
private

Keeps track of whether the current stream is open or not.

Definition at line 211 of file FormattedTable.h.

Referenced by close(), dataLoad(), FormattedTable(), and open().


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