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 (unsigned int size, age_t age)
 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, 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...
 
unsigned int _patch_sample_size
 Number of individuals to sample from each patch for the file handlers. More...
 
age_t _patch_sample_age
 
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:279
int _sampled_at_replicate
Definition: fileservices.h:87
Metapop * _sample_pop
Definition: fileservices.h:89
unsigned int _patch_sample_size
Number of individuals to sample from each patch for the file handlers.
Definition: fileservices.h:83
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:70
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:67
age_t _patch_sample_age
Definition: fileservices.h:85
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:87
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir")
Definition: fileservices.h:73
virtual void attach(Handler *FH)
Attaches the FileHandler to the current list (_writers) of the FileServices.
Definition: fileservices.cc:61

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:137
bool get_isInputHandler()
Definition: filehandler.h:150
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.

336  {
337 
338  if(_sample_pop) {
339 
340  // the sample pop need to be emptied without destroying the individuals because they
341  // are also in the patch containers in the main pop.
342  Patch* patch;
343  // remove the patches :
344  for(unsigned int i = 0; i < _sample_pop->getPatchArraySize(); ) {
345  // removePatch() resizes the array, i must stay = 0 to mimic pop_front() until array is empty
346  patch = _sample_pop->removePatch(i);
347  patch->clear(); // this resets all container's size to 0 without deleting content
348  delete patch; // delete the patch, without deleting individuals
349  }
350 
351  }
352 
353 }
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: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:674

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.

135 {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.
111 {return _popPtr;}

References _popPtr.

Referenced by 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.

144 {return _readers.begin();}

References _readers.

Referenced by getReader().

◆ getFirstReplicateFileName()

string & FileServices::getFirstReplicateFileName ( )

Accessor to the first replicate file name.

415 {
416  ostringstream rpl;
417 
418  rpl.fill('0');
419  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
420  rpl<< max( 1, _myenv->workerRank() );
421 
422  _rep_filename = _basename + "_" + rpl.str();
423 
424  return _rep_filename;
425 }
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.

138 {return _writers.begin();}

References _writers.

Referenced by SimRunner::printLog().

◆ getGenerationCounter()

string FileServices::getGenerationCounter ( )

Accessor to the current generation counter string.

474 {
475  ostringstream gen;
476 
477  gen.fill('0');
478  gen.width( (int)log10((double)_popPtr->getGenerations()) + 1);
480 
481  return gen.str();
482 }
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.

439 {
440  string name = _basename + "_" + getGenerationCounter() + "_" + getReplicateCounter();
441  return name;
442 }
string getGenerationCounter()
Accessor to the current generation counter string.
Definition: fileservices.cc:473
string getReplicateCounter()
Accessor to the current replicate counter string.
Definition: fileservices.cc:460

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

Referenced by 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.

147 {return _readers.end();}

References _readers.

Referenced by getReader().

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

◆ 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:80

References _mode.

Referenced by BinaryDataSaver::finish().

◆ getReader()

FileHandler * FileServices::getReader ( string &  type)

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

505 {
506  file_it file = getFirstReader(), last = getLastReader() ;
507 
508  for(;file != last; file++) {
509  if( (*file)->get_extension().compare( type ) == 0) {
510  return (*file);
511  }
512  }
513  return 0;
514 }
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().

◆ getReplicateCounter()

string FileServices::getReplicateCounter ( )

Accessor to the current replicate counter string.

461 {
462  ostringstream rpl;
463 
464  rpl.fill('0');
465  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
467 
468  return rpl.str();
469 }
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.

430 {
432 
433  return _rep_filename;
434 }

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.


454 {
455  return _root_dir;
456 }

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 == 0 && _patch_sample_age == 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 (size = %i, age = %i): ", _patch_sample_size, _patch_sample_age);
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  // check the age flag:
233  if( _patch_sample_age != 0) {
234 
235  // check if current pop age flag is compatible with user's choice:
236  // reset it to match with current age structure
239 
240  } else { // the age flag wasn't set by the user
241 
243 
244  }
245 
246  //need to set the age flag of the population
248 
249  // stop here if age flag is null after checking:
250  if( _patch_sample_age == 0 )
251  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");
252 
253 #ifdef _DEBUG_
254  message("after check, sampling age is %i; ", _patch_sample_age);
255 #endif
256 
257  // add patches to match current parameters and patch sizes (discard empty patches)
258 
259  for(unsigned int i = 0; i < _popPtr->getPatchNbr(); ++i) {
260 
261  if( _popPtr->size(_patch_sample_age, i) == 0 ) continue; //skip empty patches
262 
263  src_patch = _popPtr->getPatch(i);
264  // allocate new patches
265  sample_patch = new Patch();
266  sample_patch->setID(i);
267  sample_patch->set_K(src_patch->get_K());
268  sample_patch->set_KFem(src_patch->get_KFem());
269  sample_patch->set_KMal(src_patch->get_KMal());
270 
271  // sub sample within patch, among offspring:
272  if( _patch_sample_age & OFFSPRG) {
273 
274  subSamplePatch(FEM, OFFSx, src_patch, sample_patch);
275  subSamplePatch(MAL, OFFSx, src_patch, sample_patch);
276  }
277 
278  // sub sample within patch, among adults:
279  if( _patch_sample_age & ADULTS) {
280 
281  subSamplePatch(FEM, ADLTx, src_patch, sample_patch);
282  subSamplePatch(MAL, ADLTx, src_patch, sample_patch);
283  }
284 
285  _sample_pop->addPatch( sample_patch );
286 
287  }
288 
291 
292 #ifdef _DEBUG_
293  message("sampled pop is: \n");
294  _sample_pop->show_up();
295 #endif
296 
297 
300 
301  return _sample_pop;
302 }
void empty_sampled_pop()
Empty the sampled pop without deleting the individuals since they are also in the main population.
Definition: fileservices.cc:336
void subSamplePatch(sex_t SEX, age_idx AGE, Patch *source_patch, Patch *patch)
Create a patch from a sample of the corresponding patch in the main metapopulation.
Definition: fileservices.cc:306
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:884
unsigned int size()
Get the total number of individuals present in the population, all sex and age classes together.
Definition: metapop.h:312
unsigned int getPatchNbr()
Definition: metapop.h:276
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
void fatal(const char *str,...)
Definition: output.cc:96
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
#define ADULTS
Adults age class flag (breeders).
Definition: types.h:54
#define OFFSPRG
Offspring age class flag.
Definition: types.h:50
@ OFFSx
Definition: types.h:42
@ ADLTx
Definition: types.h:42

References _patch_sample_age, _patch_sample_size, _popPtr, _sample_pop, _sampled_at_generation, _sampled_at_replicate, Metapop::addPatch(), ADLTx, ADULTS, empty_sampled_pop(), fatal(), FEM, Patch::get_K(), Patch::get_KFem(), Patch::get_KMal(), Metapop::getCurrentAge(), Metapop::getCurrentGeneration(), Metapop::getCurrentReplicate(), Metapop::getPatch(), Metapop::getPatchArraySize(), Metapop::getPatchCapacities(), Metapop::getPatchNbr(), MAL, message(), NONE, OFFSPRG, OFFSx, Patch::set_K(), Patch::set_KFem(), Patch::set_KMal(), Metapop::setCurrentAge(), Patch::setID(), Metapop::setPatchNbr(), Metapop::setPatchSizes(), Metapop::show_up(), Metapop::size(), and subSamplePatch().

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

◆ init() [1/2]

virtual bool FileServices::init ( )
inlinevirtual

Implements Service.

99 {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:193
bool isMaster() const
Definition: MPImanager.h:128
int error(const char *str,...)
Definition: output.cc:77
void warning(const char *str,...)
Definition: output.cc:58

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.

408 {
410 }
void log_message(string &logstr)
Definition: filehandler.cc:258

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.

398 {
399  if(_mode == 4)
401  else
403 }
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:193
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:222

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.

375 {
376 #ifdef _DEBUG_
377  message("FileServices::reset\n");
378 #endif
379  //Service::reset();
380  _writers.clear(); _writers.push_back(_logWriter);
381  _readers.clear();
382 
383  if(_sample_pop != NULL) {
385  delete _sample_pop;
386  }
387 
388  _sample_pop = NULL;
389 
390 #ifdef _DEBUG_
391  message("FileServices::reset done\n");
392 #endif
393 }

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.

357  {
358 
359  if(_sample_pop) {
360 
362 
363  delete _sample_pop;
364  }
365 
366  _sample_pop = NULL;
367 
370 }

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.

114 {_popPtr = pop;}

References _popPtr.

Referenced by SimRunner::init_components().

◆ set_sampling_params()

void FileServices::set_sampling_params ( unsigned int  size,
age_t  age 
)
inline

Set the population sampling params.

176 {_patch_sample_size = size; _patch_sample_age = age;}

References _patch_sample_age, and _patch_sample_size.

Referenced by LCE_FileServicesNotifier::loadFileServices().

◆ setBasename()

void FileServices::setBasename ( string  name)

Sets the base file name of the simulation.

487 {
488  _basename = name;
489 }

References _basename.

Referenced by SimRunner::init().

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

◆ setRootDir()

void FileServices::setRootDir ( string  name)

Sets the root directory of the simulation.

494 {
495  _root_dir = name;
496 
497  if(_root_dir.size() != 0 && _root_dir[_root_dir.length()-1] != '/')
498  _root_dir += "/";
499 
500 }

References _root_dir.

Referenced by SimRunner::init().

◆ subSamplePatch()

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

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

307 {
308 
309  int* sample = NULL;
310  int size = (int)source_patch->size(SEX, AGE), sample_size;
311  Individual *ind = 0;
312 
313  sample_size = (_patch_sample_size < size ? _patch_sample_size : size);
314 
315  if( patch->size(SEX, AGE) != 0) fatal("FileServices::subSamplePatch::receiver patch container is not empty before sampling\n");
316 
317  if( sample_size != 0) {
318 
319  int sample[sample_size];
320 
321  RAND::Sample(0, size, sample_size, &sample[0], false);
322 
323  for(unsigned int j = 0; j < sample_size; ++j) {
324 
325  ind = source_patch->get(SEX, AGE, sample[j]);
326 
327  patch->add(SEX, AGE, ind);
328  }
329 
330 // delete [] sample;
331  }
332 }
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:713

References _patch_sample_size, 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

age_t FileServices::_patch_sample_age
private

◆ _patch_sample_size

unsigned int FileServices::_patch_sample_size
private

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

Referenced by getSampledPop(), set_sampling_params(), and subSamplePatch().

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