diff --git a/include/diffusion/BTCSBoundaryCondition.hpp b/include/diffusion/BTCSBoundaryCondition.hpp index 1bf5c11..b4adef4 100644 --- a/include/diffusion/BTCSBoundaryCondition.hpp +++ b/include/diffusion/BTCSBoundaryCondition.hpp @@ -96,6 +96,31 @@ public: */ void setSide(uint8_t side, boundary_condition &input_bc); + /** + * Sets the boundary condition for a specific side of the grid. + * + * \param side Side for which the given boundary condition should be set. + * \param input_bc Vector of boundary conditions for specific side. + * + * \throws std::invalid_argument Indicates wrong dimensions of the grid. + * \throws std::out_of_range Indicates a out of range value for side or + * invalid size of input vector. + */ + void setSide(uint8_t side, std::vector &input_bc); + + /** + * Returns a vector of boundary conditions of given side. Can be used to set + * custom boundary conditions and set back via setSide() with vector input. + * + * \param side Side which boundary conditions should be returned + * + * \returns Vector of boundary conditions + * + * \throws std::invalid_argument If given dimension is less or equal to 1. + * \throws std::out_of_range Indicates a out of range value for side. + */ + auto getSide(uint8_t side) -> std::vector; + /** * Get both boundary conditions of a given row (left and right). * diff --git a/src/BTCSBoundaryCondition.cpp b/src/BTCSBoundaryCondition.cpp index 5b92a00..4a0b172 100644 --- a/src/BTCSBoundaryCondition.cpp +++ b/src/BTCSBoundaryCondition.cpp @@ -1,6 +1,8 @@ +#include #include #include #include +#include constexpr uint8_t DIM_1D = 2; constexpr uint8_t DIM_2D = 4; @@ -32,6 +34,42 @@ void Diffusion::BTCSBoundaryCondition::setSide( } } +void Diffusion::BTCSBoundaryCondition::setSide( + uint8_t side, std::vector &input_bc) { + if (this->dim == 1) { + throw std::invalid_argument("setSide requires at least a 2D grid"); + } + if (side > 3) { + throw std::out_of_range("Invalid range for 2D grid"); + } + if (input_bc.size() > this->maxsize) { + throw std::out_of_range( + "Input vector is greater than maximum excpected value"); + } + + for (int i = 0; i < input_bc.size(); i++) { + bc_internal[this->maxsize * side + i] = input_bc[i]; + } +} + +auto Diffusion::BTCSBoundaryCondition::getSide(uint8_t side) + -> std::vector { + if (this->dim == 1) { + throw std::invalid_argument("getSide requires at least a 2D grid"); + } + if (side > 3) { + throw std::out_of_range("Invalid range for 2D grid"); + } + + std::vector out(this->maxsize); + + for (int i = 0; i < this->maxsize; i++) { + out[i] = this->bc_internal[this->maxsize * side + i]; + } + + return out; +} + auto Diffusion::BTCSBoundaryCondition::col(uint32_t i) const -> Diffusion::bc_tuple { if (this->dim == 1) { diff --git a/test/testBoundaryCondition.cpp b/test/testBoundaryCondition.cpp index e08ac46..f7fe646 100644 --- a/test/testBoundaryCondition.cpp +++ b/test/testBoundaryCondition.cpp @@ -66,9 +66,20 @@ TEST_CASE("2D Boundary Condition") { } SUBCASE("call of setSide") { - CHECK_NOTHROW(bc.setSide(BC_SIDE_BOTTOM, bc_set)); - CHECK_EQ(bc(BC_SIDE_BOTTOM, 1).value, bc_set.value); - CHECK_EQ(bc(BC_SIDE_BOTTOM, 1).type, bc_set.type); + CHECK_NOTHROW(bc.setSide(BC_SIDE_BOTTOM, bc_set)); + CHECK_EQ(bc(BC_SIDE_BOTTOM, 1).value, bc_set.value); + CHECK_EQ(bc(BC_SIDE_BOTTOM, 1).type, bc_set.type); + } + + SUBCASE("get and set of side") { + std::vector bc_vec; + CHECK_NOTHROW(bc_vec = bc.getSide(BC_SIDE_BOTTOM)); + bc_vec[3] = {BC_TYPE_CONSTANT, 1e-5}; + CHECK_NOTHROW(bc.setSide(BC_SIDE_BOTTOM, bc_vec)); + CHECK_EQ(bc(BC_SIDE_BOTTOM, 3).type, BC_TYPE_CONSTANT); + CHECK_EQ(bc(BC_SIDE_BOTTOM, 3).value, 1e-5); + + CHECK_EQ(bc(BC_SIDE_BOTTOM, 2).value, 0); } } @@ -76,7 +87,8 @@ TEST_CASE("Boundary Condition helpers") { boundary_condition bc_set = {BC_TYPE_CONSTANT, BC_CONST_VALUE}; SUBCASE("return boundary condition skeleton") { - boundary_condition bc_test = BTCSBoundaryCondition::returnBoundaryCondition(bc_set.type, bc_set.value); + boundary_condition bc_test = BTCSBoundaryCondition::returnBoundaryCondition( + bc_set.type, bc_set.value); CHECK_EQ(bc_test.value, bc_set.value); CHECK_EQ(bc_test.type, bc_set.type); }