Nemo  2.4.2
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 ( )
44  : _popPtr(0), _rep_filename(""), _basename(""), _root_dir(""),
47 {
48  _logWriter = new FHLogWriter();
50 }
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:281
int _sampled_at_replicate
Definition: fileservices.h:87
Metapop * _sample_pop
Definition: fileservices.h:89
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:68
TMatrix * _patch_sampled
Definition: fileservices.h:85
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:65
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:56
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:54
int _sampled_at_generation
Definition: fileservices.h:87
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir")
Definition: fileservices.h:71
TMatrix * _patch_sample_size
Number of individuals to sample from each patch for the file handlers.
Definition: fileservices.h:81
virtual void attach(Handler *FH)
Attaches the FileHandler to the current list (_writers) of the FileServices.
Definition: fileservices.cc:59
TMatrix * _patch_sample_age
Definition: fileservices.h:83

References _logWriter, and attach().

◆ ~FileServices()

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

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.

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

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
73 {
74  _readers.push_back(FH);
75  FH->set_service(this);
76 }
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:62

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.

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

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.

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

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.
111 {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.

474 {
475  return _basename;
476 }

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.

144 {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.

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

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.

138 {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.

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

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.

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

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

Referenced by GenotyperFH::FHwrite(), TTDeletMutBitstrFH::FHwrite(), GenotyperFH::print_PLINK(), 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.

147 {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.

141 {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.

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

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.

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

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.

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

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.

457 {
459 
460  return _rep_filename;
461 }

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.


481 {
482  return _root_dir;
483 }

References _root_dir.

◆ getSampledPop()

Metapop * FileServices::getSampledPop ( )

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

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

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.

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

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.

80 {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.

435 {
437 }
void log_message(string &logstr)
Definition: filehandler.cc:264

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.

425 {
426  if(_mode == 4)
428  else
430 }
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:199
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:228

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.

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

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.

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

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.

384  {
385 
386  if(_sample_pop) {
387 
389 
390  delete _sample_pop;
391  }
392 
393  _sample_pop = NULL;
394 
397 }

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.

114 {_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.

176 {_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.

514 {
515  _basename = name;
516 }

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.

117 {_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.

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

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.

335 {
336 
337  int* sample = NULL;
338  int size = (int)source_patch->size(SEX, AGE), sample_size;
339  Individual *ind = 0;
340 
341  sample_size = (num < size ? num : size);
342 
343 // cout << " ... SEX = "<<SEX<<"; sample size = "<<sample_size<< "; size of receiving patch container: "<< patch->size(SEX, AGE)<< endl;
344  if( patch->size(SEX, AGE) != 0) fatal("FileServices::subSamplePatch::receiver patch container is not empty before sampling\n");
345 
346  if( sample_size != 0) {
347 
348  int sample[sample_size];
349 
350  RAND::Sample(0, size, sample_size, &sample[0], false);
351 
352  for(unsigned int j = 0; j < sample_size; ++j) {
353 
354  ind = source_patch->get(SEX, AGE, sample[j]);
355 
356  patch->add(SEX, AGE, ind);
357  }
358  }
359 }
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:47
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:496
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:532
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:549
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:723

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.2 by  doxygen 1.9.1

Catalogued on GSR