Class for output files
More...
#include <OFile.h>
Classes | |
class | Field |
Class identifying a single field for fielded output. More... | |
Public Member Functions | |
OFile () | |
Constructor. More... | |
OFile & | addConstantField (const std::string &) |
void | backupAllFiles (const std::string &str) |
This backs up all the files that would have been created with the name str. More... | |
void | backupFile (const std::string &bstring, const std::string &fname) |
Backup a file by giving it a different name. More... | |
OFile & | clearFields () |
Resets the list of fields. More... | |
void | close () |
Closes the file Should be used only for explicitely opened files. More... | |
OFile & | enforceBackup () |
Enforce backup, even if the attached plumed object is restarting. More... | |
OFile & | enforceRestart () |
Enforce restart, also if the attached plumed object is not restarting. More... | |
FileBase & | enforceSuffix (const std::string &suffix) |
Enforce suffix. More... | |
bool | FileExist (const std::string &path) |
Check if the file exists. More... | |
FileBase & | flush () override |
Flush a file. More... | |
OFile & | fmtField (const std::string &) |
Set the format for writing double precision fields. More... | |
OFile & | fmtField () |
Reset the format for writing double precision fields to its default. More... | |
FILE * | getFILE () const |
Get the underlying file pointer. More... | |
std::string | getMode () const |
Retrieve the mode. More... | |
std::string | getPath () const |
Retrieve the path. More... | |
std::string | getSuffix () const |
Get the file suffix. More... | |
bool | isOpen () |
Check if a file is open. More... | |
FileBase & | link (FILE *) |
Link to an already open filed. More... | |
FileBase & | link (PlumedMain &) |
Link to a PlumedMain object Automatically links also the corresponding Communicator. More... | |
FileBase & | link (Communicator &) |
Link to a Communicator object. More... | |
FileBase & | link (Action &) |
Link to an Action object. More... | |
FileBase & | link (FILE *) |
Allows overloading of link. More... | |
FileBase & | link (PlumedMain &) |
Allows overloading of link. More... | |
FileBase & | link (Communicator &) |
Allows overloading of link. More... | |
FileBase & | link (Action &) |
Allows overloading of link. More... | |
OFile & | link (OFile &) |
Allows linking this OFile to another one. More... | |
virtual FileBase & | open (const std::string &name)=0 |
Allows overloading of open. More... | |
OFile & | open (const std::string &name) override |
Opens the file using automatic append/backup. More... | |
operator bool () const | |
Check for error/eof. More... | |
int | printf (const char *fmt,...) |
Formatted output with explicit format - a la printf. More... | |
OFile & | printField (const std::string &, double) |
Set the value of a double precision field. More... | |
OFile & | printField (const std::string &, int) |
Set the value of a int field. More... | |
OFile & | printField (const std::string &, const std::string &) |
Set the value of a string field. More... | |
OFile & | printField (Value *val, const double &v) |
Print a value. More... | |
OFile & | printField () |
Close a line. More... | |
OFile & | rewind () |
Rewind a file. More... | |
void | setBackupString (const std::string &) |
Set the string name to be used for automatic backup. More... | |
void | setHeavyFlush () |
Set heavyFlush flag. More... | |
OFile & | setLinePrefix (const std::string &) |
Set the prefix for output. More... | |
OFile & | setupPrintValue (Value *val) |
Used to setup printing of values. More... | |
Static Public Member Functions | |
static std::string | appendSuffix (const std::string &path, const std::string &suffix) |
Append suffix. More... | |
Protected Attributes | |
Action * | action |
pointer to corresponding action. NULL if not linked More... | |
bool | cloned |
Control closing on destructor. More... | |
Communicator * | comm |
communicator. NULL if not set More... | |
bool | eof |
Set to true when end of file is encountered. More... | |
bool | err |
Set to true when error is encountered. More... | |
FILE * | fp |
file pointer More... | |
void * | gzfp |
zip file pointer. More... | |
bool | heavyFlush |
Set to true if you want flush to be heavy (close/reopen) More... | |
std::string | mode |
mode of the opened file More... | |
std::string | path |
path of the opened file More... | |
PlumedMain * | plumed |
pointer to main plumed object. NULL if not linked More... | |
Private Member Functions | |
bool | checkRestart () const |
check if we are restarting More... | |
unsigned | findField (const std::string &name) const |
Find field index given name. More... | |
size_t | llwrite (const char *, size_t) |
Low-level write. More... | |
Private Attributes | |
int | actual_buffer_length |
This variables stores the actual buffer length. More... | |
std::string | backstring |
The string used for backing up files. More... | |
std::unique_ptr< char[]> | buffer |
Internal buffer (generic use) More... | |
std::unique_ptr< char[]> | buffer_string |
Internal buffer for printf. More... | |
int | buflen |
Internal buffer length. More... | |
std::vector< Field > | const_fields |
All the defined constant fields. More... | |
bool | enforceBackup_ |
True if backup behavior (i.e. non restart) should be forced. More... | |
bool | enforceRestart_ |
True if restart behavior should be forced. More... | |
bool | fieldChanged |
True if fields has changed. More... | |
std::string | fieldFmt |
Format for fields writing. More... | |
std::vector< Field > | fields |
All the defined variable fields. More... | |
std::string | linePrefix |
Prefix for line (e.g. "PLUMED: ") More... | |
OFile * | linked |
Pointer to a linked OFile. More... | |
std::ostringstream | oss |
Temporary ostringstream for << output. More... | |
std::vector< Field > | previous_fields |
All the previously defined variable fields. More... | |
Friends | |
template<class T > | |
OFile & | operator<< (OFile &, const T &) |
Formatted output with << operator. More... | |
Class for output files
This class provides features similar to those in the standard C "FILE*" type, but only for sequential output. See IFile for sequential input.
See the example here for a possible use:
#include "File.h" int main(){ PLMD::OFile pof; pof.open("ciao"); pof.printf("%s\n","test1"); pof.setLinePrefix("plumed: "); pof.printf("%s\n","test2"); pof.setLinePrefix(""); pof.addConstantField("x2").printField("x2",67.0); pof.printField("x1",10.0).printField("x3",20.12345678901234567890).printField(); pof.printField("x1",10.0).printField("x3",-1e70*20.12345678901234567890).printField(); pof.printField("x3",10.0).printField("x2",777.0).printField("x1",-1e70*20.12345678901234567890).printField(); pof.printField("x3",67.0).printField("x1",18.0).printField(); return 0; }
This program is expected to produce a file "ciao" which reads
test1 plumed: test2 #! FIELDS x1 x3 #! SET x2 67 10 20.12345678901234 10 -2.012345678901235e+71 #! FIELDS x1 x3 #! SET x2 777 -2.012345678901235e+71 10 18 67
Notes
When a OFile object is used in PLUMED it can be convenient to link() it to the Action object where it is defined, or to the PlumedMain object. This will save in the OFile a pointer to the linked object and will allow to have some extra information. E.g., if PLUMED is restarting, files will be appended. Notice that one can enforce this behavior using the enforceRestart() method before opening a file.
To have all files managed consistently, it is important to use OFile in the proper way. This should allow multi-replica plumed, restart and backups to work in the expected way. For this reason all the operations in OFile and IFile are synchronizing all the processors of the group, so call to OFile functions should always be performed by all processes; for this reason is also not usefull to use Log for debugging because only master threads will actually write. For debugging is better to use the standard stderr.
int main(){ // this is a growing file, containing a full history // (frames are appended, as in traditional HILLS and COLVAR) OFile grw; // this is a single-snapshopt file used e.g. for checkpointing // (rewritten every time) OFile snp; // open both files at the beginning // (will go in \ref Action constructor) grw.open("growing"); snp.open("snapshot"); // trajectory loop for(int i=0;i<nsteps;i++){ // files should be writen in the update() method of an \ref Action // write on growing file grw<<"data at step "<<i<<\n"; // flushing // it takes time, so do it only if data is critical // better to leave this choice to the user with the FLUSH keyword // grw.flush(); // write on snapshot file snp.rewind(); snp<<"snapshot at step "<<i<<"\n"; snp.flush(); // the only difference is that snp is rewound // notice that it should be rewound just before writing // because rewind is going to move the file out of the way // to have a safe copy of the file ("bck.last.filename") // Also notice that snapshots should be flushed // for this reason, it is better to write them only // rarely to avoid excessive slow down } } \notice Notice that it is not necessary to explicitely close files, since they are closed implicitely when the object goes out of scope. In case you need to explicitly close the file before it is destroyed, please check it the procedure is exception safe and, if necessary, add some `try/catch` statement.
PLMD::OFile::OFile | ( | ) |
Constructor.
OFile & PLMD::OFile::addConstantField | ( | const std::string & | name | ) |
|
staticinherited |
Append suffix.
It appends the desired suffix to the string. Notice that it conserves some suffix (e.g. gz/xtc/trr).
void PLMD::OFile::backupAllFiles | ( | const std::string & | str | ) |
This backs up all the files that would have been created with the name str.
It is used in analysis when you are not restarting. Analysis output files at different times, which are names analysis.0.<filename>, analysis.1.<filename> and <filename>, are backed up to bck.0.analysis.0.<filename>, bck.0.analysis.1.<filename> and bck.0.<filename>
void PLMD::OFile::backupFile | ( | const std::string & | bstring, |
const std::string & | fname | ||
) |
Backup a file by giving it a different name.
|
private |
check if we are restarting
OFile & PLMD::OFile::clearFields | ( | ) |
Resets the list of fields.
As it is only possible to add new constant fields (addConstantField()), this method can be used to clean the field list.
|
inherited |
Closes the file Should be used only for explicitely opened files.
OFile & PLMD::OFile::enforceBackup | ( | ) |
Enforce backup, even if the attached plumed object is restarting.
OFile & PLMD::OFile::enforceRestart | ( | ) |
Enforce restart, also if the attached plumed object is not restarting.
Useful for tests
|
inherited |
Enforce suffix.
Overrides the one set in PlumedMain&
|
inherited |
Check if the file exists.
|
private |
Find field index given name.
|
overridevirtual |
Flush a file.
Reimplemented from PLMD::FileBase.
OFile & PLMD::OFile::fmtField | ( | const std::string & | fmt | ) |
Set the format for writing double precision fields.
OFile & PLMD::OFile::fmtField | ( | ) |
Reset the format for writing double precision fields to its default.
|
inlineinherited |
Get the underlying file pointer.
It might be null even if the file is open, e.g. when the file was open as a gzipped file.
|
inlineinherited |
Retrieve the mode.
|
inlineinherited |
Retrieve the path.
|
inherited |
Get the file suffix.
|
inherited |
Check if a file is open.
|
inherited |
Link to an already open filed.
|
inherited |
Link to a PlumedMain object Automatically links also the corresponding Communicator.
|
inherited |
Link to a Communicator object.
Link to an Action object.
Automatically links also the corresponding PlumedMain and Communicator.
FileBase & PLMD::FileBase::link |
Allows overloading of link.
FileBase & PLMD::FileBase::link |
Allows overloading of link.
FileBase & PLMD::FileBase::link |
Allows overloading of link.
FileBase & PLMD::FileBase::link |
Allows overloading of link.
|
private |
Low-level write.
virtual FileBase& PLMD::FileBase::open |
Allows overloading of open.
|
overridevirtual |
Opens the file using automatic append/backup.
Implements PLMD::FileBase.
|
inherited |
Check for error/eof.
int PLMD::OFile::printf | ( | const char * | fmt, |
... | |||
) |
Formatted output with explicit format - a la printf.
OFile & PLMD::OFile::printField | ( | const std::string & | name, |
double | v | ||
) |
Set the value of a double precision field.
OFile & PLMD::OFile::printField | ( | const std::string & | name, |
int | v | ||
) |
Set the value of a int field.
OFile & PLMD::OFile::printField | ( | const std::string & | name, |
const std::string & | v | ||
) |
Set the value of a string field.
OFile & PLMD::OFile::printField | ( | ) |
Close a line.
Typically used as
of.printField("a",a).printField("b",b).printField();
OFile & PLMD::OFile::rewind | ( | ) |
Rewind a file.
void PLMD::OFile::setBackupString | ( | const std::string & | str | ) |
Set the string name to be used for automatic backup.
|
inlineinherited |
Set heavyFlush flag.
OFile & PLMD::OFile::setLinePrefix | ( | const std::string & | l | ) |
Set the prefix for output.
Typically "PLUMED: ". Notice that lines with a prefix cannot be parsed using fields in a IFile.
Formatted output with << operator.
|
protectedinherited |
pointer to corresponding action. NULL if not linked
|
private |
This variables stores the actual buffer length.
|
private |
The string used for backing up files.
|
private |
Internal buffer (generic use)
|
private |
Internal buffer for printf.
|
private |
Internal buffer length.
|
protectedinherited |
Control closing on destructor.
If true, file will not be closed in destructor
|
protectedinherited |
communicator. NULL if not set
|
private |
All the defined constant fields.
|
private |
True if backup behavior (i.e. non restart) should be forced.
|
private |
True if restart behavior should be forced.
|
protectedinherited |
Set to true when end of file is encountered.
|
protectedinherited |
Set to true when error is encountered.
|
private |
True if fields has changed.
This could be due to a change in the list of fields or a reset of a nominally constant field
|
private |
Format for fields writing.
|
private |
All the defined variable fields.
|
protectedinherited |
file pointer
|
protectedinherited |
zip file pointer.
|
protectedinherited |
Set to true if you want flush to be heavy (close/reopen)
|
private |
Prefix for line (e.g. "PLUMED: ")
|
private |
Pointer to a linked OFile.
see link(OFile&)
|
protectedinherited |
mode of the opened file
|
private |
Temporary ostringstream for << output.
|
protectedinherited |
path of the opened file
|
protectedinherited |
pointer to main plumed object. NULL if not linked
|
private |
All the previously defined variable fields.
Hosted by GitHub | 1.8.14 |