Stride Reference Manual  - generated for commit 9643b11
GeoGridProtoWriter.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 2018, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #include "GeoGridProtoWriter.h"
17 
18 #include "contact/ContactPool.h"
19 #include "contact/ContactType.h"
20 #include "geogrid.pb.h"
21 #include "geopop/GeoGrid.h"
22 #include "pop/Person.h"
23 #include "util/Exception.h"
24 #include "util/SegmentedVector.h"
25 
26 #include <iostream>
27 #include <map>
28 #include <omp.h>
29 
30 namespace geopop {
31 
32 using namespace std;
33 using namespace stride::util;
34 using namespace stride::ContactType;
35 
37 
38 void GeoGridProtoWriter::Write(GeoGrid& geoGrid, ostream& stream)
39 {
40  GOOGLE_PROTOBUF_VERIFY_VERSION;
41 
42  proto::GeoGrid protoGrid;
43  for (const auto& location : geoGrid) {
44  WriteLocation(*location, protoGrid.add_locations());
45  }
46  for (const auto& person : m_persons_found) {
47  WritePerson(person, protoGrid.add_persons());
48  }
49 
50  m_persons_found.clear();
51  if (!protoGrid.SerializeToOstream(&stream)) {
52  throw stride::util::Exception("There was an error writing the GeoGrid to the file.");
53  }
54  google::protobuf::ShutdownProtobufLibrary();
55  stream.flush();
56 }
57 
59  proto::GeoGrid_Location_ContactPools* protoContactPools)
60 {
61  static const map<Id, proto::GeoGrid_Location_ContactPools_Type> types = {
68 
69  protoContactPools->set_type(types.at(typeId));
70  for (stride::ContactPool* pool : contactPools) {
71  WriteContactPool(pool, protoContactPools->add_pools());
72  }
73 }
74 
77 {
78  protoContactPool->set_id(static_cast<google::protobuf::int64>(contactPool->GetId()));
79  for (const auto& person : *contactPool) {
80  protoContactPool->add_people(person->GetId());
81  m_persons_found.insert(person);
82  }
83 }
84 
86  proto::GeoGrid_Location_Coordinate* protoCoordinate)
87 {
88  protoCoordinate->set_longitude(boost::geometry::get<0>(coordinate));
89  protoCoordinate->set_latitude(boost::geometry::get<1>(coordinate));
90 }
91 
93 {
94  protoLocation->set_id(location.GetID());
95  protoLocation->set_name(location.GetName());
96  protoLocation->set_province(location.GetProvince());
97  protoLocation->set_population(location.GetPopCount());
98  auto coordinate = new proto::GeoGrid_Location_Coordinate();
99  WriteCoordinate(location.GetCoordinate(), coordinate);
100  protoLocation->set_allocated_coordinate(coordinate);
101 
102  auto commutes = location.CRefOutgoingCommutes();
103  for (auto commute_pair : commutes) {
104  auto commute = protoLocation->add_commutes();
105  commute->set_to(commute_pair.first->GetID());
106  commute->set_proportion(commute_pair.second);
107  }
108 
109  for (Id typ : IdList) {
110  WriteContactPools(typ, location.RefPools(typ), protoLocation->add_contactpools());
111  }
112 }
113 
115 {
116  protoPerson->set_id(person->GetId());
117  protoPerson->set_age(static_cast<google::protobuf::int64>(person->GetAge()));
118 }
119 
120 } // namespace geopop
unsigned int GetID() const
Gets ID of this Location.
Definition: Location.h:63
void WritePerson(stride::Person *person, proto::GeoGrid_Person *protoPerson)
Create a ProtoBuf Person containing all the info needed to reconstruct a Person.
std::string GetName() const
Gets the name.
Definition: Location.h:72
Header file for the Person class.
A group of Persons that potentially have contacts with one another.
Definition: ContactPool.h:38
stride::util::SegmentedVector< stride::ContactPool * > & RefPools(stride::ContactType::Id id)
Access through reference to ContactPools of type &#39;id&#39;.
Definition: Location.h:116
Header for the core ContactPool class.
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
void WriteContactPools(stride::ContactType::Id typeId, stride::util::SegmentedVector< stride::ContactPool * > &contactPools, proto::GeoGrid_Location_ContactPools *protoContactPools)
Create a ProtoBuf ContactPools structure.
void set_id(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1130
void add_people(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1151
Utilities for the project.
unsigned int GetProvince() const
Gets the province.
Definition: Location.h:81
::proto::GeoGrid_Location_ContactPools_ContactPool * add_pools()
Definition: geogrid.pb.h:1218
void set_allocated_coordinate(::proto::GeoGrid_Location_Coordinate *coordinate)
Definition: geogrid.pb.h:1391
::proto::GeoGrid_Location * add_locations()
Definition: geogrid.pb.h:1526
unsigned int GetId() const
Get the id.
Definition: Person.h:62
::proto::GeoGrid_Location_Commute * add_commutes()
Definition: geogrid.pb.h:1460
void set_province(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1339
void set_age(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1496
void set_id(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1482
void set_type(::proto::GeoGrid_Location_ContactPools_Type value)
Definition: geogrid.pb.h:1192
unsigned int GetId() const
Get the pool id.
Definition: ContactPool.h:51
void set_id(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1272
Interface and implementation for SegmentedVector class.
STL namespace.
float GetAge() const
Get the age.
Definition: Person.h:53
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
void set_to(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1240
Basic exception class: needed to prevent clang-tidy warning "thrown exception type is not nothrow cop...
Definition: Exception.h:28
::proto::GeoGrid_Person * add_persons()
Definition: geogrid.pb.h:1556
Container that stores objects "almost contiguously" (in a chain of blocks) and guarantees that pointe...
Definition of ContactPool Id Type.
void WriteCoordinate(const Coordinate &coordinate, proto::GeoGrid_Location_Coordinate *protoCoordinate)
Create a ProtoBuf Coordinate containing all the info needed to reconstruct a Coordinate..
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
const Coordinate GetCoordinate() const
Gets the Coordinate of this Location.
Definition: Location.h:60
void WriteContactPool(stride::ContactPool *contactPool, proto::GeoGrid_Location_ContactPools_ContactPool *protoContactPool)
Create a ProtoBuf ContactPool containing all the info needed to reconstruct a ContactPool.
void set_population(::google::protobuf::int64 value)
Definition: geogrid.pb.h:1353
boost::geometry::model::point< double, 2, boost::geometry::cs::geographic< boost::geometry::degree >> Coordinate
Definition: Coordinate.h:23
unsigned int GetPopCount() const
Gets the absolute population.
Definition: Location.h:78
GeoGridProtoWriter()
Construct the GeoGridProtoWriter.
::proto::GeoGrid_Location_ContactPools * add_contactpools()
Definition: geogrid.pb.h:1430
std::set< stride::Person * > m_persons_found
The persons found when looping over the ContactPools.
Location for use within the GeoGrid, contains Coordinate and index to ContactPools.
Definition: Location.h:41
Store and handle person data.
Definition: Person.h:34
void set_name(const ::std::string &value)
Definition: geogrid.pb.h:1286
void Write(GeoGrid &geoGrid, std::ostream &stream) override
Write the GeoGrid to the ostream in Protobuf format.
void set_longitude(double value)
Definition: geogrid.pb.h:1098
Namespace to manage types of ContactPool.
Definition: ContactType.cpp:28
void WriteLocation(Location &location, proto::GeoGrid_Location *protoLocation)
Create a ProtoBuf Location containing all the info needed to reconstruct a Location.
const std::vector< std::pair< Location *, double > > & CRefOutgoingCommutes() const
References outgoing commute Locations + fraction of commutes to that Location.
Definition: Location.h:149