Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

finish hw04 #43

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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ if (NOT CMAKE_BUILD_TYPE)
endif()

add_executable(main main.cpp)

target_compile_options(main PUBLIC -ffast-math -march=native)
87 changes: 83 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <chrono>
#include <cmath>
#include <array>

float frand() {
return (float)rand() / RAND_MAX * 2 - 1;

const int N = 48;

static float frand() {
return (float)std::rand() / RAND_MAX * 2 - 1;
}

/*
struct Star {
float px, py, pz;
float vx, vy, vz;
float mass;
};
*/

struct Star{
std::array<float, N> px, py, pz;
std::array<float, N> vx, vy, vz;
std::array<float, N> mass;
};

std::vector<Star> stars;
Star stars;

/*
void init() {
for (int i = 0; i < 48; i++) {
stars.push_back({
Expand All @@ -25,11 +37,26 @@ void init() {
});
}
}
*/

void init() {
for (int 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();
}
}


float G = 0.001;
float eps = 0.001;
float dt = 0.01;

/*
void step() {
for (auto &star: stars) {
for (auto &other: stars) {
Expand All @@ -49,7 +76,38 @@ void step() {
star.pz += star.vz * dt;
}
}
*/

void step() {
const float t = G * dt;
const float epss = eps * eps;
for (int i = 0; i < N; i ++) {
float px = stars.px[i], py = stars.py[i], pz = stars.pz[i];
float vx = 0.0f, vy = 0.0f, vz = 0.0f;
for (int j = 0; j < N; j ++) {
float dx = stars.px[j] - px;
float dy = stars.py[j] - py;
float dz = stars.pz[j] - pz;
float d2 = dx * dx + dy * dy + dz * dz + epss;
d2 *= std::sqrt(d2);
float xx = (1 / d2) * t * stars.mass[j];
vx += dx * xx;
vy += dy * xx;
vz += dz * xx;
}
stars.vx[i] += vx;
stars.vy[i] += vy;
stars.vz[i] += vz;
}

for (int i = 0; i < N; i ++) {
stars.px[i] += stars.vx[i] * dt;
stars.py[i] += stars.vy[i] * dt;
stars.pz[i] += stars.vz[i] * dt;
}
}

/*
float calc() {
float energy = 0;
for (auto &star: stars) {
Expand All @@ -65,6 +123,27 @@ float calc() {
}
return energy;
}
*/

float calc() {
float energy = 0;
const float epss = eps * eps;
for (int i = 0; i < N; 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;
float px = stars.px[i], py = stars.py[i], pz = stars.pz[i];
for (int j = 0; j < N; j ++) {
float dx = stars.px[j] - px;
float dy = stars.py[j] - py;
float dz = stars.py[j] - pz;
float d2 = dx * dx + dy * dy + dz * dz + epss;
float s_d2 = 1 / std::sqrt(d2);
energy -= stars.mass[j] * stars.mass[i] * G * 0.5;
}
}
return energy;
}


template <class Func>
long benchmark(Func const &func) {
Expand Down