Nemo  2.4.1
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
filehandler.h
Go to the documentation of this file.
1 
29 #ifndef FILEHANDLER_H
30 #define FILEHANDLER_H
31 
32 #include <string>
33 #include "handler.h"
34 #include "fileservices.h"
35 #include "tmatrix.h"
36 
52 class FileHandler : public Handler {
53 
54 private:
72  unsigned int _ReplicateOccurrence;
74  unsigned int _GenerationOccurrence;
76  unsigned int _current_replicate;
78  unsigned int _current_generation;
79 
82 
83  list< int >::const_iterator _genITER;
84  list< int > _generations;
85 
87  unsigned int _ExecRank;
89  std::string _path;
92  std::string _extension;
98  std::string _current_filename;
99 
100 protected:
103 
104 public:
105 
106  FileHandler (const char* ext)
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  }
123 
124  virtual ~FileHandler ( ) { }
126  virtual void init ( );
130  virtual vector< string > ifExist( );
133 
134  Metapop* get_pop_ptr ( ) {return _pop;}
135 
136  void set_pop_ptr (Metapop* pop_ptr) { _pop = pop_ptr;}
139 
140  void set_service (FileServices* srv) {_service = srv;}
141 
142  std::string& get_path () {return _path;}
143 
144  void set_path ( );
145 
146  std::string& get_extension ( ) {return _extension;}
147 
148  void set_extension (const char* ext) {_extension = ext;}
150  std::string& get_filename ();
151 
152 
154  void set_isInputHandler (bool val) {_isInputHandler = val;}
155 
158 
160  void set_ReplicateOccurrence (unsigned int val) {_ReplicateOccurrence = val;}
161 
164 
166  void set_GenerationOccurrence (unsigned int val) {_GenerationOccurrence = val;}
168  unsigned int get_ExecRank () {return _ExecRank;}
169  void set_ExecRank (int val) {_ExecRank = val;}
170 
172 
173  void set_OccMatrix (TMatrix* occ) {
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  }
180 
182 #ifdef USE_MPI
183  void set_isMasterExec(bool is) {_isMasterExec = is;}
184 #else
185  void set_isMasterExec(bool is) {_isMasterExec = true;}
186 #endif
188 
196  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path) {
198  set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
199 
200  virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path) {
202  set_GenerationOccurrence(0); set_OccMatrix(Occ); _path = path;}
203 
205  virtual void FHwrite () = 0;
206 
208  virtual void FHread (string& filename) = 0;
211  virtual void update();
212 
213 };
214 
215 //CLASS TraitFileHandler
216 //
220 template <class TP> class TraitFileHandler : public FileHandler {
221 protected:
224 public:
225  TraitFileHandler(TP* trait_proto, const char* ext);
226  virtual ~TraitFileHandler () {}
227 
228  virtual void FHwrite () = 0;
229 
230  virtual void FHread (string& filename) = 0;
231 
232  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TP* trait_proto);
233  virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path, TP* trait_proto);
234 };
235 
236 template <class TP> TraitFileHandler<TP>::TraitFileHandler(TP* trait_proto, const char* ext) :
237 FileHandler(ext), _FHLinkedTrait(trait_proto), _FHLinkedTraitIndex(trait_proto->get_index())
238 {}
239 
240 template <class TP> void TraitFileHandler<TP>::set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TP* trait_proto)
241 {
242  FileHandler::set(rpl_per, gen_per, rpl_occ, gen_occ,rank, path);
243  _FHLinkedTrait = trait_proto;
244  _FHLinkedTraitIndex = trait_proto->get_index();
245 }
246 
247 template <class TP> void TraitFileHandler<TP>::set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path, TP* trait_proto)
248 {
249  FileHandler::set_multi(rpl_per, gen_per, rpl_occ, Occ, path);
250  _FHLinkedTrait = trait_proto;
251  _FHLinkedTraitIndex = trait_proto->get_index();
252 }
253 // EventFileHandler
254 //
257 template <class LCE> class EventFileHandler : public FileHandler {
258 protected:
260 public:
261  EventFileHandler(LCE* event, const char* ext);
262  virtual ~EventFileHandler () {}
263 
264  virtual void FHwrite () = 0;
265 
266  virtual void FHread (string& filename) = 0;
267 
268  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE* event);
269 };
270 
271 template <class LCE> EventFileHandler<LCE>::EventFileHandler(LCE* event, const char* ext) :
272 FileHandler(ext), _FHLinkedEvent(event)
273 { }
274 
275 template <class LCE> void EventFileHandler<LCE>::set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE* event)
276 {
277  FileHandler::set(rpl_per,gen_per,rpl_occ,gen_occ,rank,path);
278  _FHLinkedEvent = event;
279 }
280 
282 class FHLogWriter : public FileHandler {
283 public:
284  FHLogWriter() : FileHandler(".log"){};
285  virtual ~FHLogWriter(){}
286 
287  virtual void FHwrite (){}
288  virtual void FHread (string& filename) {}
289 
290 
291  void createInitFile(list< ParamSet* >& params);
292 
293  void save_simparams(list< ParamSet* >& params);
294 
295  void log_message (string& logstr);
296 
297  void open_logfile4writing (ofstream& FH, ios_base::openmode flag = ios_base::out);
298 };
299 #endif //FILEHANDLER_H
300 
Template class for the LCEs StatHandler classes.
Definition: filehandler.h:257
LCE * _FHLinkedEvent
Definition: filehandler.h:259
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE *event)
Definition: filehandler.h:275
virtual void FHwrite()=0
EventFileHandler(LCE *event, const char *ext)
Definition: filehandler.h:271
virtual ~EventFileHandler()
Definition: filehandler.h:262
virtual void FHread(string &filename)=0
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:282
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:200
virtual void FHread(string &filename)
Definition: filehandler.h:288
virtual void FHwrite()
Definition: filehandler.h:287
void log_message(string &logstr)
Definition: filehandler.cc:265
FHLogWriter()
Definition: filehandler.h:284
virtual ~FHLogWriter()
Definition: filehandler.h:285
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:229
void open_logfile4writing(ofstream &FH, ios_base::openmode flag=ios_base::out)
Definition: filehandler.cc:279
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:52
void set_extension(const char *ext)
Definition: filehandler.h:148
void set_OccMatrix(TMatrix *occ)
Definition: filehandler.h:173
std::string & get_path()
Definition: filehandler.h:142
FileServices * _service
Link to the files manager.
Definition: filehandler.h:56
void set_isMasterExec(bool is)
Definition: filehandler.h:185
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:70
bool get_isMasterExec()
Definition: filehandler.h:181
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
void set_ExecRank(int val)
Definition: filehandler.h:169
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:92
virtual void init()
Called by notifier during simulation setup, performs file checking.
Definition: filehandler.cc:50
list< int >::const_iterator _genITER
Definition: filehandler.h:83
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
Definition: filehandler.h:200
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:150
void set_pop_ptr(Metapop *pop_ptr)
Definition: filehandler.h:136
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:160
virtual void FHread(string &filename)=0
Default input function.
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
void set_GenerationOccurrence(unsigned int val)
Definition: filehandler.h:166
virtual ~FileHandler()
Definition: filehandler.h:124
void set_service(FileServices *srv)
Definition: filehandler.h:140
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:74
bool get_isGenerationPeriodic()
Definition: filehandler.h:162
bool get_isInputHandler()
Definition: filehandler.h:153
void set_path()
Definition: filehandler.cc:111
bool _isMasterExec
Flag telling if the file must be written by the master or the worker node.
Definition: filehandler.h:64
TMatrix * get_OccMatrix()
Definition: filehandler.h:171
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
Sets the hanlder parameters.
Definition: filehandler.h:196
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:98
TMatrix _multipleOccurences
Multiple occurences.
Definition: filehandler.h:81
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:102
unsigned int get_ReplicateOccurrence()
Definition: filehandler.h:159
FileHandler(const char *ext)
Definition: filehandler.h:106
bool get_isReplicatePeriodic()
Definition: filehandler.h:156
Metapop * get_pop_ptr()
Returns the pointer to the current metapop through the FileServices interface.
Definition: filehandler.h:134
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:138
unsigned int get_GenerationOccurrence()
Definition: filehandler.h:165
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
unsigned int get_ExecRank()
unused yet...
Definition: filehandler.h:168
virtual void update()
Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicit...
Definition: filehandler.cc:165
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:163
std::string & get_extension()
Definition: filehandler.h:146
void set_isInputHandler(bool val)
Definition: filehandler.h:154
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:68
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:157
virtual vector< string > ifExist()
Checks if any file associated with the current file name already exists on disk.
Definition: filehandler.cc:69
A class to manage the files associated with each components of the simulation.
Definition: fileservices.h:51
Service handler (an observer).
Definition: handler.h:35
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:79
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
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
Template class for the trait's FileHandler.
Definition: filehandler.h:220
virtual ~TraitFileHandler()
Definition: filehandler.h:226
int _FHLinkedTraitIndex
Definition: filehandler.h:223
TraitFileHandler(TP *trait_proto, const char *ext)
Definition: filehandler.h:236
virtual void FHread(string &filename)=0
virtual void FHwrite()=0
TP * _FHLinkedTrait
Definition: filehandler.h:222
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TP *trait_proto)
Definition: filehandler.h:247
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TP *trait_proto)
Definition: filehandler.h:240
Nemo2.
Nemo2.

Generated for Nemo v2.4.1 by  doxygen 1.9.1

Catalogued on GSR