diff --git a/CMakeLists.txt b/CMakeLists.txt index 29b152c..030e791 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,3 +7,4 @@ if (NOT CMAKE_BUILD_TYPE) endif() add_executable(main main.cpp) +target_compile_options(main PUBLIC --fast-math -march=native -O3) \ No newline at end of file diff --git a/main.cpp b/main.cpp index cf6369b..d028124 100644 --- a/main.cpp +++ b/main.cpp @@ -3,64 +3,68 @@ #include #include #include - +#include float frand() { return (float)rand() / RAND_MAX * 2 - 1; } - +const size_t num = 48; struct Star { - float px, py, pz; - float vx, vy, vz; - float mass; + float px[num], py[num], pz[num]; + float vx[num], vy[num], vz[num]; + float mass[num]; }; -std::vector stars; +Star stars; void init() { - for (int i = 0; i < 48; i++) { - stars.push_back({ - frand(), frand(), frand(), - frand(), frand(), frand(), - frand() + 1, - }); + for (int i = 0; i < num; i++) { + stars.px[i] = frand(); + stars.py[i] = frand(); + stars.pz[i] = frand(); + stars.vx[i] = frand(); + stars.vy[i] = frand(); + stars.vz[i] = frand(); + stars.mass[i] = frand()+1; } } float G = 0.001; float eps = 0.001; float dt = 0.01; - +float eps2 = eps * eps; void step() { - for (auto &star: stars) { - for (auto &other: stars) { - float dx = other.px - star.px; - float dy = other.py - star.py; - float dz = other.pz - star.pz; - float d2 = dx * dx + dy * dy + dz * dz + eps * eps; - d2 *= sqrt(d2); - star.vx += dx * other.mass * G * dt / d2; - star.vy += dy * other.mass * G * dt / d2; - star.vz += dz * other.mass * G * dt / d2; + float G_dt = G*dt; + for (size_t i = 0;i