Skip to content

speed #40

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ if (NOT CMAKE_BUILD_TYPE)
endif()

add_executable(main main.cpp)

if(MSVC)
else()
target_compile_options(main PUBLIC
-Wall
-Werror
-O3
-march=native
-ffast-math
)
endif()
85 changes: 48 additions & 37 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,74 @@
#include <chrono>
#include <cmath>

constexpr float daoshu = 1 / RAND_MAX;
float frand() {
return (float)rand() / RAND_MAX * 2 - 1;
return (float)rand() * daoshu * 2 - 1;
}


constexpr int N = 48;
struct Star {
float px, py, pz;
float vx, vy, vz;
float mass;
float px[N];
float py[N];
float pz[N];
float vx[N];
float vy[N];
float vz[N];
float mass[N];
};

std::vector<Star> stars;
Star 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 < 48; 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;

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;
void step() {

for(size_t i = 0; i < 48; i++) {
for(size_t j = 0; j < 48; j++) {
float dx = stars.px[j] - stars.px[i];
float dy = stars.py[j] - stars.py[i];
float dz = stars.pz[j] - stars.pz[i];
float d2 = dx * dx + dy * dy + dz * dz + eps * eps;
d2 *= std::sqrt(d2);
float dao = 1 / d2;

stars.vx[i] += dx * stars.mass[j] * (G * dt * dao);
stars.vy[i] += dy * stars.mass[j] * (G * dt * dao);
stars.vz[i] += dz * stars.mass[j] * (G * dt * dao);
}
stars.px[i] += stars.vx[i] * dt;
stars.py[i] += stars.vy[i] * dt;
stars.pz[i] += stars.vz[i] * dt;
}
}
for (auto &star: stars) {
star.px += star.vx * dt;
star.py += star.vy * dt;
star.pz += star.vz * dt;
}
}

float calc() {
float energy = 0;
for (auto &star: stars) {
float v2 = star.vx * star.vx + star.vy * star.vy + star.vz * star.vz;
energy += star.mass * v2 / 2;
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;
energy -= other.mass * star.mass * G / sqrt(d2) / 2;
for(size_t i = 0; i < 48; ++i) {
float v2 = stars.vx[i] * stars.vx[i] + stars.vy[i] * stars.vy[i] + stars.vz[i] * stars.vz[i];
energy += stars.mass[i] * v2 * 0.5f;
for(size_t j = 0; j < 48; j++) {
float dx = stars.px[j] - stars.px[i];
float dy = stars.py[j] - stars.py[i];
float dz = stars.pz[j] - stars.pz[i];
float d2 = dx * dx + dy * dy + dz * dz + (eps * eps);
energy -= stars.mass[j] * stars.mass[i] / std::sqrt(d2) * (0.5f * G);
}
}
return energy;
Expand Down