The SKIRT project
advanced radiative transfer for astrophysics
Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
TextOutFile Class Reference

#include <TextOutFile.hpp>

Inheritance diagram for TextOutFile:
Inheritance graph
[legend]

Public Member Functions

 TextOutFile (const SimulationItem *item, string filename, string description)
 
 ~TextOutFile ()
 
void addColumn (string quantityDescription, string unitDescription=string(), char format='e', int precision=9)
 
void close ()
 
void writeLine (string line)
 
template<typename... Values, typename = std::enable_if_t<CompileTimeUtils::isNumericArgList<Values...>()>>
void writeRow (Values... values)
 
void writeRow (vector< double > values)
 

Protected Attributes

std::ofstream _out
 
Units_units
 

Private Member Functions

void writeRowPrivate (size_t n, const double *values)
 

Private Attributes

vector< char > _formats
 
Log_log
 
string _message
 
size_t _ncolumns
 
vector< int > _precisions
 

Detailed Description

This class allows writing text to a file specified in the constructor, with explicit support for formatting columns of floating point or integer numbers. Text is written per line, by calling the writeLine() or writeRow() functions. In a multiprocessing environment, only the root process will be allowed to write to the specified file; calls to writeLine() or writeRow() performed by other processes will have no effect.

Constructor & Destructor Documentation

◆ TextOutFile()

TextOutFile::TextOutFile ( const SimulationItem item,
string  filename,
string  description 
)

The constructor of the TextOutFile class. If the constructor is invoked from the root process, the output file is opened and a stream for the file is initialized. For other processes, the output stream remains uninitialized and is never used, i.e. calling the writeLine() function has no effect. The constructor takes several arguments: (1) item specifies a simulation item in the hierarchy of the caller (usually the caller itself) used to retrieve the output file path and an appropriate logger, and to determine whether this is the root process; (2) filename specifies the name of the file, excluding path, simulation prefix and filename extension; (3) description describes the contents of the file for use in the log message issued after the file is successfully closed.

◆ ~TextOutFile()

TextOutFile::~TextOutFile ( )

The destructor calls the close() function. It is important to call close() or allow the object to go out of scope before logging other messages or starting another significant chunk of work.

Member Function Documentation

◆ addColumn()

void TextOutFile::addColumn ( string  quantityDescription,
string  unitDescription = string(),
char  format = 'e',
int  precision = 9 
)

This function (virtually) adds a new column to the text file, characterized by a certain description and formatting. The format is 'd' for integer values, 'e' for scientific notation, 'f' for floating point notation and 'g' for the most concise 'e' or 'f'. For the 'e' and 'f' formats, the precision represents the number of digits after the decimal point. For the 'g' format, the precision represents the maximum number of significant digits (trailing zeroes are omitted). The description of each column is added to the header of the text file, along with the column number.

◆ close()

void TextOutFile::close ( )

In the root process, this function closes the file and logs an informational message, if the file was not already closed. It is important to call close() or allow the object to go out of scope before logging other messages or starting another significant chunk of work.

◆ writeLine()

void TextOutFile::writeLine ( string  line)

This function writes the specified string to the file as a new line. If the calling process is not the root, this function will have no effect.

◆ writeRow() [1/2]

template<typename... Values, typename = std::enable_if_t<CompileTimeUtils::isNumericArgList<Values...>()>>
void TextOutFile::writeRow ( Values...  values)
inline

This template function writes the specified list of values to the text file, on a single row where adjacent values are seperated by a space. The values are formatted according to the 'format' and 'precision' specified by the addColumn function. If the number of values in the list does not match the number of columns, a FatalError is thrown.

◆ writeRow() [2/2]

void TextOutFile::writeRow ( vector< double >  values)

This function writes the specified list of (double) values to the text file, on a single row where adjacent values are seperated by a space. The values are formatted according to the 'format' and 'precision' specified by the addColumn function. If the number of values in the list does not match the number of columns, a FatalError is thrown.

◆ writeRowPrivate()

void TextOutFile::writeRowPrivate ( size_t  n,
const double *  values 
)
private

This function writes the specified list of (double) values to the text file with the same semantics as the other writeRow() functions. It is intended for private use from the template writeRow() functions.


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