From efed757a9eafb1e5a52c1eb38f1330dffdcf7ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Fri, 12 Aug 2022 21:16:43 +0200 Subject: [PATCH] style: Use enumerations for macros and use more useful function names Update the pre-compiler macros defined by `BTCSBoundaryCondition` to enumerations. Update method names for getting row and column boundary conditions. --- include/grid/BTCSBoundaryCondition.hpp | 52 ++++++++------------------ src/BTCSBoundaryCondition.cpp | 4 +- src/BTCSDiffusion.cpp | 6 +-- test/testBoundaryCondition.cpp | 6 +-- 4 files changed, 24 insertions(+), 44 deletions(-) diff --git a/include/grid/BTCSBoundaryCondition.hpp b/include/grid/BTCSBoundaryCondition.hpp index cc6ced1..90fe9f8 100644 --- a/include/grid/BTCSBoundaryCondition.hpp +++ b/include/grid/BTCSBoundaryCondition.hpp @@ -11,40 +11,20 @@ typedef uint8_t bctype; namespace Diffusion { -/** - * Defines a closed/Neumann boundary condition. - */ -static const bctype BC_TYPE_CLOSED = 0; +enum { + BC_TYPE_CLOSED, /**< Defines a closed/Neumann boundary condition. */ + BC_TYPE_FLUX, /**< Defines a flux/Cauchy boundary condition. */ + BC_TYPE_CONSTANT /**< Defines a constant/Dirichlet boundary condition. */ +}; -/** - * Defines a flux/Cauchy boundary condition. - */ -static const bctype BC_TYPE_FLUX = 1; - -/** - * Defines a constant/Dirichlet boundary condition. - */ -static const bctype BC_TYPE_CONSTANT = 2; - -/** - * Defines boundary conditions for the left side of the grid. - */ -static const uint8_t BC_SIDE_LEFT = 0; - -/** - * Defines boundary conditions for the right side of the grid. - */ -static const uint8_t BC_SIDE_RIGHT = 1; - -/** - * Defines boundary conditions for the top of the grid. - */ -static const uint8_t BC_SIDE_TOP = 2; - -/** - * Defines boundary conditions for the bottom of the grid. - */ -static const uint8_t BC_SIDE_BOTTOM = 3; +enum { + BC_SIDE_LEFT, /**< Defines boundary conditions for the left side of the grid. + */ + BC_SIDE_RIGHT, /**< Defines boundary conditions for the right side of the + grid. */ + BC_SIDE_TOP, /**< Defines boundary conditions for the top of the grid. */ + BC_SIDE_BOTTOM /**< Defines boundary conditions for the bottom of the grid. */ +}; /** * Defines the boundary condition type and according value. @@ -57,7 +37,7 @@ typedef struct boundary_condition_s { } boundary_condition; /** - * Internal use only. + * Represents both boundary conditions of a row/column. */ typedef std::array bc_tuple; @@ -129,7 +109,7 @@ public: * \returns Left and right boundary values as an array (defined as data * type bc_tuple). */ - auto row(uint32_t i) const -> bc_tuple; + auto row_boundary(uint32_t i) const -> bc_tuple; /** * Get both boundary conditions of a given column (top and bottom). @@ -139,7 +119,7 @@ public: * \returns Top and bottom boundary values as an array (defined as data * type bc_tuple). */ - auto col(uint32_t i) const -> bc_tuple; + auto col_boundary(uint32_t i) const -> bc_tuple; /** * Create an instance of boundary_condition data type. Can be seen as a helper diff --git a/src/BTCSBoundaryCondition.cpp b/src/BTCSBoundaryCondition.cpp index fcfda1a..9ee8103 100644 --- a/src/BTCSBoundaryCondition.cpp +++ b/src/BTCSBoundaryCondition.cpp @@ -92,7 +92,7 @@ auto Diffusion::BTCSBoundaryCondition::getSide(uint8_t side) return out; } -auto Diffusion::BTCSBoundaryCondition::col(uint32_t i) const +auto Diffusion::BTCSBoundaryCondition::col_boundary(uint32_t i) const -> Diffusion::bc_tuple { if (this->dim == 1) { throw_invalid_argument("Access of column requires at least 2D grid"); @@ -105,7 +105,7 @@ auto Diffusion::BTCSBoundaryCondition::col(uint32_t i) const this->bc_internal[BC_SIDE_BOTTOM * this->maxsize + i]}; } -auto Diffusion::BTCSBoundaryCondition::row(uint32_t i) const +auto Diffusion::BTCSBoundaryCondition::row_boundary(uint32_t i) const -> Diffusion::bc_tuple { if (i >= this->sizes[0]) { throw_out_of_range("Index out of range"); diff --git a/src/BTCSDiffusion.cpp b/src/BTCSDiffusion.cpp index 232322a..6e66ff8 100644 --- a/src/BTCSDiffusion.cpp +++ b/src/BTCSDiffusion.cpp @@ -129,7 +129,7 @@ void Diffusion::BTCSDiffusion::simulate1D( DVectorRowMajor input_field = c.row(0); - simulate_base(input_field, bc.row(0), alpha, dx, time_step, size, + simulate_base(input_field, bc.row_boundary(0), alpha, dx, time_step, size, Eigen::VectorXd::Constant(size, 0)); c.row(0) << input_field; @@ -150,7 +150,7 @@ void Diffusion::BTCSDiffusion::simulate2D( #pragma omp parallel for schedule(dynamic) for (int i = 0; i < n_rows; i++) { DVectorRowMajor input_field = c.row(i); - simulate_base(input_field, bc.row(i), alpha.row(i), dx, local_dt, n_cols, + simulate_base(input_field, bc.row_boundary(i), alpha.row(i), dx, local_dt, n_cols, d_ortho.row(i)); c.row(i) << input_field; } @@ -163,7 +163,7 @@ void Diffusion::BTCSDiffusion::simulate2D( #pragma omp parallel for schedule(dynamic) for (int i = 0; i < n_cols; i++) { DVectorRowMajor input_field = c.col(i); - simulate_base(input_field, bc.col(i), alpha.col(i), dx, local_dt, n_rows, + simulate_base(input_field, bc.col_boundary(i), alpha.col(i), dx, local_dt, n_rows, d_ortho.row(i)); c.col(i) << input_field.transpose(); } diff --git a/test/testBoundaryCondition.cpp b/test/testBoundaryCondition.cpp index a132aee..602a295 100644 --- a/test/testBoundaryCondition.cpp +++ b/test/testBoundaryCondition.cpp @@ -30,15 +30,15 @@ TEST_CASE("1D Boundary Condition") { SUBCASE("valid row getter") { bc(BC_SIDE_LEFT) = bc_set; - bc_tuple tup = bc.row(0); + bc_tuple tup = bc.row_boundary(0); CHECK_EQ(tup[0].value, BC_CONST_VALUE); CHECK_EQ(tup[1].value, 0); } SUBCASE("invalid row and col getter") { - CHECK_THROWS(bc.row(1)); - CHECK_THROWS(bc.col(0)); + CHECK_THROWS(bc.row_boundary(1)); + CHECK_THROWS(bc.col_boundary(0)); } }