From ab22436283010b379b9d70426c7f70cf4b9f114c Mon Sep 17 00:00:00 2001 From: philippun Date: Thu, 3 Aug 2023 18:17:21 +0200 Subject: [PATCH] finished commentary and checks for Grid class --- include/tug/Grid.hpp | 131 +++++++++++++++++++++++++++++++++---------- src/Grid.cpp | 118 ++++++++++++++++++++++++++++---------- 2 files changed, 187 insertions(+), 62 deletions(-) diff --git a/include/tug/Grid.hpp b/include/tug/Grid.hpp index 72f68d5..72593dc 100644 --- a/include/tug/Grid.hpp +++ b/include/tug/Grid.hpp @@ -1,3 +1,10 @@ +/** + * @file Grid.hpp + * @brief API of Grid class, that holds a matrix with concenctrations and a + * respective matrix/matrices of alpha coefficients. + * + */ + #include using namespace Eigen; @@ -6,81 +13,143 @@ class Grid { public: /** - * @brief Construct a new 1D Grid object, which represents the fields holding the concentrations. + * @brief Constructs a new 1D-Grid object of a given length, which holds a matrix + * with concentrations and a respective matrix of alpha coefficients. * - * @param col Defines the length of the grid in terms of cells. Input must be an integer > 3. + * @param length Length of the 1D-Grid. Must be greater than 3. */ - Grid(int col); + Grid(int length); /** - * @brief Construct a new Grid object + * @brief Constructs a new 2D-Grid object of given dimensions, which holds a matrix + * with concentrations and the respective matrices of alpha coefficient for + * each direction. * - * @param row - * @param col + * @param row Length of the 2D-Grid in y-direction. Must be greater than 3. + * @param col Length of the 2D-Grid in x-direction. Must be greater than 3. */ Grid(int row, int col); - // TODO - // Grid(MatrixXd concentrations); - /** - * @brief Set the Concentrations object + * @brief Sets the concentrations matrix for a 1D or 2D-Grid. * - * @param concentrations + * @param concentrations An Eigen3 MatrixXd holding the concentrations. Matrix must + * have correct dimensions as defined in row and col, or length, + * respectively. */ void setConcentrations(MatrixXd concentrations); /** - * @brief Get the Concentrations object + * @brief Gets the concentrations matrix for a Grid. * - * @return MatrixXd + * @return MatrixXd An Eigen3 matrix holding the concentrations and having the + * same dimensions as the grid. */ MatrixXd getConcentrations(); /** - * @brief Set the Alpha object + * @brief Set the alpha coefficients of a 1D-Grid. Grid must be one dimensional. * - * @param alpha + * @param alpha An Eigen3 MatrixXd with 1 row holding the alpha coefficients. Matrix + * columns must have same size as length of grid. */ void setAlpha(MatrixXd alpha); /** - * @brief Set the Alpha object + * @brief Set the alpha coefficients of a 2D-Grid. Grid must be two dimensional. * - * @param alpha_x - * @param alpha_y + * @param alphaX An Eigen3 MatrixXd holding the alpha coefficients in x-direction. + * Matrix must be of same size as the grid. + * @param alphaY An Eigen3 MatrixXd holding the alpha coefficients in y-direction. + * Matrix must be of same size as the grid. */ - void setAlpha(MatrixXd alpha_x, MatrixXd alpha_y); + void setAlpha(MatrixXd alphaX, MatrixXd alphaY); + /** + * @brief Gets the matrix of alpha coefficients of a 1D-Grid. Grid must be one dimensional. + * + * @return MatrixXd A matrix with 1 row holding the alpha coefficients. + */ + MatrixXd getAlpha(); + + /** + * @brief Gets the matrix of alpha coefficients in x-direction of a 2D-Grid. Grid must be + * two dimensional. + * + * @return MatrixXd A matrix holding the alpha coefficients in x-direction. + */ MatrixXd getAlphaX(); + /** + * @brief Gets the matrix of alpha coefficients in y-direction of a 2D-Grid. Grid must be + * two dimensional. + * + * @return MatrixXd A matrix holding the alpha coefficients in y-direction. + */ MatrixXd getAlphaY(); + /** + * @brief Gets the dimensions of the grid. + * + * @return int Dimensions, either 1 or 2. + */ int getDim(); + /** + * @brief Gets the number of rows of the grid. + * + * @return int Number of rows. + */ int getRow(); + /** + * @brief Gets the number of columns of the grid. + * + * @return int Number of columns. + */ int getCol(); - void setDomain(int domain_col); + /** + * @brief Sets the domain length of a 1D-Grid. Grid must be one dimensional. + * + * @param domainLength An integer of the domain length. Must be positive. + */ + void setDomain(int domainLength); - void setDomain(int domain_row, int domain_col); + /** + * @brief Sets the domain size of a 2D-Grid. Grid must be two dimensional. + * + * @param domainRow An integer of the domain size in y-direction. Must be positive. + * @param domainCol An integer of the domain size in x-direction. Must be positive. + */ + void setDomain(int domainRow, int domainCol); + /** + * @brief Gets the delta value in x-direction. + * + * @return double Delta value in x-direction. + */ double getDeltaCol(); + /** + * @brief Gets the delta value in y-direction. + * + * @return double Delta value in y-direction. + */ double getDeltaRow(); private: - int dim; - int col; - int row; - int domain_col; - int domain_row; - double delta_col; - double delta_row; - MatrixXd concentrations; - MatrixXd alpha_x; - MatrixXd alpha_y; + int col; // number of grid columns + int row; // number of grid rows + int dim; // 1D or 2D + int domainCol; // number of domain columns + int domainRow; // number of domain rows + double deltaCol; // delta in x-direction (between columns) + double deltaRow; // delta in y-direction (between rows) + MatrixXd concentrations; + MatrixXd alphaX; + MatrixXd alphaY; + }; \ No newline at end of file diff --git a/src/Grid.cpp b/src/Grid.cpp index 003f854..03a55cc 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -2,36 +2,46 @@ #include #include -Grid::Grid(int col) { - this->col = col; - this->domain_col = col; - this->delta_col = double(this->domain_col)/double(this->col); +Grid::Grid(int length) { + if (length <= 3) { + throw_invalid_argument("Given grid length too small. Must be greater than 3."); + } + this->row = 1; + this->col = length; + this->domainCol = length; // default: same size as length + this->deltaCol = double(this->domainCol)/double(this->col); // -> 1 this->dim = 1; + + // TODO move to the case when Simulation is set to constant and use as default this->concentrations = MatrixXd::Constant(1, col, 20); - this->alpha_x = MatrixXd::Constant(1, col, 1); + this->alphaX = MatrixXd::Constant(1, col, 1); } Grid::Grid(int row, int col) { - // TODO check for reasonable dimensions - if (row < 1 || col < 1) { - throw_invalid_argument("Either row or col too small!"); + if (row <= 3 || col <= 3) { + throw_invalid_argument("Given grid dimensions too small. Must each be greater than 3."); } this->row = row; this->col = col; - this->domain_row = row; - this->domain_col = col; - this->delta_row = double(this->domain_row)/double(this->row); - this->delta_col = double(this->domain_col)/double(this->col); - + this->domainRow = row; // default: same size as row + this->domainCol = col; // default: same size as col + this->deltaRow = double(this->domainRow)/double(this->row); // -> 1 + this->deltaCol = double(this->domainCol)/double(this->col); // -> 1 this->dim = 2; + + // TODO move to the case when Simulation is set to constant and use as default this->concentrations = MatrixXd::Constant(row, col, 20); - this->alpha_x = MatrixXd::Constant(row, col, 1); - this->alpha_y = MatrixXd::Constant(row, col, 1); + this->alphaX = MatrixXd::Constant(row, col, 1); + this->alphaY = MatrixXd::Constant(row, col, 1); } void Grid::setConcentrations(MatrixXd concentrations) { + if (concentrations.rows() != this->row || concentrations.cols() != this->col) { + throw_invalid_argument("Given matrix of concentrations mismatch with Grid dimensions!"); + } + this->concentrations = concentrations; } @@ -40,20 +50,53 @@ MatrixXd Grid::getConcentrations() { } void Grid::setAlpha(MatrixXd alpha) { - this->alpha_x = alpha; + if (dim != 1) { + throw_invalid_argument("Grid is not one dimensional, you should probably use 2D setter function!"); + } + if (alpha.rows() != 1 || alpha.cols() != this->col) { + throw_invalid_argument("Given matrix of alpha coefficients mismatch with Grid dimensions!"); + } + + this->alphaX = alpha; } -void Grid::setAlpha(MatrixXd alpha_x, MatrixXd alpha_y) { - this->alpha_x = alpha_x; - this->alpha_y = alpha_y; +void Grid::setAlpha(MatrixXd alphaX, MatrixXd alphaY) { + if (dim != 2) { + throw_invalid_argument("Grid is not two dimensional, you should probably use 1D setter function!"); + } + if (alphaX.rows() != this->row || alphaX.cols() != this->col) { + throw_invalid_argument("Given matrix of alpha coefficients in x-direction mismatch with GRid dimensions!"); + } + if (alphaY.rows() != this->row || alphaY.cols() != this->col) { + throw_invalid_argument("Given matrix of alpha coefficients in y-direction mismatch with GRid dimensions!"); + } + + this->alphaX = alphaX; + this->alphaY = alphaY; +} + +MatrixXd Grid::getAlpha() { + if (dim != 1) { + throw_invalid_argument("Grid is not one dimensional, you should probably use either getAlphaX() or getAlphaY()!"); + } + + return this->alphaX; } MatrixXd Grid::getAlphaX() { - return this->alpha_x; + if (dim != 2) { + throw_invalid_argument("Grid is not two dimensional, you should probably use getAlpha()!"); + } + + return this->alphaX; } MatrixXd Grid::getAlphaY() { - return this->alpha_y; + if (dim != 2) { + throw_invalid_argument("Grid is not two dimensional, you should probably use getAlpha()!"); + } + + return this->alphaY; } int Grid::getDim() { @@ -68,23 +111,36 @@ int Grid::getCol() { return col; } -void Grid::setDomain(int domain_col) { - this->domain_col = domain_col; - this->delta_col = double(this->domain_col)/this->col; +void Grid::setDomain(int domainLength) { + if (dim != 1) { + throw_invalid_argument("Grid is not one dimensional, you should probaly use the 2D domain setter!"); + } + if (domainLength < 1) { + throw_invalid_argument("Given domain length is not positive!"); + } + + this->domainCol = domainLength; + this->deltaCol = double(this->domainCol)/this->col; } -void Grid::setDomain(int domain_row, int domain_col) { - this->domain_row = domain_row; - this->domain_col = domain_col; +void Grid::setDomain(int domainRow, int domainCol) { + if (dim != 2) { + throw_invalid_argument("Grid is not two dimensional, you should probably use the 1D domain setter!"); + } + if (domainRow < 1 || domainCol < 1) { + throw_invalid_argument("Given domain size is not positive!"); + } - this->delta_row = double(this->domain_row)/double(this->row); - this->delta_col = double(this->domain_col)/double(this->col); + this->domainRow = domainRow; + this->domainCol = domainCol; + this->deltaRow = double(this->domainRow)/double(this->row); + this->deltaCol = double(this->domainCol)/double(this->col); } double Grid::getDeltaCol() { - return this->delta_col; + return this->deltaCol; } double Grid::getDeltaRow() { - return this->delta_row; + return this->deltaRow; }