Stride Reference Manual  - generated for commit 9643b11
HealthSeeder.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 "HealthSeeder.h"
22 
23 #include "Health.h"
24 #include "contact/ContactHandler.h"
25 #include "pop/Population.h"
26 #include "util/Assert.h"
27 #include <boost/property_tree/ptree.hpp>
28 #include <omp.h>
29 
30 using namespace boost::property_tree;
31 using namespace stride::util;
32 using namespace std;
33 
34 namespace stride {
35 
36 HealthSeeder::HealthSeeder(const boost::property_tree::ptree& diseasePt)
37  : m_start_symptomatic(), m_time_asymptomatic(), m_time_infectious(), m_time_symptomatic()
38 {
39  GetDistribution(m_start_symptomatic, diseasePt, "disease.start_symptomatic");
40  GetDistribution(m_time_asymptomatic, diseasePt, "disease.time_asymptomatic");
41  GetDistribution(m_time_infectious, diseasePt, "disease.time_infectious");
42  GetDistribution(m_time_symptomatic, diseasePt, "disease.time_symptomatic");
43 
44  AssertThrow((abs(m_start_symptomatic.back() - 1.0) < 1.e-10), "Error in start_symptomatic", nullptr);
45  AssertThrow((abs(m_time_asymptomatic.back() - 1.0) < 1.e-10), "Error in time_asymptomatic", nullptr);
46  AssertThrow((abs(m_time_infectious.back() - 1.0) < 1.e-10), "Error in time_infectious", nullptr);
47  AssertThrow((abs(m_time_symptomatic.back() - 1.0) < 1.e-10), "Error in time_symptomatic", nullptr);
48 }
49 
50 void HealthSeeder::GetDistribution(vector<double>& distribution, const ptree& rootPt, const string& xmlTag)
51 {
52  const boost::property_tree::ptree& subtree = rootPt.get_child(xmlTag);
53  for (const auto& tree : subtree) {
54  distribution.push_back(tree.second.get<double>(""));
55  }
56 }
57 
58 unsigned short int HealthSeeder::Sample(const vector<double>& distribution, double random01)
59 {
60  auto ret = static_cast<unsigned short int>(distribution.size());
61  for (unsigned short int i = 0; i < distribution.size(); i++) {
62  if (random01 <= distribution[i]) {
63  ret = i;
64  break;
65  }
66  }
67  return ret;
68 }
69 
70 void HealthSeeder::Seed(const std::shared_ptr<stride::Population>& pop, vector<ContactHandler>& handlers)
71 {
72  auto& population = *pop;
73 
74 #pragma omp parallel num_threads(handlers.size())
75  {
76  auto& gen01 = handlers[static_cast<size_t>(omp_get_thread_num())];
77 #pragma omp for
78  for (size_t i = 0; i < population.size(); ++i) {
79  const auto startSymptomatic = Sample(m_start_symptomatic, gen01());
80  const auto startInfectiousness = startSymptomatic - Sample(m_time_asymptomatic, gen01());
81  const auto timeInfectious = Sample(m_time_infectious, gen01());
82  const auto timeSymptomatic = Sample(m_time_symptomatic, gen01());
83  population[i].GetHealth() =
84  Health(startInfectiousness, startSymptomatic, timeInfectious, timeSymptomatic);
85  }
86  }
87 }
88 
89 } // namespace stride
void Seed(const std::shared_ptr< Population > &pop, std::vector< ContactHandler > &handlers)
Seeds the population with Health data.
Utilities for the project.
void GetDistribution(std::vector< double > &distribution, const boost::property_tree::ptree &rootPt, const std::string &xmlTag)
Utility method to etract distribution from data in ptree.
std::vector< double > m_time_symptomatic
Definition: HealthSeeder.h:57
Holds a person&#39;s health data.
Definition: Health.h:38
std::vector< double > m_start_symptomatic
Definition: HealthSeeder.h:54
#define AssertThrow(CONDITION, MESSAGE, LOGGER)
Definition: Assert.h:68
Header for the ContactHandler class.
unsigned short int Sample(const std::vector< double > &distribution, double random01)
Sample for each of the health data item individually.
std::vector< double > m_time_asymptomatic
Definition: HealthSeeder.h:55
STL namespace.
Header for the Health class.
Definition of .
Header file for the core Population class.
std::vector< double > m_time_infectious
Definition: HealthSeeder.h:56
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Header file for the HealthSeeder.