|
21 | 21 | #include <format> |
22 | 22 | #include <thread> |
23 | 23 | #include <exception> |
| 24 | +#include <fstream> |
| 25 | +#include <iomanip> |
24 | 26 |
|
25 | 27 | #include "Dynamics.hpp" |
26 | 28 | #include "Agent.hpp" |
@@ -174,6 +176,11 @@ namespace dsm { |
174 | 176 | std::unordered_map<Id, std::array<long, 4>> turnMapping() const { |
175 | 177 | return m_turnMapping; |
176 | 178 | } |
| 179 | + |
| 180 | + /// @brief Save the travel speeds of the agents in csv format |
| 181 | + /// @param filename The name of the file |
| 182 | + /// @param reset If true, the travel speeds are cleared after the computation |
| 183 | + void saveTravelSpeeds(const std::string& filename, bool reset = false); |
177 | 184 | }; |
178 | 185 |
|
179 | 186 | template <typename delay_t> |
@@ -884,4 +891,34 @@ namespace dsm { |
884 | 891 | return res; |
885 | 892 | } |
886 | 893 |
|
| 894 | + template <typename delay_t> |
| 895 | + requires(is_numeric_v<delay_t>) |
| 896 | + void RoadDynamics<delay_t>::saveTravelSpeeds(const std::string& filename, bool reset) { |
| 897 | + bool bEmptyFile{false}; |
| 898 | + { |
| 899 | + std::ifstream file(filename); |
| 900 | + bEmptyFile = file.peek() == std::ifstream::traits_type::eof(); |
| 901 | + } |
| 902 | + std::ofstream file(filename, std::ios::app); |
| 903 | + if (!file.is_open()) { |
| 904 | + logger.error(std::format("Error opening file \"{}\" for writing.", filename)); |
| 905 | + } |
| 906 | + if (bEmptyFile) { |
| 907 | + file << "time;speeds" << std::endl; |
| 908 | + } |
| 909 | + file << this->time() << ';'; |
| 910 | + for (auto i{0}; i < m_travelTimes.size(); ++i) { |
| 911 | + file << std::fixed << std::setprecision(2) |
| 912 | + << m_travelDistances[i] / m_travelTimes[i]; |
| 913 | + if (i < m_travelTimes.size() - 1) { |
| 914 | + file << ','; |
| 915 | + } |
| 916 | + } |
| 917 | + file << std::endl; |
| 918 | + file.close(); |
| 919 | + if (reset) { |
| 920 | + m_travelTimes.clear(); |
| 921 | + m_travelDistances.clear(); |
| 922 | + } |
| 923 | + } |
887 | 924 | }; // namespace dsm |
0 commit comments