Nemo  2.3.46
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),
115  _isReplicatePeriodic(0), _isGenerationPeriodic(0),
116  _ReplicateOccurrence(0), _GenerationOccurrence(0),
117  _current_replicate(0), _current_generation(0), _ExecRank(0),
118  _path(), _extension(ext), _current_filename(), _pop(0)
119  {}
120 
121  virtual ~FileHandler ( ) { }
123  virtual void init ( );
127  virtual bool ifExist( );
130 
131  Metapop* get_pop_ptr ( ) {return _pop;}
132 
133  void set_pop_ptr (Metapop* pop_ptr) { _pop = pop_ptr;}
136 
137  void set_service (FileServices* srv) {_service = srv;}
138 
139  std::string& get_path () {return _path;}
140 
141  void set_path ( );
142 
143  std::string& get_extension ( ) {return _extension;}
144 
145  void set_extension (const char* ext) {_extension = ext;}
147  std::string& get_filename ();
148 
149 
151  void set_isInputHandler (bool val) {_isInputHandler = val;}
152 
154  void set_isReplicatePeriodic (bool val) {_isReplicatePeriodic = val;}
155 
157  void set_ReplicateOccurrence (unsigned int val) {_ReplicateOccurrence = val;}
158 
160  void set_isGenerationPeriodic (bool val) {_isGenerationPeriodic = val;}
161 
163  void set_GenerationOccurrence (unsigned int val) {_GenerationOccurrence = val;}
165  unsigned int get_ExecRank () {return _ExecRank;}
166  void set_ExecRank (int val) {_ExecRank = val;}
167 
169 
170  void set_OccMatrix (TMatrix* occ) {
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  }
177 
179 #ifdef USE_MPI
180  void set_isMasterExec(bool is) {_isMasterExec = is;}
181 #else
182  void set_isMasterExec(bool is) {_isMasterExec = true;}
183 #endif
184 
193  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path) {
195  set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
196 
197  virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix* Occ, string path) {
199  set_GenerationOccurrence(0); set_OccMatrix(Occ); _path = path;}
200 
202  virtual void FHwrite () = 0;
203 
205  virtual void FHread (string& filename) = 0;
208  virtual void update();
209 
210 };
211 
212 //CLASS TraitFileHandler
213 //
217 template <class TP> class TraitFileHandler : public FileHandler {
218 protected:
221 public:
222  TraitFileHandler(TP* trait_proto, const char* ext);
223  virtual ~TraitFileHandler () {}
224 
225  virtual void FHwrite () = 0;
226 
227  virtual void FHread (string& filename) = 0;
228 
229  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TP* trait_proto);
230 };
231 
232 template <class TP> TraitFileHandler<TP>::TraitFileHandler(TP* trait_proto, const char* ext) :
233 FileHandler(ext), _FHLinkedTrait(trait_proto), _FHLinkedTraitIndex(trait_proto->get_index())
234 { }
235 
236 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)
237 {
238  FileHandler::set(rpl_per, gen_per, rpl_occ, gen_occ,rank, path);
239  _FHLinkedTrait = trait_proto;
240  _FHLinkedTraitIndex = trait_proto->get_index();
241 }
242 
243 // EventFileHandler
244 //
247 template <class LCE> class EventFileHandler : public FileHandler {
248 protected:
250 public:
251  EventFileHandler(LCE* event, const char* ext);
252  virtual ~EventFileHandler () {}
253 
254  virtual void FHwrite () = 0;
255 
256  virtual void FHread (string& filename) = 0;
257 
258  virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE* event);
259 };
260 
261 template <class LCE> EventFileHandler<LCE>::EventFileHandler(LCE* event, const char* ext) :
262 FileHandler(ext), _FHLinkedEvent(event)
263 { }
264 
265 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)
266 {
267  FileHandler::set(rpl_per,gen_per,rpl_occ,gen_occ,rank,path);
268  _FHLinkedEvent = event;
269 }
270 
272 class FHLogWriter : public FileHandler {
273 public:
274  FHLogWriter() : FileHandler(".log"){};
275  virtual ~FHLogWriter(){}
276 
277  virtual void FHwrite (){}//@TODO implement log writer procedures
278  virtual void FHread (string& filename) {}
279 
280 
281  void createInitFile(list< ParamSet* >& params);
282 
283  void save_simparams(list< ParamSet* >& params);
284 
285  void log_message (string& logstr);
286 
287  void open_logfile4writing (ofstream& FH, ios_base::openmode flag = ios_base::out);
288 };
289 #endif //FILEHANDLER_H
290 
Template class for the LCEs StatHandler classes.
Definition: filehandler.h:247
TMatrix _multipleOccurences
Multiple occurences.
Definition: filehandler.h:82
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:154
Template class for the trait's FileHandler.
Definition: filehandler.h:217
virtual void init()
Called by notifier during simulation setup, performs file checking.
Definition: filehandler.cc:51
void set_GenerationOccurrence(unsigned int val)
Definition: filehandler.h:163
TP * _FHLinkedTrait
Definition: filehandler.h:219
bool get_isMasterExec()
Definition: filehandler.h:178
virtual void FHwrite()=0
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:53
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:160
unsigned int ncols()
Definition: tmatrix.h:143
void set_pop_ptr(Metapop *pop_ptr)
Definition: filehandler.h:133
EventFileHandler(LCE *event, const char *ext)
Definition: filehandler.h:261
unsigned int get_ExecRank()
unused yet...
Definition: filehandler.h:165
virtual ~TraitFileHandler()
Definition: filehandler.h:223
list< int > _generations
Definition: filehandler.h:85
void set_isMasterExec(bool is)
Definition: filehandler.h:182
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:157
unsigned int get_ReplicateOccurrence()
Definition: filehandler.h:156
void set_extension(const char *ext)
Definition: filehandler.h:145
FileHandler(const char *ext)
Definition: filehandler.h:107
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:272
bool _isMasterExec
Flag telling if the file must be written by the master or the slave node.
Definition: filehandler.h:65
unsigned int _current_generation
The current generation number of the simulation.
Definition: filehandler.h:79
list< int >::const_iterator _genITER
Definition: filehandler.h:84
virtual void update()
Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicit...
Definition: filehandler.cc:163
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:135
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:79
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE *event)
Definition: filehandler.h:265
virtual bool ifExist()
Checks if any file associated with the current file name already exists on disk.
Definition: filehandler.cc:70
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:148
double get(unsigned int i, unsigned int j)
Accessor to element at row i and column j.
Definition: tmatrix.h:120
virtual ~FileHandler()
Definition: filehandler.h:121
void set_path()
Definition: filehandler.cc:109
std::string & get_extension()
Definition: filehandler.h:143
virtual void FHread(string &filename)
Definition: filehandler.h:278
std::string _path
File path as set during initialization.
Definition: filehandler.h:90
virtual void FHread(string &filename)=0
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:236
bool get_isGenerationPeriodic()
Definition: filehandler.h:159
unsigned int _ReplicateOccurrence
Tells every what replicate the file should be written.
Definition: filehandler.h:73
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:93
TraitFileHandler(TP *trait_proto, const char *ext)
Definition: filehandler.h:232
unsigned int _current_replicate
The current replicate number of the simulation.
Definition: filehandler.h:77
virtual void FHwrite()
Definition: filehandler.h:277
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:71
bool get_isReplicatePeriodic()
Definition: filehandler.h:153
TMatrix * get_OccMatrix()
Definition: filehandler.h:168
virtual void FHwrite()=0
Service handler (an observer).
Definition: handler.h:36
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:99
void set_ExecRank(int val)
Definition: filehandler.h:166
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:189
Metapop * get_pop_ptr()
Returns the pointer to the current metapop through the FileServices interface.
Definition: filehandler.h:131
void set_OccMatrix(TMatrix *occ)
Definition: filehandler.h:170
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:218
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
Definition: filehandler.h:197
void log_message(string &logstr)
Definition: filehandler.cc:254
unsigned int _ExecRank
unused...
Definition: filehandler.h:88
virtual ~EventFileHandler()
Definition: filehandler.h:252
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:193
void set_service(FileServices *srv)
Definition: filehandler.h:137
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
FileServices * _service
Link to the files manager.
Definition: filehandler.h:57
virtual ~FHLogWriter()
Definition: filehandler.h:275
unsigned int get_GenerationOccurrence()
Definition: filehandler.h:162
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions.
Definition: tmatrix.h:97
std::string & get_path()
Definition: filehandler.h:139
bool get_isInputHandler()
Definition: filehandler.h:150
LCE * _FHLinkedEvent
Definition: filehandler.h:249
A class to manage the files associated with each components of the simulation.
Definition: fileservices.h:51
virtual void FHread(string &filename)=0
Default input function.
bool _isInputHandler
Writing mode flag.
Definition: filehandler.h:67
void open_logfile4writing(ofstream &FH, ios_base::openmode flag=ios_base::out)
Definition: filehandler.cc:268
FHLogWriter()
Definition: filehandler.h:274
int _FHLinkedTraitIndex
Definition: filehandler.h:220
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:75
void set_isInputHandler(bool val)
Definition: filehandler.h:151
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
unsigned int nrows()
Definition: tmatrix.h:140
double * getValArray() const
Definition: tmatrix.h:129
virtual void FHread(string &filename)=0
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:69

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR