Stride Reference Manual  - generated for commit 9643b11
RunConfigManager.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, Kuylen E, Willem L, Broeckhove J
14  */
20 #include "RunConfigManager.h"
21 
22 #include "util/ConfigInfo.h"
23 #include "util/FileSys.h"
24 
25 #include <boost/property_tree/ptree.hpp>
26 #include <boost/property_tree/xml_parser.hpp>
27 #include <initializer_list>
28 #include <map>
29 #include <sha1.h>
30 #include <sstream>
31 #include <string>
32 
33 using namespace boost::property_tree;
34 using namespace boost::property_tree::xml_parser;
35 using namespace std;
36 
37 namespace stride {
38 namespace util {
39 
40 void RunConfigManager::CleanConfigFile(ptree pt)
41 {
42  pt.sort();
43  const string sha1 = RunConfigManager::ToShortSha1(pt);
44  const string fName = sha1 + ".xml";
45  cout << "Rewriting config to file " << fName << " in current directory." << endl;
46  FileSys::WritePtreeFile(fName, pt);
47 }
48 
49 ptree RunConfigManager::Create(const std::string& configName)
50 {
51  static map<string, string (*)()> creators{
52  // clang-format off
53  make_pair("BenchInfluenza", &CreateBenchInfluenza),
54  make_pair("BenchMeasles", &CreateBenchMeasles),
55  make_pair("Default", &CreateDefault),
56  make_pair("TestsInfluenza", &CreateTestsInfluenza),
57  make_pair("TestsMeasles", &CreateTestsMeasles)
58  }; // clang-format on
59  return FromString(creators.at(configName)());
60 }
61 
62 void RunConfigManager::AddGeoPopConfig(ptree& pt)
63 {
64  const string geopop_str = R"###(
65 <run>
66  <population_file>gengeopop.proto</population_file>
67  <population_type>generate</geopopulation_type>
68  <geopop_gen>
69  <cities_file>flanders_cities.csv</cities_file>
70  <commuting_file>flanders_commuting.csv</commuting_file>
71  <fraction_college_commuters>0.5</fraction_college_commuters>
72  <fraction_workplace_commuters>0.5</fraction_workplace_commuters>
73  <household_file>households_flanders.csv</household_file>
74  <participation_college>0.5</participation_college>
75  <particpation_workplace>0.75</particpation_workplace>
76  <population_size>600000</population_size>
77  </geopop_gen>
78 </run>
79  )###";
80  ptree geopop_ptree = FromString(geopop_str);
81  pt.put_child("run.population_file", geopop_ptree.get_child("run.population_file"));
82  pt.put_child("run.population_type", geopop_ptree.get_child("run.population_type"));
83  pt.put_child("run.geopop_gen", geopop_ptree.get_child("run.geopop_gen"));
84 }
85 
86 string RunConfigManager::CreateBenchInfluenza()
87 {
88  return R"###(
89 <?xml version="1.0" encoding="utf-8"?>
90 <run>
91  <age_contact_matrix_file>contact_matrix_flanders_subpop.xml</age_contact_matrix_file>
92  <contact_log_level>None</contact_log_level>
93  <contact_output_file>false</contact_output_file>
94  <disease_config_file>disease_influenza.xml</disease_config_file>
95  <holidays_file>holidays_none.json</holidays_file>
96  <immunity_link_probability>0</immunity_link_probability>
97  <immunity_profile>Random</immunity_profile>
98  <immunity_rate>0</immunity_rate>
99  <num_days>10</num_days>
100  <num_participants_survey>10</num_participants_survey>
101  <num_threads>1</num_threads>
102  <output_prefix></output_prefix>
103  <population_file>pop_flanders600.csv</population_file>
104  <population_type>default</geopopulation_type>
105  <rng_seed>1097253,2387652,9963540,4730214</rng_seed>
106  <r0>1.5</r0>
107  <seeding_rate>0.001</seeding_rate>
108  <seeding_age_min>1</seeding_age_min>
109  <seeding_age_max>99</seeding_age_max>
110  <start_date>2017-01-01</start_date>
111  <stride_log_level>info</stride_log_level>
112  <track_index_case>false</track_index_case>
113  <use_install_dirs>true</use_install_dirs>
114  <vaccine_profile>None</vaccine_profile>
115 </run>
116  )###";
117 }
118 
119 string RunConfigManager::CreateBenchMeasles()
120 {
121  return R"###(
122 <?xml version="1.0" encoding="utf-8"?>
123 <run>
124  <age_contact_matrix_file>contact_matrix_flanders_subpop.xml</age_contact_matrix_file>
125  <contact_log_level>None</contact_log_level>
126  <contact_output_file>false</contact_output_file>
127  <disease_config_file>disease_measles.xml</disease_config_file>
128  <holidays_file>holidays_none.json</holidays_file>
129  <immunity_profile>None</immunity_profile>
130  <immunity_rate>0.01</immunity_rate>
131  <num_days>30</num_days>
132  <num_participants_survey>10</num_participants_survey>
133  <num_threads>1</num_threads>
134  <output_prefix></output_prefix>
135  <population_file>pop_flanders600.csv</population_file>
136  <population_type>default</geopopulation_type>
137  <rng_seed>1097253,2387652,9963540,4730214</rng_seed>
138  <r0>16</r0>
139  <seeding_age_max>99</seeding_age_max>
140  <seeding_age_min>1</seeding_age_min>
141  <seeding_rate>0.05</seeding_rate>
142  <start_date>2017-01-01</start_date>
143  <stride_log_level>info</stride_log_level>
144  <track_index_case>false</track_index_case>
145  <use_install_dirs>true</use_install_dirs>
146  <vaccine_link_probability>0</vaccine_link_probability>
147  <vaccine_profile>Random</vaccine_profile>
148  <vaccine_rate>0</vaccine_rate>
149 </run>
150  )###";
151 }
152 
153 string RunConfigManager::CreateDefault()
154 {
155  return R"###(
156 <?xml version="1.0" encoding="utf-8"?>
157 <run>
158  <age_contact_matrix_file>contact_matrix_flanders_subpop.xml</age_contact_matrix_file>
159  <contact_log_level>Transmissions</contact_log_level>
160  <contact_output_file>true</contact_output_file>
161  <disease_config_file>disease_measles.xml</disease_config_file>
162  <global_information_policy>NoGlobalInformation</global_information_policy>
163  <holidays_file>holidays_none.json</holidays_file>
164  <immunity_profile>None</immunity_profile>
165  <immunity_rate>0.8</immunity_rate>
166  <num_days>30</num_days>
167  <num_participants_survey>10</num_participants_survey>
168  <num_threads>1</num_threads>
169  <output_adopted>false</output_adopted>
170  <output_cases>true</output_cases>
171  <output_persons>false</output_persons>
172  <output_prefix></output_prefix>
173  <output_summary>false</output_summary>
174  <population_file>pop_flanders600.csv</population_file>
175  <population_type>default</geopopulation_type>
176  <rng_seed>1097253,2387652,9963540,4730214</rng_seed>
177  <r0>11</r0>
178  <seeding_age_max>99</seeding_age_max>
179  <seeding_age_min>1</seeding_age_min>
180  <seeding_rate>0.003</seeding_rate>
181  <start_date>2017-01-01</start_date>
182  <stride_log_level>info</stride_log_level>
183  <track_index_case>false</track_index_case>
184  <use_install_dirs>true</use_install_dirs>
185  <vaccine_link_probability>0</vaccine_link_probability>
186  <vaccine_profile>Random</vaccine_profile>
187  <vaccine_rate>0.8</vaccine_rate>
188 </run>
189  )###";
190 }
191 
192 vector<unsigned int> RunConfigManager::CreateNumThreads(unsigned int maxNum)
193 {
194  maxNum = max(maxNum, ConfigInfo::NumberAvailableThreads());
195  vector<unsigned int> num{1U};
196 
197  if (4 > maxNum && maxNum >= 2) {
198  num.push_back(2U);
199  }
200  if (8 > maxNum && maxNum >= 4) {
201  num.push_back(2U);
202  num.push_back(4U);
203  }
204  if (12 > maxNum && maxNum >= 8) {
205  num.push_back(2U);
206  num.push_back(4U);
207  num.push_back(8U);
208  }
209  if (16 > maxNum && maxNum >= 12) {
210  num.push_back(4U);
211  num.push_back(8U);
212  num.push_back(12U);
213  }
214  if (maxNum >= 16) {
215  num.push_back(4U);
216  num.push_back(8U);
217  num.push_back(12U);
218  num.push_back(16U);
219  }
220 
221  return num;
222 }
223 
224 string RunConfigManager::CreateTestsInfluenza()
225 {
226  return R"###(
227 <?xml version="1.0" encoding="utf-8"?>
228 <run>
229  <age_contact_matrix_file>contact_matrix_flanders_subpop.xml</age_contact_matrix_file>
230  <contact_log_level>None</contact_log_level>
231  <contact_output_file>false</contact_output_file>
232  <disease_config_file>disease_influenza.xml</disease_config_file>
233  <holidays_file>holidays_none.json</holidays_file>
234  <immunity_link_probability>0</immunity_link_probability>
235  <immunity_profile>Random</immunity_profile>
236  <immunity_rate>0</immunity_rate>
237  <num_days>30</num_days>
238  <num_participants_survey>10</num_participants_survey>
239  <num_threads>1</num_threads>
240  <output_prefix></output_prefix>
241  <population_file>pop_flanders600.csv</population_file>
242  <population_type>default</geopopulation_type>
243  <rng_seed>1097253,2387652,9963540,4730214</rng_seed>
244  <r0>3</r0>
245  <seeding_rate>0.00089999999999999998</seeding_rate>
246  <seeding_age_min>1</seeding_age_min>
247  <seeding_age_max>99</seeding_age_max>
248  <start_date>2017-01-01</start_date>
249  <stride_log_level>info</stride_log_level>
250  <track_index_case>false</track_index_case>
251  <use_install_dirs>true</use_install_dirs>
252  <vaccine_profile>None</vaccine_profile>
253 </run>
254  )###";
255 }
256 
257 string RunConfigManager::CreateTestsMeasles()
258 {
259  return R"###(
260 <?xml version="1.0" encoding="utf-8"?>
261 <run>
262  <age_contact_matrix_file>contact_matrix_flanders_subpop.xml</age_contact_matrix_file>
263  <contact_log_level>Transmissions</contact_log_level>
264  <contact_output_file>false</contact_output_file>
265  <disease_config_file>disease_measles.xml</disease_config_file>
266  <holidays_file>holidays_none.json</holidays_file>
267  <immunity_rate>0.80000000000000004</immunity_rate>
268  <immunity_profile>None</immunity_profile>
269  <num_days>50</num_days>
270  <num_participants_survey>10</num_participants_survey>
271  <num_threads>1</num_threads>
272  <output_prefix></output_prefix>
273  <population_file>pop_flanders600.csv</population_file>
274  <population_type>default</geopopulation_type>
275  <rng_seed>1097253,2387652,9963540,4730214</rng_seed>
276  <seeding_age_max>99</seeding_age_max>
277  <seeding_age_min>1</seeding_age_min>
278  <seeding_rate>0.002</seeding_rate>
279  <start_date>2017-01-01</start_date>
280  <stride_log_level>info</stride_log_level>
281  <track_index_case>false</track_index_case>
282  <use_install_dirs>true</use_install_dirs>
283  <vaccine_link_probability>0</vaccine_link_probability>
284  <vaccine_profile>Random</vaccine_profile>
285  <vaccine_rate>0.80000000000000004</vaccine_rate>
286 </run>
287  )###";
288 }
289 
290 ptree RunConfigManager::FromString(const string& s)
291 {
292  ptree pt;
293  istringstream is(s);
294  read_xml(is, pt, trim_whitespace);
295  return pt;
296 }
297 
298 string RunConfigManager::ToString(const ptree& pt)
299 {
300  ostringstream ss;
301  write_xml(ss, pt, xml_writer_make_settings<ptree::key_type>(' ', 8));
302  return ss.str();
303 }
304 
305 std::string RunConfigManager::ToSha1(const boost::property_tree::ptree& pt) { return sha1(ToString(pt)); }
306 
307 std::string RunConfigManager::ToShortSha1(const boost::property_tree::ptree& pt, unsigned int n)
308 {
309  return ToSha1(pt).substr(0, n);
310 }
311 
312 } // namespace util
313 } // namespace stride
T FromString(const std::string &s)
Builds a value of type T representation from a string.
Definition: StringUtils.h:49
string ToString(Id l)
Converts a LogMode value to corresponding name.
STL namespace.
Info on configuration.
Produce run config ptree.
Interface for install directory queries.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28