Nemo  2.4.0
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 ~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... 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
107  : _service(0),
108 #ifdef USE_MPI
109  _isMasterExec(false),
110 #else
111  _isMasterExec(true),
112 #endif
113  _isInputHandler(0),
117  _path(), _extension(ext), _current_filename(), _pop(0)
118  {
119  // initialize the generation list to avoid having a non-allocated list
120  _generations.assign(1,0);
121  _genITER = _generations.begin();
122  }
FileServices * _service
Link to the files manager.
Definition: filehandler.h:56
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:70
list< int > _generations
Definition: filehandler.h:84
bool _isInputHandler
Writing mode flag.
Definition: filehandler.h:66
unsigned int _ExecRank
unused...
Definition: filehandler.h:87
std::string _path
File path as set during initialization.
Definition: filehandler.h:89
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:92
list< int >::const_iterator _genITER
Definition: filehandler.h:83
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:74
bool _isMasterExec
Flag telling if the file must be written by the master or the worker node.
Definition: filehandler.h:64
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:98
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:102
unsigned int _ReplicateOccurrence
Tells every what replicate the file should be written.
Definition: filehandler.h:72
unsigned int _current_generation
The current generation number of the simulation.
Definition: filehandler.h:78
unsigned int _current_replicate
The current replicate number of the simulation.
Definition: filehandler.h:76
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:68

References _generations, and _genITER.

◆ ~FileHandler()

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

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 EventFileHandler< LCE >, TraitFileHandler< TP >, TTWolbachiaFH, TTQOhtaStats, TTQFreqExtractor, TTQuantiFH, TTNOhtaStats, TTNeutralGenesFH, TTDeletMutBitstrFH, TTBDMI_FH, LCE_StatFH, GenotyperFH, MPFileHandler, LCE_SelectionFH, FHPedigreeReader, FHLogWriter, and BinaryDataSaver.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ FHwrite()

◆ get_ExecRank()

unsigned int FileHandler::get_ExecRank ( )
inline

unused yet...

168 {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.

151 {
155  else
157  else
159 
160  return _current_filename;
161 }
string & getReplicateFileName()
Accessor to the current replicate file name.
Definition: fileservices.cc:457
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:466
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:474

Referenced by LCE_SelectionFH::FHwrite(), MPFileHandler::FHwrite(), GenotyperFH::FHwrite(), TTBDMI_FH::FHwrite(), TTNOhtaStats::FHwrite(), TTQOhtaStats::FHwrite(), TTWolbachiaFH::FHwrite(), BinaryDataSaver::finish(), BinaryDataSaver::printHeader(), BinaryDataSaver::setFileDescriptor(), and TTQuantiFH::write_TABLE().

+ Here is the caller graph for this function:

◆ get_GenerationOccurrence()

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

References _GenerationOccurrence.

Referenced by BinaryDataSaver::ifExist().

+ Here is the caller graph for this function:

◆ get_isGenerationPeriodic()

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

References _isGenerationPeriodic.

◆ get_isInputHandler()

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

References _isInputHandler.

Referenced by FileServices::attach().

+ Here is the caller graph for this function:

◆ get_isMasterExec()

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

References _isMasterExec.

◆ get_isReplicatePeriodic()

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

References _isReplicatePeriodic.

◆ get_OccMatrix()

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

References _multipleOccurences.

◆ get_path()

◆ get_pop_ptr()

Metapop* FileHandler::get_pop_ptr ( )
inline

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

134 {return _pop;}

References _pop.

Referenced by TTDeletMutBitstrFH::FHwrite(), and TTNOhtaStats::FHwrite().

+ Here is the caller graph for this function:

◆ get_ReplicateOccurrence()

unsigned int FileHandler::get_ReplicateOccurrence ( )
inline
159 {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 LCE_StatFH, and BinaryDataSaver.

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

◆ init()

void FileHandler::init ( )
virtual

Called by notifier during simulation setup, performs file checking.

Implements Handler.

51 {
52  // this is called after set() is called
53  // so, nothing should be done here that can overwrite user's settings
54 
56  //check if the occurrences exceed the pop parameters:
59 
62 
63  set_path();
64 
65 }
void set_path()
Definition: filehandler.cc:111
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:112

◆ 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
196  {
198  set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
void set_ExecRank(int val)
Definition: filehandler.h:169
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:160
void set_GenerationOccurrence(unsigned int val)
Definition: filehandler.h:166
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:163
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:157

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
169 {_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
148 {_extension = ext;}

References _extension.

Referenced by TProtoNeutralGenes::loadFileServices(), and LCE_FileServicesNotifier::setGenotyperParameters().

+ Here is the caller graph for this function:

◆ set_GenerationOccurrence()

void FileHandler::set_GenerationOccurrence ( unsigned int  val)
inline
166 {_GenerationOccurrence = val;}

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
163 {_isGenerationPeriodic = val;}

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
185 {_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
157 {_isReplicatePeriodic = val;}

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
173  {
174  _multipleOccurences.reset(occ->nrows(), occ->ncols(), occ->getValArray());
175  _generations.clear();
176  for(unsigned int i = 0; i < _multipleOccurences.ncols(); i++)
177  _generations.push_back((int)_multipleOccurences.get(0, i));
178  _genITER = _generations.begin();
179  }
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions and all elements to 0.
Definition: tmatrix.h:160
double * getValArray() const
Definition: tmatrix.h:201
unsigned int ncols() const
Definition: tmatrix.h:215
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:192
unsigned int nrows() const
Definition: tmatrix.h:212

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

References error(), and warning().

◆ set_pop_ptr()

void FileHandler::set_pop_ptr ( Metapop pop_ptr)
inline
136 { _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
160 {_ReplicateOccurrence = val;}

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
140 {_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.

166 {
168 
170 
171  // reset the generation iterator to the first in the list for new replicates
172  if(_genITER == _generations.end() && _current_generation == 1)
173  _genITER = _generations.begin();
174 
177 
178  if(_isGenerationPeriodic) { // multiple occurrences are to be expected (at least one)
179 
180  if(_GenerationOccurrence != 0) { // periodicity is set
181 
183  FHwrite();
184 
185  } else // periodicity is not set, check the list of generations at which the function is called
186  if(_current_generation == (unsigned)(*_genITER) && _myenv->isMaster() == _isMasterExec) {
187 
188  FHwrite();
189  _genITER++;
190 
191  }
192  } else // the writer prints data only at one possible generation, no periodicity
194  FHwrite();
195  }
196 }
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:294
unsigned int getCurrentGeneration()
Definition: metapop.h:295
MPIenv * _myenv
Definition: MPImanager.cc:35

References _myenv, 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.

◆ _current_generation

unsigned int FileHandler::_current_generation
private

The current generation number of the simulation.

Set by FileHandler::update().

◆ _current_replicate

unsigned int FileHandler::_current_replicate
private

The current replicate number of the simulation.

Set by FileHandler::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(), 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(), and set_GenerationOccurrence().

◆ _generations

list< int > FileHandler::_generations
private

Referenced by FileHandler(), and set_OccMatrix().

◆ _genITER

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

Referenced by FileHandler(), and set_OccMatrix().

◆ _isGenerationPeriodic

bool FileHandler::_isGenerationPeriodic
private

Flag setting the per generation periodicity behaviour of the file.

Referenced by get_isGenerationPeriodic(), and set_isGenerationPeriodic().

◆ _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(), and set_isMasterExec().

◆ _isReplicatePeriodic

bool FileHandler::_isReplicatePeriodic
private

Flag telling if the file should be written each replicate.

Referenced by get_isReplicatePeriodic(), and set_isReplicatePeriodic().

◆ _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_path(), set(), and set_multi().

◆ _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(), and set_ReplicateOccurrence().

◆ _service

FileServices* FileHandler::_service
private

Link to the files manager.

Referenced by get_service(), and set_service().


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

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR