Nemo  2.3.46
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 ()
 
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...
 
LifeCycleEventget_current_event (string &name)
 Accessor to the list of current LCEs. 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
 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 ( Metapop pop)
inline

References attach_pop().

106  : _my_mpi_manager(0)
107  {
108  attach_pop(pop);
109  }
void attach_pop(Metapop *pop)
Attach a pop to the simulation.
Definition: simulation.h:166
MPImanager * _my_mpi_manager
Definition: simulation.h:56
SimRunner::~SimRunner ( )

Dstror.

References message(), and reset().

149 {
150 #ifdef _DEBUG_
151  message("SimRunner::~SimRunner\n");
152 #endif
153 
154  reset();
155 
156 }
void message(const char *message,...)
Definition: output.cc:40
void reset()
Resets all the parameters to the unset state, resets the services.
Definition: simulation.cc:160

Member Function Documentation

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

References ComponentManager::_components.

Referenced by SimRunner().

166 {_thePop = pop;this->_components.push_back(_thePop);}
Metapop * _thePop
Definition: simulation.h:58
list< SimComponent * > _components
List of all the simulation components.
Definition: basicsimulation.h:71
bool SimRunner::build_pop ( )

Calls the Metapop init procedure with current traits and LCEs.

void SimRunner::Cycle ( char *  startTime)

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

Parameters
startTimethe starting time of the current replicate.

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

Referenced by Replicate_LOOP().

561 {
562 
563  // ------------------------------ CYCLE --------------------------------
564 
566 
567  // -------------------------- PRINT STUFF ------------------------------
568 #if !defined(LOW_VERBOSE) && !defined(USE_MPI)
569  if( !(_currentGeneration % 100) || _currentGeneration < 100){
570  message("\r replicate %i/%i [%s] %i/%i", _currentReplicate, _replicates
571  ,startTime, _currentGeneration, _generations);
572  fflush(stdout);
573  }
574 #endif
575 
576 #ifdef _DEBUG_
577  message("____Generation %i/%i____\n", _currentGeneration, _generations);
578 #endif
579 
580  // -------------------------- STEP ONE GEN ------------------------------
582 
583  // do one iteration of the life cycle:
584  if(_thePop->isAlive())
585 
586  step(1);
587 
588  else {
589 #if !defined(LOW_VERBOSE) && !defined(USE_MPI)
590  message("\r replicate %i/%i [%s] %i/%i -> Pop extinction !\n", _currentReplicate, _replicates
591  ,startTime, _currentGeneration, _generations);
592 #endif
594  break;
595  }
596 
597  }
598  // --------------------------- END OF CYCLE --------------------------
599 }
void message(const char *message,...)
Definition: output.cc:40
Metapop * _thePop
Definition: simulation.h:58
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:76
unsigned int _currentGeneration
The current generation in the generation loop, starts at 1.
Definition: simulation.h:78
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class...
Definition: metapop.h:299
void setCurrentGeneration(unsigned int gen)
Definition: metapop.h:288
void step(unsigned int nb_gen)
Iterates the life cycle.
Definition: simulation.cc:623
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
list<StatRecBase*> SimRunner::get_allRegisteredStats ( )
inline

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

References StatServices::getAllStats().

150 {return _StatServices.getAllStats();}
StatServices _StatServices
Definition: simulation.h:100
list< StatRecBase * > getAllStats()
Definition: statservices.cc:389
FileServices* SimRunner::get_FileServices ( )
inline

Returns the FileServices.

References _FileServices.

Referenced by Metapop::loadPopFromTraitFile().

146 {return &_FileServices;}
FileServices _FileServices
Definition: simulation.h:98
Metapop* SimRunner::get_pop ( )
inline

Accessor to the pop ptr.

Returns
the pop ptr

References _thePop.

170 {return _thePop;}
Metapop * _thePop
Definition: simulation.h:58
StatServices* SimRunner::get_StatServices ( )
inline

Returns the StatServices.

References _StatServices.

Referenced by LCE_StatServiceNotifier::LCE_StatServiceNotifier().

148 {return &_StatServices;}
StatServices _StatServices
Definition: simulation.h:100
unsigned int SimRunner::getCurrentGeneration ( )
inline

References _currentGeneration.

Referenced by SIMenv::getCurrentGeneration().

188 {return _currentGeneration;}
unsigned int _currentGeneration
The current generation in the generation loop, starts at 1.
Definition: simulation.h:78
int SimRunner::getCurrentRankInLifeCycle ( )
inline

References _currentRankInLifeCycle.

Referenced by SIMenv::getCurrentRankInLifeCycle().

195 {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:80
unsigned int SimRunner::getCurrentReplicate ( )
inline

References _currentReplicate.

Referenced by SIMenv::getCurrentReplicate().

192 {return _currentReplicate;}
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:76
unsigned int SimRunner::getGenerations ( )
inline

References _generations.

Referenced by SIMenv::getGenerations().

190 {return _generations;}
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
unsigned int SimRunner::getReplicates ( )
inline

References _replicates.

Referenced by SIMenv::getReplicates().

194 {return _replicates;}
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
bool SimRunner::init ( )

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

References _do_postexec, _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().

62 {
63  if( !(_paramSet.isSet("filename")) ) {
64  error("parameter \"filename\" is not properly set!\n");
65  return false;
66  }
67 
69 
70  if(_paramSet.isSet("root_dir")) {
72  }
73 
74  if(_paramSet.isSet("run_mode")) {
75 
76  _modeArg = _paramSet.getArg("run_mode");
77 
78  if(_modeArg == "run"){
80  _doRun = true;
81  _mode = 0;
82  SILENT_RUN = false; //declared in output.h
83  } else if( _modeArg == "overwrite" ) {
85  _doRun = true;
86  _mode = 1;
87  SILENT_RUN = false;
88  } else if(_modeArg == "skip"){
90  _doRun = true;
91  _mode = 2;
92  SILENT_RUN = false;
93  } else if(_modeArg == "dryrun"){
95  _doRun = false;
96  _mode = 3;
97  SILENT_RUN = false;
98  } else if(_modeArg == "create_init"){ //the logWriter creates an ini file, see FileServices::log_simparams()
100  _doRun = false;
101  _mode = 4;
102  SILENT_RUN = false;
103  } else if(_modeArg == "silent_run"){
105  _doRun = true;
106  _mode = 0;
107  SILENT_RUN = true;
108  } else {
109  error("simulation run mode \"%s\" unknown\n",_modeArg.c_str());
110  return false;
111  }
112  } else {
113  _modeArg = "run";
114  _doRun = true;
116  SILENT_RUN = false;
117  }
118 
119  _replicates = (unsigned int)_paramSet.getValue("replicates");
120 
121  _generations = (unsigned int)_paramSet.getValue("generations");
122 
123 
124  if( _paramSet.isSet("logfile") ) {
125  _logfile = _paramSet.getArg("logfile");
126  } else
127  _logfile = "nemo.log";
128 
129  printLogHeader();
130 
131  if( _paramSet.isSet("postexec_script") ) {
132  _postexec_script = _paramSet.getArg("postexec_script");
133  _do_postexec = true;
134 
135  if( _paramSet.isSet("postexec_args") )
136  _postexec_args = _paramSet.getArg("postexec_args");
137  else
138  _postexec_args = "";
139 
140  } else
141  _do_postexec = false;
142 
143  return true;
144 }
ParamSet _paramSet
The ParamSet param set of the simulation.
Definition: basicsimulation.h:145
void printLogHeader()
Definition: simulation.cc:662
unsigned int _mode
The run mode code (0 = run, 1 = overwrite, 2 = skip, 3 = dryrun, 4 = create_init) ...
Definition: simulation.h:86
bool _doRun
Boolean set when not in dryrun mode.
Definition: simulation.h:88
void setBasename(string name)
Sets the base file name of the simulation.
Definition: fileservices.cc:266
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
FileServices _FileServices
Definition: simulation.h:98
int error(const char *str,...)
Definition: output.cc:73
std::string _postexec_script
The path to the script to be executed after last simulation.
Definition: simulation.h:90
void setRootDir(string name)
Sets the root directory of the simulation.
Definition: fileservices.cc:273
std::string _modeArg
The run mode ('overwrite', 'run', 'skip', 'dryrun', 'create_init').
Definition: simulation.h:84
bool _do_postexec
Boolean set when a post-exec script must be executed.
Definition: simulation.h:94
std::string _logfile
Definition: simulation.h:60
double getValue(string name)
Accessor the parameters value.
Definition: param.h:287
bool SILENT_RUN
Definition: output.cc:37
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:285
bool isSet()
Accessor to the status flag.
Definition: param.h:273
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
std::string _postexec_args
Arguments to pass to the post-exec script.
Definition: simulation.h:92
void setMode(unsigned int m)
Mode setter, determines if file will get overwritten or not.
Definition: fileservices.h:107
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

References _FileServices, _generations, SimBuilder::_LifeCycle, _ParamUpdaterManager, _replicates, _StatServices, _thePop, SimBuilder::build_currentParams(), SimBuilder::build_currentTraits(), error(), UpdaterServices::hasTemporals(), UpdaterServices::init(), StatServices::init(), init(), LifeCycleEvent::init(), Metapop::init(), init_random_seed(), MPIenv::isMaster(), 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().

222 {
223 #ifdef _DEBUG_
224  message("SimRunner::init_components\n");
225 #endif
226  //first reset all paramSets and services (clear the handlers' lists)
227  reset();
228 
231 
232 #ifdef _DEBUG_
233  message("SimRunner::init_components: building current params\n");
234 #endif
235 
236  //build the list of active component parameters from the simulation record:
237  if(!this->build_currentParams(simparams)){
238  error("SimRunner::init_components:couldn't build current params\n");
239  return false;
240  }
241 
242  //initialize the random generator's seed:
244 
245  //init the sim and pop (set parameters from input values)
246  if( !init() || !_thePop->init() ) return false;
247 
248  //propagate replicate and generation numbers to metapop, often used by LCEs
251 
252  //the traits and LCEs components are set only after the simulation and
253  //population parameters are set
254 
255  //build the Individual prototype, and init the TTraits
256  //---> SimComponents::setParameters() is called here, this will also build the genetic map
258 
259  //build the list of life cycle events, and init LCEs
260  //---> SimComponent::setParameters() is called here
261  setLifeCycle( );
262 
263  //load the stat-, file-, and updater-handlers of the simulation components:
265 
266  //StatServices: build the lists of stat recorders:
267  if( !_StatServices.init() ) return false;
268 
269  if( !_ParamUpdaterManager.init() ) return false;
270 
273  updater->setManager( &_ParamUpdaterManager );
274  updater->init(_thePop);
275  _LifeCycle[ -1 ] = updater; //this is never destroyed...
276  }
277 
278  return true;
279 }
virtual bool init()
Definition: updaterservices.cc:39
void message(const char *message,...)
Definition: output.cc:40
void register_component_handlers()
Register all the Handlers of the currently active simulation components.
Definition: simulation.cc:198
StatServices _StatServices
Definition: simulation.h:100
UpdaterServices _ParamUpdaterManager
Definition: simulation.h:102
MPIenv * _myenv
Definition: simulation.cc:47
Metapop * _thePop
Definition: simulation.h:58
map< trait_t, TraitPrototype * > & build_currentTraits()
Selects the trait prototypes that have their parameters set.
Definition: basicsimulation.cc:647
void setReplicates(unsigned int repl)
Definition: metapop.h:268
void makePrototype(map< trait_t, TraitPrototype * > TTlist)
Creates the individuals prototype from the selected trait prototypes.
Definition: indfactory.cc:50
void setGenerations(unsigned int gen)
Definition: metapop.h:266
virtual void init(Metapop *popPtr)
Sets the pointer to the current Metapop and the trait link if applicable.
Definition: lifecycleevent.h:114
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
bool init()
Checks simulation parameters and init the FileServices with the base filename.
Definition: simulation.cc:61
FileServices _FileServices
Definition: simulation.h:98
void reset()
Resets all the parameters to the unset state, resets the services.
Definition: simulation.cc:160
void set_pop_ptr(Metapop *pop)
Definition: statservices.h:108
int error(const char *str,...)
Definition: output.cc:73
bool build_currentParams(map< string, string > &simparams)
Builds the list of parameters from user's defined input parameters.
Definition: basicsimulation.cc:621
virtual bool init()
Definition: statservices.cc:47
bool hasTemporals()
Checks if any updater has been uploaded.
Definition: updaterservices.h:92
Calls the UpdaterServices to notify its components of a generation change.
Definition: servicenotifiers.h:41
void setManager(UpdaterServices *mng)
Definition: servicenotifiers.h:51
void init_random_seed()
Initialize the seed of the random generator.
Definition: simulation.cc:283
bool init()
Inits the population parameters from the ParamSet and builds the pop (adds patches), the prototypes and the life cycle.
Definition: metapop.cc:95
void setLifeCycle()
Sets the list of LifeCyckeEvent's currently active.
Definition: simulation.cc:211
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:204
bool isMaster() const
Definition: MPImanager.h:127
virtual void set_pop_ptr(Metapop *pop)
Sets the Metapop reference.
Definition: fileservices.h:104
void SimRunner::init_random_seed ( )

Initialize the seed of the random generator.

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

Referenced by init_components().

284 {
285 
286  if(this->_paramSet.isSet("random_seed")) {
287  _random_seed = (unsigned long) this->_paramSet.getValue("random_seed");
288  message( "setting random seed from input value: %i\n", _random_seed);
289  } else
290  _random_seed = 2*time(0)+1;
291 
293 }
ParamSet _paramSet
The ParamSet param set of the simulation.
Definition: basicsimulation.h:145
void message(const char *message,...)
Definition: output.cc:40
static void init(unsigned long seed)
Initialize the random generator's seed.
Definition: Uniform.h:80
double getValue(string name)
Accessor the parameters value.
Definition: param.h:287
bool isSet()
Accessor to the status flag.
Definition: param.h:273
unsigned long _random_seed
The startup random seed of the random generator.
Definition: simulation.h:82
void SimRunner::printLog ( )

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

691 {
692  ofstream FH(_logfile.c_str(),ios::app);
693 
694  if(!FH.is_open()){
695  error("could not open simulation logfile \"%s\"\n",_logfile.c_str());
696  return;
697  }
698 
699  FH<<"| ";
700  FH.width(40);
701  FH.setf(ios::left,ios::adjustfield);
703 
704  FH <<"| "<< _startTime <<" | "<< _endTime <<" | ";
705 
706  FH.width(10);
707  FH.setf(ios::right,ios::adjustfield);
708  FH<< _simElapsedTime <<" | ";
709 
710  FH.width(9);
711  FH << _replicates <<" | ";
712 
713  FH.width(10);
714  FH << setElapsedTime( _meanReplElapsedTime ) << " | ";
715 
716  FH.width(8);
717  FH << _meanGenLength <<" | ";
718 
719  FH<<" "<<MAIN_VERSION<<"."<<MINOR_VERSION<<"."<<REVISION<<RELEASE
720  <<" "<<VERSION_DATE;
721 
722  FH<<" | ";
723  FH.width(20);
724  FH.setf(ios::left,ios::adjustfield);
725  char* host;
726  if( (host = getenv("HOST")) != NULL )
727  FH << host << " |";
728  else if ( (host = getenv("HOSTNAME")) != NULL )
729  FH << host << " |";
730  else
731  FH << "-" << " |";
732 
734 
735  for(;file != last; file++)
736  FH << " \"" << (*file)->get_extension() << "\":" << (*file)->get_path();
737 
738  FH << std::endl;
739 
740  FH.close();
741 }
#define REVISION
Definition: version.h:29
char _startTime[20]
Definition: simulation.h:62
#define RELEASE
Definition: version.h:30
#define MINOR_VERSION
Definition: version.h:28
#define MAIN_VERSION
Definition: version.h:27
file_it getLastWriter()
Accessor to last element of the list of output FileHandlers.
Definition: fileservices.h:131
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
FileServices _FileServices
Definition: simulation.h:98
int error(const char *str,...)
Definition: output.cc:73
#define VERSION_DATE
Definition: version.h:31
std::string setElapsedTime(clock_t time)
Compute and print the simulation's elapsed time to stdout.
Definition: simulation.cc:637
clock_t _meanReplElapsedTime
Clock counter, for logging.
Definition: simulation.h:66
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:226
char _endTime[20]
Definition: simulation.h:62
std::string _logfile
Definition: simulation.h:60
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:83
std::string _simElapsedTime
Definition: simulation.h:64
unsigned int _meanGenLength
Generation counter, for logging.
Definition: simulation.h:68
file_it getFirstWriter()
Accessor to first element of the list of output FileHandlers.
Definition: fileservices.h:128
void SimRunner::printLogHeader ( )

References _logfile, and error().

Referenced by init().

663 {
664  ofstream FH;
665  ifstream IF;
666  //check is the logfile already exists:
667  IF.open(_logfile.c_str(),ios::in);
668  if(IF){
669  IF.close();
670  return;
671  }
672  IF.close();
673 
674  FH.open(_logfile.c_str(),ios::out);
675  if(!FH) {
676  error("could not create simulation logfile \"%s\"\n",_logfile.c_str());
677  return;
678  }
679 
680  FH<<"--- N E M O ---\n"
681  <<" LOGFILE\n\n\n";
682  FH<<"| basename | start time | stop time | e-time CPU |"
683  <<" repl done | rpl e-time | mean gen | version | hostname | output files \n";
684 
685  FH.close();
686 }
int error(const char *str,...)
Definition: output.cc:73
std::string _logfile
Definition: simulation.h:60
void SimRunner::register_component ( SimComponent cmpt)

Register the different Handler's attached to a SimComponent.

Parameters
cmpta SimComponent

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

Referenced by register_component_handlers().

190 {
191  _FileServices.load(cmpt);
192  _StatServices.load(cmpt);
194 }
StatServices _StatServices
Definition: simulation.h:100
UpdaterServices _ParamUpdaterManager
Definition: simulation.h:102
FileServices _FileServices
Definition: simulation.h:98
virtual void load(SimComponent *sc)
Loads the updaters in case a component's parameter has temporal arguments.
Definition: updaterservices.cc:82
virtual void load(SimComponent *sc)
tell the SimComponent to load its stat handlers
Definition: statservices.cc:170
virtual void load(SimComponent *sc)
Tells the SimComponent to load its file handlers.
Definition: fileservices.cc:78
void SimRunner::register_component_handlers ( )

Register all the Handlers of the currently active simulation components.

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

Referenced by init_components().

199 {
201 
202  for(TRAIT_ITER trait = _currentTraits.begin(); trait != _currentTraits.end(); trait++)
203  register_component(trait->second);
204 
205  for(LCE_ITER LCE = _LifeCycle.begin(); LCE != _LifeCycle.end(); LCE++)
206  register_component(LCE->second);
207 }
Metapop * _thePop
Definition: simulation.h:58
void register_component(SimComponent *cmpt)
Register the different Handler's attached to a SimComponent.
Definition: simulation.cc:189
map< trait_t, TraitPrototype * > _currentTraits
List of the selected trait prototypes from the user defined input parameters.
Definition: basicsimulation.h:202
map< trait_t, TraitPrototype * >::const_iterator TRAIT_ITER
Definition: basicsimulation.h:207
map< int, LifeCycleEvent * >::const_iterator LCE_ITER
Definition: basicsimulation.h:206
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:204
void SimRunner::Replicate_LOOP ( )

Replicate loop, iterates the life cycle _replicates times.

References _currentGeneration, _currentReplicate, _FileServices, _generations, _meanGenLength, _meanReplElapsedTime, _my_mpi_manager, _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(), Metapop::setCurrentGeneration(), Metapop::setCurrentReplicate(), and setForFirstGeneration().

Referenced by run().

443 {
444 
445 #ifndef USE_MPI
446 
447  clock_t start;
448  clock_t stop;
449  time_t t;
450  char t_buff[10];
451 
453  _meanGenLength = 0;
454  _currentGeneration = 0;
455 
456  //---------------------------------- REPLICATE LOOP -------------------------------------
458 
459 
460  // -------------------------- PRINT STUFF ------------------------------
461  t=time(0);
462  //print some output:
463  strftime(t_buff,10,"%H:%M:%S",localtime(&t));
464 
465 #ifdef LOW_VERBOSE
466  message(" replicate %i/%i [%s] \n",_currentReplicate, _replicates, t_buff);
467 #else
468  message("\r replicate %i/%i [%s] 1/%i ",_currentReplicate, _replicates, t_buff, _generations);
469  fflush(stdout);
470 #endif
471 
472  //---------------- SET POPULATION FOR FIRST GENERATION ------------------
473 
475 
476  //--------------------------- GENERATION LOOP ---------------------------
477  start = clock();
478 
479  Cycle(t_buff);
480 
481  stop = clock();
482  //-----------------------------------------------------------------------
483 
484  _meanReplElapsedTime += (stop - start);
485 
487 
488  //call the file services to print the replicate stats in case of pop extinction
489  if( !_thePop->isAlive() && _currentGeneration-1 <= _generations ) {
493  }
494 
495  }
496  //--------------------------------- /REPLICATE LOOP -------------------------------------
497 
504  }
505 
508 
509 #else
510  //else ifndef USE_MPI
511  //------------------------------- MPI REPLICATE LOOP ------------------------------------
513 
514  while ( _currentReplicate <= _replicates )
516 
518 
519  if( _myenv->isMaster() ) {
524 
525  //write the stat files, only if "save_stats" LCE is part of the life cycle
526  if(get_LCE("save_stats")->get_paramset()->isSet()) {
527  FileHandler& statWriter = dynamic_cast<LCE_StatServiceNotifier*> ( get_LCE("save_stats") )->getFH();
528  for (unsigned int i = 1; i <= _replicates; ++i) {
529  _currentReplicate = i;
530  statWriter.FHwrite();
531  }
532  }
533  }
534  //------------------------------ /MPI REPLICATE LOOP ------------------------------------
535 #endif
536  //endif USE_MPI
537 
538  //delete all individuals present in the population and delete the patches:
539  _thePop->clear();
540 }
void message(const char *message,...)
Definition: output.cc:40
ParamSet * get_paramset(string &name)
Looks for paramset with "name" in the list of parameter sets.
Definition: basicsimulation.cc:180
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:53
StatServices _StatServices
Definition: simulation.h:100
MPIenv * _myenv
Definition: simulation.cc:47
Metapop * _thePop
Definition: simulation.h:58
LifeCycleEvent * get_LCE(string name)
Search for component with "name" in the life cycle events list.
Definition: basicsimulation.cc:77
virtual unsigned int init(StatServices *StatManager)=0
virtual void finish(StatServices *StatManager, unsigned int *_gen, unsigned int _repl)
void Cycle(char *startTime)
Life cycle loop, executes the list of LCEs _generations times.
Definition: simulation.cc:560
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:76
FileServices _FileServices
Definition: simulation.h:98
unsigned int _currentGeneration
The current generation in the generation loop, starts at 1.
Definition: simulation.h:78
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class...
Definition: metapop.h:299
Initiates the StatServices' parameters (log time) when registering, calls StatServices::notify() when...
Definition: servicenotifiers.h:125
virtual void iterate(SimRunner *_sim, StatServices *StatManager, unsigned int *_gen, unsigned int *_repl)=0
void setForFirstGeneration()
Sets the population and the services ready for the first generation of a new replicate.
Definition: simulation.cc:544
clock_t _meanReplElapsedTime
Clock counter, for logging.
Definition: simulation.h:66
void clear()
Called at the end of each simulation, empties the pop and the garbage collector; the Individuals are ...
Definition: metapop.cc:1016
void setCurrentGeneration(unsigned int gen)
Definition: metapop.h:288
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
MPImanager * _my_mpi_manager
Definition: simulation.h:56
void setCurrentReplicate(unsigned int repl)
Definition: metapop.h:287
unsigned int _meanGenLength
Generation counter, for logging.
Definition: simulation.h:68
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
virtual void notify()
Definition: fileservices.cc:159
bool isMaster() const
Definition: MPImanager.h:127
void SimRunner::reset ( )

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

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

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

161 {
162 #ifdef _DEBUG_
163  message("SimRunner::reset\n");
164 #endif
165  //reset all the parameter to the "unset" state
166  list<ParamSet*>::iterator current_paramset = this->_allParams.begin();
167  while(current_paramset != this->_allParams.end()) {
168  (*current_paramset)->reset();
169  current_paramset++;
170  }
171 
172  reset_services();
173 }
void message(const char *message,...)
Definition: output.cc:40
void reset_services()
Resets the FileServices and StatServices.
Definition: simulation.cc:177
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:137
void SimRunner::reset_services ( )

Resets the FileServices and StatServices.

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

Referenced by reset().

178 {
182 #ifdef _DEBUG_
183  message("SimRunner::reset_services::done\n");
184 #endif
185 }
void message(const char *message,...)
Definition: output.cc:40
StatServices _StatServices
Definition: simulation.h:100
UpdaterServices _ParamUpdaterManager
Definition: simulation.h:102
FileServices _FileServices
Definition: simulation.h:98
virtual void reset()
Clears the list of FileHandlers.
Definition: fileservices.cc:168
virtual void reset()
clear the list of StatHandler
Definition: statservices.cc:208
virtual void reset()
Clears the containers.
Definition: updaterservices.cc:61
bool SimRunner::run ( int  ARGC,
char **  ARGV 
)

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

References _my_mpi_manager, _myenv, _thePop, ParamManager::build_allParams(), ParamManager::build_records(), fatal(), MPIenv::finish(), ParamsParser::getParsedParameters(), MPIenv::isMaster(), MAIN_VERSION, message(), MINOR_VERSION, RELEASE, REVISION, run(), and VERSION_DATE.

Referenced by main().

298 {
299  //initialize the MPI environment:
300  _myenv = new MPIenv( ARGC, ARGV, _my_mpi_manager );
301 
302  //--------------------------------------------------------------------
303  if(_myenv->isMaster()) {
304  message("\n N E M O %i.%i.%i%s %s\n", MAIN_VERSION, MINOR_VERSION, REVISION, RELEASE, VERSION_DATE);
305  message("\n Copyright (C) 2006-2017 Frederic Guillaume");
306  message("\n This is free software; see the source for copying");
307  message("\n conditions. There is NO warranty; not even for ");
308  message("\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
309  message("\n http://nemo2.sourceforge.net");
310  message("\n------------------------------------------------\n");
311  }
312 
313  //--------------------------------------------------------------------
314  //check for the presence of a metapop:
315  if(_thePop == NULL) fatal("SimRunner::run: no population is attached at startup!\n");
316 
317  //--------------------------------------------------------------------
318  //build the list of params from the components list
319  this->build_allParams();
320 
321  //--------------------------------------------------------------------
322  //get the input parameters
323  FileParser Reader("");
324  if (ARGC == 1)
325  build_records(Reader.getParsedParameters("Nemo2.ini"));
326  else
327  for (int i = 1; i < ARGC; ++i)
328  build_records(Reader.getParsedParameters(ARGV[i]));
329 
330  //--------------------------------------------------------------------
331  //run the simulation
332  bool status = run();
333 
335 
336  delete _myenv;
337 
338  return status;
339 }
#define REVISION
Definition: version.h:29
void message(const char *message,...)
Definition: output.cc:40
MPIenv * _myenv
Definition: simulation.cc:47
Metapop * _thePop
Definition: simulation.h:58
#define RELEASE
Definition: version.h:30
#define MINOR_VERSION
Definition: version.h:28
#define MAIN_VERSION
Definition: version.h:27
void fatal(const char *str,...)
Definition: output.cc:90
bool run()
First loop of the simulation, performs the simulations stored in the ParamManager base class...
Definition: simulation.cc:343
#define VERSION_DATE
Definition: version.h:31
Text input parameter file parser.
Definition: fileparser.h:37
MPI environment setup.
Definition: MPImanager.h:121
static void finish(MPImanager *p)
Definition: MPImanager.cc:68
void build_allParams()
Clears and fills the _allParams list with the ParamSet's of the simulation components.
Definition: basicsimulation.cc:115
MPImanager * _my_mpi_manager
Definition: simulation.h:56
void build_records(map< string, vector< string > > &initParams)
Builds the list of simulation parameters from the parsed input file(s).
Definition: basicsimulation.cc:198
bool isMaster() const
Definition: MPImanager.h:127
bool SimRunner::run ( )

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

References SimBuilder::_currentTraits, _do_postexec, _doRun, _endTime, _FileServices, SimBuilder::_LifeCycle, _mode, _modeArg, _random_seed, _simElapsedTime, ParamManager::_simRecords, _startTime, _thePop, IndFactory::clearPrototype(), FileServices::getBaseFileName(), IndFactory::getIndividualProtoype(), Metapop::getSourceFileType(), Metapop::getSourceName(), Individual::getTraitNumber(), FileServices::init(), init_components(), MPIenv::isMaster(), Metapop::isSourceLoad(), FileServices::log(), FileServices::log_simparams(), message(), printLog(), Replicate_LOOP(), runPostExec(), setElapsedTime(), tstring::ulong2str(), and warning().

Referenced by run().

344 {
345  time_t t;
346  unsigned int sim = 0, simnbre = _simRecords.size();
347 
348  list< map< string,string > >::iterator currentSim = _simRecords.begin();
349 
350  //first loop: perform all simulations contained in _simRecords:
351  //---------------------------------------------------------------------------------------
352  while(currentSim != _simRecords.end()) {
353 
354  sim++;
355 
356  //clear and build the lists of traits, LCEs, stats and files handlers, random seed
357  //create trait prototypes and the genetic map within IndFactory::makePrototype
358  if(!init_components(*currentSim)) return false;
359 
360  //output a few info:
361  if ( _myenv->isMaster() ) {
362  t = time(NULL);
363  strftime(_startTime, 20, "%d-%m-%Y %H:%M:%S", localtime(&t));
364  message("\n--- SIMULATION %i/%i ---- [ %s ]\n\n",sim,simnbre,_FileServices.getBaseFileName().c_str());
365  message(" start: %s\n",_startTime);
366  message(" mode: %s\n",_modeArg.c_str());
367  message(" traits: ");
368 
369  map<trait_t, TraitPrototype* >::iterator trait = _currentTraits.begin();
370  while(trait != _currentTraits.end()) {
371  message("%s",trait->first.c_str());
372  trait++;
373  if(trait != _currentTraits.end()) message(", ");
374  }
375  message("\n LCEs: ");
376 
377  for(LCE_ITER LCE = _LifeCycle.begin(); LCE != _LifeCycle.end(); LCE++) {
378  message("%s(%i)",LCE->second->get_event_name().c_str(),LCE->first);
379  if(LCE != _LifeCycle.end()) message(", ");
380  }
381  message("\n");
382  }
383 
384  //init the file services:
385  //->files check, false means the user wants to skip this simulation.
386  if( !(_FileServices.init(this->_currentParams)) ) {
387  currentSim++;
389  continue;
390  }
391 
392  if(_thePop->isSourceLoad()) {
393  message(" loading population from: %s_*%s\n",_thePop->getSourceName().c_str(),
394  _thePop->getSourceFileType().c_str());
396  warning("while resetting genetic map, the locus positions saved in source population are ignored");
397  warning("loci positions are set from parameters in the init file only");
398  warning("please check manually for mismatch\n");
399  }
400  }
401  message("\n");
402 
403  //->save simparameters in log files, add the current random seed
405  _FileServices.log("#random_seed "+tstring::ulong2str( _random_seed ));
406 
407  clock_t start = clock();
408 
409  //run the simulation
410  //-------------------------------------------------------------------------------------
411  if(_doRun) Replicate_LOOP( );
412 
413  clock_t stop = clock();
414 
415  t = time(NULL);
416 
417  strftime(_endTime, 20, "%d-%m-%Y %H:%M:%S", localtime(&t));
418 
419  _simElapsedTime = setElapsedTime(stop - start);
420 
421  if ( _myenv->isMaster() && _mode != 3 && _mode != 4) {
422  printLog();
423  message("\n\n end: %s\n",_endTime);
424  message("--- done (CPU time: %ss)\n",_simElapsedTime.c_str());
425  //don't log this when in 'dryrun' or 'create_init' mode:
426  _FileServices.log("\n# simulation finished " + string(_endTime));
427  _FileServices.log("# CPU time used: " + _simElapsedTime + "s");
428  }
429 
431 
432  currentSim++;
433  }
434  //---------------------------------------------------------------------------------------
435  if( _do_postexec && _myenv->isMaster() ) runPostExec();
436 
437  return true;
438 }
void message(const char *message,...)
Definition: output.cc:40
char _startTime[20]
Definition: simulation.h:62
string getSourceFileType()
Definition: metapop.h:278
MPIenv * _myenv
Definition: simulation.cc:47
Metapop * _thePop
Definition: simulation.h:58
unsigned int _mode
The run mode code (0 = run, 1 = overwrite, 2 = skip, 3 = dryrun, 4 = create_init) ...
Definition: simulation.h:86
bool _doRun
Boolean set when not in dryrun mode.
Definition: simulation.h:88
map< trait_t, TraitPrototype * > _currentTraits
List of the selected trait prototypes from the user defined input parameters.
Definition: basicsimulation.h:202
string getSourceName()
Definition: metapop.h:277
Individual * getIndividualProtoype()
Individual prototype accessor.
Definition: indfactory.h:109
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:143
void log_simparams()
Saves the current simulation parameters to the default parameter logfile.
Definition: fileservices.cc:177
FileServices _FileServices
Definition: simulation.h:98
void clearPrototype()
Reset the trait prototypes, mostly done to unregister the genetic maps.
Definition: indfactory.cc:103
unsigned int getTraitNumber()
Accessor to the size of the traits table.
Definition: individual.h:247
std::string setElapsedTime(clock_t time)
Compute and print the simulation's elapsed time to stdout.
Definition: simulation.cc:637
bool isSourceLoad()
Definition: metapop.h:276
void Replicate_LOOP()
Replicate loop, iterates the life cycle _replicates times.
Definition: simulation.cc:442
bool init_components(map< string, string > &simparams)
Performs the initialization of the different components of the simulation.
Definition: simulation.cc:221
void warning(const char *str,...)
Definition: output.cc:56
void runPostExec()
Definition: simulation.cc:745
std::string _modeArg
The run mode ('overwrite', 'run', 'skip', 'dryrun', 'create_init').
Definition: simulation.h:84
void printLog()
Definition: simulation.cc:690
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:226
char _endTime[20]
Definition: simulation.h:62
virtual bool init()
Definition: fileservices.h:89
bool _do_postexec
Boolean set when a post-exec script must be executed.
Definition: simulation.h:94
void log(string message)
Write to the parameter logfile.
Definition: fileservices.cc:187
std::string _simElapsedTime
Definition: simulation.h:64
map< int, LifeCycleEvent * >::const_iterator LCE_ITER
Definition: basicsimulation.h:206
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:204
bool isMaster() const
Definition: MPImanager.h:127
unsigned long _random_seed
The startup random seed of the random generator.
Definition: simulation.h:82
static string ulong2str(const unsigned long i)
Writes an integer value into a string.
Definition: tstring.h:105
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.

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

604 {
605  //before calling that fction, first set the params and call init_components()!!
606 
607  LifeCycleEvent* event = this->get_current_event(name);
608 
609  if(event == NULL) {
610  error("SimRunner::run_event:event \"%s\" not found in set events!\n",name.c_str());
611  return false;
612  }
613 
614  if( !(event->get_paramset()->isSet()) ) return false;//highly unlikely!!
615 
616  event->execute();
617 
618  return true;
619 }
LifeCycleEvent * get_current_event(string &name)
Accessor to the list of current LCEs.
Definition: basicsimulation.cc:606
int error(const char *str,...)
Definition: output.cc:73
Base class of the Life Cycle Events, declares the LCE interface.
Definition: lifecycleevent.h:72
void SimRunner::runPostExec ( )

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

Referenced by run().

746 {
747  ifstream script(_postexec_script.c_str(),ios::in);
748  string cmd;
749 
750  if(!script.is_open()) {
751  error("could not open post simulation shell script!\n");
752  return;
753  }
754 
755  message("Executing shell script \"%s\" ",_postexec_script.c_str());
756  fflush(stdout);
757 
758  cmd = "sh " + _postexec_script + " " + _postexec_args;
759 
760  if(system(cmd.c_str()) < 0){
761  error("execution of `sh %s %s' failed: %s\n",_postexec_script.c_str(), _postexec_args.c_str(),strerror(errno));
762  return;
763  }
764 
765  message("...done\n");
766 }
void message(const char *message,...)
Definition: output.cc:40
int error(const char *str,...)
Definition: output.cc:73
std::string _postexec_script
The path to the script to be executed after last simulation.
Definition: simulation.h:90
std::string _postexec_args
Arguments to pass to the post-exec script.
Definition: simulation.h:92
void SimRunner::setCurrentGeneration ( unsigned int  gen)
inline

References Metapop::setCurrentGeneration().

Metapop * _thePop
Definition: simulation.h:58
unsigned int _currentGeneration
The current generation in the generation loop, starts at 1.
Definition: simulation.h:78
void setCurrentGeneration(unsigned int gen)
Definition: metapop.h:288
void SimRunner::setCurrentReplicate ( unsigned int  repl)
inline

References Metapop::setCurrentReplicate().

Metapop * _thePop
Definition: simulation.h:58
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:76
void setCurrentReplicate(unsigned int repl)
Definition: metapop.h:287
std::string SimRunner::setElapsedTime ( clock_t  time)

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

Parameters
timeelapsed time in ticks count

Referenced by printLog(), and run().

638 {
639  int e_time = time / CLOCKS_PER_SEC;
640  int hour = e_time / 3600;
641  int minute = ((e_time % 3600) / 60);
642  int sec = (e_time % 3600) % 60;
643 
644  std::ostringstream out(ios::out);
645 
646  out.fill('0');
647  out.width(2);
648  out<<hour<<":";
649  out.width(2);
650  out<<minute<<":";
651  out.precision(2);
652  out.fill('0');
653  out.width(2);
654  out<<sec;
655 
656  return(out.str());
657 }
void SimRunner::setForFirstGeneration ( )

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

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

Referenced by Replicate_LOOP().

545 {
547 
548  //reset temporal parameters/components to their first generation value/state
550 
551  //reset stat recording iterator to first occurrence
553 
554  //build metapopulation for the current replicate (build first generation)
556 }
StatServices _StatServices
Definition: simulation.h:100
UpdaterServices _ParamUpdaterManager
Definition: simulation.h:102
void setPopulation(unsigned int currentReplicate, unsigned int replicates)
Sets the population for the first generation of each replicates.
Definition: metapop.cc:682
Metapop * _thePop
Definition: simulation.h:58
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
unsigned int _currentReplicate
The current replicate in the replicate loop, starts at 1.
Definition: simulation.h:76
virtual void notify()
Definition: updaterservices.h:78
void resetCurrentOccurrence()
Resets the occurrence iterator to the beginning of the list of generation occurrences.
Definition: statservices.h:138
void setCurrentGeneration(unsigned int gen)
Definition: metapop.h:288
void SimRunner::setGenerations ( unsigned int  gen)
inline

References Metapop::setGenerations().

189 {_generations = gen; _thePop->setGenerations(gen);}
Metapop * _thePop
Definition: simulation.h:58
void setGenerations(unsigned int gen)
Definition: metapop.h:266
unsigned int _generations
Number of generations to iterate.
Definition: simulation.h:72
void SimRunner::setLifeCycle ( )

Sets the list of LifeCyckeEvent's currently active.

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

Referenced by init_components().

212 {
213  build_LifeCycle();
214 
215  for( LCE_ITER LCE = _LifeCycle.begin(); LCE != _LifeCycle.end(); LCE++)
216  LCE->second->init(_thePop);
217 }
void build_LifeCycle()
Selects the life cycle events that have their parameters set.
Definition: basicsimulation.cc:668
Metapop * _thePop
Definition: simulation.h:58
map< int, LifeCycleEvent * >::const_iterator LCE_ITER
Definition: basicsimulation.h:206
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:204
void SimRunner::setReplicates ( unsigned int  repl)
inline

References Metapop::setReplicates().

193 {_replicates = repl; _thePop->setReplicates(repl);}
Metapop * _thePop
Definition: simulation.h:58
void setReplicates(unsigned int repl)
Definition: metapop.h:268
unsigned int _replicates
Number of replicates to iterate.
Definition: simulation.h:74
void SimRunner::step ( unsigned int  nb_gen)

Iterates the life cycle.

Parameters
nb_gennumber of iterations to perform.

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

Referenced by Cycle().

624 {
625  LCE_ITER LCE = _LifeCycle.begin();
626 
627  while(LCE != _LifeCycle.end()) {
628  _currentRankInLifeCycle = LCE->second->get_rank();
629  LCE->second->execute();
630  _thePop->setCurrentAge(LCE->second);
631  LCE++;
632  }
633 }
int _currentRankInLifeCycle
The current rank in the life cycle, corresponds to the rank of the current LCE, before it executes...
Definition: simulation.h:80
Metapop * _thePop
Definition: simulation.h:58
void setCurrentAge(age_t age)
Sets the age flag.
Definition: metapop.h:293
map< int, LifeCycleEvent * >::const_iterator LCE_ITER
Definition: basicsimulation.h:206
map< int, LifeCycleEvent * > _LifeCycle
List of the selected life cycle events from the user defined input parameters.
Definition: basicsimulation.h:204

Member Data Documentation

unsigned int SimRunner::_currentGeneration
private

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

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

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

unsigned int SimRunner::_currentReplicate
private

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

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

bool SimRunner::_do_postexec
private

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

Referenced by init(), and run().

bool SimRunner::_doRun
private

Boolean set when not in dryrun mode.

Referenced by init(), and run().

char SimRunner::_endTime[20]
private

Referenced by printLog(), and run().

unsigned int SimRunner::_generations
private

Number of generations to iterate.

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

std::string SimRunner::_logfile
private

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

unsigned int SimRunner::_meanGenLength
private

Generation counter, for logging.

Referenced by printLog(), and Replicate_LOOP().

clock_t SimRunner::_meanReplElapsedTime
private

Clock counter, for logging.

Referenced by printLog(), and Replicate_LOOP().

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

std::string SimRunner::_modeArg
private

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

Referenced by init(), and run().

MPImanager* SimRunner::_my_mpi_manager
private

Referenced by Replicate_LOOP(), and run().

std::string SimRunner::_postexec_args
private

Arguments to pass to the post-exec script.

Referenced by init(), and runPostExec().

std::string SimRunner::_postexec_script
private

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

Referenced by init(), and runPostExec().

unsigned long SimRunner::_random_seed
private

The startup random seed of the random generator.

Referenced by init_random_seed(), and run().

unsigned int SimRunner::_replicates
private

Number of replicates to iterate.

Referenced by Cycle(), getReplicates(), init(), init_components(), printLog(), Replicate_LOOP(), and setForFirstGeneration().

std::string SimRunner::_simElapsedTime
private

Referenced by printLog(), and run().

char SimRunner::_startTime[20]
private

Referenced by printLog(), and run().


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

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR