finished commentary and checks for Grid class

This commit is contained in:
philippun 2023-08-03 18:17:21 +02:00
parent ad2fdabac9
commit ab22436283
2 changed files with 187 additions and 62 deletions

View File

@ -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 <Eigen/Core> #include <Eigen/Core>
using namespace Eigen; using namespace Eigen;
@ -6,81 +13,143 @@ class Grid {
public: 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 row Length of the 2D-Grid in y-direction. Must be greater than 3.
* @param col * @param col Length of the 2D-Grid in x-direction. Must be greater than 3.
*/ */
Grid(int row, int col); 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); 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(); 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); 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 alphaX An Eigen3 MatrixXd holding the alpha coefficients in x-direction.
* @param alpha_y * 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(); 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(); MatrixXd getAlphaY();
/**
* @brief Gets the dimensions of the grid.
*
* @return int Dimensions, either 1 or 2.
*/
int getDim(); int getDim();
/**
* @brief Gets the number of rows of the grid.
*
* @return int Number of rows.
*/
int getRow(); int getRow();
/**
* @brief Gets the number of columns of the grid.
*
* @return int Number of columns.
*/
int getCol(); 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(); double getDeltaCol();
/**
* @brief Gets the delta value in y-direction.
*
* @return double Delta value in y-direction.
*/
double getDeltaRow(); double getDeltaRow();
private: private:
int dim; int col; // number of grid columns
int col; int row; // number of grid rows
int row; int dim; // 1D or 2D
int domain_col; int domainCol; // number of domain columns
int domain_row; int domainRow; // number of domain rows
double delta_col; double deltaCol; // delta in x-direction (between columns)
double delta_row; double deltaRow; // delta in y-direction (between rows)
MatrixXd concentrations; MatrixXd concentrations;
MatrixXd alpha_x; MatrixXd alphaX;
MatrixXd alpha_y; MatrixXd alphaY;
}; };

View File

@ -2,36 +2,46 @@
#include <tug/Grid.hpp> #include <tug/Grid.hpp>
#include <iostream> #include <iostream>
Grid::Grid(int col) { Grid::Grid(int length) {
this->col = col; if (length <= 3) {
this->domain_col = col; throw_invalid_argument("Given grid length too small. Must be greater than 3.");
this->delta_col = double(this->domain_col)/double(this->col); }
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; 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->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) { Grid::Grid(int row, int col) {
// TODO check for reasonable dimensions if (row <= 3 || col <= 3) {
if (row < 1 || col < 1) { throw_invalid_argument("Given grid dimensions too small. Must each be greater than 3.");
throw_invalid_argument("Either row or col too small!");
} }
this->row = row; this->row = row;
this->col = col; this->col = col;
this->domain_row = row; this->domainRow = row; // default: same size as row
this->domain_col = col; this->domainCol = col; // default: same size as col
this->delta_row = double(this->domain_row)/double(this->row); this->deltaRow = double(this->domainRow)/double(this->row); // -> 1
this->delta_col = double(this->domain_col)/double(this->col); this->deltaCol = double(this->domainCol)/double(this->col); // -> 1
this->dim = 2; 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->concentrations = MatrixXd::Constant(row, col, 20);
this->alpha_x = MatrixXd::Constant(row, col, 1); this->alphaX = MatrixXd::Constant(row, col, 1);
this->alpha_y = MatrixXd::Constant(row, col, 1); this->alphaY = MatrixXd::Constant(row, col, 1);
} }
void Grid::setConcentrations(MatrixXd concentrations) { 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; this->concentrations = concentrations;
} }
@ -40,20 +50,53 @@ MatrixXd Grid::getConcentrations() {
} }
void Grid::setAlpha(MatrixXd alpha) { 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) { void Grid::setAlpha(MatrixXd alphaX, MatrixXd alphaY) {
this->alpha_x = alpha_x; if (dim != 2) {
this->alpha_y = alpha_y; 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() { 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() { 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() { int Grid::getDim() {
@ -68,23 +111,36 @@ int Grid::getCol() {
return col; return col;
} }
void Grid::setDomain(int domain_col) { void Grid::setDomain(int domainLength) {
this->domain_col = domain_col; if (dim != 1) {
this->delta_col = double(this->domain_col)/this->col; 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) { void Grid::setDomain(int domainRow, int domainCol) {
this->domain_row = domain_row; if (dim != 2) {
this->domain_col = domain_col; 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->domainRow = domainRow;
this->delta_col = double(this->domain_col)/double(this->col); this->domainCol = domainCol;
this->deltaRow = double(this->domainRow)/double(this->row);
this->deltaCol = double(this->domainCol)/double(this->col);
} }
double Grid::getDeltaCol() { double Grid::getDeltaCol() {
return this->delta_col; return this->deltaCol;
} }
double Grid::getDeltaRow() { double Grid::getDeltaRow() {
return this->delta_row; return this->deltaRow;
} }