From 6aa61c3e38a164b5d015d6ab67e15a46a5b9bc15 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 6 Feb 2019 14:07:46 +0100 Subject: relax_test: use the new boundary API to simplify code --- relax_test.c | 61 +++++++++++++++--------------------------------------------- 1 file changed, 15 insertions(+), 46 deletions(-) (limited to 'relax_test.c') diff --git a/relax_test.c b/relax_test.c index c635949..1e9b962 100644 --- a/relax_test.c +++ b/relax_test.c @@ -9,6 +9,9 @@ #include "mg2d_boundary.h" #include "mg2d_constants.h" +#define ARRAY_ELEMS(x) (sizeof(x) / sizeof(*x)) +#define DOMAIN_SIZE 1.0 + #if 1 static double sol(double x, double y) { @@ -79,41 +82,17 @@ int main(int argc, char **argv) ctx->logger.log = mg2di_log_default_callback; - ctx->step[0] = 1.0 / (N - 1.0); - ctx->step[1] = 1.0 / (N - 1.0); + ctx->step[0] = DOMAIN_SIZE / (N - 1.0); + ctx->step[1] = DOMAIN_SIZE / (N - 1.0); ctx->fd_stencil = 2; - { - MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0L]; - - bnd->type = MG2D_BC_TYPE_FIXVAL; - - memset(bnd->val, 0, N * sizeof(*bnd->val)); - - 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[1] + j; k++) - dst[k] = sol(-j * ctx->step[0], k * ctx->step[1]); - } - } - { - MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0U]; - - bnd->type = MG2D_BC_TYPE_FIXVAL; - - memset(bnd->val, 0, N * sizeof(*bnd->val)); - - for (int j = 1; j < ctx->fd_stencil; j++) { - double *dst = bnd->val + j * bnd->val_stride; + 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); - for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size[1] + j; k++) - dst[k] = sol((N - 1 + j) * ctx->step[0], k * ctx->step[1]); - } - } - { - MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1L]; + double coord[2]; bnd->type = MG2D_BC_TYPE_FIXVAL; @@ -122,22 +101,12 @@ 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[0] + j; k++) - dst[k] = sol(k * ctx->step[0], -j * ctx->step[1]); - } - } - { - MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1U]; + coord[ci] = mg2d_bnd_is_upper(bnd_loc) * DOMAIN_SIZE + bnd_dir * j * ctx->step[ci]; - bnd->type = MG2D_BC_TYPE_FIXVAL; - - memset(bnd->val, 0, N * sizeof(*bnd->val)); - - 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[0] + j; k++) - dst[k] = sol(k * ctx->step[0], (N - 1 + j) * ctx->step[1]); + for (ptrdiff_t k = -j; k < N + j; k++) { + coord[!ci] = k * ctx->step[!ci]; + dst[k] = sol(coord[0], coord[1]); + } } } -- cgit v1.2.3