From 843b0fd61b1e31ddb683a8bdd344f28caa46f662 Mon Sep 17 00:00:00 2001 From: Pierce_qiang <2579404335@qq.com> Date: Mon, 10 Jan 2022 21:27:20 +0800 Subject: [PATCH 1/2] Done! --- CMakeLists.txt | 1 + main.cpp | 110 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 75 insertions(+), 36 deletions(-) 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..df4fbbc 100644 --- a/main.cpp +++ b/main.cpp @@ -3,64 +3,102 @@ #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; + // stars[i] = { + // frand(), frand(), frand(), + // frand(), frand(), frand(), + // 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 (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 + eps2; + // d2 *= std::sqrt(d2); + // float inv_d2 = 1.0f/d2; + // star.vx += dx * other.mass * G_dt * inv_d2; + // star.vy += dy * other.mass * G_dt * inv_d2; + // star.vz += dz * other.mass * G_dt * inv_d2; + // } + // } + for (size_t i = 0;i Date: Mon, 10 Jan 2022 21:39:38 +0800 Subject: [PATCH 2/2] done! --- main.cpp | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/main.cpp b/main.cpp index df4fbbc..d028124 100644 --- a/main.cpp +++ b/main.cpp @@ -25,11 +25,6 @@ void init() { stars.vy[i] = frand(); stars.vz[i] = frand(); stars.mass[i] = frand()+1; - // stars[i] = { - // frand(), frand(), frand(), - // frand(), frand(), frand(), - // frand() + 1, - // }; } } @@ -39,19 +34,6 @@ float dt = 0.01; float eps2 = eps * eps; void step() { float G_dt = G*dt; - // 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 + eps2; - // d2 *= std::sqrt(d2); - // float inv_d2 = 1.0f/d2; - // star.vx += dx * other.mass * G_dt * inv_d2; - // star.vy += dy * other.mass * G_dt * inv_d2; - // star.vz += dz * other.mass * G_dt * inv_d2; - // } - // } for (size_t i = 0;i