Stride Reference Manual  - generated for commit 9643b11
Population.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 "Population.h"
22 
23 #include "disease/Health.h"
24 #include "pop/DefaultPopBuilder.h"
25 #include "pop/GeoPopBuilder.h"
26 #include "pop/ImportPopBuilder.h"
27 #include "util/Assert.h"
28 #include "util/FileSys.h"
29 #include "util/LogUtils.h"
30 #include "util/RnMan.h"
31 #include "util/RunConfigManager.h"
32 #include "util/SegmentedVector.h"
33 #include "util/StringUtils.h"
34 
35 #include <boost/property_tree/ptree.hpp>
36 #include <utility>
37 
38 using namespace boost::property_tree;
39 using namespace std;
40 using namespace stride::util;
41 using namespace stride::ContactType;
42 
43 namespace stride {
44 
45 Population::Population() : m_pool_sys(), m_contact_logger(), m_geo_grid(this) {}
46 
47 std::shared_ptr<Population> Population::Create(const boost::property_tree::ptree& config, util::RnMan rnMan,
48  std::shared_ptr<spdlog::logger> strideLogger)
49 {
50  if (!strideLogger) {
51  strideLogger = LogUtils::CreateNullLogger("Population_logger");
52  }
53 
54  // --------------------------------------------------------------
55  // Create empty population & and give it a ContactLogger.
56  // --------------------------------------------------------------
57  const auto pop = Create();
58  if (config.get<bool>("run.contact_output_file", true)) {
59  const auto prefix = config.get<string>("run.output_prefix");
60  const auto logPath = FileSys::BuildPath(prefix, "contact_log.txt");
61  pop->RefContactLogger() = LogUtils::CreateRotatingLogger("contact_logger", logPath.string());
62  pop->RefContactLogger()->set_pattern("%v");
63  strideLogger->info("Contact logging requested; logger set up.");
64  } else {
65  pop->RefContactLogger() = LogUtils::CreateNullLogger("contact_logger");
66  strideLogger->info("No contact logging requested.");
67  }
68 
69  // -----------------------------------------------------------------------------------------
70  // Build population.
71  // -----------------------------------------------------------------------------------------
72  std::string pop_type = config.get<std::string>("run.population_type", "default");
73  if (pop_type == "import") {
74  strideLogger->info("Invoking ImportPopBuilder.");
75  ImportPopBuilder(config, rnMan, strideLogger).Build(pop);
76  } else if (pop_type == "generate") {
77  strideLogger->info("Invoking GeoPopBuilder.");
78  GeoPopBuilder(config, rnMan, strideLogger).Build(pop);
79  } else {
80  strideLogger->info("Invoking DefaultPopBuilder.");
81  DefaultPopBuilder(config, rnMan, strideLogger).Build(pop);
82  }
83 
84  // -----------------------------------------------------------------------------------------
85  // Done.
86  // -----------------------------------------------------------------------------------------
87  return pop;
88 }
89 
90 std::shared_ptr<Population> Population::Create(const string& configString, util::RnMan rnMan,
91  std::shared_ptr<spdlog::logger> stride_logger)
92 {
93  return Create(RunConfigManager::FromString(configString), std::move(rnMan), std::move(stride_logger));
94 }
95 
96 std::shared_ptr<Population> Population::Create()
97 {
98  // --------------------------------------------------------------
99  // Create (empty) population and return it
100  // --------------------------------------------------------------
101  struct make_shared_enabler : public Population
102  {
103  };
104  auto r = make_shared<make_shared_enabler>();
105  return r;
106 }
107 
108 Person* Population::CreatePerson(unsigned int id, double age, unsigned int householdId, unsigned int k12SchoolId,
109  unsigned int college, unsigned int workId, unsigned int primaryCommunityId,
110  unsigned int secondaryCommunityId)
111 {
112  return emplace_back(id, age, householdId, k12SchoolId, college, workId, primaryCommunityId,
113  secondaryCommunityId);
114 }
115 
116 unsigned int Population::GetInfectedCount() const
117 {
118  unsigned int total{0U};
119  for (const auto& p : *this) {
120  const auto& h = p.GetHealth();
121  total += h.IsInfected() || h.IsRecovered();
122  }
123  return total;
124 }
125 
126 } // namespace stride
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Generates a synthetic population.
Initialize populations.
static std::shared_ptr< Population > Create()
Create an empty Population, used in gengeopop.
Definition: Population.cpp:96
Utilities for the project.
unsigned int GetInfectedCount() const
Get the cumulative number of cases.
Definition: Population.cpp:116
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Build Population and return it afterwards.
Interface and implementation for SegmentedVector class.
static std::shared_ptr< spdlog::logger > CreateRotatingLogger(const std::string &logger_name, const std::string &file_name)
Return a (not-yet-registered) rotating logger, without registering it.
Definition: LogUtils.cpp:88
Initialize populations.
STL namespace.
Header for the Health class.
Interface of RnMan.
Key Data structure: container for (a) all individuals in the population (b) the ContactPoolSys wchich...
Definition: Population.h:47
Logging (spdlog) utilities.
Person * CreatePerson(unsigned int id, double age, unsigned int householdId, unsigned int k12SchoolId, unsigned int collegeId, unsigned int workId, unsigned int primaryCommunityId, unsigned int secondaryCommunityId)
Create Person in the population.
Definition: Population.cpp:108
static filesys::path BuildPath(const std::string &output_prefix, const std::string &filename)
Interpret prefix (directory or filename prefix) and return appropriate path.
Definition: FileSys.cpp:52
Initializes Population objects.
Definition of .
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Creates a population by importing it.
Builds poulation of a geographic grid.
Definition: GeoPopBuilder.h:41
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.
Initialize populations.
Miscellaneous string utilities.
Person * emplace_back(Args &&...args)
Constructs element in-place at the end.
Interface for install directory queries.
Store and handle person data.
Definition: Person.h:34
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
static std::shared_ptr< spdlog::logger > CreateNullLogger(const std::string &logger_name="null_logger")
Return a (not-yet-registered) null logger, without registering it.
Definition: LogUtils.cpp:71
Initializes Population objects.
Namespace to manage types of ContactPool.
Definition: ContactType.cpp:28