35 #include <boost/property_tree/xml_parser.hpp> 36 #include <boost/optional/optional.hpp> 37 #include <boost/algorithm/string.hpp> 38 #include <boost/filesystem.hpp> 39 #include <boost/filesystem/fstream.hpp> 57 const string file_name_d = pt_config.get<
string>(
"run.disease.config");
59 if (!is_regular_file(file_path_d)) {
60 throw runtime_error(std::string(__func__) +
"> No file " + file_path_d.string());
62 read_xml(file_path_d.string(), pt_disease);
66 const string file_name_c = pt_config.get(
"run.age_contact_matrix_file",
"contact_matrix.xml");
68 if (!is_regular_file(file_path_c)) {
69 throw runtime_error(
string(__func__) +
"> No file " + file_path_c.string());
71 read_xml(file_path_c.string(), pt_contact);
74 return build(pt_config, pt_disease, pt_contact);
78 const ptree& pt_disease,
const ptree& pt_contact) {
79 auto sim = make_shared<Simulator>();
82 sim->m_config_pt = pt_config;
85 sim->m_track_index_case = pt_config.get(
"run.track_index_case",
false);
88 sim->m_num_threads = pt_config.get(
"run.num_threads", -1);
89 sim->m_parallel.setNumThreads(sim->m_num_threads);
92 sim->m_calendar = make_shared<Calendar>(pt_config);
95 const string l = pt_config.get<
string>(
"run.outputs.log.<xmlattr>.level",
"None");
97 string(__func__) +
"> Invalid input for LogMode.");
100 int seed = pt_config.get<
int>(
"run.regions.region.rng_seed");
101 sim->m_rng = make_shared<util::Random>(seed);
105 if (pt_config.get(
"run.regions.region.population",
"") ==
"") {
106 pt_pop.put(
"population.people", pt_config.get<
string>(
"run.regions.region.raw_population"));
109 pt_pop, xml_parser::trim_whitespace);
112 sim->m_config_pop = pt_pop;
115 unsigned int max_id = 0;
116 const Population& population = *(sim->m_population.get());
117 for (
auto& person: population) {
118 max_id = std::max(
uint(max_id),
uint(person.getId()));
121 sim->m_next_id = max_id + 1;
125 for (
auto& hh: sim->m_households) {
126 max_id = std::max(
uint(max_id),
uint(hh.getId()));
129 sim->m_next_hh_id = max_id + 1;
132 initializeDistricts(sim, pt_pop);
135 initializeFacilities(sim, pt_pop);
138 initializeClusters(sim, pt_pop);
141 sim->m_disease_profile.initialize(pt_config, pt_disease);
158 unsigned int max_id_households = 0U;
159 unsigned int max_id_school_clusters = 0U;
160 unsigned int max_id_work_clusters = 0U;
161 unsigned int max_id_primary_community = 0U;
162 unsigned int max_id_secondary_community = 0U;
165 for (
const auto& p : population) {
167 max_id_school_clusters = std::max(max_id_school_clusters, p.getClusterId(
ClusterType::School));
168 max_id_work_clusters = std::max(max_id_work_clusters, p.getClusterId(
ClusterType::Work));
170 max_id_secondary_community = std::max(max_id_secondary_community,
175 unsigned int cluster_id = 1;
177 string cluster_filename =
"";
180 boost::optional<const ptree&> cluster_locations_config = pt_config.get_child_optional(
"population.clusters");
181 if (cluster_locations_config) {
182 cluster_filename = pt_config.get<
string>(
"population.clusters");
185 map<pair<ClusterType, uint>,
GeoCoordinate> locations = initializeLocations(cluster_filename);
187 for (
size_t i = 0; i <= max_id_households; i++) {
188 sim->m_households.emplace_back(
192 for (
size_t i = 0; i <= max_id_school_clusters; i++) {
193 sim->m_school_clusters.emplace_back(
197 for (
size_t i = 0; i <= max_id_work_clusters; i++) {
198 sim->m_work_clusters.emplace_back(
202 for (
size_t i = 0; i <= max_id_primary_community; i++) {
207 for (
size_t i = 0; i <= max_id_secondary_community; i++) {
216 for (
auto& p: population) {
219 sim->m_households[hh_id].addPerson(&p);
223 sim->m_school_clusters[sc_id].addPerson(&p);
227 sim->m_work_clusters[wo_id].addPerson(&p);
230 if (primCom_id > 0) {
231 sim->m_primary_community[primCom_id].addPerson(&p);
235 sim->m_secondary_community[secCom_id].addPerson(&p);
242 boost::optional<const ptree&> districts_config = pt_config.get_child_optional(
"population.districts");
243 if (districts_config) {
244 string district_filename = pt_config.get<
string>(
"population.districts");
245 double influence_speed = pt_config.get<
double>(
"population.sphere_of_influence.<xmlattr>.speed");
246 double influence_minimum = pt_config.get<
double>(
"population.sphere_of_influence.<xmlattr>.min");
247 unsigned int influence_size = pt_config.get <
unsigned 248 int > (
"population.sphere_of_influence.<xmlattr>.size");
252 if (!is_regular_file(file_path)) {
253 throw runtime_error(
string(__func__)
254 +
">Districts file " + file_path.string() +
" not present. Aborting.");
258 boost::filesystem::ifstream districts_file;
259 districts_file.open(file_path.string());
260 if (!districts_file.is_open()) {
261 throw runtime_error(
string(__func__)
262 +
"> Error opening districts file " + file_path.string());
267 getline(districts_file, line);
269 while (getline(districts_file, line)) {
273 values[1].erase(values[1].begin());
274 values[1].erase(values[1].end() - 1);
277 auto search_duplicate = [&](
const District& district) {
return district.getName() == values[1]; };
278 if (find_if(sim->m_districts.cbegin(), sim->m_districts.cend(), search_duplicate) ==
279 sim->m_districts.cend()) {
280 sim->m_districts.push_back(
District(values[1],
285 StringUtils::fromString<double>(values[7]))));
293 map<pair<ClusterType, uint>,
GeoCoordinate> cluster_locations;
295 if (filename !=
"") {
298 if (!is_regular_file(file_path)) {
299 throw runtime_error(
string(__func__)
300 +
">Cluster location file " + file_path.string() +
" not present. Aborting.");
304 boost::filesystem::ifstream locations_file;
305 locations_file.open(file_path.string());
306 if (!locations_file.is_open()) {
307 throw runtime_error(
string(__func__)
308 +
"> Error opening cluster location file " + file_path.string());
313 getline(locations_file, line);
315 while (getline(locations_file, line)) {
319 StringUtils::fromString<unsigned int>(values[0]))] =
GeoCoordinate(
320 StringUtils::fromString<double>(values[2]),
321 StringUtils::fromString<double>(values[3]));
324 return cluster_locations;
330 if (!pt_config.get_child_optional(
"population.cities"))
333 for (
auto pot_city : pt_config.get_child(
"population.cities")) {
334 if (pot_city.first ==
"city") {
335 ptree city_tree = pot_city.second;
336 for (
auto it : city_tree) {
337 if (it.first ==
"airport") {
339 auto find_district = [&](
const District& district) {
return district.getName() ==
340 pot_city.second.get<
string>(
343 auto le_city = find_if(sim->m_districts.begin(), sim->m_districts.end(), find_district);
346 if (le_city != sim->m_districts.end()) {
347 le_city->addFacility(it.second.get<
string>(
"<xmlattr>.name"));
static void initializeDistricts(std::shared_ptr< Simulator > sim, const boost::property_tree::ptree &pt_config)
Initialize the districts, duplicate city names are ignored (only the first occurrence is counted) ...
Interface for install directory queries.
static void addContactProfile(ClusterType cluster_type, const ContactProfile &profile)
Add contact profile.
bool isLogMode(const string &s)
Check whether string is name of LogMode value.
Header for the Infector class.
Header file for the Calendar class.
Time Dependent Person DataType.
Conversion from or to string.
static boost::filesystem::path getDataDir()
Utility method: get path to the directory for data files.
Utilities for the project.
A district is either a city or a village (currently, there is no difference between city and village)...
Header for the SimulatorBuilder class.
ClusterType toClusterType(const string &s)
Converts a string with name to ClusterType value.
Header file for the core Population class.
Container for persons in population.
static std::shared_ptr< Population > build(const boost::property_tree::ptree &pt_config, const boost::property_tree::ptree &pt_disease, const boost::property_tree::ptree &pt_pop, util::Random &rng)
Initializes a Population: add persons, set immunity, seed infection.
Definition of ClusterType.
static void initializeClusters(std::shared_ptr< Simulator > sim, const boost::property_tree::ptree &pt_config)
Initialize the clusters.
LogMode toLogMode(const string &s)
Converts a string with name to LogMode value.
Represents a location for social contacts, an group of people.
static std::shared_ptr< Simulator > build(const boost::property_tree::ptree &pt_config)
Build simulator.
static std::vector< std::string > split(const std::string &str, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
static std::map< std::pair< ClusterType, uint >, util::GeoCoordinate > initializeLocations(std::string filename)
Initialize the locations (read the from the given file) and return them If the filename is ""...
Header for the core Cluster class.
static void initializeFacilities(std::shared_ptr< Simulator > sim, const boost::property_tree::ptree &pt_config)
Initialize the facilities, duplicate facility names are ignored (only the first occurrence is counted...