diff --git a/forces.cpp b/forces.cpp index f44b419..c5c7163 100644 --- a/forces.cpp +++ b/forces.cpp @@ -1,11 +1,12 @@ #include "forces.hpp" #include -// # Done +// # 8 flops = 5 MULTs, 2 ADD, 1 SQRT double w(Vector2d e, Vector2d f) { if (e.dot(f) >= f.norm() * cos_phi) return 1.0; - return c; + else + return c; } // # 2 DIVs @@ -26,10 +27,8 @@ void compute_acceleration_term(Pedestrian *p) { Vector2d desired_velocity = p->e * p->desired_v0; // $\vec{v}_{\alpha}(t)$ Vector2d actual_velocity = p->curr_vel; - - double tau_alpha_inv = 1 / tau_alpha; - - p->F_i += (desired_velocity - actual_velocity) * tau_alpha_inv; + // # 6 flops = 4 ADDs, 2 DIVs + p->f_i += (desired_velocity - actual_velocity) / tau_alpha; } // # 55(N - 1) = 55N - 55 flops @@ -102,12 +101,12 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // # 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; + Obstacle *obstacles = board.obstacles; + int n_borders = board.n_obstacles; for (int i = 0; i < n_borders; i++) { - Vector2d border = borders[i]; + Obstacle obstacle = obstacles[i]; // # 17 flops // $\vec{r}_{\alpha B}$ diff --git a/forces.hpp b/forces.hpp index e6625aa..6a24f9a 100644 --- a/forces.hpp +++ b/forces.hpp @@ -37,6 +37,8 @@ const double dt = 0.001; double w(Vector2d e, Vector2d f); +double g(double v_max_alpha, double w_alpha_norm); + void compute_acceleration_term(Pedestrian *p); void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N); diff --git a/main.cpp b/main.cpp index 6de3083..d247b91 100644 --- a/main.cpp +++ b/main.cpp @@ -2,14 +2,15 @@ #include "forces.hpp" #include "math.h" #include "pedestrian.hpp" +#include "tsc_x86.h" #include "vector2d.hpp" +#include +#include +#include +#include -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; -} +#define BOARD_WIDTH 10.0 +#define BOARD_LENGTH 50.0 using namespace std; @@ -23,20 +24,44 @@ using namespace std; Pedestrian *init_pedestrians(int n_p) { Pedestrian *P = new Pedestrian[n_p]; - for (int i = 0; i < n_p; i++) { - P[i] = Pedestrian(); + std::random_device rd; // use for truly random simulation + // std::mt19937 gen(rd()); // use rd instead of 42 as seed for real randomness + std::mt19937 gen(42); // use fixed seed for reproducibility + std::normal_distribution velocity_gen(1.34, 0.26); // 1.34 and 0.26 are the mean and stdrd deviation used in paper + std::uniform_real_distribution x_gen(2, BOARD_LENGTH - 2); + std::uniform_real_distribution y_gen(2, BOARD_WIDTH - 2); + 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); + // double desired_v0 = velocity_gen(gen); + double desired_v0; + do { + desired_v0 = velocity_gen(gen); + } while (desired_v0 <= 0.1); // to avoid small values (Way below the social force -> that would make the walk backwards) (we discovered some weird stuff in the simulation) + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(WHITE, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d()); + } + + 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); + // double desired_v0 = velocity_gen(gen); + double desired_v0; + do { + desired_v0 = velocity_gen(gen); + } while (desired_v0 <= 0.1); + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(BLACK, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d(0, 0)); } return P; } -Board init_board(int width, int length) { - Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)}; - return Board(width, length, 4, obs); -} - int main(int argc, const char *argv[]) { - Board B = init_board(10, 15); + Obstacle obs[] = {}; + Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs); int n_p = atoi(argv[1]); int n_iter = atoi(argv[2]); @@ -51,13 +76,17 @@ int main(int argc, const char *argv[]) { Pedestrian *pedestrians = init_pedestrians(n_p); - for (int t = 0; t < n_iter; t++) { + // # Start here or right after function signature + uint64_t start = start_tsc(); + // # (8 + 55N - 55 + 192 + 4 + 8 + 4 + 10)*n_p*n_iter + for (int t = 0; t < n_iter; t++) { + // Pass1: conmpute all forces from the current state for (int i = 0; i < n_p; i++) { Pedestrian &p = pedestrians[i]; - p.F_i.reset(); + p.f_i.reset(); // # 8 flops // diff --git a/pedestrian.hpp b/pedestrian.hpp index 8a0b281..03e7a0b 100644 --- a/pedestrian.hpp +++ b/pedestrian.hpp @@ -2,6 +2,9 @@ #include "random" #include "vector2d.hpp" +// For visual simulation +enum Color { BLACK, WHITE }; + struct Pedestrian { Color color; // $\vec{v}_{\alpha}(t)$