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

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR