Nemo  2.4.0
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
FileServices Class Reference

A class to manage the files associated with each components of the simulation. More...

#include <fileservices.h>

+ Inheritance diagram for FileServices:
+ Collaboration diagram for FileServices:

Public Types

typedef std::list< FileHandler * >::const_iterator file_it
 

Public Member Functions

 FileServices ()
 
virtual ~FileServices ()
 
virtual bool init ()
 
virtual void notify ()
 
bool init (list< ParamSet * > &params)
 Checks if files with _basename already exist and save the simulation parameters in log files. More...
 
virtual Metapopget_pop_ptr ()
 Accessor to the pointer to the main population. More...
 
virtual void set_pop_ptr (Metapop *pop)
 Sets the Metapop reference. More...
 
void setMode (unsigned int m)
 Mode setter, determines if file will get overwritten or not. More...
 
unsigned int getMode ()
 Writting mode getter. More...
 
void setBasename (string name)
 Sets the base file name of the simulation. More...
 
void setRootDir (string name)
 Sets the root directory of the simulation. More...
 
void log_simparams ()
 Saves the current simulation parameters to the default parameter logfile. More...
 
void log (string message)
 Write to the parameter logfile. More...
 
list< ParamSet * > & get_params ()
 Accessor to the list of the current parameters of the simulation. More...
 
file_it getFirstWriter ()
 Accessor to first element of the list of output FileHandlers. More...
 
file_it getLastWriter ()
 Accessor to last element of the list of output FileHandlers. More...
 
file_it getFirstReader ()
 Accessor to first element of the list of input FileHandlers. More...
 
file_it getLastReader ()
 Accessor to last element of the list of input FileHandlers. More...
 
FileHandlergetReader (string &type)
 Accessor to a specific file handler specified by its extension string. More...
 
string & getBaseFileName ()
 Accessor to the base file name of the simulation. More...
 
string & getRootDir ()
 Accessor to the name of the simulation's root output directory. More...
 
string getReplicateCounter ()
 Accessor to the current replicate counter string. More...
 
string & getFirstReplicateFileName ()
 Accessor to the first replicate file name. More...
 
string & getReplicateFileName ()
 Accessor to the current replicate file name. More...
 
string getGenerationCounter ()
 Accessor to the current generation counter string. More...
 
string getGenerationReplicateFileName ()
 Accessor to the current file name with generation and replicate counters added. More...
 
virtual void load (SimComponent *sc)
 Tells the SimComponent to load its file handlers. More...
 
virtual void attach (Handler *FH)
 Attaches the FileHandler to the current list (_writers) of the FileServices. More...
 
virtual void attach_reader (FileHandler *FH)
 Attaches the FileHandler to the current list (_readers) of the FileServices. More...
 
virtual void reset ()
 Clears the list of FileHandlers. More...
 
population sampling for output files
void set_sampling_params (TMatrix *size, TMatrix *age, TMatrix *patches)
 Set the population sampling params. More...
 
void empty_sampled_pop ()
 Empty the sampled pop without deleting the individuals since they are also in the main population. More...
 
void reset_sampled_pop ()
 Remove the existing sampled metapop, if it has been allocated previously. More...
 
MetapopgetSampledPop ()
 Sets the down-sampled population and provides accessor to file handlers. More...
 
void subSamplePatch (sex_t SEX, age_idx AGE, unsigned int num, Patch *source_patch, Patch *patch)
 Create a patch from a sample of the corresponding patch in the main metapopulation. More...
 
- Public Member Functions inherited from Service
 Service ()
 
virtual ~Service ()
 

Private Attributes

Metapop_popPtr
 a pointer to the current Metapop More...
 
FHLogWriter_logWriter
 a FileHandler used to save the simulation parameters on disk. More...
 
list< FileHandler * > _writers
 the list of the FileHandler's registered by the SimComponent in output mode More...
 
list< FileHandler * > _readers
 the list of the FileHandler's registered by the SimComponent in input mode More...
 
string _rep_filename
 the file name associated with the current simulation replicate More...
 
string _basename
 the base file name of the simulation, read from the init file (param "filename") More...
 
string _root_dir
 the root directory for the simulation's results, read from the init file (param "root_dir") More...
 
list< ParamSet * > _params
 the list of the parameters of the current simulation. More...
 
unsigned int _mode
 File mode, sets behavior when file must be overwritten or not. More...
 
TMatrix_patch_sample_size
 Number of individuals to sample from each patch for the file handlers. More...
 
TMatrix_patch_sample_age
 
TMatrix_patch_sampled
 
int _sampled_at_replicate
 
