diff --git a/CMakeLists.txt b/CMakeLists.txt index 29b152c..47c2e30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,11 @@ cmake_minimum_required(VERSION 3.12) project(hellocmake LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) -if (NOT CMAKE_BUILD_TYPE) +set(CMAKE_BUILD_TYPE Release) + +#[[if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) -endif() +endif()]] add_executable(main main.cpp) +target_compile_options(main PUBLIC -ffast-math -march=native) \ No newline at end of file diff --git a/main.cpp b/main.cpp index cf6369b..de8f1f8 100644 --- a/main.cpp +++ b/main.cpp @@ -8,59 +8,63 @@ float frand() { return (float)rand() / RAND_MAX * 2 - 1; } +constexpr int N = 48; struct Star { - float px, py, pz; - float vx, vy, vz; - float mass; -}; + float px[N], py[N], pz[N]; + float vx[N], vy[N], vz[N]; + float mass[N]; +} stars; -std::vector stars; void init() { - for (int i = 0; i < 48; i++) { - stars.push_back({ - frand(), frand(), frand(), - frand(), frand(), frand(), - frand() + 1, - }); + for (size_t i = 0; i < N; 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; +constexpr float G = 0.001; +constexpr float eps2 = 0.001 * 0.001; +constexpr float dt = 0.01; +constexpr float con = G * dt; 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; + for (size_t i=0; i