Skip to content

Commit 45e99fb

Browse files
authored
Merge pull request #403 from physycom/db
Move output from `CSV` files to a `SQL` db
2 parents 9611d9e + c48f2a9 commit 45e99fb

26 files changed

+1181
-1618
lines changed

.github/workflows/binding.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ jobs:
127127
- name: Verify package metadata
128128
run: |
129129
python -c "
130-
import pkg_resources
130+
import importlib.metadata
131131
try:
132-
dist = pkg_resources.get_distribution('dsf')
133-
print(f'Package name: {dist.project_name}')
132+
dist = importlib.metadata.distribution('dsf')
133+
print(f'Package name: {dist.name}')
134134
print(f'Package version: {dist.version}')
135-
print(f'Package location: {dist.location}')
136-
except pkg_resources.DistributionNotFound:
135+
location = str(dist.locate_file(''))
136+
print(f'Package location: {location}')
137+
except importlib.metadata.PackageNotFoundError:
137138
print('Warning: Package metadata not found')
138139
"
139140

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ test/data/*dsf
4848

4949
webapp/data/*
5050
*egg-info*
51+
52+
# Jupyter Notebook (temporary checks)
53+
*.ipynb

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ if(NOT simdjson_POPULATED)
138138
endif()
139139
# Check if the user has TBB installed
140140
find_package(TBB REQUIRED CONFIG)
141+
# Get SQLiteCpp
142+
# Disable cppcheck on Windows to avoid issues with missing cppcheck installation
143+
if(WIN32)
144+
set(SQLITECPP_RUN_CPPCHECK
145+
OFF
146+
CACHE BOOL "Run cppcheck on SQLiteCpp" FORCE)
147+
endif()
148+
FetchContent_Declare(
149+
SQLiteCpp
150+
GIT_REPOSITORY https://github.com/SRombauts/SQLiteCpp
151+
GIT_TAG 3.3.3)
152+
FetchContent_GetProperties(SQLiteCpp)
153+
if(NOT SQLiteCpp_POPULATED)
154+
FetchContent_MakeAvailable(SQLiteCpp)
155+
endif()
141156

142157
add_library(dsf STATIC ${SOURCES})
143158
target_compile_definitions(dsf PRIVATE SPDLOG_USE_STD_FORMAT)
@@ -151,7 +166,7 @@ target_include_directories(
151166
target_include_directories(dsf PRIVATE ${rapidcsv_SOURCE_DIR}/src)
152167

153168
# Link other libraries - no csv dependency needed now
154-
target_link_libraries(dsf PRIVATE TBB::tbb simdjson::simdjson spdlog::spdlog)
169+
target_link_libraries(dsf PUBLIC TBB::tbb SQLiteCpp PRIVATE simdjson::simdjson spdlog::spdlog)
155170

156171
# Install dsf library
157172
install(

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![SPDLOG](https://img.shields.io/badge/spdlog-1.17.0-blue.svg)](https://github.com/gabime/spdlog)
77
[![CSV](https://img.shields.io/badge/rapidcsv-8.89-blue.svg)](https://github.com/d99kris/rapidcsv)
88
[![JSON](https://img.shields.io/badge/simdjson-4.2.1-blue.svg)](https://github.com/simdjson/simdjson)
9+
[![SQLite](https://img.shields.io/badge/SQLiteCpp-3.3.3-blue.svg)](https://github.com/SRombauts/SQLiteCpp)
910
[![codecov](https://codecov.io/gh/physycom/DynamicalSystemFramework/graph/badge.svg?token=JV53J6IUJ3)](https://codecov.io/gh/physycom/DynamicalSystemFramework)
1011

1112
The aim of this project is to rework the original [Traffic Flow Dynamics Model](https://github.com/Grufoony/TrafficFlowDynamicsModel).
@@ -35,7 +36,7 @@ print(dsf.__version__)
3536
## Installation (from source)
3637

3738
### Requirements
38-
The project requires `C++20` or greater, `cmake`, `tbb` `simdjson`, `spdlog` and `rapidcsv`.
39+
The project requires `C++20` or greater, `cmake`, `tbb` `simdjson`, `spdlog`, `rapidcsv` and `SQLiteCpp`.
3940
To install requirements on Ubuntu:
4041
```shell
4142
sudo apt install cmake libtbb-dev

examples/slow_charge_rb.cpp

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ std::atomic<bool> bExitFlag{false};
2727

2828
// uncomment these lines to print densities, flows and speeds
2929
#define PRINT_DENSITIES
30-
// #define PRINT_FLOWS
31-
// #define PRINT_SPEEDS
3230

3331
using RoadNetwork = dsf::mobility::RoadNetwork;
3432
using Dynamics = dsf::mobility::FirstOrderDynamics;
@@ -68,14 +66,13 @@ int main(int argc, char** argv) {
6866
std::to_string(SEED))}; // output folder
6967
constexpr auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation
7068

71-
// Clear output folder or create it if it doesn't exist
69+
// Create output folder if it doesn't exist (preserve existing database)
7270
if (!fs::exists(BASE_OUT_FOLDER)) {
7371
fs::create_directory(BASE_OUT_FOLDER);
7472
}
75-
if (fs::exists(OUT_FOLDER)) {
76-
fs::remove_all(OUT_FOLDER);
73+
if (!fs::exists(OUT_FOLDER)) {
74+
fs::create_directory(OUT_FOLDER);
7775
}
78-
fs::create_directory(OUT_FOLDER);
7976
// Starting
8077
std::cout << "Using dsf version: " << dsf::version() << '\n';
8178
RoadNetwork graph{};
@@ -119,24 +116,18 @@ int main(int argc, char** argv) {
119116
// dynamics.setForcePriorities(true);
120117
dynamics.setSpeedFluctuationSTD(0.1);
121118

119+
// Connect database for saving data
120+
dynamics.connectDataBase(OUT_FOLDER + "simulation_data.db");
121+
122+
// Configure data saving: interval=10, saveAverageStats=true, saveStreetData=true
123+
#ifdef PRINT_DENSITIES
124+
dynamics.saveData(300, true, true, false);
125+
#else
126+
dynamics.saveData(300, true, false, false);
127+
#endif
128+
122129
std::cout << "Done." << std::endl;
123130
std::cout << "Running simulation...\n";
124-
#ifdef PRINT_FLOWS
125-
std::ofstream streetFlow(OUT_FOLDER + "flows.csv");
126-
streetFlow << "time";
127-
for (const auto& [id, street] : dynamics.graph().edges()) {
128-
streetFlow << ';' << id;
129-
}
130-
streetFlow << '\n';
131-
#endif
132-
#ifdef PRINT_SPEEDS
133-
std::ofstream streetSpeed(OUT_FOLDER + "speeds.csv");
134-
streetSpeed << "time;";
135-
for (const auto& [id, street] : dynamics.graph().edges()) {
136-
streetSpeed << ';' << id;
137-
}
138-
streetSpeed << '\n';
139-
#endif
140131

141132
int deltaAgents{std::numeric_limits<int>::max()};
142133
int previousAgents{0};
@@ -176,47 +167,12 @@ int main(int argc, char** argv) {
176167
}
177168

178169
if (dynamics.time_step() % 300 == 0) {
179-
dynamics.saveCoilCounts(std::format("{}coil_counts.csv", OUT_FOLDER));
170+
// Data is now saved automatically by saveData() configuration
180171
printLoadingBar(dynamics.time_step(), MAX_TIME);
181-
dynamics.saveMacroscopicObservables(std::format("{}data.csv", OUT_FOLDER));
182-
}
183-
if (dynamics.time_step() % 10 == 0) {
184-
#ifdef PRINT_DENSITIES
185-
dynamics.saveStreetDensities(OUT_FOLDER + "densities.csv", true);
186-
#endif
187-
#ifdef PRINT_FLOWS
188-
streetFlow << dynamics.time_step();
189-
for (const auto& [id, street] : dynamics.graph().edges()) {
190-
const auto& meanSpeed = dynamics.streetMeanSpeed(id);
191-
if (meanSpeed.has_value()) {
192-
streetFlow << ';' << meanSpeed.value() * street->density();
193-
} else {
194-
streetFlow << ';';
195-
}
196-
}
197-
streetFlow << std::endl;
198-
#endif
199-
#ifdef PRINT_SPEEDS
200-
streetSpeed << dynamics.time_step();
201-
for (const auto& [id, street] : dynamics.graph().edges()) {
202-
const auto& meanSpeed = dynamics.streetMeanSpeed(id);
203-
if (meanSpeed.has_value()) {
204-
streetSpeed << ';' << meanSpeed.value();
205-
} else {
206-
streetSpeed << ';';
207-
}
208-
}
209-
streetSpeed << std::endl;
210-
#endif
211172
}
173+
// Street densities are now saved automatically by saveData() configuration
212174
++progress;
213175
}
214-
#ifdef PRINT_FLOWS
215-
streetFlow.close();
216-
#endif
217-
#ifdef PRINT_SPEEDS
218-
streetSpeed.close();
219-
#endif
220176
// std::cout << std::endl;
221177
// std::map<uint8_t, std::string> turnNames{
222178
// {0, "left"}, {1, "straight"}, {2, "right"}, {3, "u-turn"}};

examples/slow_charge_tl.cpp

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ std::atomic<bool> bExitFlag{false};
2828

2929
// uncomment these lines to print densities, flows and speeds
3030
#define PRINT_DENSITIES
31-
// #define PRINT_FLOWS
32-
// #define PRINT_SPEEDS
3331
// #define PRINT_TP
3432

3533
using RoadNetwork = dsf::mobility::RoadNetwork;
@@ -74,14 +72,13 @@ int main(int argc, char** argv) {
7472
ERROR_PROBABILITY,
7573
std::to_string(SEED))}; // output folder
7674
constexpr auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation
77-
// Clear output folder or create it if it doesn't exist
75+
// Create output folder if it doesn't exist (preserve existing database)
7876
if (!fs::exists(BASE_OUT_FOLDER)) {
7977
fs::create_directory(BASE_OUT_FOLDER);
8078
}
81-
if (fs::exists(OUT_FOLDER)) {
82-
fs::remove_all(OUT_FOLDER);
79+
if (!fs::exists(OUT_FOLDER)) {
80+
fs::create_directory(OUT_FOLDER);
8381
}
84-
fs::create_directory(OUT_FOLDER);
8582
// Starting
8683
std::cout << "Using dsf version: " << dsf::version() << '\n';
8784
RoadNetwork graph{};
@@ -175,26 +172,20 @@ int main(int argc, char** argv) {
175172
if (OPTIMIZE)
176173
dynamics.setDataUpdatePeriod(30); // Store data every 30 time steps
177174

175+
// Connect database for saving data
176+
dynamics.connectDataBase(OUT_FOLDER + "simulation_data.db");
177+
178+
// Configure data saving: interval=10, saveAverageStats=true, saveStreetData=true
179+
#ifdef PRINT_DENSITIES
180+
dynamics.saveData(300, true, true, false);
181+
#else
182+
dynamics.saveData(300, true, false, false);
183+
#endif
184+
178185
const auto TM = dynamics.turnMapping();
179186

180187
std::cout << "Done." << std::endl;
181188
std::cout << "Running simulation...\n";
182-
#ifdef PRINT_FLOWS
183-
std::ofstream streetFlow(OUT_FOLDER + "flows.csv");
184-
streetFlow << "time";
185-
for (const auto& [id, street] : dynamics.graph().edges()) {
186-
streetFlow << ';' << id;
187-
}
188-
streetFlow << '\n';
189-
#endif
190-
#ifdef PRINT_SPEEDS
191-
std::ofstream streetSpeed(OUT_FOLDER + "speeds.csv");
192-
streetSpeed << "time";
193-
for (const auto& [id, street] : dynamics.graph().edges()) {
194-
streetSpeed << ';' << id;
195-
}
196-
streetSpeed << '\n';
197-
#endif
198189
#ifdef PRINT_TP
199190
std::ofstream outTP(OUT_FOLDER + "turn_probabilities.csv");
200191
outTP << "time";
@@ -256,8 +247,7 @@ int main(int argc, char** argv) {
256247
if (dynamics.time_step() % 300 == 0) {
257248
// printLoadingBar(dynamics.time_step(), MAX_TIME);
258249
// deltaAgents = std::labs(dynamics.agents().size() - previousAgents);
259-
dynamics.saveCoilCounts(std::format("{}coil_counts.csv", OUT_FOLDER));
260-
dynamics.saveMacroscopicObservables(std::format("{}data.csv", OUT_FOLDER));
250+
// Data is now saved automatically by saveData() configuration
261251
// deltas.push_back(deltaAgents);
262252
// previousAgents = dynamics.agents().size();
263253
#ifdef PRINT_TP
@@ -292,43 +282,9 @@ int main(int argc, char** argv) {
292282
outTP << std::endl;
293283
#endif
294284
}
295-
if (dynamics.time_step() % 10 == 0) {
296-
#ifdef PRINT_DENSITIES
297-
dynamics.saveStreetDensities(OUT_FOLDER + "densities.csv", true);
298-
#endif
299-
#ifdef PRINT_FLOWS
300-
streetFlow << ';' << dynamics.time_step();
301-
for (const auto& [id, street] : dynamics.graph().edges()) {
302-
const auto& meanSpeed = dynamics.streetMeanSpeed(id);
303-
if (meanSpeed.has_value()) {
304-
streetFlow << ';' << meanSpeed.value() * street->density();
305-
} else {
306-
streetFlow << ';';
307-
}
308-
}
309-
streetFlow << std::endl;
310-
#endif
311-
#ifdef PRINT_SPEEDS
312-
streetSpeed << dynamics.time_step();
313-
for (const auto& [id, street] : dynamics.graph().edges()) {
314-
const auto& meanSpeed = dynamics.streetMeanSpeed(id);
315-
if (meanSpeed.has_value()) {
316-
streetSpeed << ';' << meanSpeed.value();
317-
} else {
318-
streetSpeed << ';';
319-
}
320-
}
321-
streetSpeed << std::endl;
322-
#endif
323-
}
285+
// Street densities are now saved automatically by saveData() configuration
324286
++progress;
325287
}
326-
#ifdef PRINT_FLOWS
327-
streetFlow.close();
328-
#endif
329-
#ifdef PRINT_SPEEDS
330-
streetSpeed.close();
331-
#endif
332288
// std::cout << std::endl;
333289
// std::map<uint8_t, std::string> turnNames{
334290
// {0, "left"}, {1, "straight"}, {2, "right"}, {3, "u-turn"}};

0 commit comments

Comments
 (0)