Stride Reference Manual  - generated for commit 9643b11
GenPopController.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 "GenPopController.h"
22 
23 #include "geopop/GeoGrid.h"
26 
27 #include "pop/GeoPopBuilder.h"
28 #include "pop/Population.h"
29 #include "util/FileSys.h"
30 
31 #ifdef BOOST_FOUND
32 #include <boost/filesystem.hpp>
33 #include <boost/filesystem/path.hpp>
34 namespace filesys = boost::filesystem;
35 #else
36 #include <filesystem>
37 namespace filesys = std::filesystem;
38 #endif
39 
40 #include <boost/property_tree/ptree.hpp>
41 #include <fstream>
42 #include <memory>
43 #include <spdlog/spdlog.h>
44 
45 using namespace std;
46 using namespace stride::util;
47 using namespace geopop;
48 using namespace boost::property_tree;
49 
50 namespace stride {
51 
52 GenPopController::GenPopController(const ptree& config, const string& name) : ControlHelper(config, name) {}
53 
55 {
56  // -----------------------------------------------------------------------------------------
57  // Prelims.
58  // -----------------------------------------------------------------------------------------
59  CheckEnv();
61  InstallLogger();
62  LogStartup();
63 
64  // -----------------------------------------------------------------------------------------
65  // MakePersons scenario: step 1, build a random number manager.
66  // -----------------------------------------------------------------------------------------
67  const RnInfo info{m_config.get<string>("run.rng_seed", "1,2,3,4"), "",
68  m_config.get<unsigned int>("run.num_threads")};
69  RnMan rnMan{info};
70 
71  // -----------------------------------------------------------------------------------------
72  // MakePersons scenario: step 2, set up the GenPopBuilder and build population with GeoGrid.
73  // -----------------------------------------------------------------------------------------
74  m_stride_logger->trace("GenPopBuilder invoked.");
75 
76  GeoPopBuilder geoPopBuilder(m_config, rnMan, m_stride_logger);
77  const auto pop = Population::Create();
78  geoPopBuilder.Build(pop);
79 
80  m_stride_logger->trace("GenPopBuilder done.");
81 
82  // ----------------------------------------------------------------------------------------
83  // MakePersons scenario: step 3, write to population to file.
84  // ----------------------------------------------------------------------------------------
85  m_stride_logger->trace("Start writing population to file.");
86 
87  const auto prefix = m_config.get<string>("run.output_prefix");
88  const auto popFileName = m_config.get<string>("run.population_file", "genpop.proto");
89  const auto popFilePath = FileSys::BuildPath(prefix, popFileName);
90  m_stride_logger->info("Population written to file {}.", popFilePath.string());
91  shared_ptr<GeoGridWriter> geoGridWriter = GeoGridWriterFactory::CreateGeoGridWriter(popFileName);
92  ofstream outputFileStream(popFilePath.string());
93  geoGridWriter->Write(pop->RefGeoGrid(), outputFileStream);
94  outputFileStream.close();
95 
96  m_stride_logger->trace("Done writing population to file.");
97 }
98 
99 } // namespace stride
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Generates a synthetic population.
Initialize populations.
Header for the command line controller.
boost::property_tree::ptree m_config
Main configuration for run and sim.
Definition: ControlHelper.h:78
void CheckEnv()
Check install environment.
static std::shared_ptr< Population > Create()
Create an empty Population, used in gengeopop.
Definition: Population.cpp:96
Utilities for the project.
std::shared_ptr< spdlog::logger > m_stride_logger
General logger.
Definition: ControlHelper.h:82
void LogStartup()
Logs info on setup for cli environment to stride_logger.
static std::shared_ptr< GeoGridWriter > CreateGeoGridWriter(const std::string &filename)
Create a GeoGridWriter based on the provided extension in the filename.
STL namespace.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
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
Builds poulation of a geographic grid.
Definition: GeoPopBuilder.h:41
Header file for the core Population class.
Controls a simulation run initiated with the command line interface (cli).
Definition: ControlHelper.h:45
Interface for install directory queries.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Information on random number management state.
Definition: RnInfo.h:32
void InstallLogger()
Make the appropriate logger for cli environment and register as stride_logger.
void Control()
Actual run of the simulator.