Skip to content

Commit

Permalink
particles should be modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
Chlorophytus committed Aug 12, 2019
1 parent 4f21ff1 commit 3e96637
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 2 additions & 0 deletions nbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ int main(int argc, const char *argv[]) {
while (nbody::framebuffer::poll_and_tick())
std::this_thread::sleep_for(std::chrono::milliseconds(1));

std::this_thread::sleep_for(std::chrono::milliseconds(10));
nbody::framebuffer::deinit();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return EXIT_SUCCESS;
}
8 changes: 4 additions & 4 deletions nbody_framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void framebuffer::init(std::uint16_t width, std::uint16_t height, int flags, std

void framebuffer::deinit() {
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_FreeSurface(surface);
SDL_DestroyWindow(window);
delete[] pixels;
}
Expand Down Expand Up @@ -57,9 +57,9 @@ bool framebuffer::poll_and_tick() {
std::uint32_t oldr = (oldpixels & 0xFF000000) >> 24;
std::uint32_t oldg = (oldpixels & 0x00FF0000) >> 16;
std::uint32_t oldb = (oldpixels & 0x0000FF00) >> 8;
oldr = oldr * 0.99f;
oldg = oldg * 0.99f;
oldb = oldb * 0.99f;
oldr = oldr * 0.9f;
oldg = oldg * 0.9f;
oldb = oldb * 0.9f;
pixels[(i * w) + j] = (oldr << 24) | (oldg << 16) | (oldb << 8) | 0x000000FF;
}
}
Expand Down
25 changes: 11 additions & 14 deletions nbody_particle_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ bool particle_system::step(std::size_t stride) {
float particles_dy[9]{0.0f};

for(std::uint8_t i = 0; i < 9; i++) {
while(!single_file.try_lock())
std::this_thread::sleep_for(std::chrono::microseconds(1));
while(!_single_file.try_lock());
auto j = _particles.at(stride).front().get();
single_file.unlock();
_single_file.unlock();
particles_mass[i] = j->mass;
particles_x[i] = j->x;
particles_dx[i] = j->dx;
particles_y[i] = j->y;
particles_dy[i] = j->dy;
while(!single_file.try_lock())
std::this_thread::sleep_for(std::chrono::microseconds(1));
while(!_single_file.try_lock());
_particles.at(stride).pop_front();
single_file.unlock();
_single_file.unlock();
}

auto leader_x = _mm256_set1_ps(particles_x[0]);
Expand All @@ -40,8 +38,8 @@ bool particle_system::step(std::size_t stride) {
auto v_dx = _mm256_loadu_ps(particles_dx + 1);
auto v_dy = _mm256_loadu_ps(particles_dy + 1);
auto v_force = _mm256_set1_ps(force);
// Do the calculations now.

// Do the calculations now.
auto v_ord_x = _mm256_sub_ps(v_x, leader_x);
auto v_ord_y = _mm256_sub_ps(v_y, leader_y);
v_dx = _mm256_fmadd_ps(v_force, _mm256_mul_ps(leader_mass, v_ord_x), v_dx);
Expand All @@ -67,28 +65,27 @@ bool particle_system::step(std::size_t stride) {
_mm256_storeu_ps(particles_dy + 1, v_dy);

for(std::uint8_t i = 0; i < 9; i++) {
while(!single_file.try_lock())
std::this_thread::sleep_for(std::chrono::microseconds(1));
while(!_single_file.try_lock());
auto j = new particle {
particles_x[i],
fmodf(particles_x[i], static_cast<float>(_w)),
particles_dx[i],
particles_y[i],
fmodf(particles_y[i], static_cast<float>(_h)),
particles_dy[i],
particles_mass[i]
};
_late_particles.emplace(j);

single_file.unlock();
_single_file.unlock();
}

return true;
}

particle_system::particle_system(std::size_t num_particles, std::uint16_t w, std::uint16_t h) : _rand(new randomiser<float>{}){
particle_system::particle_system(std::size_t num_particles, std::uint16_t w, std::uint16_t h) : _rand(new randomiser<float>{}), _w(w), _h(h) {
auto i = num_particles;
while(i--) {
auto p = new particle {
_rand->shake(static_cast<float>(w)/16.0f), 0.0f, _rand->shake(static_cast<float>(h)/16.0f), 0.0f, fabsf(_rand->shake(32.0f))
_rand->shake(static_cast<float>(w)/4.0f), 0.0f, _rand->shake(static_cast<float>(h)/4.0f), 0.0f, fabsf(_rand->shake(32.0f))
};
_late_particles.emplace(p);
}
Expand Down
6 changes: 3 additions & 3 deletions nbody_particle_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace nbody {
std::queue<std::unique_ptr<particle>> _late_particles{};
std::forward_list<std::unique_ptr<particle>> _early_particles; // an initial sort determines which particles go where
std::vector<std::forward_list<std::unique_ptr<particle>>> _particles{}; // a quicker way to determine if a stack is at its full 16 limit
std::mutex single_file;

std::mutex _single_file;
std::uint16_t _w, _h;
public:
static bool compare_two_particles(const std::unique_ptr<particle> &, const std::unique_ptr<particle> &);
constexpr static float force = -0.0001f;
constexpr static float force = -0.00001f;
explicit particle_system(std::size_t, std::uint16_t, std::uint16_t);
bool step(std::size_t);
std::vector<particle *> stir();
Expand Down

0 comments on commit 3e96637

Please sign in to comment.