Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
filehandler.h
Go to the documentation of this file.
1 
30 #ifndef FILEHANDLER_H
31 #define FILEHANDLER_H
32 
33 #include <string>
34 #include "handler.h"
35 #include "fileservices.h"
36 #include "tmatrix.h"
37 
53 class FileHandler : public Handler {
54 
55 private:
73  unsigned int _ReplicateOccurrence;
75  unsigned int _GenerationOccurrence;
77  unsigned int _current_replicate;
79  unsigned int _current_generation;
80 
83 
84  list< int >::const_iterator _genITER;
85  list< int > _generations;
86 
88  unsigned int _ExecRank;
90  std::string _path;
93  std::string _extension;
99  std::string _current_filename;
100 
101 protected:
104 
105 public:
106 
107  FileHandler (const char* ext)
108  : _service(0),
109 #ifdef USE_MPI
110  _isMasterExec(false),
111 #else
112  _isMasterExec(true),
113 #endif
114  _isInputHandler(0),
118  _path(), _extension(ext), _current_filename(), _pop(0)
119  {
120  // initialize the generation list to avoid having a non-allocated list
121  _generations.assign(1,0);
122  _genITER = _generations.begin();
123  }
124 
125  virtual ~FileHandler ( ) { }
127  virtual void init ( );
131  virtual vector< string > ifExist( );
134 
135  Metapop* get_pop_ptr ( ) {return _pop;}
136 
137  void set_pop_ptr (Metapop* pop_ptr) { _pop = pop_ptr;}
140 
141  void set_service (FileServices* srv) {_service = srv;}
142 
143  std::string& get_path () {return _path;}
144 
145  void set_path ( );
146 
147  std::string& get_extension ( ) {return _extension;}
148 
149  void set_extension (const char* ext) {_extension = ext;}
151  std::string& get_filename ();
152 
153 
155  void set_isInputHandler (bool val) {_isInputHandler = val;}
156 
159 
161  void set_ReplicateOccurrence (unsigned int val) {_ReplicateOccurrence = val;}
162 
165 
167  void set_GenerationOccurrence (unsigned int val) {_GenerationOccurrence = val;}
169  unsigned int get_ExecRank () {return _ExecRank;}
170  void set_ExecRank (int val) {_ExecRank = val;}
171 
173 
174  void set_OccMatrix (TMatrix* occ) {
175  _multipleOccurences.reset(occ->nrows(), occ->ncols(), occ->getValArray());
176  _generations.clear();
177  for(unsigned int i = 0; i < _multipleOccurences.ncols(); i++)
178  _generations.push_back((int)_multipleOccurences.get(0, i));
179  _genITER = _generations.begin();
180  }
181 
183 #ifdef USE_MPI
184  void set_isMasterExec(bool is) {_isMasterExec = is;}
185 #else
186  void set_isMasterExec(bool is) {_isMasterExec = true;}
187 #endif
189 
197  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path) {
199  set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
200 
201  virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path) {
203  set_GenerationOccurrence(0); set_OccMatrix(Occ); _path = path;}
204 
206  virtual void FHwrite () = 0;
207 
209  virtual void FHread (string& filename) = 0;
212  virtual void update();
213 
214 };
215 
216 //CLASS TraitFileHandler
217 //
221 template <class TP> class TraitFileHandler : public FileHandler {
222 protected:
225 public:
226  TraitFileHandler(TP* trait_proto, const char* ext);
227  virtual ~TraitFileHandler () {}
228 
229  virtual void FHwrite () = 0;
230 
231  virtual void FHread (string& filename) = 0;
232 
233  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TP* trait_proto);
234  virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path, TP* trait_proto);
235 };
236 
237 template <class TP> TraitFileHandler<TP>::TraitFileHandler(TP* trait_proto, const char* ext) :
238 FileHandler(ext), _FHLinkedTrait(trait_proto), _FHLinkedTraitIndex(trait_proto->get_index())
239 {}
240 
241 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)
242 {
243  FileHandler::set(rpl_per, gen_per, rpl_occ, gen_occ,rank, path);
244  _FHLinkedTrait = trait_proto;
245  _FHLinkedTraitIndex = trait_proto->get_index();
246 }
247 
248 template <class TP> void TraitFileHandler<TP>::set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path, TP* trait_proto)
249 {
250  FileHandler::set_multi(rpl_per, gen_per, rpl_occ, Occ, path);
251  _FHLinkedTrait = trait_proto;
252  _FHLinkedTraitIndex = trait_proto->get_index();
253 }
254 // EventFileHandler
255 //
258 template <class LCE> class EventFileHandler : public FileHandler {
259 protected:
261 public:
262  EventFileHandler(LCE* event, const char* ext);
263  virtual ~EventFileHandler () {}
264 
265  virtual void FHwrite () = 0;
266 
267  virtual void FHread (string& filename) = 0;
268 
269  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE* event);
270 };
271 
272 template <class LCE> EventFileHandler<LCE>::EventFileHandler(LCE* event, const char* ext) :
273 FileHandler(ext), _FHLinkedEvent(event)
274 { }
275 
276 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)
277 {
278  FileHandler::set(rpl_per,gen_per,rpl_occ,gen_occ,rank,path);
279  _FHLinkedEvent = event;
280 }
281 
283 class FHLogWriter : public FileHandler {
284 public:
285  FHLogWriter() : FileHandler(".log"){};
286  virtual ~FHLogWriter(){}
287 
288  virtual void FHwrite (){}
289  virtual void FHread (string& filename) {}
290 
291 
292  void createInitFile(list< ParamSet* >& params);
293 
294  void save_simparams(list< ParamSet* >& params);
295 
296  void log_message (string& logstr);
297 
298  void open_logfile4writing (ofstream& FH, ios_base::openmode flag = ios_base::out);
299 };
300 #endif //FILEHANDLER_H
301 
Template class for the LCEs StatHandler classes.
Definition: filehandler.h:258
LCE * _FHLinkedEvent
Definition: filehandler.h:260
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE *event)
Definition: filehandler.h:276
virtual void FHwrite()=0
EventFileHandler(LCE *event, const char *ext)
Definition: filehandler.h:272
virtual ~EventFileHandler()
Definition: filehandler.h:263
virtual void FHread(string &filename)=0
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:283
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:201
virtual void FHread(string &filename)
Definition: filehandler.h:289
virtual void FHwrite()
Definition: filehandler.h:288
void log_message(string &logstr)
Definition: filehandler.cc:266
FHLogWriter()
Definition: filehandler.h:285
virtual ~FHLogWriter()
Definition: filehandler.h:286
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:230
void open_logfile4writing(ofstream &FH, ios_base::openmode flag=ios_base::out)
Definition: filehandler.cc:280
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:53
void set_extension(const char *ext)
Definition: filehandler.h:149
void set_OccMatrix(TMatrix *occ)
Definition: filehandler.h:174
std::string & get_path()
Definition: filehandler.h:143
FileServices * _service
Link to the files manager.
Definition: filehandler.h:57
void set_isMasterExec(bool is)
Definition: filehandler.h:186
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:71
bool get_isMasterExec()
Definition: filehandler.h:182
list< int > _generations
Definition: filehandler.h:85
bool _isInputHandler
Writing mode flag.
Definition: filehandler.h:67
unsigned int _ExecRank
unused...
Definition: filehandler.h:88
std::string _path
File path as set during initialization.
Definition: filehandler.h:90
void set_ExecRank(int val)
Definition: filehandler.h:170
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:93
virtual void init()
Called by notifier during simulation setup, performs file checking.
Definition: filehandler.cc:51
list< int >::const_iterator _genITER
Definition: filehandler.h:84
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
Definition: filehandler.h:201
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:151
void set_pop_ptr(Metapop *pop_ptr)
Definition: filehandler.h:137
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:161
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:167
virtual ~FileHandler()
Definition: filehandler.h:125
void set_service(FileServices *srv)
Definition: filehandler.h:141
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:75
bool get_isGenerationPeriodic()
Definition: filehandler.h:163
bool get_isInputHandler()
Definition: filehandler.h:154
void set_path()
Definition: filehandler.cc:112
bool _isMasterExec
Flag telling if the file must be written by the master or the worker node.
Definition: filehandler.h:65
TMatrix * get_OccMatrix()
Definition: filehandler.h:172
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:197
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:99
TMatrix _multipleOccurences
Multiple occurences.
Definition: filehandler.h:82
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
unsigned int get_ReplicateOccurrence()
Definition: filehandler.h:160
FileHandler(const char *ext)
Definition: filehandler.h:107
bool get_isReplicatePeriodic()
Definition: filehandler.h:157
Metapop * get_pop_ptr()
Returns the pointer to the current metapop through the FileServices interface.
Definition: filehandler.h:135
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:139
unsigned int get_GenerationOccurrence()
Definition: filehandler.h:166
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
unsigned int get_ExecRank()
unused yet...
Definition: filehandler.h:169
virtual void update()
Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicit...
Definition: filehandler.cc:166
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:164
std::string & get_extension()
Definition: filehandler.h:147
void set_isInputHandler(bool val)
Definition: filehandler.h:155
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:69
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:158
virtual vector< string > ifExist()
Checks if any file associated with the current file name already exists on disk.
Definition: filehandler.cc:70
A class to manage the files associated with each components of the simulation.
Definition: fileservices.h:52
Service handler (an observer).
Definition: handler.h:36
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:80
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
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:161
double * getValArray() const
Definition: tmatrix.h:202
unsigned int ncols() const
Definition: tmatrix.h:216
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:193
unsigned int nrows() const
Definition: tmatrix.h:213
Template class for the trait's FileHandler.
Definition: filehandler.h:221
virtual ~TraitFileHandler()
Definition: filehandler.h:227
int _FHLinkedTraitIndex
Definition: filehandler.h:224
TraitFileHandler(TP *trait_proto, const char *ext)
Definition: filehandler.h:237
virtual void FHread(string &filename)=0
virtual void FHwrite()=0
TP * _FHLinkedTrait
Definition: filehandler.h:223
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TP *trait_proto)
Definition: filehandler.h:248
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:241
Nemo2.

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

Locations of visitors to this page
Catalogued on GSR