summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2019-02-06 14:07:46 +0100
committerAnton Khirnov <anton@khirnov.net>2019-02-06 14:08:46 +0100
commit5fdcc27aeaffe0da45808f860416b94b0795e235 (patch)
treefb09b16e3d4cc431b671a0251ede35b4e5702139
parent6aa61c3e38a164b5d015d6ab67e15a46a5b9bc15 (diff)
mg2d_test: use the new boundary API to simplify code
-rw-r--r--mg2d_test.c69
1 files changed, 16 insertions, 53 deletions
diff --git a/mg2d_test.c b/mg2d_test.c
index 9c3ac17..a60c5b2 100644
--- a/mg2d_test.c
+++ b/mg2d_test.c
@@ -8,9 +8,13 @@
#include "mg2d_boundary.h"
#include "mg2d_constants.h"
+#define ARRAY_ELEMS(x) (sizeof(x) / sizeof(*x))
+
#define MAXITER 64
#define TOL 1e-9
+#define DOMAIN_SIZE 1.0
+
static const double pde_coeffs[MG2D_DIFF_COEFF_NB] = {
[MG2D_DIFF_COEFF_00] = 1.0,
[MG2D_DIFF_COEFF_10] = 0.9,
@@ -105,8 +109,8 @@ int main(int argc, char **argv)
return 1;
}
- ctx->step[0] = 1.0 / (gridsize - 1);
- ctx->step[1] = 1.0 / (gridsize - 1);
+ ctx->step[0] = DOMAIN_SIZE / (gridsize - 1);
+ ctx->step[1] = DOMAIN_SIZE / (gridsize - 1);
ctx->fd_stencil = 2;
@@ -118,24 +122,12 @@ int main(int argc, char **argv)
ctx->nb_threads = 1;
ctx->log_level = MG2D_LOG_VERBOSE;
- {
- MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0L];
-
- bnd->type = BC_TYPE;
+ for (int bnd_loc = 0; bnd_loc < ARRAY_ELEMS(ctx->boundaries); bnd_loc++) {
+ MG2DBoundary *bnd = ctx->boundaries[bnd_loc];
+ const int ci = mg2d_bnd_coord_idx(bnd_loc);
+ const int bnd_dir = mg2d_bnd_out_dir(bnd_loc);
- memset(bnd->val, 0, gridsize * sizeof(*bnd->val));
-
- if (bnd->type == MG2D_BC_TYPE_FIXVAL) {
- for (int j = 1; j < ctx->fd_stencil; j++) {
- double *dst = bnd->val + j * bnd->val_stride;
-
- for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
- dst[k] = sol[MG2D_DIFF_COEFF_00](-j * ctx->step[0], k * ctx->step[1]);
- }
- }
- }
- {
- MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0U];
+ double coord[2];
bnd->type = BC_TYPE;
@@ -145,45 +137,16 @@ int main(int argc, char **argv)
for (int j = 1; j < ctx->fd_stencil; j++) {
double *dst = bnd->val + j * bnd->val_stride;
- for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
- dst[k] = sol[MG2D_DIFF_COEFF_00]((gridsize - 1 + j) * ctx->step[0], k * ctx->step[1]);
- }
- }
- }
- {
- MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1L];
-
- bnd->type = BC_TYPE;
+ coord[ci] = mg2d_bnd_is_upper(bnd_loc) * DOMAIN_SIZE + bnd_dir * j * ctx->step[ci];
- memset(bnd->val, 0, gridsize * sizeof(*bnd->val));
-
- if (bnd->type == MG2D_BC_TYPE_FIXVAL) {
- for (int j = 1; j < ctx->fd_stencil; j++) {
- double *dst = bnd->val + j * bnd->val_stride;
-
- for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
- dst[k] = sol[MG2D_DIFF_COEFF_00](k * ctx->step[0], -j * ctx->step[1]);
- }
- }
- }
- {
- MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1U];
-
- bnd->type = BC_TYPE;
-
- memset(bnd->val, 0, gridsize * sizeof(*bnd->val));
-
- if (bnd->type == MG2D_BC_TYPE_FIXVAL) {
- for (int j = 1; j < ctx->fd_stencil; j++) {
- double *dst = bnd->val + j * bnd->val_stride;
-
- for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
- dst[k] = sol[MG2D_DIFF_COEFF_00](k * ctx->step[0], (gridsize - 1 + j) * ctx->step[1]);
+ for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++) {
+ coord[!ci] = k * ctx->step[!ci];
+ dst[k] = sol[MG2D_DIFF_COEFF_00](coord[0], coord[1]);
+ }
}
}
}
-
for (size_t y = 0; y < ctx->domain_size; y++) {
const double y_coord = y * ctx->step[1];