Add inner boundary conditions for 1D and 2D grids

This commit is contained in:
Max Luebke 2024-04-04 13:48:10 +02:00
parent 2dc959b993
commit cb0c21eab9
3 changed files with 106 additions and 3 deletions

View File

@ -8,10 +8,11 @@
#define BOUNDARY_H_
#include "Grid.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <map>
#include <utility>
#include <vector>
namespace tug {
@ -375,6 +376,90 @@ public:
}
}
/**
*
* @param index Index of the inner constant boundary condition
* @param value Value of the inner constant boundary condition
*/
void setInnerBoundary(std::uint32_t index, T value) {
if (this->dim != 1) {
throw std::invalid_argument(
"This function is only available for 1D grids.");
}
if (index >= this->cols) {
throw std::invalid_argument("Index is out of bounds.");
}
this->inner_boundary[std::make_pair(0, index)] = value;
}
/**
* @brief Set inner constant boundary condition in 2D case.
*
* @param row Row index of the inner constant boundary condition
* @param col Column index of the inner constant boundary condition
* @param value Value of the inner constant boundary condition
*/
void setInnerBoundary(std::uint32_t row, std::uint32_t col, T value) {
if (this->dim != 2) {
throw std::invalid_argument(
"This function is only available for 2D grids.");
}
if (row >= this->rows || col >= this->cols) {
throw std::invalid_argument("Index is out of bounds.");
}
this->inner_boundary[std::make_pair(row, col)] = value;
}
/**
* @brief Get inner constant boundary condition in 1D case.
*
* @param index Index of the inner constant boundary condition
* @return std::pair<bool, T> Pair of boolean (whether constant boundary was
* set or not) and value of the inner constant boundary condition
*/
std::pair<bool, T> getInnerBoundary(std::uint32_t index) const {
if (this->dim != 1) {
throw std::invalid_argument(
"This function is only available for 1D grids.");
}
if (index >= this->cols) {
throw std::invalid_argument("Index is out of bounds.");
}
auto it = this->inner_boundary.find(std::make_pair(0, index));
if (it == this->inner_boundary.end()) {
return std::make_pair(false, -1);
}
return std::make_pair(true, it->second);
}
/**
* @brief Get inner constant boundary condition in 2D case.
*
* @param row Row index of the inner constant boundary condition
* @param col Column index of the inner constant boundary condition
* @return std::pair<bool, T> Pair of boolean (whether constant boundary was
* set or not) and value of the inner constant boundary condition
*/
std::pair<bool, T> getInnerBoundary(std::uint32_t row,
std::uint32_t col) const {
if (this->dim != 2) {
throw std::invalid_argument(
"This function is only available for 2D grids.");
}
if (row >= this->rows || col >= this->cols) {
throw std::invalid_argument("Index is out of bounds.");
}
auto it = this->inner_boundary.find(std::make_pair(row, col));
if (it == this->inner_boundary.end()) {
return std::make_pair(false, -1);
}
return std::make_pair(true, it->second);
}
private:
const std::uint8_t dim;
const std::uint32_t cols;
@ -382,6 +467,9 @@ private:
std::vector<std::vector<BoundaryElement<T>>>
boundaries; // Vector with Boundary Element information
// Inner boundary conditions for 1D and 2D grids identified by (row,col)
std::map<std::pair<std::uint32_t, std::uint32_t>, T> inner_boundary;
};
} // namespace tug
#endif // BOUNDARY_H_

View File

@ -4,6 +4,7 @@
#include <string>
#include <tug/Boundary.hpp>
#include <typeinfo>
#include <utility>
using namespace std;
using namespace tug;
@ -35,6 +36,10 @@ TEST_CASE("Boundary Class") {
Boundary boundary2D = Boundary(grid2D);
vector<BoundaryElement<double>> boundary1DVector(1, BoundaryElement(1.0));
constexpr double inner_condition_value = -5;
constexpr std::pair<bool, double> innerBoundary =
std::make_pair(true, inner_condition_value);
SUBCASE("Boundaries 1D case") {
CHECK_NOTHROW(Boundary boundary(grid1D));
CHECK_EQ(boundary1D.getBoundarySide(BC_SIDE_LEFT).size(), 1);
@ -52,6 +57,11 @@ TEST_CASE("Boundary Class") {
BC_TYPE_CONSTANT);
CHECK_EQ(boundary1D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(),
boundary1DVector[0].getType());
CHECK_NOTHROW(boundary1D.setInnerBoundary(0, inner_condition_value));
CHECK_THROWS(boundary1D.setInnerBoundary(0, 0, inner_condition_value));
CHECK_EQ(boundary1D.getInnerBoundary(0), innerBoundary);
CHECK_EQ(boundary1D.getInnerBoundary(1).first, false);
}
SUBCASE("Boundaries 2D case") {
@ -73,5 +83,10 @@ TEST_CASE("Boundary Class") {
BC_TYPE_CONSTANT);
CHECK_EQ(boundary2D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(),
boundary1DVector[0].getType());
CHECK_THROWS(boundary2D.setInnerBoundary(0, inner_condition_value));
CHECK_NOTHROW(boundary2D.setInnerBoundary(0, 1, inner_condition_value));
CHECK_EQ(boundary2D.getInnerBoundary(0, 1), innerBoundary);
CHECK_EQ(boundary2D.getInnerBoundary(0, 2).first, false);
}
}

View File

@ -1,10 +1,10 @@
#include "TestUtils.hpp"
#include <tug/Simulation.hpp>
#include <Eigen/src/Core/Matrix.h>
#include <doctest/doctest.h>
#include <stdio.h>
#include <string>
#include <tug/Simulation.hpp>
// include the configured header file
#include <testSimulation.hpp>