Merge remote-tracking branch 'origin/smail' into lbaumberger

This commit is contained in:
lbaumberger
2026-04-27 14:27:23 +02:00
12 changed files with 1000263 additions and 1000090 deletions
Vendored
BIN
View File
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "macos-clang-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
+13
View File
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "/Users/smailalijagic/Desktop/Universität/26FS/Advanced Systems Lab/team70",
"program": "/Users/smailalijagic/Desktop/Universität/26FS/Advanced Systems Lab/team70/build/Debug/outDebug"
}
]
}
+59
View File
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
+44
View File
@@ -0,0 +1,44 @@
# Advanced Systems Lab - Spring 2026
This is the GitLab repository of group 70.
Project topic: Social Force Model for Pedestrian Dynamics
The goal of this project was to implement a fast version of Helbing's social force model. The final report includes an analysis and results of the work.
This repository contains all C++ and Python code that was used to generate and analyze the data.
## Compilation
Linux
``
Windows
``
Apple
`clang++ -O3 -march=native -o main_test_visualisation main.cpp forces.cpp`
Compile the code with running the bash script 'src/main/run_simulation.sh'. After compilation was succesfull the user has 3 different option:
1. Benchmark
2. Test
3. Visualization
The flags used are:
`gcc -lm -O3 -mavx2 -mfma -ffast-math -g`
## Storing coordinates in a csv
`./main_test_visualisation <number of pedestrians> <number of iterations> <csv file>`
## Visualization
`python3 visualization.py <csv file> <mp4 file>`
## Benchmark
## Test
+49 -35
View File
@@ -8,15 +8,22 @@ double w(Vector2d e, Vector2d f) {
return c;
}
// # Done
// # 2 DIVs
double g(double v_max_alpha, double w_alpha_norm) {
if (w_alpha_norm <= v_max_alpha) {
return 1;
}
return v_max_alpha / w_alpha_norm;
}
// # 8 flops
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
void compute_acceleration_term(Pedestrian *p) {
// # 2 flops
// $v_{\alpha}^{0}\vec{e}_{\alpha}$
Vector2d desired_velocity = p->e * p->desired_v0;
// $\vec{v}_{\alpha}(t)$
Vector2d actual_velocity = p->curr_vel;
@@ -25,11 +32,10 @@ void compute_acceleration_term(Pedestrian *p) {
p->F_i += (desired_velocity - actual_velocity) * tau_alpha_inv;
}
// # Done
// # 55(N - 1) = 55N - 55 flops
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
//
void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) {
for (int i = 0; i < N; i++) {
@@ -43,56 +49,58 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr
// continue;
// }
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos;
// # 4 flops = 1 SQRT, 2 MULTs, 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
double r_ab_norm = r_ab.norm();
// $\vec{e}_{\beta}$
Vector2d e_beta = pedestrians[i].e;
Vector2d e_b = pedestrians[i].e;
// # 2 MULTs
// $v_\beta \Delta t$
double v_beta_delta_t = pedestrians[i].desired_v0 * delta_t;
double s_b = pedestrians[i].curr_vel.norm() * delta_t;
// # 4 flops = 2 ADDs, 2 MULTs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab_minus_v_beta_delta_t_e_beta = r_ab - (e_beta * v_beta_delta_t);
Vector2d r_ab2 = r_ab - (e_b * s_b);
// # 4 flops = 1 SQRT, 2 MULTs, 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_minus_v_beta_delta_t_e_beta_norm = r_ab_minus_v_beta_delta_t_e_beta.norm();
double r_ab2_norm = r_ab2.norm();
// (4):
// # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
// $(4):2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double b = sqrt((r_ab_norm + r_ab2_norm) * (r_ab_norm + r_ab2_norm) - s_b * s_b) / 2;
double b = sqrt((r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) - (v_beta_delta_t) * (v_beta_delta_t)) / 2;
// # 12 flops = 6 DIVs, 3 ADDs, 3 MULTs
//
// $(7):\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert) (\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{4b}$
//
Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b);
// (7):
// # 6 flops = 3 MULTs, 2 DIVs, 1 EXP
//
// $\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert) (\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{4b}$
// $(5):\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$
//
//
Vector2d delta_r_alpha_beta_b = (((r_ab / r_ab_norm) + (r_ab_minus_v_beta_delta_t_e_beta / r_ab_minus_v_beta_delta_t_e_beta_norm)) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm)) / (4 * b);
Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA)));
// (5):
// # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT
//
// $\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$
// $(11):\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
//
Vector2d f_alpha_beta_r_alpha_beta = delta_r_alpha_beta_b * ((V0_AB / SIGMA) * (exp(-b / SIGMA)));
// (11):
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
// Add repulsive effects from other pedestrians to current pedestrian's social force
p->F_i += f_alpha_beta_r_alpha_beta * w(p->e, f_alpha_beta_r_alpha_beta * -1);
p->f_i += f_ped * w(p->e, f_ped * -1);
}
}
// # Done
void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p) {
// # Assume that the four borders are also 4 obstacles
// # 48 n_obstacles flops = 192 flops
void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) {
Vector2d *borders = B.borders;
int n_borders = B.n_borders;
@@ -101,20 +109,26 @@ void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p) {
Vector2d border = borders[i];
// # 17 flops
// $\vec{r}_{\alpha B}$
Vector2d r_alpha_B = p->curr_pos - border;
Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos);
// # 17 flops = 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD, 2 MULTs, 1 DIV, 2 MULTs, 1 EXP, 1 SQRT, 2 MULTs, 1 ADD, 1 DIV
//
// $\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R);
Vector2d F_alpha_B = (r_alpha_B / r_alpha_B.norm()) * (U0_AB / R) * std::exp(-r_alpha_B.norm() / R);
// # 2 ADDs
// p->f_i += f_obst; //commented since this version dont takes the field of view / angle to the border into account
// (12):
// I have read the paper again and on page 6 they say
// However, the formulas above for attractive and repulsive effects only hold for situations that are perceived in the desired direction e_alpha(t) of motion.
// I assumed that with formulas above its also meant for equation 8 (even though not explicitly mentioned their)
// # 14 flops: 9 MULTs, 4 ADDS, 1 SQRT
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$
// $(12):\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$
//
p->F_i += F_alpha_B * w(p->e, F_alpha_B * (-1));
p->f_i += f_obst * w(p->e, f_obst * -1);
}
}
+3 -9
View File
@@ -4,40 +4,34 @@
#include <cmath>
// $\tau_{\alpha}$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double tau_alpha = 0.5;
// $V^0_{\alpha \beta}$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double V0_AB = 2.1;
// $\sigma$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double SIGMA = 0.3;
// $U^0_{\alpha B}$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double U0_AB = 10.0;
// $R$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double R = 0.2;
// $c$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double c = 0.5;
// $\varphi$
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double phi = 100.0;
const double phi = 100.0 / 180.0 * M_PI;
const double cos_phi = cos(phi);
// $\varDelta_{t}$
// step size = 0.2 seconds
// https://arxiv.org/pdf/cond-mat/9805244, page 8
const double delta_t = 2;
const double dt = 0.001;
// # Optional (forces.cpp: compute_repulsive_effects_from_pedestrians)
// const double DISTANCE = 2.0;
+69 -14
View File
@@ -41,6 +41,14 @@ int main(int argc, const char *argv[]) {
int n_p = atoi(argv[1]);
int n_iter = atoi(argv[2]);
// Logic to get/dump the simulation data
const int dump_interval = 50; // dump every 5 iterations -> 20 fps (since dt = 0.01)
const char *dump_path = (argc >= 4) ? argv[3] : nullptr;
std::vector<std::tuple<int, int, double, double, int>> samples; // t, id, x, y, color
if (dump_path)
samples.reserve((size_t)n_iter / dump_interval * n_p);
Pedestrian *pedestrians = init_pedestrians(n_p);
for (int t = 0; t < n_iter; t++) {
@@ -51,41 +59,88 @@ int main(int argc, const char *argv[]) {
p.F_i.reset();
// # 8 flops
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
compute_acceleration_term(&p);
// # 55N - 55 flops
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
//
compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p);
// # 192 flops
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
compute_repulsive_effects_from_borders(B, &p);
compute_repulsive_effects_from_borders(board, &p);
}
// Pass2: integrate the forces and update the current state
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 4 flops = 2 ADDs, 2 MULTs
// integrate preferred velocity: dw/dt = F (Eq. 10)
p.w = p.w + p.f_i * dt;
// Vector2d w_alpha = p.curr_vel + p.f_i * dt; // commented since this was using p.curr_vel instead of p.w (different from paper)
//
// $\vec{w}_i = \vec{v}_i + \vec{F}_idt$
//
p.w_alpha = p.curr_vel + p.F_i * delta_t;
//
// $\vec{v}_i^{t+1} = \vec{w}_ig(\frac{v^{max}_i}{\Arrowvert \vec{w}_i \Arrowvert}) $
//
p.next_vel = p.w_alpha * g(p.v_max, p.w_alpha.norm());
// # 8 flops = 2 MULTs, 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD
p.curr_vel = p.w * g(p.v_max, p.w.norm());
// # 4 flops = 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$
//
p.curr_pos = p.curr_pos + p.curr_vel * delta_t;
}
p.curr_pos = p.curr_pos + p.curr_vel * dt;
for (int i = 0; i < n_p; i++) {
pedestrians[i].update();
// how to handle if a pedestrian reached the end of our simulation/end of the hallway
// The option we do here is to just "teleport" the person back to the beginning
// the good thing is we will have a constant flow of pedestrians and in the simulation we should see how lines build up
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
// basically just keep the y position and reset the x
p.curr_pos.x = 1.0;
// p.destination.y = p.curr_pos.y;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
// p.destination.y = p.curr_pos.y;
}
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
// if we have a dump path/file we wanna save the data for visualisation
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.color);
}
}
}
long long W = (long long)(8 + 55 * n_p - 55 + 192 + 4 + 8 + 4 + 10) * n_p * n_iter;
std::cout << "W(n): ";
std::cout << W;
std::cout << " flops\n";
double T = stop_tsc(start);
std::cout << "T(n): ";
std::cout << T;
std::cout << " cycles\n";
double P = W / T;
std::cout << "P(n): ";
std::cout << P;
std::cout << " flops/cycle\n";
// now we wanna save the data to a csv
if (dump_path) {
std::ofstream out(dump_path);
out << "time,id,x,y,color\n";
for (auto &[t, id, x, y, c] : samples) {
out << t << ',' << id << ',' << x << ',' << y << ',' << c << '\n';
}
}
delete[] pedestrians;
return 0;
}
Regular → Executable
BIN
View File
Binary file not shown.
+9 -33
View File
@@ -3,51 +3,27 @@
#include "vector2d.hpp"
struct Pedestrian {
// TODO: Update initial values
Pedestrian() : curr_vel(), curr_pos(), e(), v_max(1.34 * 1.3), desired_v0(1.34), destination(), F_i(), next_pos(), next_vel(), w_alpha() {}
Pedestrian(Vector2d c_v, Vector2d c_p, Vector2d e, Vector2d d, Vector2d f, Vector2d n_p, Vector2d n_v, Vector2d w_alpha) : curr_vel(c_v), curr_pos(c_p), e(e), v_max(), desired_v0(), destination(d), F_i(f), next_pos(n_p), next_vel(n_v), w_alpha(w_alpha) {}
Color color;
// $\vec{v}_{\alpha}(t)$
Vector2d curr_vel;
Vector2d curr_vel; // v_alpha (actual, ≤ v_max)
// $\vec{w}_{\alpha}$
Vector2d w; // w_alpha (preferred, integrates force)
// $\vec{r}_\alpha(t)$
Vector2d curr_pos;
// $\vec{e}_{\alpha}(t)$
Vector2d e;
// $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$
double v_max;
// $v_\alpha^0$
double desired_v0;
// # =============================================================
// $\vec{r}^0_\alpha$
Vector2d destination;
// social force
Vector2d F_i;
Vector2d f_i;
// # =============================================================
Pedestrian() : color(WHITE), curr_vel(0.0, 0.0), w(0.0, 0.0), curr_pos(0.0, 0.0), e(0.0, 0.0), v_max(0.0), desired_v0(0.0), destination(0.0, 0.0), f_i(0.0, 0.0) {}
// $\vec{r}_\alpha(t+1)$
Vector2d next_pos;
// $\vec{v}_{\alpha}(t + 1)$
Vector2d next_vel;
// $\vec{w}_{\alpha}$
Vector2d w_alpha;
// # =============================================================
void update() {
curr_pos = next_pos;
curr_vel = next_vel;
e = (destination - curr_pos) / (destination - curr_pos).norm();
}
Pedestrian(Color clr, Vector2d c_v, Vector2d c_p, Vector2d e, double v_max, double desired_v0, Vector2d d, Vector2d f)
: color(clr), curr_vel(c_v), w(c_v), // initialize w = v at t=0
curr_pos(c_p), e(e), v_max(v_max), desired_v0(desired_v0), destination(d), f_i(f) {}
};
+999999 -999999
View File
File diff suppressed because it is too large Load Diff
BIN
View File
Binary file not shown.