int _sampled_at_generation
 
Metapop_sample_pop
 

Detailed Description

A class to manage the files associated with each components of the simulation.

Implements the Observer design pattern (is the concrete subject), stores the base filename of the simulation and updates the replicate filenames. It also performs files checking and saves the simulation parameters on init.

Member Typedef Documentation

◆ file_it

typedef std::list< FileHandler* >::const_iterator FileServices::file_it

Constructor & Destructor Documentation

◆ FileServices()

FileServices::FileServices ( )
45  : _popPtr(0), _rep_filename(""), _basename(""), _root_dir(""),
48 {
49  _logWriter = new FHLogWriter();
51 }
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:282
int _sampled_at_replicate
Definition: fileservices.h:88
Metapop * _sample_pop
Definition: fileservices.h:90
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
TMatrix * _patch_sampled
Definition: fileservices.h:86
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:66
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
int _sampled_at_generation
Definition: fileservices.h:88
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir")
Definition: fileservices.h:72
TMatrix * _patch_sample_size
Number of individuals to sample from each patch for the file handlers.
Definition: fileservices.h:82
virtual void attach(Handler *FH)
Attaches the FileHandler to the current list (_writers) of the FileServices.
Definition: fileservices.cc:60
TMatrix * _patch_sample_age
Definition: fileservices.h:84

References _logWriter, and attach().

◆ ~FileServices()

FileServices::~FileServices ( )
virtual
53  {
54  if(_logWriter) delete _logWriter;
55  if(_sample_pop) delete _sample_pop;
56 }

References _logWriter, and _sample_pop.

Member Function Documentation

◆ attach()

void FileServices::attach ( Handler FH)
virtual

Attaches the FileHandler to the current list (_writers) of the FileServices.

Parameters
FHthe FileHandler

Implements Service.

61 {
62 // Service::attach(FH);
63  FileHandler* FH = dynamic_cast<FileHandler*> (H);
64  _writers.push_back(FH);
65 
66  if(FH->get_isInputHandler()) attach_reader(FH);
67 
68  FH->set_service(this);
69 }
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:52
void set_service(FileServices *srv)
Definition: filehandler.h:140
bool get_isInputHandler()
Definition: filehandler.h:153
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
virtual void attach_reader(FileHandler *FH)
Attaches the FileHandler to the current list (_readers) of the FileServices.
Definition: fileservices.cc:73

References _writers, attach_reader(), FileHandler::get_isInputHandler(), and FileHandler::set_service().

Referenced by FileServices(), BinaryDataSaver::loadFileServices(), LCE_Selection_base::loadFileServices(), Metapop::loadFileServices(), LCE_FileServicesNotifier::loadFileServices(), LCE_StatServiceNotifier::loadFileServices(), TProtoBDMI::loadFileServices(), TProtoDeletMutations_bitstring::loadFileServices(), TProtoNeutralGenes::loadFileServices(), TProtoQuanti::loadFileServices(), and LCE_Breed_Wolbachia::loadFileServices().

+ Here is the caller graph for this function:

◆ attach_reader()

void FileServices::attach_reader ( FileHandler FH)
virtual

Attaches the FileHandler to the current list (_readers) of the FileServices.

Parameters
FHthe FileHandler
74 {
75  _readers.push_back(FH);
76  FH->set_service(this);
77 }
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:63

References _readers, and FileHandler::set_service().

Referenced by attach(), LCE_Cross::loadFileServices(), TProtoDeletMutations_bitstring::loadFileServices(), TProtoNeutralGenes::loadFileServices(), and TProtoQuanti::loadFileServices().

+ Here is the caller graph for this function:

◆ empty_sampled_pop()

void FileServices::empty_sampled_pop ( )

Empty the sampled pop without deleting the individuals since they are also in the main population.

364  {
365 
366  if(_sample_pop) {
367 
368  // the sample pop need to be emptied without destroying the individuals because they
369  // are also in the patch containers in the main pop.
370  Patch* patch;
371  // remove the patches :
372  for(unsigned int i = 0; i < _sample_pop->getPatchArraySize(); ) {
373  // removePatch() resizes the array, i must stay = 0 to mimic pop_front() until array is empty
374  patch = _sample_pop->removePatch(i);
375  patch->clear(); // this resets all container's size to 0 without deleting content
376  delete patch; // delete the patch, without deleting individuals
377  }
378 
379  }
380 
381 }
Patch * removePatch(unsigned int i)
Removes a patch from the patch array and returns it pointer.
Definition: metapop.h:868
unsigned int getPatchArraySize()
Definition: metapop.h:269
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:431
void clear(sex_t SEX, age_idx AGE)
Sets the size of the appropriate container to zero.
Definition: metapop.h:675

References _sample_pop, Patch::clear(), Metapop::getPatchArraySize(), and Metapop::removePatch().

Referenced by getSampledPop(), reset(), and reset_sampled_pop().

+ Here is the caller graph for this function:

◆ get_params()

list< ParamSet* >& FileServices::get_params ( )
inline

Accessor to the list of the current parameters of the simulation.

136 {return _params;};
list< ParamSet * > _params
the list of the parameters of the current simulation.
Definition: fileservices.h:76

References _params.

Referenced by BinaryDataSaver::printHeader().

+ Here is the caller graph for this function:

◆ get_pop_ptr()

virtual Metapop* FileServices::get_pop_ptr ( )
inlinevirtual

Accessor to the pointer to the main population.

Returns
the pointer to the current Metapop as set during simulation setup.
112 {return _popPtr;}

References _popPtr.

Referenced by GenotyperFH::FHwrite(), TTBDMI_FH::FHwrite(), TTDeletMutBitstrFH::FHwrite(), TTNeutralGenesFH::FHwrite(), TTQuantiFH::FHwrite(), TTQFreqExtractor::FHwrite(), TTNeutralGenesFH::write_Fst_i(), and TTNeutralGenesFH::write_varcompWC().

+ Here is the caller graph for this function:

◆ getBaseFileName()

string & FileServices::getBaseFileName ( )

Accessor to the base file name of the simulation.

475 {
476  return _basename;
477 }

References _basename.

Referenced by LCE_StatFH::FHwrite(), BinaryDataSaver::finish(), BinaryDataSaver::ifExist(), LCE_StatFH::ifExist(), SimRunner::printLog(), LCE_StatFH::PrintStat_byGen(), SimRunner::run(), and SimRunner::runPostExecReplicateWise().

+ Here is the caller graph for this function:

◆ getFirstReader()

file_it FileServices::getFirstReader ( )
inline

Accessor to first element of the list of input FileHandlers.

145 {return _readers.begin();}

References _readers.

Referenced by getReader().

+ Here is the caller graph for this function:

◆ getFirstReplicateFileName()

string & FileServices::getFirstReplicateFileName ( )

Accessor to the first replicate file name.

443 {
444  ostringstream rpl;
445 
446  rpl.fill('0');
447  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
448  rpl<< max( 1, _myenv->workerRank() );
449 
450  _rep_filename = _basename + "_" + rpl.str();
451 
452  return _rep_filename;
453 }
int workerRank() const
Definition: MPImanager.h:129
unsigned int getReplicates()
Definition: metapop.h:274
MPIenv * _myenv
Definition: MPImanager.cc:35

References _basename, _myenv, _popPtr, _rep_filename, Metapop::getReplicates(), and MPIenv::workerRank().

◆ getFirstWriter()

file_it FileServices::getFirstWriter ( )
inline

Accessor to first element of the list of output FileHandlers.

139 {return _writers.begin();}

References _writers.

Referenced by SimRunner::printLog().

+ Here is the caller graph for this function:

◆ getGenerationCounter()

string FileServices::getGenerationCounter ( )

Accessor to the current generation counter string.

502 {
503  ostringstream gen;
504 
505  gen.fill('0');
506  gen.width( (int)log10((double)_popPtr->getGenerations()) + 1);
508 
509  return gen.str();
510 }
unsigned int getGenerations()
Definition: metapop.h:272
unsigned int getCurrentGeneration()
Definition: metapop.h:295

References _popPtr, Metapop::getCurrentGeneration(), and Metapop::getGenerations().

Referenced by getGenerationReplicateFileName().

+ Here is the caller graph for this function:

◆ getGenerationReplicateFileName()

string FileServices::getGenerationReplicateFileName ( )

Accessor to the current file name with generation and replicate counters added.

467 {
468  string name = _basename + "_" + getGenerationCounter() + "_" + getReplicateCounter();
469  return name;
470 }
string getGenerationCounter()
Accessor to the current generation counter string.
Definition: fileservices.cc:501
string getReplicateCounter()
Accessor to the current replicate counter string.
Definition: fileservices.cc:488

References _basename, getGenerationCounter(), and getReplicateCounter().

Referenced by GenotyperFH::FHwrite(), TTDeletMutBitstrFH::FHwrite(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), TTNeutralGenesFH::write_GENEPOP(), TTNeutralGenesFH::write_PLINK(), TTQuantiFH::write_PLINK(), TTNeutralGenesFH::write_TAB(), and TTNeutralGenesFH::write_varcompWC().

+ Here is the caller graph for this function:

◆ getLastReader()

file_it FileServices::getLastReader ( )
inline

Accessor to last element of the list of input FileHandlers.

148 {return _readers.end();}

References _readers.

Referenced by getReader().

+ Here is the caller graph for this function:

◆ getLastWriter()

file_it FileServices::getLastWriter ( )
inline

Accessor to last element of the list of output FileHandlers.

142 {return _writers.end();}

References _writers.

Referenced by SimRunner::printLog().

+ Here is the caller graph for this function:

◆ getMode()

unsigned int FileServices::getMode ( )
inline

Writting mode getter.

121 {return _mode;}
unsigned int _mode
File mode, sets behavior when file must be overwritten or not.
Definition: fileservices.h:79

References _mode.

Referenced by BinaryDataSaver::finish().

+ Here is the caller graph for this function:

◆ getReader()

FileHandler * FileServices::getReader ( string &  type)

Accessor to a specific file handler specified by its extension string.

533 {
534  file_it file = getFirstReader(), last = getLastReader() ;
535 
536  for(;file != last; file++) {
537  if( (*file)->get_extension().compare( type ) == 0) {
538  return (*file);
539  }
540  }
541  return 0;
542 }
file_it getFirstReader()
Accessor to first element of the list of input FileHandlers.
Definition: fileservices.h:145
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:94
file_it getLastReader()
Accessor to last element of the list of input FileHandlers.
Definition: fileservices.h:148

References getFirstReader(), and getLastReader().

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ getReplicateCounter()

string FileServices::getReplicateCounter ( )

Accessor to the current replicate counter string.

489 {
490  ostringstream rpl;
491 
492  rpl.fill('0');
493  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
495 
496  return rpl.str();
497 }
unsigned int getCurrentReplicate()
Definition: metapop.h:294

References _popPtr, Metapop::getCurrentReplicate(), and Metapop::getReplicates().

Referenced by getGenerationReplicateFileName(), and getReplicateFileName().

+ Here is the caller graph for this function:

◆ getReplicateFileName()

string & FileServices::getReplicateFileName ( )

Accessor to the current replicate file name.

458 {
460 
461  return _rep_filename;
462 }

References _basename, _rep_filename, and getReplicateCounter().

Referenced by TTQFreqExtractor::FHwrite().

+ Here is the caller graph for this function:

◆ getRootDir()

std::string & FileServices::getRootDir ( )

Accessor to the name of the simulation's root output directory.


482 {
483  return _root_dir;
484 }

References _root_dir.

◆ getSampledPop()

Metapop * FileServices::getSampledPop ( )

Sets the down-sampled population and provides accessor to file handlers.

