Skip to content

Commit

Permalink
Initialize simulation based on a scenario file
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmb committed May 31, 2017
1 parent 4cff784 commit 4a39cf8
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ endif()
set(SOURCES
main.cxx
misc/terminal.cxx
misc/file.cxx
)

add_executable(euler ${SOURCES})
75 changes: 54 additions & 21 deletions main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

#include "math/vec2f.h"
#include "misc/terminal.h"
#include "misc/file.h"

const size_t X = 20;
const size_t Y = 20;
const size_t X = 100;
const size_t Y = 40;

struct args_t {
const char* scenario_file;
};

const float k_s = 1; // side length
const float k_invs = 1/k_s;
Expand All @@ -35,7 +40,8 @@ struct sparse_entry_t {
int8_t a_plus_j;
} g_a[Y][X];

const size_t N = 8*8*4;
const size_t N = 4*Y*X;
size_t g_markers_length;
vec2f g_markers[N];
uint8_t g_marker_count[Y][X];
uint8_t g_old_marker_count[Y][X];
Expand All @@ -55,7 +61,7 @@ float clampf(float min, float x, float max) {
void refresh_marker_counts() {
memcpy(g_old_marker_count, g_marker_count, sizeof(g_old_marker_count));
memset(g_marker_count, '\0', sizeof(g_marker_count));
for (size_t i = 0; i < N; ++i) {
for (size_t i = 0; i < g_markers_length; ++i) {
int x = (int)floorf(g_markers[i].x()*k_invs);
int y = (int)floorf(g_markers[i].y()*k_invs);
g_marker_count[y][x]++;
Expand Down Expand Up @@ -122,32 +128,49 @@ void extrapolate_velocity_field() {
}
}

void sim_init() {
// setup walls
for (size_t i = 0; i < X; ++i) {
g_solid[0][i] = true;
void sim_init(args_t in) {
int length;
char* contents = load_file(in.scenario_file, &length);
if (!contents) {
fprintf(stderr, "Could not load %s!\n", in.scenario_file);
exit(1);
}
for (size_t i = 0; i < Y; ++i) {
g_solid[i][0] = true;
g_solid[i][X-1] = true;

// parse the scenario file to init our fluid
int i = 0;
bool fluid[Y][X] = {};
for (size_t y = Y-2; y > 0 && i < length; --y) {
for (size_t x = 1; x < X-1 && i < length; ++x) {
char c = contents[i++];
if (c == '\n') {
break;
} else if (c == 'X') {
g_solid[y][x] = true;
} else if (c == '0') {
fluid[y][x] = true;
}
}
}
release_file(contents);

std::mt19937 rng_engine(123456789u);
std::uniform_real_distribution<float> distribution(0.f, 0.5f);
auto rng = [&](){ return distribution(rng_engine); };

// setup fluid markers, 4 per cell, jittered
size_t idx = 0;
for (size_t i = 1; i < 9; ++i) {
for (size_t j = 11; j < 19; ++j) {
for (size_t k = 0; k < 4; ++k) {
float x = i + (k < 2 ? 0 : 0.5f) + rng();
float y = j + (k % 2 ? 0 : 0.5f) + rng();
assert(idx < N);
g_markers[idx++] = k_s*vec2f{x,y};
for (size_t i = 0; i < X; ++i) {
for (size_t j = 0; j < Y; ++j) {
if (fluid[j][i]) {
for (size_t k = 0; k < 4; ++k) {
float x = i + (k < 2 ? 0 : 0.5f) + rng();
float y = j + (k % 2 ? 0 : 0.5f) + rng();
g_markers[idx++] = k_s*vec2f{x,y};
}
}
}
}
g_markers_length = idx;
refresh_marker_counts();
}

Expand Down Expand Up @@ -469,7 +492,7 @@ vec2f velocity_at(vec2f pos) {
// digital differential analyzer collisions
// https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)
void advect_markers(float dt) {
for (size_t i = 0; i < N; ++i) {
for (size_t i = 0; i < g_markers_length; ++i) {
vec2f p = g_markers[i];
vec2f v = velocity_at(p);
if (v.x() == 0.f && v.y() == 0.f) {
Expand Down Expand Up @@ -903,13 +926,23 @@ void process_keypress() {
}
}

args_t parse_args(int argc, char** argv) {
args_t in;
if (argc < 2) {
fprintf(stderr, "usage: %s <scenario>\n", argv[0]);
exit(1);
}
in.scenario_file = argv[1];
return in;
}

int main(int argc, char** argv) {
(void)argc; (void)argv;
args_t in = parse_args(argc, argv);

enable_raw_mode();
buffer buf = { 0, 0 };

sim_init();
sim_init(in);

while (1) {
refresh_screen(&buf);
Expand Down
16 changes: 16 additions & 0 deletions scenarios/basic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
XXXXXXXXXXXXXXXXXXXX
X X
X 00000000 X
X 00000000 X
X 00000000 X
X 00000000 X
X 00000000 X
X 00000000 X
X 00000000 X
X 00000000 X
X X
X X
X X
X X
X X
XXXXXXXXXXXXXXXXXXXX
38 changes: 38 additions & 0 deletions scenarios/big.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X X
X X
X X
X X
X X
X X
X X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X 000000000000000000000000000000000000000000000000000 X
X X
X X
X X
X X
X X
X X
X X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
31 changes: 31 additions & 0 deletions scenarios/filter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X 0000000000000000000 X
X X X
X X X
X X X
XXXXXXXXXXXXXXXXXXXXXXXXXX X
X X
X X X
X X X
X X X
XXXXXXXXXXXXXXXXXXXXXXXXXX X
X X
X X
X X
X X
XXXXXXXXXXXXXXXXXXXXXXXXXX
26 changes: 26 additions & 0 deletions scenarios/weird-edges.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X000000000000000 XX 00000000000000 XX 000000000000000X
X XX XX X
X XX XX X
X XX XX X
X XX XX X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
X0000000000000000X
X0000000000000000X
X0000000000000000X
X0000000000000000X
X0000000000000000X
X0000000000000000X
X0000000000000000X
X X
X X
X X
X X
XXXXXXXXXXXXXXXXXX

0 comments on commit 4a39cf8

Please sign in to comment.