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

Performs the setup of the Metapop and SimComponents and runs the simulation. More...

#include <simulation.h>

+ Inheritance diagram for SimRunner:
+ Collaboration diagram for SimRunner:

Public Member Functions

 SimRunner (Metapop *pop)
 
 ~SimRunner ()
 Dstror. More...
 
bool init ()
 Checks simulation parameters and init the FileServices with the base filename. More...
 
bool init_components (map< string, string > &simparams)
 Performs the initialization of the different components of the simulation. More...
 
void setLifeCycle ()
 Sets the list of LifeCyckeEvent's currently active. More...
 
void setForFirstGeneration ()
 Sets the population and the services ready for the first generation of a new replicate. More...
 
void init_random_seed ()
 Initialize the seed of the random generator. More...
 
void reset ()
 Resets all the parameters to the unset state, resets the services. More...
 
std::string setElapsedTime (clock_t time)
 Compute and print the simulation's elapsed time to stdout. More...
 
void register_component (SimComponent *cmpt)
 Register the different Handler's attached to a SimComponent. More...
 
void register_component_handlers ()
 Register all the Handlers of the currently active simulation components. More...
 
void reset_services ()
 Resets the FileServices and StatServices. More...
 
FileServicesget_FileServices ()
 Returns the FileServices. More...
 
StatServicesget_StatServices ()
 Returns the StatServices. More...
 
list< StatRecBase * > get_allRegisteredStats ()
 Returns the complete list of the stat recorders loaded after parameters initialization. More...
 
bool run (int ARGC, char **ARGV)
 First loop of the simulation, performs the simulations stored in the ParamManager base class. More...
 
bool run ()
 First loop of the simulation, performs the simulations stored in the ParamManager base class. More...
 
bool run_event (string &name)
 Execute one specific life cycle event, if present in the list of current events. More...
 
void step (unsigned int nb_gen)
 Iterates the life cycle. More...
 
void attach_pop (Metapop *pop)
 Attach a pop to the simulation. More...
 
Metapopget_pop ()
 Accessor to the pop ptr. More...
 
bool build_pop ()
 Calls the Metapop init procedure with current traits and LCEs. More...
 
void setCurrentGeneration (unsigned int gen)
 
unsigned int getCurrentGeneration ()
 
void setGenerations (unsigned int gen)
 
unsigned int getGenerations ()
 
void setCurrentReplicate (unsigned int repl)
 
unsigned int getCurrentReplicate ()
 
void setReplicates (unsigned int repl)
 
unsigned int getReplicates ()
 
int getCurrentRankInLifeCycle ()
 
void printLogHeader ()
 
void printLog ()
 
void runPostExec ()
 Run the post-exec script after all simulations have finished. More...
 
void runPostExecReplicateWise ()
 Run the post-exec script after each replicate, adds filename and replicate number as args. More...
 
Main loops
void Replicate_LOOP ()
 Replicate loop, iterates the life cycle _replicates times. More...
 
void Cycle (char *startTime)
 Life cycle loop, executes the list of LCEs _generations times. More...
 
- Public Member Functions inherited from SimBuilder
 SimBuilder ()
 
 SimBuilder (const SimBuilder &SB)
 copy cstor. More...
 
 ~SimBuilder ()
 
bool build_currentParams (map< string, string > &simparams)
 Builds the list of parameters from user's defined input parameters. More...
 
map< trait_t, TraitPrototype * > & build_currentTraits ()
 Selects the trait prototypes that have their parameters set. More...
 
void build_LifeCycle ()
 Selects the life cycle events that have their parameters set. More...
 
TraitPrototypeget_current_trait (trait_t type)
 Accessor to the list of current trait prototypes. More...
 
TraitPrototypeget_current_traitWithParameter (string &param)
 Accessor to the current trait prototypes conditioned on ownership of a specified parameter. More...
 
LifeCycleEventget_current_event (string &name)
 Accessor to the list of current LCEs. More...
 
LifeCycleEventget_current_eventWithParameter (string &param)
 Accessor to the current LCEs conditioned on ownership of a specified parameter. More...
 
list< ParamSet * > & get_currentParams ()
 Accessor to the list of the selected parameter sets. More...
 
age_t getFirstRequiredAgeInLifeCycle ()
 
- Public Member Functions inherited from ParamManager
 ParamManager ()
 Cstor. More...
 
 ~ParamManager ()
 
void add_paramset (ParamSet *paramset)
 Adds a ParamSet to the list of the parameter sets of the simulation. More...
 
ParamSetget_paramset (string &name)
 Looks for paramset with "name" in the list of parameter sets. More...
 
void build_allParams ()
 Clears and fills the _allParams list with the ParamSet's of the simulation components. More...
 
list< ParamSet * > & get_allParams ()
 Accessor of the whole list of the parameter sets. More...
 
bool set_parameters (map< string, string > &simparams, bool silent)
 Sets the parameters of the simulation with the argument strings given in input. More...
 
bool param_consistency_check ()
 Checks if all the mandatory parameters are set so that the simulation can be launched. More...
 
void build_records (map< string, vector< string > > &initParams)
 Builds the list of simulation parameters from the parsed input file(s). More...
 
list< map< string, string > > & get_simRecords ()
 Accessor to the simulations parameter list. More...
 
map< string, string > & get_firstRecord ()
 Accessor to the first element in the simulations parameter list. More...
 
int get_nbSims ()
 Accessor to the size of the simulations parameter list, i.e. More...
 
- Public Member Functions inherited from ComponentManager
 ComponentManager ()
 
 ~ComponentManager ()
 
void build_component_list ()
 Clears and builds the list of all components from the lists of trait prototype templates and life cycle event templates. More...
 
void add_component (SimComponent *cmpt)
 Push a component at the back of the component list. More...
 
void add_trait (TraitPrototype *trait)
 Add a trait prototype to the template and component lists. More...
 
void add_LCE (LifeCycleEvent *event)
 Add a life cycle event to the template and component lists. More...
 
TraitPrototypeget_trait (string name)
 Search for component with "name" in the trait prototype list. More...
 
LifeCycleEventget_LCE (string name)
 Search for component with "name" in the life cycle events list. More...
 

Public Attributes

FileServices _FileServices
 
StatServices _StatServices
 
UpdaterServices _ParamUpdaterManager
 

Private Attributes

MPImanager_my_mpi_manager
 
Metapop_thePop
 
std::string _logfile
 
char _startTime [20]
 
char _endTime [20]
 
std::string _simElapsedTime
 
clock_t _meanReplElapsedTime
 Clock counter, for logging. More...
 
unsigned int _meanGenLength
 Generation counter, for logging. More...
 
unsigned int _generations
 Number of generations to iterate. More...
 
unsigned int _replicates
 Number of replicates to iterate. More...
 
unsigned int _currentReplicate
 The current replicate in the replicate loop, starts at 1. More...
 
unsigned int _currentGeneration
 The current generation in the generation loop, starts at 1. More...
 
int _currentRankInLifeCycle
 The current rank in the life cycle, corresponds to the rank of the current LCE, before it executes. More...
 
unsigned long _random_seed
 The startup random seed of the random generator. More...
 
std::string _modeArg
 The run mode ('overwrite', 'run', 'skip', 'dryrun', 'create_init'). More...
 
unsigned int _mode
 The run mode code (0 = run, 1 = overwrite, 2 = skip, 3 = dryrun, 4 = create_init) More...
 
bool _doRun
 Boolean set when not in dryrun mode. More...
 
std::string _postexec_script
 The path to the script to be executed after last simulation. More...
 
std::string _postexec_args
 Arguments to pass to the post-exec script. More...
 
bool _do_postexec_replicatewise
 Boolean to trigger replicate-wise execution of post script. More...
 
bool _do_postexec
 Boolean set when a post-exec script must be executed. More...
 

Additional Inherited Members

- Protected Types inherited from SimBuilder
typedef map< int, LifeCycleEvent * >::const_iterator LCE_ITER
 
typedef map< trait_t, TraitPrototype * >::const_iterator TRAIT_ITER
 
- Protected Attributes inherited from SimBuilder
list< ParamSet * > _currentParams
 List of the selected simulation components from the user defined input parameters. More...
 
map< trait_t, TraitPrototype * > _currentTraits
 List of the selected trait prototypes from the user defined input parameters. More...
 
map< int, LifeCycleEvent * > _LifeCycle
 List of the selected life cycle events from the user defined input parameters. More...
 
- Protected Attributes inherited from ParamManager
list< ParamSet * > _allParams
 A list of all the parameter sets of all the simulation components loaded in the _component list of the ComponentManager. More...
 
map< string, string > _inputParams
 A map of the parameters and their arguments of the current (running) simulation. More...
 
map< unsigned int, list< pair< string, string > > > _temporalParams
 Lists of parameters to be updated during a simulation indexed by generation update time. More...
 
list< map< string, string > > _simRecords
 Sets of parameters of all the simulations to perform. More...
 
ParamSet _paramSet
 The ParamSet param set of the simulation. More...
 
- Protected Attributes inherited from ComponentManager
list< SimComponent * > _components
 List of all the simulation components. More...
 
list< TraitPrototype * > _TTrait_Templates
 List of all trait prototypes of the simulation, a subset of _components list. More...
 
list< LifeCycleEvent * > _LCE_Templates
 List of all the life-cycle events of the simulation, a subset of _components list. More...
 

Detailed Description

Performs the setup of the Metapop and SimComponents and runs the simulation.

This class implements the two main loops of a simulation, the replicate and the generation loops. The replicate loop iterates the generation loop which itself iterates the life cycle loop composed of the LCEs selected by the user. A SimRunner brings together the basic simulation components and a metapopulation on which they act. It perfoms the setups necessary to have a Metapop ready for the simulation and runs the different simulations stored in its ParamManager base class. Also hosts the file and stat services.

Constructor & Destructor Documentation

◆ SimRunner()

SimRunner::SimRunner ( Metapop pop)
inline
108  {
109  attach_pop(pop);
110  }
void attach_pop(Metapop *pop)
Attach a pop to the simulation.
Definition: simulation.h:167
MPImanager * _my_mpi_manager
Definition: simulation.h:55
bool _do_postexec_replicatewise
Boolean to trigger replicate-wise execution of post script.
Definition: simulation.h:93
bool _do_postexec
Boolean set when a post-exec script must be executed.
Definition: simulation.h:95

References attach_pop().

◆ ~SimRunner()

SimRunner::~SimRunner ( )

Dstror.

179 {
180 #ifdef _DEBUG_
181  message("SimRunner::~SimRunner\n");
182 #endif
183 
184  reset();
185 
186 }
void reset()
Resets all the parameters to the unset state, resets the services.
Definition: simulation.cc:190
void message(const char *message,...)
Definition: output.cc:39

References message(), and reset().

Member Function Documentation

◆ attach_pop()

void SimRunner::attach_pop ( Metapop pop)
inline

Attach a pop to the simulation.

Adds it to the components list.

Parameters
popptr to the pop object
167 {_thePop = pop;this->_components.push_back(_thePop);}
list< SimComponent * > _components
List of all the simulation components.
Definition: basicsimulation.h:70
Metapop * _thePop
Definition: simulation.h:57

References ComponentManager::_components, and _thePop.

Referenced by SimRunner().

+ Here is the caller graph for this function:

◆ build_pop()

bool SimRunner::build_pop ( )

Calls the Metapop init procedure with current traits and LCEs.

◆ Cycle()

void SimRunner::Cycle ( char *  startTime)

Life cycle loop, executes the list of LCEs _generations times.

Parameters
startTimethe starting time of the current replicate.
639 {
640 
641  time_t t;
642  // ------------------------------ CYCLE --------------------------------
643 
645 
646  // -------------------------- PRINT STUFF ------------------------------
647 #if !defined(LOW_VERBOSE) && !defined(USE_MPI)
648 
649  if( !(_currentGeneration % 100) || _currentGeneration < 100){
650  //update timer:
651  t = time(0);
652  strftime(startTime, 10, "%H:%M:%S", localtime(&t));
653 
654  message("\r replicate %i/%i [%s] %i/%i", _currentReplicate, _replicates
655  ,startTime, _currentGeneration, _generations);
656 
657  fflush(stdout);
658  }
659 #endif
660 
661 #ifdef _DEBUG_
662  message("____Generation %i/%i____\n", _currentGeneration, _generations);
663 #endif
664 
665  // -------------------------- STEP ONE GEN ------------------------------
667 
668  // do one iteration of the life cycle:
669  if(_thePop->isAlive())
670 
671  step(1);
672 
673  else {
674 #if !defined(LOW_VERBOSE) && !defined(USE_MPI)
675  message("\r replicate %i/%i [%s] %i/%i -> Pop extinction !\n", _currentReplicate, _replicates
676  ,startTime, _currentGeneration, _generations);
677 #endif
679  break;
680  }
681 
682  }
683  // --------------------------- END OF CYCLE --------------------------
684 }
void setCurrentGeneration(unsigned int gen)
Definition: metapop.h:297
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class.
Definition: metapop.h:308
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:73
void step(unsigned int nb_gen)
Iterates the life cycle.
Definition: simulation.cc:708
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:71
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:75
unsigned int _currentGeneration
The current generation in the generation loop, starts at 1.
Definition: simulation.h:77

References _currentGeneration, _currentReplicate, _generations, _replicates, _thePop, Metapop::isAlive(), message(), Metapop::setCurrentGeneration(), and step().

Referenced by Replicate_LOOP().

+ Here is the caller graph for this function:

◆ get_allRegisteredStats()

list<StatRecBase*> SimRunner::get_allRegisteredStats ( )
inline

Returns the complete list of the stat recorders loaded after parameters initialization.

151 {return _StatServices.getAllStats();}
StatServices _StatServices
Definition: simulation.h:101
list< StatRecBase * > getAllStats()
Definition: statservices.cc:404

References _StatServices, and StatServices::getAllStats().

◆ get_FileServices()

FileServices* SimRunner::get_FileServices ( )
inline

Returns the FileServices.

147 {return &_FileServices;}
FileServices _FileServices
Definition: simulation.h:99

References _FileServices.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ get_pop()

Metapop* SimRunner::get_pop ( )
inline

Accessor to the pop ptr.

Returns
the pop ptr
171 {return _thePop;}

References _thePop.

Referenced by GenotyperFH::collate_trait_map_positions(), and GenotyperFH::prepare_data_table().

+ Here is the caller graph for this function:

◆ get_StatServices()

StatServices* SimRunner::get_StatServices ( )
inline

Returns the StatServices.

149 {return &_StatServices;}

References _StatServices.

Referenced by LCE_StatServiceNotifier::LCE_StatServiceNotifier().

+ Here is the caller graph for this function:

◆ getCurrentGeneration()

unsigned int SimRunner::getCurrentGeneration ( )
inline
189 {return _currentGeneration;}

References _currentGeneration.

Referenced by SIMenv::getCurrentGeneration().

+ Here is the caller graph for this function:

◆ getCurrentRankInLifeCycle()

int SimRunner::getCurrentRankInLifeCycle ( )
inline
196 {return _currentRankInLifeCycle;}
int _currentRankInLifeCycle
The current rank in the life cycle, corresponds to the rank of the current LCE, before it executes.
Definition: simulation.h:79

References _currentRankInLifeCycle.

Referenced by SIMenv::getCurrentRankInLifeCycle().

+ Here is the caller graph for this function:

◆ getCurrentReplicate()

unsigned int SimRunner::getCurrentReplicate ( )
inline
193 {return _currentReplicate;}

References _currentReplicate.

Referenced by SIMenv::getCurrentReplicate().

+ Here is the caller graph for this function:

◆ getGenerations()

unsigned int SimRunner::getGenerations ( )
inline
191 {return _generations;}

References _generations.

Referenced by SIMenv::getGenerations().

+ Here is the caller graph for this function:

◆ getReplicates()

unsigned int SimRunner::getReplicates ( )
inline
195 {return _replicates;}

References _replicates.

Referenced by SIMenv::getReplicates().

+ Here is the caller graph for this function:

◆ init()

bool SimRunner::init ( )

Checks simulation parameters and init the FileServices with the base filename.

68 {
69  if( !(_paramSet.isSet("filename")) ) {
70  return error("parameter \"filename\" is not properly set!\n");
71  }
72 
74 
75  if(_paramSet.isSet("root_dir")) {
77  }
78 
79  if(_paramSet.isSet("run_mode")) {
80 
81  _modeArg = _paramSet.getArg("run_mode");
82 
83  if(_modeArg == "run"){
84 
86  _doRun = true;
87  _mode = 0;
88  SILENT_RUN = false; //declared in output.h
89 
90  } else if( _modeArg == "overwrite" ) {
91 
93  _doRun = true;
94  _mode = 1;
95  SILENT_RUN = false;
96 
97  } else if(_modeArg == "skip"){
98 
100  _doRun = true;
101  _mode = 2;
102  SILENT_RUN = false;
103 
104  } else if(_modeArg == "dryrun" || _modeArg == "dry"){
105 
107  _doRun = false;
108  _mode = 3;
109  SILENT_RUN = false;
110 
111  } else if(_modeArg == "create_init"){ //the logWriter creates an ini file, see FileServices::log_simparams()
112 
114  _doRun = false;
115  _mode = 4;
116  SILENT_RUN = false;
117 
118  } else if(_modeArg == "silent_run" || _modeArg == "silent"){
119 
120  _FileServices.setMode(2); // skip simulation if files from previous sims are found, o/w it hangs
121  _doRun = true;
122  _mode = 2;
123  SILENT_RUN = true;
124 
125  } else {
126 
127  error("simulation run mode \"%s\" unknown\n",_modeArg.c_str());
128 
129  return false;
130 
131  }
132  // else, set default run mode values (skip):
133  } else {
134  _modeArg = "skip";
135  _doRun = true;
137  SILENT_RUN = false;
138  }
139 
140  _replicates = (unsigned int)_paramSet.getValue("replicates");
141 
142  _generations = (unsigned int)_paramSet.getValue("generations");
143 
144 
145  if( _paramSet.isSet("logfile") ) {
146  _logfile = _paramSet.getArg("logfile");
147  } else {
148  _logfile = "nemo.log";
149  }
150 
151  // create the simulation logging file
152  printLogHeader();
153 
154  // set POSTEXEC options:
155  if( _paramSet.isSet("postexec_script") ) {
156  _postexec_script = _paramSet.getArg("postexec_script");
157  _do_postexec = true;
158 
159  if( _paramSet.isSet("postexec_args") )
160  _postexec_args = _paramSet.getArg("postexec_args");
161  else
162  _postexec_args = "";
163 
164  if( _paramSet.isSet("postexec_replicate_wise") )
166  else
168 
169 
170  } else
171  _do_postexec = false;
172 
173  return true;
174 }
void setBasename(string name)
Sets the base file name of the simulation.
Definition: fileservices.cc:514
void setMode(unsigned int m)
Mode setter, determines if file will get overwritten or not.
Definition: fileservices.h:118
void setRootDir(string name)
Sets the root directory of the simulation.
Definition: fileservices.cc:521
ParamSet _paramSet
The ParamSet param set of the simulation.
Definition: basicsimulation.h:150
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:299
double getValue(string name)
Accessor the parameters value.
Definition: param.h:301
bool isSet()
Accessor to the status flag.
Definition: param.h:287
std::string _postexec_args
Arguments to pass to the post-exec script.
Definition: simulation.h:91
unsigned int _mode
The run mode code (0 = run, 1 = overwrite, 2 = skip, 3 = dryrun, 4 = create_init)
Definition: simulation.h:85
std::string _logfile
Definition: simulation.h:59
bool _doRun
Boolean set when not in dryrun mode.
Definition: simulation.h:87
std::string _postexec_script
The path to the script to be executed after last simulation.
Definition: simulation.h:89
std::string _modeArg
The run mode ('overwrite', 'run', 'skip', 'dryrun', 'create_init').
Definition: simulation.h:83
void printLogHeader()
Definition: simulation.cc:750
bool SILENT_RUN
Definition: output.cc:36
int error(const char *str,...)
Definition: output.cc:78

References _do_postexec, _do_postexec_replicatewise, _doRun, _FileServices, _generations, _logfile, _mode, _modeArg, ParamManager::_paramSet, _postexec_args, _postexec_script, _replicates, error(), ParamSet::getArg(), ParamSet::getValue(), ParamSet::isSet(), printLogHeader(), FileServices::setBasename(), FileServices::setMode(), FileServices::setRootDir(), and SILENT_RUN.

Referenced by init_components().

+ Here is the caller graph for this function:

◆ init_components()

bool SimRunner::init_components ( map< string, string > &  simparams)

Performs the initialization of the different components of the simulation.

Builds the list of the simulation parameters and load the components that have their ParamSet in the set state. Builds the population, the TraitPrototype, and life cycle, register the various services and init the StatServices and ParameterUpdaterManager.

Parameters
simparamsthe hashtable containing the parameters and their arguments parsed from the init file
252 {
253 #ifdef _DEBUG_
254  message("SimRunner::init_components\n");
255 #endif
256  //first reset all paramSets and services (clear the handlers' lists)
257  reset();
258 
261 
262 #ifdef _DEBUG_
263  message("SimRunner::init_components: building current params\n");
264 #endif
265 
266  //build the list of active component parameters from the simulation record:
267  if(!this->build_currentParams(simparams)){
268  error("SimRunner::init_components:couldn't build current params\n");
269  return false;
270  }
271 
272  //initialize the random generator's seed:
273  //if(_myenv->isMaster()) // in MPI context, all processes must initialize the generator
275 
276  //init the sim and pop (set parameters from input values)
277  if( !init() || !_thePop->init() ) return false;
278 
279  //propagate replicate and generation numbers to metapop, often used by LCEs
282 
283  //the traits and LCEs components are set only after the simulation and
284  //population parameters are set
285 
286  //build the Individual prototype, and init the TTraits
287  //---> SimComponents::setParameters() is called here, this will also build the genetic map
289 
290  //build the list of life cycle events, and init LCEs
291  //---> SimComponent::setParameters() is called here
292  setLifeCycle( );
293 
294  //load the stat-, file-, and updater-handlers of the simulation components:
296 
297  //StatServices: build the lists of stat recorders:
298  if( !_StatServices.init() ) return false;
299 
300  if( !_ParamUpdaterManager.init() ) return false;
301 
304  updater->setManager( &_ParamUpdaterManager );
305  updater->init(_thePop);
306  _LifeCycle[ -1 ] = updater; //this is never destroyed...
307  }
308 
309  return true;
310 }
virtual void set_pop_ptr(Metapop *pop)
Sets the Metapop reference.
Definition: fileservices.h:115
void makePrototype(map< trait_t, TraitPrototype * > TTlist)
Creates the individuals prototype from the selected trait prototypes.
Definition: indfactory.cc:49
Calls the UpdaterServices to notify its components of a generation change.
Definition: servicenotifiers.h:40
void setManager(UpdaterServices *mng)
Definition: servicenotifiers.h:50
virtual void init(Metapop *popPtr)
Sets the pointer to the current Metapop and the trait link if applicable.
Definition: lifecycleevent.h:113
void setReplicates(unsigned int repl)
Definition: metapop.h:273
bool init()
Inits the population parameters from the ParamSet and builds the pop (adds patches),...
Definition: metapop.cc:96
void setGenerations(unsigned int gen)
Definition: metapop.h:271
bool build_currentParams(map< string, string > &simparams)
Builds the list of parameters from user's defined input parameters.
Definition: basicsimulation.cc:889
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:223
map< trait_t, TraitPrototype * > & build_currentTraits()
Selects the trait prototypes that have their parameters set.
Definition: basicsimulation.cc:915
UpdaterServices _ParamUpdaterManager
Definition: simulation.h:103
bool init()
Checks simulation parameters and init the FileServices with the base filename.
Definition: simulation.cc:67
void setLifeCycle()
Sets the list of LifeCyckeEvent's currently active.
Definition: simulation.cc:241
void init_random_seed()
Initialize the seed of the random generator.
Definition: simulation.cc:314
void register_component_handlers()
Register all the Handlers of the currently active simulation components.
Definition: simulation.cc:228
virtual bool init()
Definition: statservices.cc:46
void set_pop_ptr(Metapop *pop)
Definition: statservices.h:107
bool hasTemporals()
Checks if any updater has been uploaded.
Definition: updaterservices.h:91
virtual bool init()
Definition: updaterservices.cc:38

References _FileServices, _generations, SimBuilder::_LifeCycle, _ParamUpdaterManager, _replicates, _StatServices, _thePop, SimBuilder::build_currentParams(), SimBuilder::build_currentTraits(), error(), UpdaterServices::hasTemporals(), Metapop::init(), init(), StatServices::init(), UpdaterServices::init(), LifeCycleEvent::init(), init_random_seed(), IndFactory::makePrototype(), message(), register_component_handlers(), reset(), FileServices::set_pop_ptr(), StatServices::set_pop_ptr(), Metapop::setGenerations(), setLifeCycle(), LCE_ParamUpdaterNotifier::setManager(), and Metapop::setReplicates().

Referenced by run().

+ Here is the caller graph for this function:

◆ init_random_seed()

void SimRunner::init_random_seed ( )

Initialize the seed of the random generator.

315 {
316  if(_myenv->isMaster()) {
317 
318  if(this->_paramSet.isSet("random_seed")) {
319 
320  _random_seed = (unsigned long) this->_paramSet.getValue("random_seed");
321 
322  message( "setting random seed from input value: %i\n", _random_seed);
323 
324  } else {
325  //set the random seed from the current time
326  _random_seed = 2*time(0)+1;
327 
328  message( "setting random seed from current time: %i\n", _random_seed);
329  }
330  }
331 
332 #if defined(USE_MPI) && defined(HAS_SPRNG)
333 
334  // broadcast the random seed, needs to be equal on all processes with SPRNG
335  MPI_Bcast(&_random_seed,1,MPI_LONG,0,MPI_COMM_WORLD );
336 
337 #else
338 #endif
339 
340  //initialization in any case
342 
343 }
bool isMaster() const
Definition: MPImanager.h:127
static void init(unsigned long seed)
Initialize the random generator's seed.
Definition: Uniform.h:81
unsigned long _random_seed
The startup random seed of the random generator.
Definition: simulation.h:81
MPIenv * _myenv
Definition: MPImanager.cc:35

References _myenv, ParamManager::_paramSet, _random_seed, ParamSet::getValue(), RAND::init(), MPIenv::isMaster(), ParamSet::isSet(), and message().

Referenced by init_components().

+ Here is the caller graph for this function:

◆ printLog()

void SimRunner::printLog ( )
779 {
780  ofstream FH(_logfile.c_str(),ios::app);
781 
782  if(!FH.is_open()){
783  error("could not open simulation logfile \"%s\"\n",_logfile.c_str());
784  return;
785  }
786 
787  FH<<"| ";
788  FH.width(40);
789  FH.setf(ios::left,ios::adjustfield);
791 
792  FH <<"| "<< _startTime <<" | "<< _endTime <<" | ";
793 
794  FH.width(10);
795  FH.setf(ios::right,ios::adjustfield);
796  FH<< _simElapsedTime <<" | ";
797 
798  FH.width(9);
799  FH << _replicates <<" | ";
800 
801  FH.width(10);
802  FH << setElapsedTime( _meanReplElapsedTime ) << " | ";
803 
804  FH.width(8);
805  FH << _meanGenLength <<" | ";
806 
807  FH<<" "<<MAIN_VERSION<<"."<<MINOR_VERSION<<"."<<REVISION<<RELEASE
808  <<" "<<VERSION_DATE;
809 
810  FH<<" | ";
811  FH.width(20);
812  FH.setf(ios::left,ios::adjustfield);
813  char* host;
814  if( (host = getenv("HOST")) != NULL )
815  FH << host << " |";
816  else if ( (host = getenv("HOSTNAME")) != NULL )
817  FH << host << " |";
818  else
819  FH << "-" << " |";
820 
822 
823  for(;file != last; file++)
824  FH << " \"" << (*file)->get_extension() << "\":" << (*file)->get_path();
825 
826  FH << std::endl;
827 
828  FH.close();
829 }
file_it getFirstWriter()
Accessor to first element of the list of output FileHandlers.
Definition: fileservices.h:139
file_it getLastWriter()
Accessor to last element of the list of output FileHandlers.
Definition: fileservices.h:142
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:94
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:474
clock_t _meanReplElapsedTime
Clock counter, for logging.
Definition: simulation.h:65
char _startTime[20]
Definition: simulation.h:61
std::string setElapsedTime(clock_t time)
Compute and print the simulation's elapsed time to stdout.
Definition: simulation.cc:725
unsigned int _meanGenLength
Generation counter, for logging.
Definition: simulation.h:67
char _endTime[20]
Definition: simulation.h:61
std::string _simElapsedTime
Definition: simulation.h:63
#define VERSION_DATE
Definition: version.h:29
#define MINOR_VERSION
Definition: version.h:26
#define RELEASE
Definition: version.h:28
#define MAIN_VERSION
Definition: version.h:25
#define REVISION
Definition: version.h:27

References _endTime, _FileServices, _logfile, _meanGenLength, _meanReplElapsedTime, _replicates, _simElapsedTime, _startTime, error(), FileServices::getBaseFileName(), FileServices::getFirstWriter(), FileServices::getLastWriter(), MAIN_VERSION, MINOR_VERSION, RELEASE, REVISION, setElapsedTime(), and VERSION_DATE.

Referenced by run().

+ Here is the caller graph for this function:

◆ printLogHeader()

void SimRunner::printLogHeader ( )
751 {
752  ofstream FH;
753  ifstream IF;
754  //check is the logfile already exists:
755  IF.open(_logfile.c_str(),ios::in);
756  if(IF){
757  IF.close();
758  return;
759  }
760  IF.close();
761 
762  FH.open(_logfile.c_str(),ios::out);
763  if(!FH) {
764  error("could not create simulation logfile \"%s\"\n",_logfile.c_str());
765  return;
766  }
767 
768  FH<<"--- N E M O ---\n"
769  <<" LOGFILE\n\n\n";
770  FH<<"| basename | start time | stop time | e-time CPU |"
771  <<" repl done | rpl e-time | mean gen | version | hostname | output files \n";
772 
773  FH.close();
774 }

References _logfile, and error().

Referenced by init().

+ Here is the caller graph for this function:

◆ register_component()

void SimRunner::register_component ( SimComponent cmpt)

Register the different Handler's attached to a SimComponent.

Parameters
cmpta SimComponent
220 {
221  _FileServices.load(cmpt);
222  _StatServices.load(cmpt);
224 }
virtual void load(SimComponent *sc)
Tells the SimComponent to load its file handlers.
Definition: fileservices.cc:81
virtual void load(SimComponent *sc)
tell the SimComponent to load its stat handlers
Definition: statservices.cc:169
virtual void load(SimComponent *sc)
Loads the updaters in case a component's parameter has temporal arguments.
Definition: updaterservices.cc:81

References _FileServices, _ParamUpdaterManager, _StatServices, FileServices::load(), StatServices::load(), and UpdaterServices::load().

Referenced by register_component_handlers().

+ Here is the caller graph for this function:

◆ register_component_handlers()

void SimRunner::register_component_handlers ( )

Register all the Handlers of the currently active simulation components.

229 {
231 
232  for(TRAIT_ITER trait = _currentTraits.begin(); trait != _currentTraits.end(); trait++)
233  register_component(trait->second);
234 
235  for(LCE_ITER LCE = _LifeCycle.begin(); LCE != _LifeCycle.end(); LCE++)
236  register_component(LCE->second);
237 }
map< trait_t, TraitPrototype * > _currentTraits
List of the selected trait prototypes from the user defined input parameters.
Definition: basicsimulation.h:221
map< int, LifeCycleEvent * >::const_iterator LCE_ITER
Definition: basicsimulation.h:225
map< trait_t, TraitPrototype * >::const_iterator TRAIT_ITER
Definition: basicsimulation.h:226
void register_component(SimComponent *cmpt)
Register the different Handler's attached to a SimComponent.
Definition: simulation.cc:219

References SimBuilder::_currentTraits, SimBuilder::_LifeCycle, _thePop, and register_component().

Referenced by init_components().

+ Here is the caller graph for this function:

◆ Replicate_LOOP()

void SimRunner::Replicate_LOOP ( )

Replicate loop, iterates the life cycle _replicates times.


519 {
520 
521 #ifndef USE_MPI
522 
523  clock_t start;
524  clock_t stop;
525  time_t t;
526  char t_buff[10];
527 
529  _meanGenLength = 0;
530  _currentGeneration = 0;
531 
532  //---------------------------------- REPLICATE LOOP -------------------------------------
534 
535 
536  // -------------------------- PRINT STUFF ------------------------------
537  t=time(0);
538  //print some output:
539  strftime(t_buff,10,"%H:%M:%S",localtime(&t));
540 
541 #ifdef LOW_VERBOSE
542  message(" replicate %i/%i [%s] \n",_currentReplicate, _replicates, t_buff);
543 #else
544  message("\r replicate %i/%i [%s] 1/%i ",_currentReplicate, _replicates, t_buff, _generations);
545  fflush(stdout);
546 #endif
547 
548  //---------------- SET POPULATION FOR FIRST GENERATION ------------------
549 
551 
552  //--------------------------- GENERATION LOOP ---------------------------
553  start = clock();
554 
555  Cycle(t_buff);
556 
557  stop = clock();
558  //-----------------------------------------------------------------------
559 
560  _meanReplElapsedTime += (stop - start);
561 
563 
564  //call the file services to print the replicate stats in case of pop extinction
565  if( !_thePop->isAlive() && _currentGeneration-1 <= _generations ) {
569  }
570 
571  if( _do_postexec_replicatewise ) runPostExecReplicateWise(); //will execute on each node
572  }
573  //--------------------------------- /REPLICATE LOOP -------------------------------------
574 
581  }
582 
585 
586 #else
587  //else ifndef USE_MPI
588  //------------------------------- MPI REPLICATE LOOP ------------------------------------
590 
591  while ( _currentReplicate <= _replicates )
593 
595 
596  if( _myenv->isMaster() ) {
601 
602  //write the stat files, only if "save_stats" LCE is part of the life cycle
603  if(get_LCE("save_stats")->get_paramset()->isSet()) {
604  FileHandler& statWriter = dynamic_cast<LCE_StatServiceNotifier*> ( get_LCE("save_stats") )->getFH();
605  for (unsigned int i = 1; i <= _replicates; ++i) {
606  _currentReplicate = i;
607  statWriter.FHwrite();
608  }
609  }
610  }
611  //------------------------------ /MPI REPLICATE LOOP ------------------------------------
612 #endif
613  //endif USE_MPI
614 
615  //delete all individuals present in the population and delete the patches:
616  _thePop->clear();
617 }
LifeCycleEvent * get_LCE(string name)
Search for component with "name" in the life cycle events list.
Definition: basicsimulation.cc:78
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:52
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
virtual void notify()
Definition: fileservices.cc:188
Initiates the StatServices' parameters (log time) when registering, calls StatServices::notify() when...
Definition: servicenotifiers.h:183
virtual unsigned int init(StatServices *StatManager)=0
virtual void iterate(SimRunner *_sim, StatServices *StatManager, unsigned int *_gen, unsigned int *_repl)=0
virtual void finish(StatServices *StatManager, unsigned int *_gen, unsigned int _repl)
void setCurrentReplicate(unsigned int repl)
Definition: metapop.h:296
void clear()
Called at the end of each simulation, empties the pop and the garbage collector; the Individuals are ...
Definition: metapop.cc:1191
ParamSet * get_paramset(string &name)
Looks for paramset with "name" in the list of parameter sets.
Definition: basicsimulation.cc:233
void setForFirstGeneration()
Sets the population and the services ready for the first generation of a new replicate.
Definition: simulation.cc:621
void Cycle(char *startTime)
Life cycle loop, executes the list of LCEs _generations times.
Definition: simulation.cc:638
void runPostExecReplicateWise()
Run the post-exec script after each replicate, adds filename and replicate number as args.
Definition: simulation.cc:858

References _currentGeneration, _currentReplicate, _do_postexec_replicatewise, _FileServices, _generations, _meanGenLength, _meanReplElapsedTime, _my_mpi_manager, _myenv, _replicates, _StatServices, _thePop, Metapop::clear(), Cycle(), FileHandler::FHwrite(), MPImanager::finish(), ComponentManager::get_LCE(), ParamManager::get_paramset(), MPImanager::init(), Metapop::isAlive(), MPIenv::isMaster(), MPImanager::iterate(), message(), FileServices::notify(), runPostExecReplicateWise(), Metapop::setCurrentGeneration(), Metapop::setCurrentReplicate(), and setForFirstGeneration().

Referenced by run().

+ Here is the caller graph for this function:

◆ reset()

void SimRunner::reset ( )

Resets all the parameters to the unset state, resets the services.

191 {
192 #ifdef _DEBUG_
193  message("SimRunner::reset\n");
194 #endif
195  //reset all the parameter to the "unset" state
196  list<ParamSet*>::iterator current_paramset = this->_allParams.begin();
197  while(current_paramset != this->_allParams.end()) {
198  (*current_paramset)->reset();
199  current_paramset++;
200  }
201 
202  reset_services();
203 }
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:142
void reset_services()
Resets the FileServices and StatServices.
Definition: simulation.cc:207

References ParamManager::_allParams, message(), and reset_services().

Referenced by init_components(), and ~SimRunner().

+ Here is the caller graph for this function:

◆ reset_services()

void SimRunner::reset_services ( )

Resets the FileServices and StatServices.

208 {
212 #ifdef _DEBUG_
213  message("SimRunner::reset_services::done\n");
214 #endif
215 }
virtual void reset()
Clears the list of FileHandlers.
Definition: fileservices.cc:402
virtual void reset()
clear the list of StatHandler
Definition: statservices.cc:207
virtual void reset()
Clears the containers.
Definition: updaterservices.cc:60

References _FileServices, _ParamUpdaterManager, _StatServices, message(), FileServices::reset(), StatServices::reset(), and UpdaterServices::reset().

Referenced by reset().

+ Here is the caller graph for this function:

◆ run() [1/2]

bool SimRunner::run ( )

First loop of the simulation, performs the simulations stored in the ParamManager base class.

399 {
400  time_t t;
401  unsigned int sim = 0, simnbre = _simRecords.size();
402 
403  list< map< string,string > >::iterator currentSim = _simRecords.begin();
404 
405  //first loop: perform all simulations contained in _simRecords:
406  //---------------------------------------------------------------------------------------
407  while(currentSim != _simRecords.end()) {
408 
409  sim++;
410 
411  //clear and build the lists of traits, LCEs, stats and files handlers, random seed
412  //create trait prototypes and the genetic map within IndFactory::makePrototype
413  if(!init_components(*currentSim)) return false;
414 
415  //output a few info:
416  if ( _myenv->isMaster() ) {
417 
418  t = time(NULL);
419 
420  strftime(_startTime, 20, "%d-%m-%Y %H:%M:%S", localtime(&t));
421 
422  message("\n--- SIMULATION %i/%i ---- [ %s ]\n\n",sim,simnbre,_FileServices.getBaseFileName().c_str());
423  message(" start: %s\n",_startTime);
424  message(" mode: %s\n",_modeArg.c_str());
425  message(" population: 2 stages, %i patch%s",
426  _thePop->getPatchNbr(), (_thePop->getPatchNbr() > 1 ? "es" : ""));
427 
428  // Wright-Fisher population?
429  if(get_LCE("breed")->get_parameter("mating_isWrightFisher")->isSet() ||
430  get_LCE("breed_selection")->get_parameter("mating_isWrightFisher")->isSet())
431  message(", Wright-Fisher\n");
432  else
433  message(", non Wright-Fisher\n");
434 
435  message(" traits: ");
436 
437  map<trait_t, TraitPrototype* >::iterator trait = _currentTraits.begin();
438 
439  while(trait != _currentTraits.end()) {
440  message("%s",trait->first.c_str());
441  message("(%i)", _thePop->getTraitIndex(trait->first.c_str()));
442  trait++;
443  if(trait != _currentTraits.end()) message(", ");
444  }
445 
446  message("\n LCEs: ");
447 
448  LCE_ITER LCE = _LifeCycle.begin();
449  while( LCE != _LifeCycle.end()) {
450  message("%s(%i)",LCE->second->get_event_name().c_str(),LCE->first);
451  LCE++;
452  if(LCE != _LifeCycle.end()) message(", ");
453  }
454 
455  message("\n");
456  }
457 
458  //init the file services:
459  //->files check, false means the user wants to skip this simulation.
460  if( !(_FileServices.init(this->_currentParams)) ) {
461  currentSim++;
463  continue;
464  }
465 
466  // Load the population from a source file?
467  if(_thePop->isSourceLoad()) {
468  message(" loading population from: %s (%s)\n",_thePop->getSourceName().c_str(),
469  _thePop->getSourceFileType().c_str());
470  } // endif isSourceLoad
471 
472  if ( _myenv->isMaster() ) message("\n");
473 
474  //->save simulation parameters in log files, add the current random seed
475  // this is where ini files are created in `run_mode create_init' (mode == 4)
477  _FileServices.log("#random_seed "+tstring::ulong2str( _random_seed ));
478 
479  clock_t start = clock();
480 
481  //run the simulation
482  //-------------------------------------------------------------------------------------
483  if(_doRun) Replicate_LOOP( );
484 
485  clock_t stop = clock();
486 
487  t = time(NULL);
488 
489  strftime(_endTime, 20, "%d-%m-%Y %H:%M:%S", localtime(&t));
490 
491  _simElapsedTime = setElapsedTime(stop - start);
492 
493  if ( _myenv->isMaster() && _mode != 3 && _mode != 4) {
494  printLog();
495  message("\n\n end: %s\n",_endTime);
496  message("--- done (CPU time: %ss)\n",_simElapsedTime.c_str());
497  //don't log this when in 'dryrun' or 'create_init' mode:
498  _FileServices.log("\n# simulation finished " + string(_endTime));
499  _FileServices.log("# CPU time used: " + _simElapsedTime + "s");
500  }
501 
503 
504  // free all allocated memory of the random number generator
505  // it will be re-allocated in `init_components -> ini_random_seed'
506  RAND::free();
507 
508  currentSim++;
509  }
510  //---------------------------------------------------------------------------------------
511  if( _do_postexec && _myenv->isMaster() ) runPostExec();
512 
513  return true;
514 }
void log(string message)
Write to the parameter logfile.
Definition: fileservices.cc:435
virtual bool init()
Definition: fileservices.h:100
void log_simparams()
Saves the current simulation parameters to the default parameter logfile.
Definition: fileservices.cc:425
int getTraitIndex(trait_t type)
Gives the index of trait with type.
Definition: indfactory.cc:127
void clearPrototype()
Reset the trait prototypes, mostly done to unregister the genetic maps.
Definition: indfactory.cc:102
bool isSourceLoad()
Definition: metapop.h:281
unsigned int getPatchNbr()
Definition: metapop.h:275
string getSourceFileType()
Definition: metapop.h:283
string getSourceName()
Definition: metapop.h:282
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:148
static void free()
Memory de-allocation.
Definition: Uniform.h:110
void runPostExec()
Run the post-exec script after all simulations have finished.
Definition: simulation.cc:833
bool init_components(map< string, string > &simparams)
Performs the initialization of the different components of the simulation.
Definition: simulation.cc:251
void printLog()
Definition: simulation.cc:778
void Replicate_LOOP()
Replicate loop, iterates the life cycle _replicates times.
Definition: simulation.cc:518
static string ulong2str(const unsigned long i)
Writes an integer value into a string.
Definition: tstring.h:104

References SimBuilder::_currentTraits, _do_postexec, _doRun, _endTime, _FileServices, SimBuilder::_LifeCycle, _mode, _modeArg, _myenv, _random_seed, _simElapsedTime, ParamManager::_simRecords, _startTime, _thePop, IndFactory::clearPrototype(), RAND::free(), ComponentManager::get_LCE(), FileServices::getBaseFileName(), Metapop::getPatchNbr(), Metapop::getSourceFileType(), Metapop::getSourceName(), IndFactory::getTraitIndex(), FileServices::init(), init_components(), MPIenv::isMaster(), Metapop::isSourceLoad(), FileServices::log(), FileServices::log_simparams(), message(), printLog(), Replicate_LOOP(), runPostExec(), setElapsedTime(), and tstring::ulong2str().

Referenced by run().

+ Here is the caller graph for this function:

◆ run() [2/2]

bool SimRunner::run ( int  ARGC,
char **  ARGV 
)

First loop of the simulation, performs the simulations stored in the ParamManager base class.

348 {
349 
350  //initialize the MPI environment:
351  _myenv = new MPIenv( &ARGC, &ARGV, _my_mpi_manager );
352 
353 
354  // initialize the random number generator with default seed
355  // it will be reinitialized later with user-provided seed, if any
356  RAND::init(time(0));
357 
358  //--------------------------------------------------------------------
359  if(_myenv->isMaster()) {
360 
361  START_MESSAGE();
362 
363  }
364 
365  //--------------------------------------------------------------------
366  //check for the presence of a metapop:
367  if(_thePop == NULL) fatal("SimRunner::run: no population is attached at startup!\n");
368 
369  //--------------------------------------------------------------------
370  //build the list of params from the components list
371  this->build_allParams();
372 
373  //--------------------------------------------------------------------
374  //get the input parameters from the input parameter file
375 
376  // create a stream parser attached to the file reader
377  FileParser Reader("");
378 
379  if (ARGC == 1)
380  build_records(Reader.getParsedParameters("Nemo2.ini")); // this calls StreamParser::ParamParser::getParsedParameters
381  else
382  for (int i = 1; i < ARGC; ++i)
383  build_records(Reader.getParsedParameters(ARGV[i]));
384 
385  //--------------------------------------------------------------------
386  //run the simulation
387  bool status = run();
388 
390 
391  delete _myenv;
392 
393  return status;
394 }
Text input parameter file parser.
Definition: fileparser.h:36
MPI environment setup.
Definition: MPImanager.h:121
static void finish(MPImanager *p)
Definition: MPImanager.cc:89
void build_records(map< string, vector< string > > &initParams)
Builds the list of simulation parameters from the parsed input file(s).
Definition: basicsimulation.cc:251
void build_allParams()
Clears and fills the _allParams list with the ParamSet's of the simulation components.
Definition: basicsimulation.cc:117
bool run()
First loop of the simulation, performs the simulations stored in the ParamManager base class.
Definition: simulation.cc:398
void fatal(const char *str,...)
Definition: output.cc:99
void START_MESSAGE()
Definition: version.h:32

References _my_mpi_manager, _myenv, _thePop, ParamManager::build_allParams(), ParamManager::build_records(), fatal(), MPIenv::finish(), ParamsParser::getParsedParameters(), RAND::init(), MPIenv::isMaster(), run(), and START_MESSAGE().

Referenced by main().

+ Here is the caller graph for this function:

◆ run_event()

bool SimRunner::run_event ( string &  name)

Execute one specific life cycle event, if present in the list of current events.

Parameters
namethe name of the (paramSet of the) LifeCycleEvent to execute.
689 {
690  //before calling that fction, first set the params and call init_components()!!
691 
692  LifeCycleEvent* event = this->get_current_event(name);
693 
694  if(event == NULL) {
695  error("SimRunner::run_event:event \"%s\" not found in set events!\n",name.c_str());
696  return false;
697  }
698 
699  if( !(event->get_paramset()->isSet()) ) return false;//highly unlikely!!
700 
701  event->execute();
702 
703  return true;
704 }
Base class of the Life Cycle Events, declares the LCE interface.
Definition: lifecycleevent.h:72
LifeCycleEvent * get_current_event(string &name)
Accessor to the list of current LCEs.
Definition: basicsimulation.cc:857

References error(), and SimBuilder::get_current_event().

◆ runPostExec()

void SimRunner::runPostExec ( )

Run the post-exec script after all simulations have finished.

834 {
835  ifstream script(_postexec_script.c_str(),ios::in);
836  string cmd;
837 
838  if(!script.is_open()) {
839  error("could not open post simulation shell script!\n");
840  return;
841  }
842 
843  message("Executing shell script \"%s\" ",_postexec_script.c_str());
844  fflush(stdout);
845 
846  cmd = "sh " + _postexec_script + " " + _postexec_args;
847 
848  if(system(cmd.c_str()) < 0){
849  error("execution of `sh %s %s' failed: %s\n",_postexec_script.c_str(), _postexec_args.c_str(),strerror(errno));
850  return;
851  }
852 
853  message("...done\n");
854 }

References _postexec_args, _postexec_script, error(), and message().

Referenced by run().

+ Here is the caller graph for this function:

◆ runPostExecReplicateWise()

void SimRunner::runPostExecReplicateWise ( )

Run the post-exec script after each replicate, adds filename and replicate number as args.

859 {
860  ifstream script(_postexec_script.c_str(),ios::in);
861  string cmd;
862 
863  if(!script.is_open()) {
864  error("could not open post simulation shell script!\n");
865  return;
866  }
867 
868  message("Executing shell script \"%s\" ",_postexec_script.c_str());
869  fflush(stdout);
870 
871  // runs the script with base sim filename and replicate number as two first arguments
873 
874  if(system(cmd.c_str()) < 0){
875  error("execution of `sh %s %s' failed: %s\n",_postexec_script.c_str(), _postexec_args.c_str(),strerror(errno));
876  return;
877  }
878 
879  message("...done\n");
880 }
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:94

References _currentReplicate, _FileServices, _postexec_args, _postexec_script, error(), FileServices::getBaseFileName(), tstring::int2str(), and message().

Referenced by Replicate_LOOP().

+ Here is the caller graph for this function:

◆ setCurrentGeneration()

void SimRunner::setCurrentGeneration ( unsigned int  gen)
inline

◆ setCurrentReplicate()

void SimRunner::setCurrentReplicate ( unsigned int  repl)
inline

◆ setElapsedTime()

std::string SimRunner::setElapsedTime ( clock_t  time)

Compute and print the simulation's elapsed time to stdout.

Parameters
timeelapsed time in ticks count
726 {
727  int e_time = time / CLOCKS_PER_SEC;
728  int hour = e_time / 3600;
729  int minute = ((e_time % 3600) / 60);
730  int sec = (e_time % 3600) % 60;
731 
732  std::ostringstream out(ios::out);
733 
734  out.fill('0');
735  out.width(2);
736  out<<hour<<":";
737  out.width(2);
738  out<<minute<<":";
739  out.precision(2);
740  out.fill('0');
741  out.width(2);
742  out<<sec;
743 
744  return(out.str());
745 }

Referenced by printLog(), and run().

+ Here is the caller graph for this function:

◆ setForFirstGeneration()

void SimRunner::setForFirstGeneration ( )

Sets the population and the services ready for the first generation of a new replicate.

622 {
624 
625  //reset temporal parameters/components to their first generation value/state
627 
628  //reset stat recording iterator to first occurrence
630 
631  //build metapopulation for the current replicate (build first generation)
633 
634 }
void setPopulation(unsigned int currentReplicate, unsigned int replicates)
Sets the population for the first generation of each replicates.
Definition: metapop.cc:707
void resetCurrentOccurrence()
Resets the occurrence iterator to the beginning of the list of generation occurrences.
Definition: statservices.h:137
virtual void notify()
Definition: updaterservices.h:77

References _currentReplicate, _ParamUpdaterManager, _replicates, _StatServices, _thePop, UpdaterServices::notify(), StatServices::resetCurrentOccurrence(), Metapop::setCurrentGeneration(), and Metapop::setPopulation().

Referenced by Replicate_LOOP().

+ Here is the caller graph for this function:

◆ setGenerations()

void SimRunner::setGenerations ( unsigned int  gen)
inline

◆ setLifeCycle()

void SimRunner::setLifeCycle ( )

Sets the list of LifeCyckeEvent's currently active.

242 {
243  build_LifeCycle();
244 
245  for( LCE_ITER LCE = _LifeCycle.begin(); LCE != _LifeCycle.end(); LCE++)
246  LCE->second->init(_thePop);
247 }
void build_LifeCycle()
Selects the life cycle events that have their parameters set.
Definition: basicsimulation.cc:936

References SimBuilder::_LifeCycle, _thePop, and SimBuilder::build_LifeCycle().

Referenced by init_components().

+ Here is the caller graph for this function:

◆ setReplicates()

void SimRunner::setReplicates ( unsigned int  repl)
inline

◆ step()

void SimRunner::step ( unsigned int  nb_gen)

Iterates the life cycle.

Parameters
nb_gennumber of iterations to perform.
709 {
710  LCE_ITER LCE = _LifeCycle.begin();
711 
712  while(LCE != _LifeCycle.end()) {
713  _currentRankInLifeCycle = LCE->second->get_rank();
714 #ifdef _DEBUG_
715  message("LCE rank %i: %s\n", _currentRankInLifeCycle, LCE->second->get_event_name().c_str());
716 #endif
717  LCE->second->execute();
718  _thePop->setCurrentAge(LCE->second);
719  LCE++;
720  }
721 }
void setCurrentAge(age_t age)
Sets the age flag.
Definition: metapop.h:302

References _currentRankInLifeCycle, SimBuilder::_LifeCycle, _thePop, message(), and Metapop::setCurrentAge().

Referenced by Cycle().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _currentGeneration

unsigned int SimRunner::_currentGeneration
private

The current generation in the generation loop, starts at 1.

Referenced by Cycle(), getCurrentGeneration(), Replicate_LOOP(), and setCurrentGeneration().

◆ _currentRankInLifeCycle

int SimRunner::_currentRankInLifeCycle
private

The current rank in the life cycle, corresponds to the rank of the current LCE, before it executes.

Referenced by getCurrentRankInLifeCycle(), and step().

◆ _currentReplicate

unsigned int SimRunner::_currentReplicate
private

The current replicate in the replicate loop, starts at 1.

Referenced by Cycle(), getCurrentReplicate(), Replicate_LOOP(), runPostExecReplicateWise(), setCurrentReplicate(), and setForFirstGeneration().

◆ _do_postexec

bool SimRunner::_do_postexec
private

Boolean set when a post-exec script must be executed.

Referenced by init(), and run().

◆ _do_postexec_replicatewise

bool SimRunner::_do_postexec_replicatewise
private

Boolean to trigger replicate-wise execution of post script.

Referenced by init(), and Replicate_LOOP().

◆ _doRun

bool SimRunner::_doRun
private

Boolean set when not in dryrun mode.

Referenced by init(), and run().

◆ _endTime

char SimRunner::_endTime[20]
private

Referenced by printLog(), and run().

◆ _FileServices

◆ _generations

unsigned int SimRunner::_generations
private

Number of generations to iterate.

Referenced by Cycle(), getGenerations(), init(), init_components(), Replicate_LOOP(), and setGenerations().

◆ _logfile

std::string SimRunner::_logfile
private

Referenced by init(), printLog(), and printLogHeader().

◆ _meanGenLength

unsigned int SimRunner::_meanGenLength
private

Generation counter, for logging.

Referenced by printLog(), and Replicate_LOOP().

◆ _meanReplElapsedTime

clock_t SimRunner::_meanReplElapsedTime
private

Clock counter, for logging.

Referenced by printLog(), and Replicate_LOOP().

◆ _mode

unsigned int SimRunner::_mode
private

The run mode code (0 = run, 1 = overwrite, 2 = skip, 3 = dryrun, 4 = create_init)

Referenced by init(), and run().

◆ _modeArg

std::string SimRunner::_modeArg
private

The run mode ('overwrite', 'run', 'skip', 'dryrun', 'create_init').

Referenced by init(), and run().

◆ _my_mpi_manager

MPImanager* SimRunner::_my_mpi_manager
private

Referenced by Replicate_LOOP(), and run().

◆ _ParamUpdaterManager

◆ _postexec_args

std::string SimRunner::_postexec_args
private

Arguments to pass to the post-exec script.

Referenced by init(), runPostExec(), and runPostExecReplicateWise().

◆ _postexec_script

std::string SimRunner::_postexec_script
private

The path to the script to be executed after last simulation.

Referenced by init(), runPostExec(), and runPostExecReplicateWise().

◆ _random_seed

unsigned long SimRunner::_random_seed
private

The startup random seed of the random generator.

Referenced by init_random_seed(), and run().

◆ _replicates

unsigned int SimRunner::_replicates
private

◆ _simElapsedTime

std::string SimRunner::_simElapsedTime
private

Referenced by printLog(), and run().

◆ _startTime

char SimRunner::_startTime[20]
private

Referenced by printLog(), and run().

◆ _StatServices

◆ _thePop


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR