From e3f624d950612277a5de0dacc8c53a1112b55d05 Mon Sep 17 00:00:00 2001 From: Cory Bloor Date: Tue, 25 Feb 2020 04:49:18 -0700 Subject: [PATCH] Refactor maxabs_u and maxabs_v into one function --- main.c | 37 +++++++++++++++---------------------- misc/util.h | 8 ++++++++ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index c545bf3..a851c15 100644 --- a/main.c +++ b/main.c @@ -160,6 +160,15 @@ bool valid(uint8_t p_validity[Y][X], vec2i idx, celltype_t type) { } } +vec2i grid_size(celltype_t type) { + switch (type) { + default: assert(false); + case P: return v2i(P_X, P_Y); + case U: return v2i(U_X, U_Y); + case V: return v2i(V_X, V_Y); + } +} + float valid_neighbor_average(float q[Y][X], vec2i lower, vec2i upper, celltype_t type) { float total = 0.f; int count = 0; @@ -614,10 +623,6 @@ int8_t get_a_minus_j(size_t y, size_t x) { return y>0 ? g_a[y-1][x].a_plus_j : 0; } -double sq(double x) { - return x*x; -} - double g_precon[Y][X]; double g_q[Y][X]; @@ -844,24 +849,12 @@ void project(float dt, float u[Y][X], float v[Y][X], float uout[Y][X], float vou } } -float maxabs_u(float q[Y][X]) { - float max = 0; - for (size_t y = 0; y < Y; ++y) { - for (size_t x = 0; x < X-1; ++x) { - float value = fabsf(q[y][x]); - if (value > max) { - max = value; - } - } - } - return max; -} - -float maxabs_v(float q[Y][X]) { +float maxsq(float q[Y][X], celltype_t type) { + vec2i size = grid_size(type); float max = 0; - for (size_t y = 0; y < Y-1; ++y) { - for (size_t x = 0; x < X; ++x) { - float value = fabsf(q[y][x]); + for (int y = 0; y < size.y; ++y) { + for (int x = 0; x < size.x; ++x) { + float value = sqf(q[y][x]); if (value > max) { max = value; } @@ -901,7 +894,7 @@ float calculate_timestep(float frame_time) { const float m = 0.75f; // maximum number of cells to traverse in one step float dt; - float max_velocity = sqrtf(sq(maxabs_u(g_u)) + sq(maxabs_v(g_v))); + float max_velocity = sqrtf(maxsq(g_u, U) + maxsq(g_v, V)); if (max_velocity < (m*k_s / frame_time)) { dt = frame_time; } else { diff --git a/misc/util.h b/misc/util.h index c7ef9e9..a3bb6a7 100644 --- a/misc/util.h +++ b/misc/util.h @@ -27,3 +27,11 @@ static inline float clampf(float min, float x, float max) { return x; } } + +static inline float sqf(float x) { + return x*x; +} + +static inline double sq(double x) { + return x*x; +}