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

References _logWriter, and attach().

◆ ~FileServices()

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

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.

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

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().

◆ attach_reader()

void FileServices::attach_reader ( FileHandler FH)
virtual

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

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

References _readers, and FileHandler::set_service().

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

◆ empty_sampled_pop()

void FileServices::empty_sampled_pop ( )

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

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

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

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

◆ get_params()

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

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

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

References _params.

Referenced by BinaryDataSaver::printHeader().

◆ 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.
113 {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().

◆ getBaseFileName()

string & FileServices::getBaseFileName ( )

◆ getFirstReader()

file_it FileServices::getFirstReader ( )
inline

Accessor to first element of the list of input FileHandlers.

146 {return _readers.begin();}

References _readers.

Referenced by getReader().

◆ getFirstReplicateFileName()

string & FileServices::getFirstReplicateFileName ( )

Accessor to the first replicate file name.

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

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.

140 {return _writers.begin();}

References _writers.

Referenced by SimRunner::printLog().

◆ getGenerationCounter()

string FileServices::getGenerationCounter ( )

Accessor to the current generation counter string.

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

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

Referenced by getGenerationReplicateFileName().

◆ getGenerationReplicateFileName()

string FileServices::getGenerationReplicateFileName ( )

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

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

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().

◆ getLastReader()

file_it FileServices::getLastReader ( )
inline

Accessor to last element of the list of input FileHandlers.

149 {return _readers.end();}

References _readers.

Referenced by getReader().

◆ getLastWriter()

file_it FileServices::getLastWriter ( )
inline

Accessor to last element of the list of output FileHandlers.

143 {return _writers.end();}

References _writers.

Referenced by SimRunner::printLog().

◆ getMode()

unsigned int FileServices::getMode ( )
inline

Writting mode getter.

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

References _mode.

Referenced by BinaryDataSaver::finish().

◆ getReader()

FileHandler * FileServices::getReader ( string &  type)

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

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

References getFirstReader(), and getLastReader().

Referenced by Metapop::loadPopFromTraitFile().

◆ getReplicateCounter()

string FileServices::getReplicateCounter ( )

Accessor to the current replicate counter string.

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

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

Referenced by getGenerationReplicateFileName(), and getReplicateFileName().

◆ getReplicateFileName()

string & FileServices::getReplicateFileName ( )

Accessor to the current replicate file name.

459 {
461 
462  return _rep_filename;
463 }

References _basename, _rep_filename, and getReplicateCounter().

Referenced by TTQFreqExtractor::FHwrite().

◆ getRootDir()

std::string & FileServices::getRootDir ( )

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


483 {
484  return _root_dir;
485 }

References _root_dir.

◆ getSampledPop()

Metapop * FileServices::getSampledPop ( )

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

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

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().

◆ init() [1/2]

virtual bool FileServices::init ( )
inlinevirtual

Implements Service.

101 {return false;}

Referenced by SimRunner::run().

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

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.

82 {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().

◆ log()

void FileServices::log ( string  message)

Write to the parameter logfile.

437 {
439 }
void log_message(string &logstr)
Definition: filehandler.cc:266

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

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

◆ log_simparams()

void FileServices::log_simparams ( )

Saves the current simulation parameters to the default parameter logfile.

427 {
428  if(_mode == 4)
430  else
432 }
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:201
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:230

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

Referenced by SimRunner::run().

◆ notify()

void FileServices::notify ( )
virtual

Implements Service.

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

References _writers.

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

◆ reset()

void FileServices::reset ( )
virtual

Clears the list of FileHandlers.

Implements Service.

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

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

Referenced by SimRunner::reset_services().

◆ reset_sampled_pop()

void FileServices::reset_sampled_pop ( )

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

386  {
387 
388  if(_sample_pop) {
389 
391 
392  delete _sample_pop;
393  }
394 
395  _sample_pop = NULL;
396 
399 }

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

Referenced by LCE_FileServicesNotifier::loadFileServices().

◆ set_pop_ptr()

virtual void FileServices::set_pop_ptr ( Metapop pop)
inlinevirtual

Sets the Metapop reference.

116 {_popPtr = pop;}

References _popPtr.

Referenced by SimRunner::init_components().

◆ set_sampling_params()

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

Set the population sampling params.

178 {_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().

◆ setBasename()

void FileServices::setBasename ( string  name)

Sets the base file name of the simulation.

516 {
517  _basename = name;
518 }

References _basename.

Referenced by SimRunner::init().

◆ setMode()

void FileServices::setMode ( unsigned int  m)
inline

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

119 {_mode = m;}

References _mode.

Referenced by SimRunner::init().

◆ setRootDir()

void FileServices::setRootDir ( string  name)

Sets the root directory of the simulation.

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

References _root_dir.

Referenced by SimRunner::init().

◆ 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.

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

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

Referenced by getSampledPop().

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.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR