Stride Reference Manual  - generated for commit 9643b11
Sim.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 "Sim.h"
22 
24 #include "contact/ContactType.h"
25 #include "contact/InfectorExec.h"
26 #include "pop/Population.h"
27 #include "sim/SimBuilder.h"
28 #include "util/RunConfigManager.h"
29 
30 #include <omp.h>
31 #include <utility>
32 
33 namespace stride {
34 
35 using namespace std;
36 using namespace util;
37 using namespace ContactLogMode;
38 
40  : m_config(), m_contact_log_mode(Id::None), m_num_threads(1U), m_track_index_case(false),
41  m_adaptive_symptomatic_behavior(false), m_calendar(nullptr), m_contact_profiles(), m_handlers(), m_infector(),
42  m_population(nullptr), m_rn_man(), m_transmission_profile(), m_public_health_agency()
43 {
44 }
45 
46 std::shared_ptr<Sim> Sim::Create(const boost::property_tree::ptree& config, shared_ptr<Population> pop,
47  util::RnMan rnMan)
48 {
49  struct make_shared_enabler : public Sim
50  {
51  explicit make_shared_enabler() : Sim() {}
52  };
53  shared_ptr<Sim> sim = make_shared<make_shared_enabler>();
54  SimBuilder(config).Build(sim, std::move(pop), std::move(rnMan));
55  return sim;
56 }
57 
58 std::shared_ptr<Sim> Sim::Create(const string& configString, std::shared_ptr<Population> pop, util::RnMan rnMan)
59 {
60  return Create(RunConfigManager::FromString(configString), std::move(pop), std::move(rnMan));
61 }
62 
64 {
65  // Logic where you compute (on the basis of input/config for initial day or on the basis of
66  // number of sick persons, duration of epidemic etc) what kind of DaysOff scheme you apply.
67  const auto daysOff = std::make_shared<DaysOffStandard>(m_calendar);
68  const bool isWorkOff = daysOff->IsWorkOff();
69  const bool isSchoolOff = daysOff->IsSchoolOff();
70 
71  // To be used in update of population & contact pools.
72  Population& population = *m_population;
73  auto& poolSys = population.RefPoolSys();
74  auto contactLogger = population.RefContactLogger();
75  const auto simDay = m_calendar->GetSimulationDay();
76  const auto& infector = *m_infector;
77 
78 #pragma omp parallel num_threads(m_num_threads)
79  {
80  // Update health status and presence/absence in contact pools
81  // depending on health status, work/school day and whether
82  // we want to track index cases without adaptive behavior
83 #pragma omp for schedule(static)
84  for (size_t i = 0; i < population.size(); ++i) {
85  population[i].Update(isWorkOff, isSchoolOff, m_adaptive_symptomatic_behavior);
86  }
87 
88  // after the health update, let the public health agency perform their work
90 
91  // Infector updates individuals for contacts & transmission within each pool.
92  // Skip pools with id = 0, because it means Not Applicable.
93  const auto thread_num = static_cast<unsigned int>(omp_get_thread_num());
94  for (auto typ : ContactType::IdList) {
95  if ((typ == ContactType::Id::Workplace && isWorkOff) ||
96  (typ == ContactType::Id::K12School && isSchoolOff) ||
97  (typ == ContactType::Id::College && isSchoolOff)) {
98  continue;
99  }
100 #pragma omp for schedule(static)
101  for (size_t i = 1; i < poolSys.RefPools(typ).size(); i++) { // NOLINT
102  infector(poolSys.RefPools(typ)[i], m_contact_profiles[typ], m_transmission_profile,
103  m_handlers[thread_num], simDay, contactLogger);
104  }
105  }
106  }
107 
108  m_population->RefContactLogger()->flush();
109  m_calendar->AdvanceDay();
110 }
111 
112 } // namespace stride
TransmissionProfile m_transmission_profile
Profile of disease.
Definition: Sim.h:98
AgeContactProfiles m_contact_profiles
Contact profiles w.r.t age.
Definition: Sim.h:92
Sim()
Constructor for empty Simulator.
Definition: Sim.cpp:39
InfectorExec * m_infector
Executes contacts/transmission loops in contact pool.
Definition: Sim.h:94
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
std::shared_ptr< Calendar > m_calendar
Management of calendar.
Definition: Sim.h:91
Header for the SimBuilder class.
Header for the InfectorExec class.
PublicHealthAgency m_public_health_agency
Agency to implement reactive strategies.
Definition: Sim.h:99
static std::shared_ptr< Sim > Create(const boost::property_tree::ptree &config, std::shared_ptr< Population > pop, util::RnMan rnMan)
Create Sim initialized by the configuration in property tree and population.
void Exec(std::shared_ptr< Population > pop, util::RnMan &rnManager, unsigned short int sim_day)
Execute.
STL namespace.
ContactPoolSys & RefPoolSys()
Reference the ContactPoolSys of the Population.
Definition: Population.h:80
Simulator can time step and reveal some of the key data.
Definition: Sim.h:46
std::vector< ContactHandler > m_handlers
Contact handlers (rng & rates).
Definition: Sim.h:93
DaysOffStandard class.
Key Data structure: container for (a) all individuals in the population (b) the ContactPoolSys wchich...
Definition: Population.h:47
Definition of ContactPool Id Type.
friend class SimBuilder
Definition: Sim.h:82
void TimeStep()
Run one time step, computing full simulation (default) or only index case.
Definition: Sim.cpp:63
bool m_adaptive_symptomatic_behavior
Should symptomatic cases stay home?
Definition: Sim.h:89
util::RnMan m_rn_man
Random number generation management.
Definition: Sim.h:96
Header file for the core Population class.
static boost::property_tree::ptree FromString(const std::string &s)
Reconstitute property tree from string representation.
Produce run config ptree.
std::shared_ptr< spdlog::logger > & RefContactLogger()
Return the contactlogger.
Definition: Population.h:77
std::size_t size() const
Returns the number of elements.
Header for the Simulator class.
std::shared_ptr< Population > m_population
Pointer to the Population.
Definition: Sim.h:95
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28