Merge branch '10-implement-getter-for-boundary-condition-as-vector' into 'main'

Resolve "Implement getter for boundary condition as vector"

Closes #10

See merge request mluebke/diffusion!21
This commit is contained in:
Max Lübke 2022-06-17 12:38:27 +02:00
commit e985a2ec24
3 changed files with 79 additions and 4 deletions

View File

@ -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<boundary_condition> &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<boundary_condition>;
/**
* Get both boundary conditions of a given row (left and right).
*

View File

@ -1,6 +1,8 @@
#include <algorithm>
#include <bits/stdint-uintn.h>
#include <diffusion/BTCSBoundaryCondition.hpp>
#include <stdexcept>
#include <vector>
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<Diffusion::boundary_condition> &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<Diffusion::boundary_condition> {
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<Diffusion::boundary_condition> 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) {

View File

@ -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<boundary_condition> 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);
}