Stride Reference Manual  - generated for commit 9643b11
SimBuilder.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 "SimBuilder.h"
22 
23 #include "contact/ContactType.h"
24 #include "contact/InfectorMap.h"
25 #include "disease/DiseaseSeeder.h"
26 #include "disease/HealthSeeder.h"
27 #include "pop/SurveySeeder.h"
28 #include "sim/Sim.h"
29 #include "util/FileSys.h"
30 #include "util/RnMan.h"
31 
32 namespace stride {
33 
34 using namespace boost::property_tree;
35 using namespace std;
36 using namespace util;
37 using namespace ContactType;
38 
39 SimBuilder::SimBuilder(const ptree& config) : m_config(config) {}
40 
41 shared_ptr<Sim> SimBuilder::Build(shared_ptr<Sim> sim, shared_ptr<Population> pop, RnMan rnMan)
42 {
43  // --------------------------------------------------------------
44  // Read config info and setup random number manager
45  // --------------------------------------------------------------
46  sim->m_config = m_config;
47  sim->m_population = std::move(pop);
48  sim->m_track_index_case = m_config.get<bool>("run.track_index_case");
49  sim->m_adaptive_symptomatic_behavior = m_config.get<bool>("run.adaptive_symptomatic_behavior", true);
50  sim->m_num_threads = m_config.get<unsigned int>("run.num_threads");
51  sim->m_calendar = make_shared<Calendar>(m_config);
52  sim->m_contact_log_mode = ContactLogMode::ToMode(m_config.get<string>("run.contact_log_level", "None"));
53  sim->m_rn_man = std::move(rnMan);
54 
55  // --------------------------------------------------------------
56  // Contact handlers, each with generator bound to different
57  // random engine stream) and infector.
58  // --------------------------------------------------------------
59  for (unsigned int i = 0; i < sim->m_num_threads; i++) {
60  auto gen = sim->m_rn_man.GetUniform01Generator(i);
61  sim->m_handlers.emplace_back(ContactHandler(gen));
62  }
63  const auto& select = make_tuple(sim->m_contact_log_mode, sim->m_track_index_case);
64  sim->m_infector = InfectorMap().at(select);
65 
66  // --------------------------------------------------------------
67  // Initialize the age-related contact profiles.
68  // --------------------------------------------------------------
69  const auto ageContactPt = ReadAgeContactPtree();
70  for (Id typ : IdList) {
71  sim->m_contact_profiles[typ] = AgeContactProfile(typ, ageContactPt);
72  }
73 
74  // --------------------------------------------------------------
75  // Initialize the transmission profile (fixes transmission probability).
76  // --------------------------------------------------------------
77  const auto diseasePt = ReadDiseasePtree();
78  sim->m_transmission_profile.Initialize(m_config, diseasePt);
79 
80  // --------------------------------------------------------------
81  // Initialize the public health agency (fixes detection probability).
82  // --------------------------------------------------------------
83  const double detection_probability = m_config.get<double>("run.case_detection_probability", 0.0);
84  sim->m_public_health_agency.Initialize(detection_probability);
85 
86  // --------------------------------------------------------------
87  // Seed the population with health data.
88  // --------------------------------------------------------------
89  HealthSeeder(diseasePt).Seed(sim->m_population, sim->m_handlers);
90 
91  // --------------------------------------------------------------
92  // Seed population with immunity/vaccination/infection.
93  // --------------------------------------------------------------
94  DiseaseSeeder(m_config, sim->m_rn_man).Seed(sim->m_population);
95 
96  // --------------------------------------------------------------
97  // Seed population with survey participants.
98  // --------------------------------------------------------------
99  SurveySeeder(m_config, sim->m_rn_man).Seed(sim->m_population);
100 
101  // --------------------------------------------------------------
102  // Done.
103  // --------------------------------------------------------------
104  return sim;
105 }
106 
108 {
109  const auto fn = m_config.get<string>("run.age_contact_matrix_file", "contact_matrix.xml");
110  const auto fp = m_config.get<bool>("run.use_install_dirs") ? FileSys::GetDataDir() /= fn : filesys::path(fn);
111  return FileSys::ReadPtreeFile(fp);
112 }
113 
115 {
116  const auto fn = m_config.get<string>("run.disease_config_file");
117  const auto fp = m_config.get<bool>("run.use_install_dirs") ? FileSys::GetDataDir() /= fn : filesys::path(fn);
118  return FileSys::ReadPtreeFile(fp);
119 }
120 
121 } // namespace stride
void Seed(const std::shared_ptr< Population > &pop, std::vector< ContactHandler > &handlers)
Seeds the population with Health data.
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
boost::property_tree::ptree ReadAgeContactPtree()
Get the contact configuration data.
Definition: SimBuilder.cpp:107
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
Seeds the population with survey participants.
Definition: SurveySeeder.h:37
Mechanism to select the appropriate Infector template to execute.
Definition: InfectorMap.h:39
Id ToMode(const string &s)
Converts a string with name to LogMode value.
SimBuilder(const boost::property_tree::ptree &config)
Initializing SimBuilder.
Definition: SimBuilder.cpp:39
Header for the DiseaseSeeder class.
Seeds population w.r.t immunity (natural immunity, vaccination, ...) and infection.
Definition: DiseaseSeeder.h:38
Header for the SimBuilder class.
Processes the contacts between persons and determines whether transmission occurs.
std::shared_ptr< Sim > Build(std::shared_ptr< Sim > sim, std::shared_ptr< Population > pop, util::RnMan rnMan)
Build the simulator and return it afterwards.
Definition: SimBuilder.cpp:41
static boost::property_tree::ptree ReadPtreeFile(const filesys::path &f_p)
Read ptree from file at path.
Definition: FileSys.cpp:208
STL namespace.
Interface of RnMan.
Definition of ContactPool Id Type.
boost::property_tree::ptree m_config
Run config in ptree.
Definition: SimBuilder.h:58
Header file for the SurveySeeder class.
boost::property_tree::ptree ReadDiseasePtree()
Get the disease configuration data.
Definition: SimBuilder.cpp:114
Header for the Simulator class.
Interface for install directory queries.
std::shared_ptr< Population > Seed(std::shared_ptr< Population > pop)
Seeds the population with survey participants.
Contact rates as a function of age.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
void Seed(std::shared_ptr< Population > pop)
Build the simulator.
Header file for the HealthSeeder.
Header for the InfectorMap.
Seeds the population with Health data.
Definition: HealthSeeder.h:36