Skip to content

Commit 28bf28c

Browse files
committed
Add saveMacroscopicObservables to RoadDynamics class
1 parent ddff4d5 commit 28bf28c

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

examples/slow_charge_rb.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ int main(int argc, char** argv) {
142142
std::cout << "Done." << std::endl;
143143
std::cout << "Running simulation...\n";
144144

145-
std::ofstream out(OUT_FOLDER + "data.csv");
146-
out << "time;n_agents;mean_speed;mean_speed_err;mean_density;mean_density_"
147-
"err;mean_flow;mean_flow_err;mean_traveltime;mean_traveltime_err;mean_flow_"
148-
"spires;mean_flow_spires_err\n";
149145
#ifdef PRINT_DENSITIES
150146
std::ofstream streetDensity(OUT_FOLDER + "densities.csv");
151147
streetDensity << "time";
@@ -232,17 +228,7 @@ int main(int argc, char** argv) {
232228
inSpires << std::endl;
233229
#endif
234230
printLoadingBar(dynamics.time(), MAX_TIME);
235-
const auto& meanSpeed{dynamics.streetMeanSpeed()};
236-
const auto& meanDensity{dynamics.streetMeanDensity()};
237-
const auto& meanFlow{dynamics.streetMeanFlow()};
238-
const auto& meanTravelTime{dynamics.meanTravelTime()};
239-
const auto& meanSpireFlow{dynamics.meanSpireOutputFlow()};
240-
241-
out << dynamics.time() << ';' << dynamics.agents().size() << ';' << meanSpeed.mean
242-
<< ';' << meanSpeed.std << ';' << meanDensity.mean << ';' << meanDensity.std
243-
<< ';' << meanFlow.mean << ';' << meanFlow.std << ';' << meanTravelTime.mean
244-
<< ';' << meanTravelTime.std << ';' << meanSpireFlow.mean << ';'
245-
<< meanSpireFlow.std << std::endl;
231+
dynamics.saveMacroscopicObservables(std::format("{}data.csv", OUT_FOLDER));
246232
}
247233
if (dynamics.time() % 10 == 0) {
248234
#ifdef PRINT_DENSITIES
@@ -279,7 +265,6 @@ int main(int argc, char** argv) {
279265
}
280266
++progress;
281267
}
282-
out.close();
283268
#ifdef PRINT_DENSITIES
284269
streetDensity.close();
285270
#endif

examples/slow_charge_tl.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,6 @@ int main(int argc, char** argv) {
233233

234234
std::cout << "Done." << std::endl;
235235
std::cout << "Running simulation...\n";
236-
237-
std::ofstream out(OUT_FOLDER + "data.csv");
238-
out << "time;n_agents;mean_speed;mean_speed_err;mean_density;mean_density_"
239-
"err;mean_flow;mean_flow_err;mean_traveltime;mean_traveltime_err;mean_flow_"
240-
"spires;mean_flow_spires_err\n";
241236
#ifdef PRINT_DENSITIES
242237
std::ofstream streetDensity(OUT_FOLDER + "densities.csv");
243238
streetDensity << "time";
@@ -343,17 +338,7 @@ int main(int argc, char** argv) {
343338
outSpires << std::endl;
344339
inSpires << std::endl;
345340
#endif
346-
const auto& meanSpeed{dynamics.streetMeanSpeed()};
347-
const auto& meanDensity{dynamics.streetMeanDensity()};
348-
const auto& meanFlow{dynamics.streetMeanFlow()};
349-
const auto& meanTravelTime{dynamics.meanTravelTime()};
350-
const auto& meanSpireFlow{dynamics.meanSpireOutputFlow()};
351-
352-
out << dynamics.time() << ';' << dynamics.agents().size() << ';' << meanSpeed.mean
353-
<< ';' << meanSpeed.std << ';' << meanDensity.mean << ';' << meanDensity.std
354-
<< ';' << meanFlow.mean << ';' << meanFlow.std << ';' << meanTravelTime.mean
355-
<< ';' << meanTravelTime.std << ';' << meanSpireFlow.mean << ';'
356-
<< meanSpireFlow.std << std::endl;
341+
dynamics.saveMacroscopicObservables(std::format("{}data.csv", OUT_FOLDER));
357342
// deltas.push_back(deltaAgents);
358343
// previousAgents = dynamics.agents().size();
359344
#ifdef PRINT_TP
@@ -423,7 +408,6 @@ int main(int argc, char** argv) {
423408
}
424409
++progress;
425410
}
426-
out.close();
427411
#ifdef PRINT_DENSITIES
428412
streetDensity.close();
429413
#endif

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 5;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 7;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 8;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/RoadDynamics.hpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ namespace dsm {
310310
/// @param filename The name of the file
311311
/// @param reset If true, the travel speeds are cleared after the computation
312312
void saveTravelSpeeds(const std::string& filename, bool reset = false);
313+
/// @brief Save the main macroscopic observables in csv format
314+
/// @param filename The name of the file
315+
/// @param separator The separator character (default is ';')
316+
/// @details The file contains the following columns:
317+
/// - time: the time of the simulation
318+
/// - n_agents: the number of agents currently in the simulation
319+
/// - mean_speed - mean_speed_std: the mean speed of the agents
320+
/// - mean_density - mean_density_std: the (normalized) mean density of the streets
321+
/// - mean_flow - mean_flow_std: the mean flow of the streets
322+
/// - mean_flow_spires - mean_flow_spires_std: the mean flow of the spires
323+
/// - mean_traveltime - mean_traveltime_std: the mean travel time of the agents
324+
/// - mean_traveldistance - mean_traveldistance_err: the mean travel distance of the agents
325+
/// - mean_travelspeed - mean_travelspeed_std: the mean travel speed of the agents
326+
///
327+
/// NOTE: the mean density is normalized in [0, 1] and reset is true for all observables which have such parameter
328+
void saveMacroscopicObservables(const std::string& filename,
329+
char const separator = ';');
313330
};
314331

315332
template <typename delay_t>
@@ -1567,4 +1584,70 @@ namespace dsm {
15671584
m_travelDTs.clear();
15681585
}
15691586
}
1587+
template <typename delay_t>
1588+
requires(is_numeric_v<delay_t>)
1589+
void RoadDynamics<delay_t>::saveMacroscopicObservables(const std::string& filename,
1590+
char const separator) {
1591+
bool bEmptyFile{false};
1592+
{
1593+
std::ifstream file(filename);
1594+
bEmptyFile = file.peek() == std::ifstream::traits_type::eof();
1595+
}
1596+
std::ofstream file(filename, std::ios::app);
1597+
if (!file.is_open()) {
1598+
Logger::error(std::format("Error opening file \"{}\" for writing.", filename));
1599+
}
1600+
if (bEmptyFile) {
1601+
file << "time;n_agents;mean_speed;mean_speed_std;mean_density;mean_density_std;"
1602+
"mean_flow;mean_flow_std;mean_flow_spires;mean_flow_spires_std;mean_"
1603+
"traveltime;mean_traveltime_std;mean_traveldistance;mean_traveldistance_"
1604+
"err;mean_travelspeed;mean_travelspeed_std\n";
1605+
}
1606+
file << this->time() << separator;
1607+
file << m_agents.size() << separator;
1608+
file << std::scientific << std::setprecision(2);
1609+
{
1610+
std::vector<double> speeds, densities, flows, spireFlows;
1611+
speeds.reserve(this->graph().nEdges());
1612+
densities.reserve(this->graph().nEdges());
1613+
flows.reserve(this->graph().nEdges());
1614+
spireFlows.reserve(this->graph().nEdges());
1615+
for (auto const& [streetId, street] : this->graph().edges()) {
1616+
speeds.push_back(this->streetMeanSpeed(streetId));
1617+
densities.push_back(street->density(true));
1618+
flows.push_back(street->density(true) * speeds.back());
1619+
if (street->isSpire()) {
1620+
auto& spire = dynamic_cast<SpireStreet&>(*street);
1621+
spireFlows.push_back(static_cast<double>(spire.inputCounts(true)) /
1622+
(this->time() - m_previousSpireTime));
1623+
}
1624+
}
1625+
auto speed{Measurement<double>(speeds)};
1626+
auto density{Measurement<double>(densities)};
1627+
auto flow{Measurement<double>(flows)};
1628+
auto spireFlow{Measurement<double>(spireFlows)};
1629+
file << speed.mean << separator << speed.std << separator;
1630+
file << density.mean << separator << density.std << separator;
1631+
file << flow.mean << separator << flow.std << separator;
1632+
file << spireFlow.mean << separator << spireFlow.std << separator;
1633+
}
1634+
{
1635+
std::vector<double> distances, times, speeds;
1636+
distances.reserve(m_travelDTs.size());
1637+
times.reserve(m_travelDTs.size());
1638+
speeds.reserve(m_travelDTs.size());
1639+
for (auto const& [distance, time] : m_travelDTs) {
1640+
distances.push_back(distance);
1641+
times.push_back(time);
1642+
speeds.push_back(distance / time);
1643+
}
1644+
auto distance{Measurement<double>(distances)};
1645+
auto time{Measurement<double>(times)};
1646+
auto speed{Measurement<double>(speeds)};
1647+
file << distance.mean << separator << distance.std << separator;
1648+
file << time.mean << separator << time.std << separator;
1649+
file << speed.mean << separator << speed.std << std::endl;
1650+
}
1651+
file.close();
1652+
}
15701653
}; // namespace dsm

0 commit comments

Comments
 (0)