198 {
199  // if the sampling parameters haven't been set, we return the main pop ptr
200  if(_patch_sample_size->length() == 0 )
201  return _popPtr;
202 
203  // if we already created a population sample, we return it:
206  {
207  return _sample_pop;
208  }
209 
210 #ifdef _DEBUG_
211  message("FileServices::setting sub-sampled pop in %i patches and %i age classes: ", _patch_sample_size->nrows(), _patch_sample_size->ncols());
212 #endif
213 
214  // else, population hasn't been sampled yet, create sample:
215 
216  Patch *sample_patch, *src_patch;
217 
218  //first time it is called, the pointer should be NULL upon instantiation of FileServices
219  if(!_sample_pop) {
220 
221  _sample_pop = new Metapop();
222 
223  } else {
224 
226 
228 
229  }
230 
231  // set the age flag as the union of all age classes selected
232  unsigned int length = _patch_sample_age->length();
233  const double* age_selected = _patch_sample_age->get();
234 
235  age_t age_flag = 0;
236 
237  if(length > 0){
238  int mask = 1;
239  for(unsigned int i = 0; i < length; ++i) {
240  mask <<= int(age_selected[i]);
241  age_flag |= mask;
242  mask = 1;
243  }
244  }
245  // check the age flag:
246  if( age_flag != 0) {
247 
248  // check if current pop age flag is compatible with user's choice:
249 
250  if(!(age_flag & _popPtr->getCurrentAge())) {
251  warning("Cannot sub-sample the population for output file writers: no individual present within age class(es) selected at time of sampling.\n");
252  }
253 
254  } else { // the age flag wasn't set by the user
255 
256  age_flag = _popPtr->getCurrentAge();
257 
258  }
259 
260  //need to set the age flag of the population
261  _sample_pop->setCurrentAge( age_flag );
262 
263  // stop here if age flag is null after checking:
264  if( age_flag == 0 )
265  fatal("The population cannot be sub-sampled for the age-class chosen (parameter \"files_sample_age\")\n Check your parameter file or contact the developers\n");
266 
267 #ifdef _DEBUG_
268  message("after check, sampling age is %i; ", age_flag);
269 #endif
270 
271  // add some double checking on matrix sizes:
273  fatal("The patch sampling parameters are mis-specified: number of patches don't match between size and patch-id input parameters\n.");
274 
276  fatal("The patch sampling parameters are mis-specified: number of stages don't match between size and age-class input parameters\n.");
277 
279  fatal("The patch sampling parameters are mis-specified: number of patches don't match between size and age-class input parameters\n.");
280 
281  // add patches to match current parameters and patch sizes (discard empty patches)
282  age_idx age;
283  unsigned int patchID;
284  for(unsigned int i = 0; i < _patch_sampled->ncols(); ++i) {
285 
286  patchID = _patch_sampled->get(0, i);
287 
288 // cout << " --- sampling patch "<< patchID << endl;
289 
290  if( _popPtr->size(age_flag, patchID) == 0 ) continue; //skip empty patches
291 
292  src_patch = _popPtr->getPatch(patchID);
293  // allocate new patches
294  sample_patch = new Patch();
295  sample_patch->setID(patchID);
296  sample_patch->set_K(src_patch->get_K());
297  sample_patch->set_KFem(src_patch->get_KFem());
298  sample_patch->set_KMal(src_patch->get_KMal());
299 
300  // sample in each age class selected (has been expanded at set up to hold the necessary number of values)
301  for(unsigned int j = 0; j < _patch_sample_size->ncols(); ++j) {
302 
303  age = age_idx(_patch_sample_age->get(i, j));
304 
305 // cout << " ... in age container "<<age<<" (age flag "<<age_flag<<")\n";
306 
307  if(age_flag & age_t(pow(2,_patch_sample_age->get(i, j)))) {
308  subSamplePatch(FEM, age, _patch_sample_size->get(i,j), src_patch, sample_patch);
309  subSamplePatch(MAL, age, _patch_sample_size->get(i,j), src_patch, sample_patch);
310  }
311 
312  }
313 
314  _sample_pop->addPatch( sample_patch );
315 
316  }
317 
320 
321 #ifdef _DEBUG_
322  message("sampled pop is: \n");
323  _sample_pop->show_up();
324 #endif
325 
326 
329 
330  return _sample_pop;
331 }
void empty_sampled_pop()
Empty the sampled pop without deleting the individuals since they are also in the main population.
Definition: fileservices.cc:364
void subSamplePatch(sex_t SEX, age_idx AGE, unsigned int num, Patch *source_patch, Patch *patch)
Create a patch from a sample of the corresponding patch in the main metapopulation.
Definition: fileservices.cc:335
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:79
void addPatch(Patch *patch)
Adds a patch to the population.
Definition: metapop.h:885
unsigned int size()
Get the total number of individuals present in the population, all sex and age classes together.
Definition: metapop.h:311
TMatrix * getPatchCapacities()
Definition: metapop.h:280
void setPatchNbr(unsigned int val)
Definition: metapop.h:286
age_t getCurrentAge()
Definition: metapop.h:298
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:256
void setPatchSizes(TMatrix &mat)
Definition: metapop.h:287
void show_up()
Definition: metapop.cc:1241
void setCurrentAge(age_t age)
Sets the age flag.
Definition: metapop.h:302
void setID(unsigned int i)
Definition: metapop.h:471
unsigned int get_K()
Definition: metapop.h:481
void set_KMal(unsigned int k)
Definition: metapop.h:474
void set_KFem(unsigned int k)
Definition: metapop.h:473
void set_K(unsigned int k)
Definition: metapop.h:472
unsigned int get_KFem()
Definition: metapop.h:483
unsigned int get_KMal()
Definition: metapop.h:484
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 length() const
Returns the number of elements in the matrix.
Definition: tmatrix.h:217
unsigned int nrows() const
Definition: tmatrix.h:212
void fatal(const char *str,...)
Definition: output.cc:99
void warning(const char *str,...)
Definition: output.cc:57
void message(const char *message,...)
Definition: output.cc:39
@ FEM
Definition: types.h:36
@ MAL
Definition: types.h:36
#define NONE
No age flag.
Definition: types.h:47
unsigned int age_t
Age class flags.
Definition: types.h:45
age_idx
Array index of the age classes in the patch sizes and containers arrays.
Definition: types.h:40

References _patch_sample_age, _patch_sample_size, _patch_sampled, _popPtr, _sample_pop, _sampled_at_generation, _sampled_at_replicate, Metapop::addPatch(), empty_sampled_pop(), fatal(), FEM, TMatrix::get(), Patch::get_K(), Patch::get_KFem(), Patch::get_KMal(), Metapop::getCurrentAge(), Metapop::getCurrentGeneration(), Metapop::getCurrentReplicate(), Metapop::getPatch(), Metapop::getPatchArraySize(), Metapop::getPatchCapacities(), TMatrix::length(), MAL, message(), TMatrix::ncols(), NONE, TMatrix::nrows(), Patch::set_K(), Patch::set_KFem(), Patch::set_KMal(), Metapop::setCurrentAge(), Patch::setID(), Metapop::setPatchNbr(), Metapop::setPatchSizes(), Metapop::show_up(), Metapop::size(), subSamplePatch(), and warning().

Referenced by GenotyperFH::FHwrite(), TTBDMI_FH::FHwrite(), TTDeletMutBitstrFH::FHwrite(), TTNeutralGenesFH::FHwrite(), TTQuantiFH::FHwrite(), and TTQOhtaStats::FHwrite().

+ Here is the caller graph for this function:

◆ init() [1/2]

virtual bool FileServices::init ( )
inlinevirtual

Implements Service.

100 {return false;}

Referenced by SimRunner::run().

+ Here is the caller graph for this function:

◆ init() [2/2]

bool FileServices::init ( list< ParamSet * > &  params)

Checks if files with _basename already exist and save the simulation parameters in log files.

Parameters
paramsa ref to the list of the current parameters of the simulation
Returns
true if the files check is ok
false if the user wants to skip this simulation
86 {
87  char yn;
88  bool ok;
89  file_it HIT;
90 
91  _params = params;
92 
93  _logWriter->set(0, 0, 0, 0, 0, "");
94 
95  //first, build path and filename for each file handler
96  HIT = _writers.begin();
97 
98  if(_myenv->isMaster()) message(" outputs: %s{",_root_dir.c_str());
99 
100  while(HIT != _writers.end()) {
101  if(_myenv->isMaster()) message("%s%s*%s",
102  (*HIT)->get_path().c_str(),
103  ((*HIT)->get_path().size() !=0? "/":""),
104  (*HIT)->get_extension().c_str());
105  (*HIT)->init();
106  HIT++;
107 
108  if(HIT != _writers.end() && _myenv->isMaster()) message(", ");
109  }
110  if(_myenv->isMaster()) message("}\n");
111 
112  //then check if any file already exists
113 check:
114 
115  ok = true;
116  HIT = _writers.begin();
117  vector<string> local_files, Xing_files;
118 
119  //we have to skip the first writer, it is the log-writer (always overwritten)
120  while(++HIT != _writers.end()) {
121 
122  Xing_files = (*HIT)->ifExist();
123 
124  if( Xing_files.size() != 0 ) {
125  local_files.insert(local_files.end(), Xing_files.begin(), Xing_files.end() );
126  ok = false;
127  } else
128  ok &= true;
129  }
130 
131  if(!ok) {
132 
133 // warning("simulation filename \"%s\" already used on disc!", getBaseFileName().c_str());
134 
135  for(unsigned int i=0; i < local_files.size(); ++i)
136  warning("\"%s\" exists", local_files[i].c_str());
137 
138  //file mode is set during simulation setup, in SimRunner::init()
139  if(_mode == 0) { //"run"
140 
141 #if defined(_R_OUTPUT_) || defined(LOW_VERBOSE) || defined(USE_MPI)
142  yn = 's';
143  message("\nPlease choose an other base filename or move the existing file(s)\n");
144 #else
145 
146  message("\nDo you want to overwrite all the files that use it ? (y/n/s(kip)): ");
147  cin>>yn;
148 #endif
149 
150  } else if( _mode == 1 ) { //"overwrite"
151 
152  warning("Overwriting existing files.\n\n");
153  yn = 'y';
154 
155  } else if( _mode == 2 ) { //"skip" & "silent_run"
156 
157  error("Files with the same simulation name already exist on disk!\n");
158  error("Choose another base filename or move the existing file(s)\n");
159  yn = 's';
160 
161  } else if( _mode == 3 || _mode == 4) { // "dryrun" & "create_init"
162  //dryrun, pretend overwriting, silently
163  yn = 'y';
164  } else {
165  fatal("Run mode not properly set, check simulation parameter \"run_mode\".\n");
166  }
167 
168 
169  switch(yn) {
170  case 'y':
171  break;
172  case 's':
173  error("Skipping this simulation\n");
174  return false;
175  default: {
176  message("Please give a new output filename : ");
177  cin>>_basename;
178  goto check;
179  }
180  }
181  }
182 
183  return true;
184 }
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
bool isMaster() const
Definition: MPImanager.h:127
int error(const char *str,...)
Definition: output.cc:78

References _basename, _logWriter, _mode, _myenv, _params, _root_dir, _writers, error(), fatal(), MPIenv::isMaster(), message(), FileHandler::set(), and warning().

◆ load()

void FileServices::load ( SimComponent sc)
virtual

Tells the SimComponent to load its file handlers.

Parameters
scthe SimComponent

Implements Service.

81 {sc->loadFileServices(this);}
virtual void loadFileServices(FileServices *loader)=0
Loads the component's FileHandler onto the FileServices.

References SimComponent::loadFileServices().

Referenced by SimRunner::register_component().

+ Here is the caller graph for this function:

◆ log()

void FileServices::log ( string  message)

Write to the parameter logfile.

436 {
438 }
void log_message(string &logstr)
Definition: filehandler.cc:265

References _logWriter, FHLogWriter::log_message(), and message().

Referenced by Metapop::loadPopFromBinarySource(), SimRunner::run(), and LCE_Selection_base::set_std_rate_of_change().

+ Here is the caller graph for this function:

◆ log_simparams()

void FileServices::log_simparams ( )

Saves the current simulation parameters to the default parameter logfile.

426 {
427  if(_mode == 4)
429  else
431 }
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:200
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:229

References _logWriter, _mode, _params, FHLogWriter::createInitFile(), and FHLogWriter::save_simparams().

Referenced by SimRunner::run().

+ Here is the caller graph for this function:

◆ notify()

void FileServices::notify ( )
virtual

Implements Service.

189 {
190  for (file_it file = _writers.begin(); file != _writers.end(); ++file) {
191  (*file)->update();
192  }
193 }

References _writers.

Referenced by LCE_FileServicesNotifier::execute(), and SimRunner::Replicate_LOOP().

+ Here is the caller graph for this function:

◆ reset()

void FileServices::reset ( )
virtual

Clears the list of FileHandlers.

Implements Service.

403 {
404 #ifdef _DEBUG_
405  message("FileServices::reset\n");
406 #endif
407  //Service::reset();
408  _writers.clear(); _writers.push_back(_logWriter);
409  _readers.clear();
410 
411  if(_sample_pop != NULL) {
413  delete _sample_pop;
414  }
415 
416  _sample_pop = NULL;
417 
418 #ifdef _DEBUG_
419  message("FileServices::reset done\n");
420 #endif
421 }

References _logWriter, _readers, _sample_pop, _writers, empty_sampled_pop(), and message().

Referenced by SimRunner::reset_services().

+ Here is the caller graph for this function:

◆ reset_sampled_pop()

void FileServices::reset_sampled_pop ( )

Remove the existing sampled metapop, if it has been allocated previously.

385  {
386 
387  if(_sample_pop) {
388 
390 
391  delete _sample_pop;
392  }
393 
394  _sample_pop = NULL;
395 
398 }

References _sample_pop, _sampled_at_generation, _sampled_at_replicate, and empty_sampled_pop().

Referenced by LCE_FileServicesNotifier::loadFileServices().

+ Here is the caller graph for this function:

◆ set_pop_ptr()

virtual void FileServices::set_pop_ptr ( Metapop pop)
inlinevirtual

Sets the Metapop reference.

115 {_popPtr = pop;}

References _popPtr.

Referenced by SimRunner::init_components().

+ Here is the caller graph for this function:

◆ set_sampling_params()

void FileServices::set_sampling_params ( TMatrix size,
TMatrix age,
TMatrix patches 
)
inline

Set the population sampling params.

177 {_patch_sample_size = size; _patch_sample_age = age; _patch_sampled = patches;}

References _patch_sample_age, _patch_sample_size, and _patch_sampled.

Referenced by LCE_FileServicesNotifier::loadFileServices().

+ Here is the caller graph for this function:

◆ setBasename()

void FileServices::setBasename ( string  name)

Sets the base file name of the simulation.

515 {
516  _basename = name;
517 }

References _basename.

Referenced by SimRunner::init().

+ Here is the caller graph for this function:

◆ setMode()

void FileServices::setMode ( unsigned int  m)
inline

Mode setter, determines if file will get overwritten or not.

118 {_mode = m;}

References _mode.

Referenced by SimRunner::init().

+ Here is the caller graph for this function:

◆ setRootDir()

void FileServices::setRootDir ( string  name)

Sets the root directory of the simulation.

522 {
523  _root_dir = name;
524 
525  if(_root_dir.size() != 0 && _root_dir[_root_dir.length()-1] != '/')
526  _root_dir += "/";
527 
528 }

References _root_dir.

Referenced by SimRunner::init().

+ Here is the caller graph for this function:

◆ subSamplePatch()

void FileServices::subSamplePatch ( sex_t  SEX,
age_idx  AGE,
unsigned int  num,
Patch source_patch,
Patch patch 
)

Create a patch from a sample of the corresponding patch in the main metapopulation.

336 {
337 
338  int* sample = NULL;
339  int size = (int)source_patch->size(SEX, AGE), sample_size;
340  Individual *ind = 0;
341 
342  sample_size = (num < size ? num : size);
343 
344 // cout << " ... SEX = "<<SEX<<"; sample size = "<<sample_size<< "; size of receiving patch container: "<< patch->size(SEX, AGE)<< endl;
345  if( patch->size(SEX, AGE) != 0) fatal("FileServices::subSamplePatch::receiver patch container is not empty before sampling\n");
346 
347  if( sample_size != 0) {
348 
349  int sample[sample_size];
350 
351  RAND::Sample(0, size, sample_size, &sample[0], false);
352 
353  for(unsigned int j = 0; j < sample_size; ++j) {
354 
355  ind = source_patch->get(SEX, AGE, sample[j]);
356 
357  patch->add(SEX, AGE, ind);
358  }
359  }
360 }
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:48
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:497
Individual * get(sex_t SEX, age_idx AGE, unsigned int at)
Returns a pointer to the individual sitting at the index passed.
Definition: metapop.h:533
void add(sex_t SEX, age_idx AGE, Individual *ind)
Adds an individual to the appropriate container, increments its size, eventually resizing it.
Definition: metapop.h:550
static void Sample(const int from, const int to, const unsigned int num, int *result, bool replace)
Creates a sample of integers within range [from, to), with or without replacement.
Definition: Uniform.h:713

References Patch::add(), fatal(), Patch::get(), RAND::Sample(), and Patch::size().

Referenced by getSampledPop().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _basename

string FileServices::_basename
private

the base file name of the simulation, read from the init file (param "filename")

Referenced by getBaseFileName(), getFirstReplicateFileName(), getGenerationReplicateFileName(), getReplicateFileName(), init(), and setBasename().

◆ _logWriter

FHLogWriter* FileServices::_logWriter
private

a FileHandler used to save the simulation parameters on disk.

Referenced by FileServices(), init(), log(), log_simparams(), reset(), and ~FileServices().

◆ _mode

unsigned int FileServices::_mode
private

File mode, sets behavior when file must be overwritten or not.

Referenced by getMode(), init(), log_simparams(), and setMode().

◆ _params

list< ParamSet* > FileServices::_params
private

the list of the parameters of the current simulation.

The list is created when init() is called from SimRunner::run().

Referenced by get_params(), init(), and log_simparams().

◆ _patch_sample_age

TMatrix* FileServices::_patch_sample_age
private

◆ _patch_sample_size

TMatrix* FileServices::_patch_sample_size
private

Number of individuals to sample from each patch for the file handlers.

Referenced by getSampledPop(), and set_sampling_params().

◆ _patch_sampled

TMatrix* FileServices::_patch_sampled
private

◆ _popPtr

Metapop* FileServices::_popPtr
private

◆ _readers

list< FileHandler* > FileServices::_readers
private

the list of the FileHandler's registered by the SimComponent in input mode

Referenced by attach_reader(), getFirstReader(), getLastReader(), and reset().

◆ _rep_filename

string FileServices::_rep_filename
private

the file name associated with the current simulation replicate

Referenced by getFirstReplicateFileName(), and getReplicateFileName().

◆ _root_dir

string FileServices::_root_dir
private

the root directory for the simulation's results, read from the init file (param "root_dir")

Referenced by getRootDir(), init(), and setRootDir().

◆ _sample_pop

Metapop* FileServices::_sample_pop
private

◆ _sampled_at_generation

int FileServices::_sampled_at_generation
private

Referenced by getSampledPop(), and reset_sampled_pop().

◆ _sampled_at_replicate

int FileServices::_sampled_at_replicate
private

Referenced by getSampledPop(), and reset_sampled_pop().

◆ _writers

list< FileHandler* > FileServices::_writers
private

the list of the FileHandler's registered by the SimComponent in output mode

Referenced by attach(), getFirstWriter(), getLastWriter(), init(), notify(), and reset().


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