Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
FileHandler Class Referenceabstract

Interface to handle file input/output for any SimComponent. More...

#include <filehandler.h>

+ Inheritance diagram for FileHandler:
+ Collaboration diagram for FileHandler:

Public Member Functions

 FileHandler (const char *ext)
 
virtual ~FileHandler ()
 
virtual void init ()
 Called by notifier during simulation setup, performs file checking. More...
 
virtual vector< string > ifExist ()
 Checks if any file associated with the current file name already exists on disk. More...
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
 Sets the hanlder parameters. More...
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
 
virtual void FHwrite ()=0
 Default behavior of the class, called by Handler::update(). More...
 
virtual void FHread (string &filename)=0
 Default input function. More...
 
virtual void update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. More...
 
Accessors
Metapopget_pop_ptr ()
 Returns the pointer to the current metapop through the FileServices interface. More...
 
void set_pop_ptr (Metapop *pop_ptr)
 
FileServicesget_service ()
 Returns pointer to the FileServices. More...
 
void set_service (FileServices *srv)
 
std::string & get_path ()
 
void set_path ()
 
std::string & get_extension ()
 
void set_extension (const char *ext)
 
std::string & get_filename ()
 Builds and returns the current file name depending on the periodicity of the file. More...
 
bool get_isInputHandler ()
 
void set_isInputHandler (bool val)
 
bool get_isReplicatePeriodic ()
 
void set_isReplicatePeriodic (bool val)
 
unsigned int get_ReplicateOccurrence ()
 
void set_ReplicateOccurrence (unsigned int val)
 
bool get_isGenerationPeriodic ()
 
void set_isGenerationPeriodic (bool val)
 
unsigned int get_GenerationOccurrence ()
 
void set_GenerationOccurrence (unsigned int val)
 
unsigned int get_ExecRank ()
 unused yet... More...
 
void set_ExecRank (int val)
 
TMatrixget_OccMatrix ()
 
void set_OccMatrix (TMatrix *occ)
 
bool get_isMasterExec ()
 
void set_isMasterExec (bool is)
 
- Public Member Functions inherited from Handler
virtual void init ()=0
 Inits state. More...
 
virtual void update ()=0
 Updates the handler state. More...
 
virtual ~Handler ()
 

Protected Attributes

Metapop_pop
 Pointer to the current metapop, set during initialization within the init function. More...
 

Private Attributes

FileServices_service
 Link to the files manager. More...
 
bool _isMasterExec
 Flag telling if the file must be written by the master or the worker node. More...
 
bool _isInputHandler
 Writing mode flag. More...
 
bool _isReplicatePeriodic
 Flag telling if the file should be written each replicate. More...
 
bool _isGenerationPeriodic
 Flag setting the per generation periodicity behaviour of the file. More...
 
unsigned int _ReplicateOccurrence
 Tells every what replicate the file should be written. More...
 
unsigned int _GenerationOccurrence
 Tells every what generation the file should be written. More...
 
unsigned int _current_replicate
 The current replicate number of the simulation. More...
 
unsigned int _current_generation
 The current generation number of the simulation. More...
 
TMatrix _multipleOccurences
 Multiple occurences. More...
 
list< int >::const_iterator _genITER
 
list< int_generations
 
unsigned int _ExecRank
 unused... yet More...
 
std::string _path
 File path as set during initialization. More...
 
std::string _extension
 File extension, should be specific to the implementation. More...
 
std::string _current_filename
 The current filename as set by FileHandler::get_filename(). More...
 

Detailed Description

Interface to handle file input/output for any SimComponent.

Stores the periodicity parameters and the file path and extension. The replicate file name is given by the FileServices. A file handler might be set to write output at a specific generation of a specific replicate or at some periodic time during the simulation. The default writing mode of a FileHandler is in output and is added as a 'writer' to the File Services as such using the FileServices::attach mode. The _isInputHandler flag is thus set to FALSE by default. To set a FileHandler as a reader instead of a writer, the method FileHandler::set_isInputHandler(TRUE) must be called prior to attaching it to the FileServices using the FileServices::attach_reader method. Furthermore, a single instance of a FileHandler can be attached as both a writer and a reader by setting the _isInputHandler flag to TRUE and then using the FileServices::attach method to add it to the list of both the reader and writers. Note on memory management: the FileServices class only deals with its lists of FileHandlers pointers and will never try to delete any one of those. All memory management must thus be taken care of by the user class (typically the TraitPrototype...).

Constructor & Destructor Documentation

◆ FileHandler()

FileHandler::FileHandler ( const char *  ext)
inline
108 : _service(0),
109#ifdef USE_MPI
110 _isMasterExec(false),
111#else
112 _isMasterExec(true),
113#endif
119 {}
FileServices * _service
Link to the files manager.
Definition: filehandler.h:57
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:71
bool _isInputHandler
Writing mode flag.
Definition: filehandler.h:67
unsigned int _ExecRank
unused... yet
Definition: filehandler.h:88
std::string _path
File path as set during initialization.
Definition: filehandler.h:90
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:93
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:75
bool _isMasterExec
Flag telling if the file must be written by the master or the worker node.
Definition: filehandler.h:65
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:99
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
unsigned int _ReplicateOccurrence
Tells every what replicate the file should be written.
Definition: filehandler.h:73
unsigned int _current_generation
The current generation number of the simulation.
Definition: filehandler.h:79
unsigned int _current_replicate
The current replicate number of the simulation.
Definition: filehandler.h:77
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:69

◆ ~FileHandler()

virtual FileHandler::~FileHandler ( )
inlinevirtual
121{ }

Member Function Documentation

◆ FHread()

virtual void FileHandler::FHread ( string &  filename)
pure virtual

Default input function.

Loads a pop from the genotypes read from the input file.

Implemented in BinaryDataSaver, FHLogWriter, FHPedigreeReader, LCE_SelectionFH, MPFileHandler, FileMergerFH, LCE_StatFH, TTBDMI_FH, TTDeletMutBitstrFH, TTNeutralGenesFH, TTQuantiFH, TTQFreqExtractor, TTWolbachiaFH, TraitFileHandler< TP >, and EventFileHandler< LCE >.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ FHwrite()

◆ get_ExecRank()

unsigned int FileHandler::get_ExecRank ( )
inline

unused yet...

165{return _ExecRank;}

References _ExecRank.

◆ get_extension()

◆ get_filename()

string & FileHandler::get_filename ( )

Builds and returns the current file name depending on the periodicity of the file.

152{
156 else
158 else
160
161 return _current_filename;
162}
string & getReplicateFileName()
Accessor to the current replicate file name.
Definition: fileservices.cc:381
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:390
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:398

References _current_filename, _extension, _isGenerationPeriodic, _isReplicatePeriodic, _path, _service, FileServices::getBaseFileName(), FileServices::getGenerationReplicateFileName(), and FileServices::getReplicateFileName().

Referenced by LCE_SelectionFH::FHwrite(), MPFileHandler::FHwrite(), TTBDMI_FH::FHwrite(), TTWolbachiaFH::FHwrite(), BinaryDataSaver::finish(), FHLogWriter::open_logfile4writing(), BinaryDataSaver::printHeader(), BinaryDataSaver::setFileDescriptor(), and TTQuantiFH::write_TABLE().

+ Here is the caller graph for this function:

◆ get_GenerationOccurrence()

unsigned int FileHandler::get_GenerationOccurrence ( )
inline
162{return _GenerationOccurrence;}

References _GenerationOccurrence.

Referenced by TTQFreqExtractor::FHwrite(), and BinaryDataSaver::ifExist().

+ Here is the caller graph for this function:

◆ get_isGenerationPeriodic()

bool FileHandler::get_isGenerationPeriodic ( )
inline
159{return _isGenerationPeriodic;}

References _isGenerationPeriodic.

◆ get_isInputHandler()

bool FileHandler::get_isInputHandler ( )
inline
150{return _isInputHandler;}

References _isInputHandler.

Referenced by FileServices::attach().

+ Here is the caller graph for this function:

◆ get_isMasterExec()

bool FileHandler::get_isMasterExec ( )
inline
178{return _isMasterExec;}

References _isMasterExec.

◆ get_isReplicatePeriodic()

bool FileHandler::get_isReplicatePeriodic ( )
inline
153{return _isReplicatePeriodic;}

References _isReplicatePeriodic.

◆ get_OccMatrix()

TMatrix * FileHandler::get_OccMatrix ( )
inline
168{return &_multipleOccurences;}
TMatrix _multipleOccurences
Multiple occurences.
Definition: filehandler.h:82

References _multipleOccurences.

◆ get_path()

◆ get_pop_ptr()

Metapop * FileHandler::get_pop_ptr ( )
inline

Returns the pointer to the current metapop through the FileServices interface.

131{return _pop;}

References _pop.

Referenced by TTDeletMutBitstrFH::FHwrite().

+ Here is the caller graph for this function:

◆ get_ReplicateOccurrence()

unsigned int FileHandler::get_ReplicateOccurrence ( )
inline
156{return _ReplicateOccurrence;}

References _ReplicateOccurrence.

Referenced by BinaryDataSaver::ifExist().

+ Here is the caller graph for this function:

◆ get_service()

◆ ifExist()

vector< string > FileHandler::ifExist ( )
virtual

Checks if any file associated with the current file name already exists on disk.

Only checks wether the first replicate file is present.

Returns
false if filename already exists on disk, true otherwise.

Reimplemented in BinaryDataSaver, and LCE_StatFH.

71{
72 //check if the basefilename is already used on disk:
73 string filename = _path + _service->getBaseFileName();
74
75 vector< string > list;
76
77 ostringstream rpl, gen;
78
79 gen.fill('0');
80 gen.width( (int)log10((double)_pop->getGenerations()) + 1);
82
83 rpl.fill('0');
84 rpl.width( (int)log10((double)_pop->getReplicates()) + 1);
86
88 filename += _extension;
89 else if(!_isGenerationPeriodic)
90 filename += "_" + rpl.str() + _extension;
91 else
92 filename += "_" + gen.str() + "_" + rpl.str() + _extension;
93
94 ifstream ifXist;
95 ifXist.setstate(ios::failbit);
96
97 ifXist.open(filename.c_str(),ios::in);
98
99 if(ifXist.is_open()) {
100// warning("filename \"%s\" used by \"%s\"\n",_service->getBaseFileName().c_str(),filename.c_str());
101 ifXist.close();
102 list.push_back( filename );
103// return false;
104 }
105 ifXist.close();
106
107 return list;
108}
unsigned int getGenerations()
Definition: metapop.h:273
unsigned int getReplicates()
Definition: metapop.h:275

References _extension, _GenerationOccurrence, _isGenerationPeriodic, _isReplicatePeriodic, _path, _pop, _ReplicateOccurrence, _service, FileServices::getBaseFileName(), Metapop::getGenerations(), and Metapop::getReplicates().

◆ init()

void FileHandler::init ( )
virtual

Called by notifier during simulation setup, performs file checking.

Implements Handler.

52{
54 //check if the occurrences exceed the pop parameters:
57
60
61 set_path();
62
63// if ( _myenv->isMaster() != _isMasterExec ) return;
64
65// message(" %s*%s\n",get_path().c_str(),get_extension().c_str());
66}
void set_path()
Definition: filehandler.cc:112
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:111

References _GenerationOccurrence, _pop, _ReplicateOccurrence, _service, FileServices::get_pop_ptr(), Metapop::getGenerations(), Metapop::getReplicates(), and set_path().

◆ set()

virtual void FileHandler::set ( bool  rpl_per,
bool  gen_per,
int  rpl_occ,
int  gen_occ,
int  rank,
string  path 
)
inlinevirtual

Sets the hanlder parameters.

Parameters
rpl_perreplicate periodicity
gen_pergeneration periodicity
rpl_occreplicate occurence
gen_occgeneration occurence
rankthe rank in the life cycle, actualy unused...
paththe file path
193 {
195 set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
void set_ExecRank(int val)
Definition: filehandler.h:166
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:157
void set_GenerationOccurrence(unsigned int val)
Definition: filehandler.h:163
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:160
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:154

References _path, set_ExecRank(), set_GenerationOccurrence(), set_isGenerationPeriodic(), set_isReplicatePeriodic(), and set_ReplicateOccurrence().

Referenced by FileServices::init(), LCE_StatFH::LCE_StatFH(), Metapop::loadFileServices(), EventFileHandler< LCE >::set(), TraitFileHandler< TP >::set(), BinaryDataSaver::setParameters(), and LCE_StatServiceNotifier::setParameters().

+ Here is the caller graph for this function:

◆ set_ExecRank()

void FileHandler::set_ExecRank ( int  val)
inline
166{_ExecRank = val;}

References _ExecRank.

Referenced by set().

+ Here is the caller graph for this function:

◆ set_extension()

void FileHandler::set_extension ( const char *  ext)
inline
145{_extension = ext;}

References _extension.

Referenced by TProtoNeutralGenes::loadFileServices().

+ Here is the caller graph for this function:

◆ set_GenerationOccurrence()

void FileHandler::set_GenerationOccurrence ( unsigned int  val)
inline

References _GenerationOccurrence.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_isGenerationPeriodic()

void FileHandler::set_isGenerationPeriodic ( bool  val)
inline

References _isGenerationPeriodic.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_isInputHandler()

void FileHandler::set_isInputHandler ( bool  val)
inline

◆ set_isMasterExec()

void FileHandler::set_isMasterExec ( bool  is)
inline
182{_isMasterExec = true;}

References _isMasterExec.

Referenced by LCE_StatFH::LCE_StatFH().

+ Here is the caller graph for this function:

◆ set_isReplicatePeriodic()

void FileHandler::set_isReplicatePeriodic ( bool  val)
inline

References _isReplicatePeriodic.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_multi()

virtual void FileHandler::set_multi ( bool  rpl_per,
bool  gen_per,
int  rpl_occ,
TMatrix Occ,
string  path 
)
inlinevirtual

◆ set_OccMatrix()

void FileHandler::set_OccMatrix ( TMatrix occ)
inline
170 {
171 _multipleOccurences.reset(occ->nrows(), occ->ncols(), occ->getValArray());
172 _generations.clear();
173 for(unsigned int i = 0; i < _multipleOccurences.ncols(); i++)
174 _generations.push_back((int)_multipleOccurences.get(0, i));
175 _genITER = _generations.begin();
176 }
list< int > _generations
Definition: filehandler.h:85
list< int >::const_iterator _genITER
Definition: filehandler.h:84
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:116
double * getValArray() const
Definition: tmatrix.h:156
unsigned int ncols()
Definition: tmatrix.h:170
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:147
unsigned int nrows()
Definition: tmatrix.h:167

References _generations, _genITER, _multipleOccurences, TMatrix::get(), TMatrix::getValArray(), TMatrix::ncols(), TMatrix::nrows(), and TMatrix::reset().

Referenced by set_multi().

+ Here is the caller graph for this function:

◆ set_path()

void FileHandler::set_path ( )
113{
114 string root_dir;
115
116 root_dir = _service->getRootDir();
117
118 if(_path.size() != 0 && _path[_path.length()-1] != '/')
119 _path += "/";
120
121 if(root_dir.size() != 0)
122 _path = root_dir + _path;
123
124 if(_path.size() != 0) {
125
126 DIR *dir = opendir(_path.c_str());
127
128 //check if we have to create the directory:
129 if(_path.size() != 0 && dir == 0) {
130
131#ifdef _WINDOWS_
132 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
133 if((mkdir(_path.c_str(), mode)) == -1)
134#else
135 string cmd = "mkdir -p " + _path;
136 if ( system( cmd.c_str() ) < 0 )
137#endif
138 {
139 error("could not create directory \"%s\", saving in current directory.\n",_path.c_str());
140 _path = "";
141 }
142 } else {
143 if(closedir(dir) == -1)
144 warning("FileHandler::set_path::closedir: %s\n",strerror(errno));
145 }
146 }
147}
string & getRootDir()
Accessor to the name of the simulation's root output directory.
Definition: fileservices.cc:405
int error(const char *str,...)
Definition: output.cc:77
void warning(const char *str,...)
Definition: output.cc:58

References _path, _service, error(), FileServices::getRootDir(), and warning().

Referenced by init().

+ Here is the caller graph for this function:

◆ set_pop_ptr()

void FileHandler::set_pop_ptr ( Metapop pop_ptr)
inline
133{ _pop = pop_ptr;}

References _pop.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ set_ReplicateOccurrence()

void FileHandler::set_ReplicateOccurrence ( unsigned int  val)
inline

References _ReplicateOccurrence.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_service()

void FileHandler::set_service ( FileServices srv)
inline
137{_service = srv;}

References _service.

Referenced by FileServices::attach(), and FileServices::attach_reader().

+ Here is the caller graph for this function:

◆ update()

void FileHandler::update ( )
virtual

Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file.

Implements Handler.

167{
169
171
172 // reset the generation iterator to the first in the list for new replicates
173 if(_genITER == _generations.end() && _current_generation == 1)
174 _genITER = _generations.begin();
175
179 if(_GenerationOccurrence != 0) {
181 FHwrite();
182 } else if(_current_generation == (unsigned)(*_genITER) && _myenv->isMaster() == _isMasterExec) {
183 FHwrite();
184 _genITER++;
185 }
187 FHwrite();
188 }
189}
MPIenv * _myenv
Definition: MPImanager.cc:36
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
bool isMaster() const
Definition: MPImanager.h:127
unsigned int getCurrentReplicate()
Definition: metapop.h:293
unsigned int getCurrentGeneration()
Definition: metapop.h:294

References _current_generation, _current_replicate, _GenerationOccurrence, _generations, _genITER, _isGenerationPeriodic, _isMasterExec, _isReplicatePeriodic, _myenv, _pop, _ReplicateOccurrence, FHwrite(), Metapop::getCurrentGeneration(), Metapop::getCurrentReplicate(), and MPIenv::isMaster().

Member Data Documentation

◆ _current_filename

std::string FileHandler::_current_filename
private

The current filename as set by FileHandler::get_filename().

Is composed by putting together the following strings: FileHandler::_path, FileServices::_basename (+ replicate and generation counters if needed), and FileHandler::_extension.

Referenced by get_filename().

◆ _current_generation

unsigned int FileHandler::_current_generation
private

The current generation number of the simulation.

Set by FileHandler::update().

Referenced by update().

◆ _current_replicate

unsigned int FileHandler::_current_replicate
private

The current replicate number of the simulation.

Set by FileHandler::update().

Referenced by update().

◆ _ExecRank

unsigned int FileHandler::_ExecRank
private

unused... yet

Referenced by get_ExecRank(), and set_ExecRank().

◆ _extension

std::string FileHandler::_extension
private

File extension, should be specific to the implementation.

Is set during construction.

Referenced by get_extension(), get_filename(), ifExist(), and set_extension().

◆ _GenerationOccurrence

unsigned int FileHandler::_GenerationOccurrence
private

Tells every what generation the file should be written.

Set by the set() method below.

Referenced by get_GenerationOccurrence(), ifExist(), init(), set_GenerationOccurrence(), and update().

◆ _generations

list< int > FileHandler::_generations
private

Referenced by set_OccMatrix(), and update().

◆ _genITER

list<int>::const_iterator FileHandler::_genITER
private

Referenced by set_OccMatrix(), and update().

◆ _isGenerationPeriodic

bool FileHandler::_isGenerationPeriodic
private

Flag setting the per generation periodicity behaviour of the file.

Referenced by get_filename(), get_isGenerationPeriodic(), ifExist(), set_isGenerationPeriodic(), and update().

◆ _isInputHandler

bool FileHandler::_isInputHandler
private

Writing mode flag.

Must be true

Referenced by get_isInputHandler(), and set_isInputHandler().

◆ _isMasterExec

bool FileHandler::_isMasterExec
private

Flag telling if the file must be written by the master or the worker node.

An example of master-only file is the stats files (.txt and _bygen.txt) that aggregate the stats from each node. Binary files or FSTAT files are written by the worker nodes, each file beeing written for one replicate only. The default behaviour is to be TRUE in the non-MPI version and FALSE in the MPI version.

Referenced by get_isMasterExec(), set_isMasterExec(), and update().

◆ _isReplicatePeriodic

bool FileHandler::_isReplicatePeriodic
private

Flag telling if the file should be written each replicate.

Referenced by get_filename(), get_isReplicatePeriodic(), ifExist(), set_isReplicatePeriodic(), and update().

◆ _multipleOccurences

TMatrix FileHandler::_multipleOccurences
private

Multiple occurences.

Referenced by get_OccMatrix(), and set_OccMatrix().

◆ _path

std::string FileHandler::_path
private

File path as set during initialization.

Usually user-defined.

Referenced by get_filename(), get_path(), ifExist(), set(), set_multi(), and set_path().

◆ _pop

◆ _ReplicateOccurrence

unsigned int FileHandler::_ReplicateOccurrence
private

Tells every what replicate the file should be written.

Set by the set() method below.

Referenced by get_ReplicateOccurrence(), ifExist(), init(), set_ReplicateOccurrence(), and update().

◆ _service

FileServices* FileHandler::_service
private

Link to the files manager.

Referenced by get_filename(), get_service(), ifExist(), init(), set_path(), and set_service().


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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR