Stride Reference Manual  - generated for commit 9643b11
SimRunner.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "SimRunner.h"
22 
23 #include "calendar/Calendar.h"
24 #include "pop/Population.h"
25 #include "sim/Sim.h"
26 
27 using namespace stride::sim_event;
28 using namespace boost::property_tree;
29 using namespace std;
30 
31 namespace stride {
32 
33 SimRunner::SimRunner(const ptree& configPt, shared_ptr<Sim> sim)
34  : m_clock("total_clock"), m_config(configPt), m_sim(std::move(sim))
35 {
36  Notify(Id::SetupBegin);
37  m_clock.Start();
38  Notify(Id::SetupEnd);
39 }
40 
41 void SimRunner::Run(unsigned int numSteps)
42 {
43  // Saveguard against repeatedly firing AtStart, so bypass if numSteps == 0.
44  if (numSteps != 0U) {
45  // Prelims.
46  m_clock.Start();
47  const auto numDays = m_config.get<unsigned int>("run.num_days");
48 
49  // We are AtStart: no steps have taken yet, so signal AtStart.
50  if (m_sim->GetCalendar()->GetSimulationDay() == 0) {
51  Notify(Id::AtStart);
52  }
53 
54  // Take numSteps but do not go beyond numDays.
55  for (unsigned int i = 0; i < numSteps; i++) {
56  // This is not the last step: execute and signal Stepped.
57  if (m_sim->GetCalendar()->GetSimulationDay() < numDays - 1) {
58  m_sim->TimeStep();
59  Notify(Id::Stepped);
60  // This is the last step so execute and afterwards signal Stepped and Finished
61  } else if (m_sim->GetCalendar()->GetSimulationDay() == numDays - 1) {
62  m_sim->TimeStep();
63  Notify(Id::Stepped);
64  Notify(Id::Finished);
65  break;
66  // We are apparently already at the end of the numDays so nothing to do or signal.
67  } else {
68  break;
69  }
70  }
71 
72  m_clock.Stop();
73  }
74 }
75 
76 void SimRunner::Run() { Run(m_config.get<unsigned int>("run.num_days")); }
77 
78 } // namespace stride
Namespace to manage events signalled by the SimRunner.
Definition: Id.cpp:27
SimRunner(const boost::property_tree::ptree &configPt, std::shared_ptr< Sim > sim)
Initialization with property tree.
Definition: SimRunner.cpp:33
Stopwatch & Start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:50
Stopwatch & Stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:60
void Run()
Run simulator for as many steps/days as indicated in config.
Definition: SimRunner.cpp:76
STL namespace.
std::shared_ptr< Sim > m_sim
Simulator object.
Definition: SimRunner.h:72
Header file for the core Population class.
util::Stopwatch m_clock
Stopwatch for timing the computation.
Definition: SimRunner.h:70
Header for the Simulator class.
Header file for the Calendar class.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
boost::property_tree::ptree m_config
Ptree with configuration.
Definition: SimRunner.h:71
Header for the SimRunner class.