diff --git a/9805244v1.pdf b/9805244v1.pdf index 37d1464..504f1f1 100644 Binary files a/9805244v1.pdf and b/9805244v1.pdf differ diff --git a/forces.cpp b/forces.cpp index 94395de..5611d06 100644 --- a/forces.cpp +++ b/forces.cpp @@ -28,7 +28,7 @@ void compute_acceleration_term(Pedestrian *p) { // # 55(N - 1) = 55N - 55 flops // -// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\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) { @@ -68,25 +68,25 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV // - // $(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}$ + // $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; // # 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}$ + // $\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); + Vector2d b_grad = ((r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm)) / (4 * b); // # 6 flops = 3 MULTs, 2 DIVs, 1 EXP // - // $(5):\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$ + // $\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$ // Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); // # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT // - // $(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})$ + // $\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})$ // p->f_i += f_ped * w(p->e, f_ped * -1); } @@ -94,6 +94,9 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // # Assume that the four borders are also 4 obstacles // # 48 n_obstacles flops = 192 flops +// +// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$ +// void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { Obstacle *obstacles = board.obstacles; diff --git a/main.cpp b/main.cpp index 150f01c..bf82ed9 100644 --- a/main.cpp +++ b/main.cpp @@ -33,7 +33,7 @@ Pedestrian *init_pedestrians(int n_p) { for (int i = 0; i < n_p / 2; i++) { double y = y_gen(gen); Vector2d start_pos = Vector2d(x_gen(gen), y); - Vector2d destination = Vector2d(BOARD_LENGTH + 300, BOARD_WIDTH / 2); + Vector2d destination = Vector2d(BOARD_LENGTH + 30, BOARD_WIDTH / 2); // double desired_v0 = velocity_gen(gen); double desired_v0; do { @@ -46,7 +46,7 @@ Pedestrian *init_pedestrians(int n_p) { for (int i = n_p / 2; i < n_p; i++) { double y = y_gen(gen); Vector2d start_pos = Vector2d(x_gen(gen), y); - Vector2d destination = Vector2d(-300, BOARD_WIDTH / 2); + Vector2d destination = Vector2d(-30, BOARD_WIDTH / 2); // double desired_v0 = velocity_gen(gen); double desired_v0; do { @@ -88,7 +88,7 @@ int main(int argc, const char *argv[]) { 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> samples; // t, id, x, y, color + std::vector> samples; // t, id, x, y, e.x, e.y, color if (dump_path) samples.reserve((size_t)n_iter / dump_interval * n_p); @@ -137,7 +137,7 @@ int main(int argc, const char *argv[]) { // # 4 flops = 2 ADDs, 2 MULTs // - // $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$ + // $\vec{r}_i^{t+1} = \vec{r}^t_i + v^t_idt$ // p.curr_pos = p.curr_pos + p.curr_vel * dt; @@ -157,9 +157,14 @@ int main(int argc, const char *argv[]) { // 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(); + // p.e = (p.curr_vel - p.destination) / (p.curr_vel - p.destination).norm(); + p.e = (p.destination - (p.curr_vel / p.curr_vel.norm())) / (p.destination - (p.curr_vel / p.curr_vel.norm())).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); + samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.e.x, p.e.y, p.color); } } } @@ -182,9 +187,9 @@ int main(int argc, const char *argv[]) { // 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'; + out << "time,id,x,y,p.e.x,p.e.y,color\n"; + for (auto &[t, id, x, y, e_x, e_y, c] : samples) { + out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n'; } } diff --git a/pedestrian.hpp b/pedestrian.hpp index 03e7a0b..0010a55 100644 --- a/pedestrian.hpp +++ b/pedestrian.hpp @@ -14,7 +14,7 @@ struct Pedestrian { // $\vec{r}_\alpha(t)$ Vector2d curr_pos; // $\vec{e}_{\alpha}(t)$ - Vector2d e; + Vector2d e; // desired direction vector // $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$ double v_max; // $v_\alpha^0$ diff --git a/test_vector_visible_run_out.mp4 b/test_vector_visible_run_out.mp4 new file mode 100644 index 0000000..0dfe538 Binary files /dev/null and b/test_vector_visible_run_out.mp4 differ diff --git a/visualization.py b/visualization.py index e937ef2..b682c49 100644 --- a/visualization.py +++ b/visualization.py @@ -1,52 +1,63 @@ import sys import pandas as pd import matplotlib.pyplot as plt - from matplotlib.animation import FuncAnimation - - - -if len(sys.argv)<2: - sys.exit("Use it like the following: python visualization.py positions.csv (out.gif)") - +if len(sys.argv) < 2: + sys.exit("Use it like the following: python visualization.py positions.csv (out.mp4)") df = pd.read_csv(sys.argv[1]) frames = sorted(df["time"].unique()) -fig, ax = plt.subplots(figsize = (10,2.2)) +fig, ax = plt.subplots(figsize=(10, 2.2)) ax.set_xlim(0, 50); ax.set_ylim(0, 10) ax.set_aspect("equal"); ax.set_facecolor("white") ax.set_xticks([]); ax.set_yticks([]) for s in ("top", "right", "left", "bottom"): ax.spines[s].set_color("gray") - -# Lets define the 2 groups -# WHITE = 1 (left -> right, drawn as filled black circles, like paper) -# BLACK = 0 (right -> left, drawn as empty circles) -s_white = ax.scatter([],[], s=30, facecolors = "black", edgecolor = "black", zorder = 2) -s_black = ax.scatter([],[], s=30, facecolors = "white", edgecolor = "black", zorder = 2) - - - -# update our graphic from t to t+1 +s_white = ax.scatter([], [], s=30, facecolors="black", edgecolor="black", zorder=2) +s_black = ax.scatter([], [], s=30, facecolors="white", edgecolor="black", zorder=2) title = ax.set_title("") + +# Keep a reference to quiver objects so we can remove them each frame +quivers = [] + def update(t): sub = df[df["time"] == t] w = sub[sub["color"] == 1] b = sub[sub["color"] == 0] + s_white.set_offsets(w[["x", "y"]].values) s_black.set_offsets(b[["x", "y"]].values) + + # Remove old quiver arrows + for q in quivers: + q.remove() + quivers.clear() + + # Draw fresh arrows each frame + arrow_scale = 1.5 # Tune this to change arrow length visually + if len(w): + q_white = ax.quiver(w["x"], w["y"], w["p.e.x"], w["p.e.y"], + color="black", angles="xy", scale_units="xy", + scale=1/arrow_scale, width=0.003, + headwidth=4, headlength=5, zorder=3) + quivers.append(q_white) + if len(b): + q_black = ax.quiver(b["x"], b["y"], b["p.e.x"], b["p.e.y"], + color="gray", angles="xy", scale_units="xy", + scale=1/arrow_scale, width=0.003, + headwidth=4, headlength=5, zorder=3) + quivers.append(q_black) + title.set_text(f"t = {t}") return s_white, s_black, title -anim = FuncAnimation(fig,update, frames = frames, interval=50, blit=False) - - +anim = FuncAnimation(fig, update, frames=frames, interval=50, blit=False) if len(sys.argv) >= 3: anim.save(sys.argv[2], writer="ffmpeg", fps=20) - print(f"saved {sys.argv[2]}") + print(f"Saved {sys.argv[2]}") else: plt.show() \ No newline at end of file