Add viewer

This commit is contained in:
ProtoByter 2023-02-08 08:27:56 +00:00
parent 17ef8dd98a
commit 22f65402c7
3 changed files with 222 additions and 14 deletions

View File

@ -17,12 +17,16 @@ add_executable(cudagravity_sim sim/main.cu common/particle.hpp common/definition
target_include_directories(cudagravity_sim PUBLIC common)
target_link_libraries(cudagravity_sim PUBLIC glm::glm SDL2::SDL2)
target_link_libraries(cudagravity_sim PUBLIC glm::glm)
set_target_properties(cudagravity_sim PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
# Viewer
find_package(PkgConfig)
pkg_check_modules(SDL2GFX REQUIRED SDL2_gfx)
add_executable(cudagravity_viewer viewer/main.cpp common/particle.hpp common/definitions.hpp)
target_include_directories(cudagravity_viewer PUBLIC common)
target_include_directories(cudagravity_viewer PUBLIC common ${SDL2GFX_INCLUDE_DIRS}})
target_link_libraries(cudagravity_viewer PUBLIC glm::glm SDL2::SDL2 ${SDL2GFX_LIBRARIES})

View File

@ -65,20 +65,18 @@ float get_random() {
}
int main() {
exportHelper sdlHelper;
exportHelper exportHelper;
size_t N = 200;
size_t N = 2000;
CudaMemory<Particle> particles = CudaMemory<Particle>(N);
CudaMemory<vec2> accelerations = CudaMemory<vec2>(N);
for (int i = 0; i < N; i++) {
accelerations[i] = vec2(0, 0);
particles[i] = Particle{1, vec2(get_random(), get_random()), vec2(get_random(), get_random())};
particles[i] = Particle{10, vec2(get_random(), get_random()), vec2(get_random(), get_random())};
}
//cudaDeviceGetAttribute((int*)(&threadsPerBlock_cpu.x), cudaDevAttrMaxThreadsPerBlock, 0);
dim3 n_blocksPerGrid = dim3(
((N) + threadsPerBlock_cpu.x - 1) / threadsPerBlock_cpu.x
);
@ -92,7 +90,7 @@ int main() {
particles.send();
accelerations.send();
sdlHelper.setParticles(&particles, N);
exportHelper.setParticles(&particles, N);
while(true) {
run_step<<<n_blocksPerGrid, threadsPerBlock_cpu>>>(particles.getDevicePointer(), accelerations.getDevicePointer(), N);
@ -101,14 +99,14 @@ int main() {
accelerations.send();
sdlHelper.epoch();
exportHelper.epoch();
if (!sdlHelper.should_stop) {
if (!exportHelper.should_stop) {
break;
}
}
sdlHelper.stop();
exportHelper.stop();
cudaDeviceSynchronize();

View File

@ -1,7 +1,213 @@
//
// Created by kai on 07/02/23.
//
#include <cstdlib>
#include <SDL2/SDL.h>
#include <SDL2_rotozoom.h>
#include <SDL2_framerate.h>
#include <vector>
#include <unistd.h>
#include "definitions.hpp"
#include "particle.hpp"
int main() {
double dt = 0.01;
double t = 1.0;
size_t current_idx = 1;
bool pause = true;
// Toggles
bool show_fps = false;
bool show_help = true;
bool show_zoom = true;
bool show_state = true;
bool show_speed = true;
bool show_grid = true;
bool auto_size = true;
bool shift = false;
bool ctrl = false;
bool update_required = true;
// Init SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Verlet", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 800, SDL_WINDOW_SHOWN);
SDL_Event event;
SDL_Surface* surf = SDL_CreateRGBSurface(0, 800, 800, 32, 0, 0, 0, 0);
std::vector<Particle> particles;
FPSmanager fps;
SDL_initFramerate(&fps);
SDL_setFramerate(&fps, 60);
float max_dim = 0.0;
while (true) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit(0);
} else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_RSHIFT:
case SDLK_LSHIFT:
shift = true;
break;
case SDLK_RCTRL:
case SDLK_LCTRL:
ctrl = true;
break;
case SDLK_SPACE:
pause = !pause;
printf("Paused: %s\n", pause ? "true" : "false");
break;
case SDLK_LEFT:
t -= dt;
current_idx = (int)t;
update_required = true;
break;
case SDLK_RIGHT:
t += dt;
current_idx = (int)t;
update_required = true;
break;
case SDLK_KP_PLUS:
dt *= 2;
printf("dt: %f\n", dt);
break;
case SDLK_KP_MINUS:
dt /= 2;
printf("dt: %f\n", dt);
break;
case SDLK_KP_0:
t = 1;
current_idx = 1;
update_required = true;
break;
case SDLK_UP:
if (shift) {
max_dim -= 1;
} else if (ctrl) {
max_dim -= 10;
} else {
max_dim -= 0.1;
}
update_required = true;
break;
case SDLK_DOWN:
if (shift) {
max_dim += 1;
} else if (ctrl) {
max_dim += 10;
} else {
max_dim += 0.1;
}
update_required = true;
break;
case SDLK_a:
auto_size = !auto_size;
update_required = true;
printf("Auto size: %s\n", auto_size ? "true" : "false");
break;
}
} else if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) {
case SDLK_RSHIFT:
case SDLK_LSHIFT:
shift = false;
break;
case SDLK_RCTRL:
case SDLK_LCTRL:
ctrl = false;
break;
}
}
}
if (!pause) {
t += dt;
current_idx = (int)t;
update_required = true;
}
if (update_required) {
SDL_FillRect(surf, nullptr, SDL_MapRGB(surf->format, 0, 0, 0));
particles.clear();
char filename[256];
sprintf(filename, "./output/%zu.dat", current_idx);
FILE* f = fopen(filename, "rb");
if (f == nullptr) {
printf("File not found: %s\n", filename);
printf("End of simulation\n");
pause = true;
} else {
char *cur_line = (char *)malloc(64 * sizeof(char));
size_t len = 64;
while (true) {
if (getline(&cur_line, &len, f) <= 0) {
break;
}
float x, y, vx, vy, m;
if (sscanf(cur_line, "%f %f %f %f %f", &x, &y, &vx, &vy, &m) != 5) {
break;
}
particles.push_back(Particle{m, {x, y}, {vx, vy}});
}
fclose(f);
if (auto_size) {
for (auto &particle: particles) {
max_dim = std::max(max_dim,
std::max(std::abs(particle.position.x), std::abs(particle.position.y)));
}
}
for (auto &particle: particles) {
particle.position /= (max_dim * 1.1);
particle.position *= 400.0;
particle.position += vec2(400, 400);
SDL_Rect rect = {(int) particle.position.x - 1, (int) particle.position.y - 1, 2, 2};
SDL_FillRect(surf, &rect, SDL_MapRGB(surf->format, 255, 255, 255));
}
}
update_required = false;
}
SDL_framerateDelay(&fps);
SDL_Surface* window_surface = SDL_GetWindowSurface(window);
SDL_BlitSurface(surf, NULL, window_surface, NULL);
SDL_UpdateWindowSurface(window);
}
return 0;
}