www.mooseframework.org
FormattedTable.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "Moose.h"
14 #include "MooseEnum.h"
15 #include "DataIO.h"
16 
17 // C++ includes
18 #include <fstream>
19 
20 // Forward declarations
21 class FormattedTable;
22 namespace libMesh
23 {
24 class ExodusII_IO;
25 }
26 
27 template <>
28 void dataStore(std::ostream & stream, FormattedTable & table, void * context);
29 template <>
30 void dataLoad(std::istream & stream, FormattedTable & v, void * context);
31 
36 {
37 public:
45 
50  FormattedTable(const FormattedTable & o);
51 
56 
60  bool empty() const;
61 
66  void append(bool append_existing_file);
67 
71  void addRow(Real time);
72 
77  void addData(const std::string & name, Real value);
78 
82  void addData(const std::string & name, Real value, Real time);
83 
88  void addData(const std::string & name, const std::vector<Real> & vector);
89 
93  Real getLastTime();
94 
98  Real & getLastData(const std::string & name);
99 
100  void clear();
101 
105  void outputTimeColumn(bool output_time) { _output_time = output_time; }
106 
107  // const std::map<Real, std::map<std::string, Real>> & getData() const { return _data; }
108 
117  void printTable(std::ostream & out, unsigned int last_n_entries = 0);
118  void printTable(std::ostream & out,
119  unsigned int last_n_entries,
120  const MooseEnum & suggested_term_width);
121  void printTable(const std::string & file_name);
122 
128  void printCSV(const std::string & file_name, int interval = 1, bool align = false);
129 
130  void printEnsight(const std::string & file_name);
131  void writeExodus(ExodusII_IO * ex_out, Real time);
132  void makeGnuplot(const std::string & base_file, const std::string & format);
133 
134  static MooseEnum getWidthModes();
135 
139  void setDelimiter(std::string delimiter) { _csv_delimiter = delimiter; }
140 
144  void setPrecision(unsigned int precision) { _csv_precision = precision; }
145 
149  void sortColumns();
150 
151 protected:
152  void printTablePiece(std::ostream & out,
153  unsigned int last_n_entries,
154  std::map<std::string, unsigned short> & col_widths,
155  std::vector<std::string>::iterator & col_begin,
156  std::vector<std::string>::iterator & col_end);
157 
158  void printOmittedRow(std::ostream & out,
159  std::map<std::string, unsigned short> & col_widths,
160  std::vector<std::string>::iterator & col_begin,
161  std::vector<std::string>::iterator & col_end) const;
162  void printRowDivider(std::ostream & out,
163  std::map<std::string, unsigned short> & col_widths,
164  std::vector<std::string>::iterator & col_begin,
165  std::vector<std::string>::iterator & col_end) const;
166 
167  void printNoDataRow(char intersect_char,
168  char fill_char,
169  std::ostream & out,
170  std::map<std::string, unsigned short> & col_widths,
171  std::vector<std::string>::iterator & col_begin,
172  std::vector<std::string>::iterator & col_end) const;
173 
177  unsigned short getTermWidth(bool use_environment) const;
178 
185  std::vector<std::pair<Real, std::map<std::string, Real>>> _data;
186 
188  std::map<std::string, unsigned int> _align_widths;
189 
191  std::vector<std::string> _column_names;
192 
194  static const unsigned short _column_width;
195 
197  static const unsigned short _min_pps_width;
198 
199 private:
201  void close();
202 
204  void open(const std::string & file_name);
205 
206  void printRow(std::pair<Real, std::map<std::string, Real>> & row_data, bool align);
207 
209  std::string _output_file_name;
210 
212  std::ofstream _output_file;
213 
218  std::size_t _output_row_index;
219 
227 
229  bool _append;
230 
233 
235  std::string _csv_delimiter;
236 
238  unsigned int _csv_precision;
239 
242 
243  friend void
244  dataStore<FormattedTable>(std::ostream & stream, FormattedTable & table, void * context);
245  friend void dataLoad<FormattedTable>(std::istream & stream, FormattedTable & v, void * context);
246 };
247 
248 template <>
249 void dataStore(std::ostream & stream, FormattedTable & table, void * context);
250 template <>
251 void dataLoad(std::istream & stream, FormattedTable & v, void * context);
252 
void dataLoad(std::istream &stream, FormattedTable &v, void *context)
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...
~FormattedTable()
The destructor is used to close the file handle.
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)
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 makeGnuplot(const std::string &base_file, const std::string &format)
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 open(const std::string &file_name)
Open or switch the underlying file stream to point to file_name. This is idempotent.
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
bool _output_time
Whether or not to output the Time column.
void close()
Close the underlying output file stream if any. This is idempotent.
void writeExodus(ExodusII_IO *ex_out, Real time)
void setPrecision(unsigned int precision)
By default printCSV prints output to a precision of 14, this allows this to be changed.
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. ...
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
std::string _csv_delimiter
*.csv file delimiter, defaults to ","
static const unsigned short _min_pps_width
The absolute minimum PPS table width.
void outputTimeColumn(bool output_time)
Set whether or not to output time column.
static MooseEnum getWidthModes()
std::ofstream _output_file
The stream handle (corresponds to _output_file_name)
void append(bool append_existing_file)
Sets append mode which means an existing file is not truncated on opening.
unsigned short getTermWidth(bool use_environment) const
Returns the width of the terminal using sys/ioctl.
void printRow(std::pair< Real, std::map< std::string, Real >> &row_data, bool align)
void dataStore(std::ostream &stream, FormattedTable &table, void *context)
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
std::map< std::string, unsigned int > _align_widths
Alignment widths (only used if asked to print aligned to CSV output)
Real & getLastData(const std::string &name)
Retrieve Data for last value of given name.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
This class is used for building, formatting, and outputting tables of numbers.
unsigned int _csv_precision
*.csv file precision, defaults to 14
static const unsigned short _column_width
The single cell width used for all columns in the table.
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 addData(const std::string &name, Real value)
Method for adding data to the output table.
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.
std::vector< std::string > _column_names
The set of column names updated when data is inserted through the setter methods. ...
FormattedTable()
Default constructor - The default constructor takes an optional parameter to turn off stateful printi...
bool _headers_output
Keeps track of whether the header has been output.
void addRow(Real time)
Force a new row in the table with the passed in time.
void sortColumns()
Sorts columns alphabetically.
void printEnsight(const std::string &file_name)
bool _append
Keeps track of whether we want to open an existing file for appending or overwriting.
void setDelimiter(std::string delimiter)
By default printCSV places "," between each entry, this allows this to be changed.
Real getLastTime()
Retrieve the last time (or independent variable) value.