fixed Eigen implementation bugs

This commit is contained in:
philippun 2023-07-19 11:19:00 +02:00
parent 99925dbd4f
commit d457c2b9a7
7 changed files with 85 additions and 70 deletions

View File

@ -8,6 +8,7 @@ int main(int argc, char *argv[]) {
Boundary bc = Boundary(grid, BC_TYPE_CONSTANT); Boundary bc = Boundary(grid, BC_TYPE_CONSTANT);
Simulation simulation = Simulation(grid, bc, FTCS_APPROACH); Simulation simulation = Simulation(grid, bc, FTCS_APPROACH);
simulation.setIterations(2);
simulation.run(); simulation.run();

View File

@ -27,7 +27,7 @@ class Boundary {
* @param grid * @param grid
* @param type * @param type
*/ */
Boundary(Grid &grid, BC_TYPE type); Boundary(Grid grid, BC_TYPE type);
/** /**
* @brief Get the Boundary Condition Type object * @brief Get the Boundary Condition Type object

View File

@ -21,7 +21,7 @@ class Simulation {
* @param bc * @param bc
* @param aproach * @param aproach
*/ */
Simulation(Grid &grid, Boundary &bc, APPROACH approach); Simulation(Grid grid, Boundary bc, APPROACH approach);
/** /**
* @brief * @brief

View File

@ -4,7 +4,7 @@
using namespace std; using namespace std;
Boundary::Boundary(Grid &grid, BC_TYPE type) : grid(grid) { Boundary::Boundary(Grid grid, BC_TYPE type) : grid(grid) {
//probably to DEBUG assignment grid //probably to DEBUG assignment grid
this->type = type; this->type = type;

View File

@ -1,6 +1,9 @@
#include <cstddef>
#include <tug/Boundary.hpp> #include <tug/Boundary.hpp>
#include <iostream> #include <iostream>
using namespace std;
auto calc_alpha_intercell(double alpha1, double alpha2, bool useHarmonic = false) { auto calc_alpha_intercell(double alpha1, double alpha2, bool useHarmonic = false) {
if (useHarmonic) { if (useHarmonic) {
return 2 / ((1/alpha1) + (1/alpha2)); return 2 / ((1/alpha1) + (1/alpha2));
@ -9,7 +12,7 @@ auto calc_alpha_intercell(double alpha1, double alpha2, bool useHarmonic = false
} }
} }
auto FTCS_constant(Grid &grid, Boundary &bc, double timestep) { MatrixXd FTCS_constant(Grid grid, Boundary bc, double timestep) {
int rowMax = grid.getRow(); int rowMax = grid.getRow();
int colMax = grid.getCol(); int colMax = grid.getCol();
double deltaRow = grid.getDeltaRow(); double deltaRow = grid.getDeltaRow();
@ -17,9 +20,13 @@ auto FTCS_constant(Grid &grid, Boundary &bc, double timestep) {
// Matrix with concentrations at time t+1 // Matrix with concentrations at time t+1
// TODO profiler / only use 2 matrices // TODO profiler / only use 2 matrices
MatrixXd concentrations_t1 = MatrixXd(rowMax, colMax); MatrixXd concentrations_t1 = MatrixXd::Constant(rowMax, colMax, 1);
// inner cells // inner cells
cout << "Concentration 5,5: " << grid.getConcentrations()(5,5) << endl;
cout << "Alpha Y 5,5: " << grid.getAlphaY()(5,5) << endl;
cout << "calc alpha Y 5,5; 5,6: " << calc_alpha_intercell(grid.getAlphaY()(5,5), grid.getAlphaY()(5,6)) << endl;
cout << "t1 Concentrations 5,5: " << concentrations_t1(5,5) << endl;
for (int row = 1; row < rowMax-1; row++) { for (int row = 1; row < rowMax-1; row++) {
for (int col = 1; col < colMax-1; col++) { for (int col = 1; col < colMax-1; col++) {
concentrations_t1(row, col) = grid.getConcentrations()(row, col) concentrations_t1(row, col) = grid.getConcentrations()(row, col)
@ -32,7 +39,7 @@ auto FTCS_constant(Grid &grid, Boundary &bc, double timestep) {
+ calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col)) + calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))
* grid.getConcentrations()(row-1,col) * grid.getConcentrations()(row-1,col)
) )
- timestep / (deltaCol*deltaCol) * ( + timestep / (deltaCol*deltaCol) * (
calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col)) calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
* grid.getConcentrations()(row,col+1) * grid.getConcentrations()(row,col+1)
- (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col)) - (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
@ -47,69 +54,69 @@ auto FTCS_constant(Grid &grid, Boundary &bc, double timestep) {
// boundary conditions // boundary conditions
// left without corners / looping over rows // left without corners / looping over rows
int col = 0; int col = 0;
for (int row = 1; row < rowMax-1; row++) { // for (int row = 1; row < rowMax-1; row++) {
concentrations_t1(row, col) = grid.getConcentrations()(row,col) // concentrations_t1(row, col) = grid.getConcentrations()(row,col)
+ timestep / (deltaCol*deltaCol) // + timestep / (deltaCol*deltaCol)
* (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col)) // * (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
* grid.getConcentrations()(row,col+1) // * grid.getConcentrations()(row,col+1)
- (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col)) // - (calc_alpha_intercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
+ 2 * grid.getAlphaX()(row,col)) * grid.getConcentrations()(row,col) // + 2 * grid.getAlphaX()(row,col)) * grid.getConcentrations()(row,col)
+ 2 * grid.getAlphaX()(row,col) * bc.getBoundaryConditionValue(BC_SIDE_LEFT)(row, 1)) // + 2 * grid.getAlphaX()(row,col) * bc.getBoundaryConditionValue(BC_SIDE_LEFT)(row))
+ timestep / (deltaRow*deltaRow) // + timestep / (deltaRow*deltaRow)
* (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col)) // * (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
* grid.getConcentrations()(row+1,col) // * grid.getConcentrations()(row+1,col)
- (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col)) // - (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
+ calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))) // + calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col)))
* grid.getConcentrations()(row,col) // * grid.getConcentrations()(row,col)
+ calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getConcentrations()(row,col)) // + calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getConcentrations()(row,col))
* grid.getConcentrations()(row-1,col)); // * grid.getConcentrations()(row-1,col));
} // }
// right without corners / looping over columns // right without corners / looping over columns
col = colMax-1; col = colMax-1;
for (int row = 1; row < rowMax-1; row++) { // for (int row = 1; row < rowMax-1; row++) {
concentrations_t1(row,col) = grid.getConcentrations()(row,col) // concentrations_t1(row,col) = grid.getConcentrations()(row,col)
+ timestep / (deltaCol*deltaCol) // + timestep / (deltaCol*deltaCol)
* (2 * grid.getAlphaX()(row,col) * bc.getBoundaryConditionValue(BC_SIDE_RIGHT)(row, 1) // * (2 * grid.getAlphaX()(row,col) * bc.getBoundaryConditionValue(BC_SIDE_RIGHT)(row)
- (calc_alpha_intercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col)) // - (calc_alpha_intercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
+ 2 * grid.getAlphaX()(row,col)) + 2 * grid.getAlphaX()(row,col) // + 2 * grid.getAlphaX()(row,col)) + 2 * grid.getAlphaX()(row,col)
* grid.getConcentrations()(row,col) // * grid.getConcentrations()(row,col)
+ calc_alpha_intercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col)) // + calc_alpha_intercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
* grid.getConcentrations()(row,col-1)) // * grid.getConcentrations()(row,col-1))
+ timestep / (deltaRow*deltaRow) // + timestep / (deltaRow*deltaRow)
* (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col)) // * (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
* grid.getConcentrations()(row+1,col) // * grid.getConcentrations()(row+1,col)
- (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col)) // - (calc_alpha_intercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
+ calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))) // + calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col)))
* grid.getConcentrations()(row,col) // * grid.getConcentrations()(row,col)
+ calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col)) // + calc_alpha_intercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))
* grid.getConcentrations()(row-1,col)); // * grid.getConcentrations()(row-1,col));
} // }
// top without corners / looping over cols // top without corners / looping over cols
for(int col=1; col<colMax-1;col++){ // for(int col=1; col<colMax-1;col++){
int row = 0; // int row = 0;
concentrations_t1(row, col) = grid.getConcentrations()(row, col) // concentrations_t1(row, col) = grid.getConcentrations()(row, col)
+ timestep/(grid.getDeltaRow()*grid.getDeltaRow()) * (calc_alpha_intercell(grid.getAlphaY()(1, col), grid.getAlphaY()(0, col)) * grid.getConcentrations()(1,col) // + timestep/(grid.getDeltaRow()*grid.getDeltaRow()) * (calc_alpha_intercell(grid.getAlphaY()(1, col), grid.getAlphaY()(0, col)) * grid.getConcentrations()(1,col)
- (calc_alpha_intercell(grid.getAlphaY()(1, col), grid.getAlphaY()(0, col)) + 2 * grid.getAlphaY()(0, col)) * grid.getConcentrations()(0, col) // - (calc_alpha_intercell(grid.getAlphaY()(1, col), grid.getAlphaY()(0, col)) + 2 * grid.getAlphaY()(0, col)) * grid.getConcentrations()(0, col)
+ 2 * grid.getAlphaY()(0, col) * bc.getBoundaryConditionValue(BC_SIDE_TOP)(1, col)) // + 2 * grid.getAlphaY()(0, col) * bc.getBoundaryConditionValue(BC_SIDE_TOP)(col))
+ timestep/(grid.getDeltaCol()*grid.getDeltaCol()) * (calc_alpha_intercell(grid.getAlphaX()(0, col+1), grid.getAlphaX()(0, col)) * grid.getConcentrations()(0, col+1) // + timestep/(grid.getDeltaCol()*grid.getDeltaCol()) * (calc_alpha_intercell(grid.getAlphaX()(0, col+1), grid.getAlphaX()(0, col)) * grid.getConcentrations()(0, col+1)
- (calc_alpha_intercell(grid.getAlphaX()(0, col+1), grid.getAlphaX()(0, col)) + calc_alpha_intercell(grid.getAlphaX()(0, col-1), grid.getAlphaX()(0, col))) * grid.getConcentrations()(0, col) // - (calc_alpha_intercell(grid.getAlphaX()(0, col+1), grid.getAlphaX()(0, col)) + calc_alpha_intercell(grid.getAlphaX()(0, col-1), grid.getAlphaX()(0, col))) * grid.getConcentrations()(0, col)
+ calc_alpha_intercell(grid.getAlphaX()(0, col-1), grid.getAlphaX()(0, col)) * grid.getConcentrations()(0, col-1)); // + calc_alpha_intercell(grid.getAlphaX()(0, col-1), grid.getAlphaX()(0, col)) * grid.getConcentrations()(0, col-1));
} // }
// bottom without corners / looping over cols // bottom without corners / looping over cols
int row = rowMax-1; int row = rowMax-1;
for(int col=1; row<colMax-1;col++){ // for(int col=1; row<colMax-1;col++){
concentrations_t1(row, col) = grid.getConcentrations()(row, col) // concentrations_t1(row, col) = grid.getConcentrations()(row, col)
+ timestep/(grid.getDeltaRow()*grid.getDeltaRow()) * (2 * grid.getAlphaY()(row, col) * bc.getBoundaryConditionValue(BC_SIDE_BOTTOM)(1, col) // + timestep/(grid.getDeltaRow()*grid.getDeltaRow()) * (2 * grid.getAlphaY()(row, col) * bc.getBoundaryConditionValue(BC_SIDE_BOTTOM)(col)
- (calc_alpha_intercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col)) + 2 * grid.getAlphaY()(row, col)) * grid.getConcentrations()(row, col) // - (calc_alpha_intercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col)) + 2 * grid.getAlphaY()(row, col)) * grid.getConcentrations()(row, col)
+ calc_alpha_intercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col))) // + calc_alpha_intercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col)))
+ timestep/(grid.getDeltaCol()*grid.getDeltaCol()) * (calc_alpha_intercell(grid.getAlphaX()(row, col+1), grid.getAlphaX()(row, col)) * grid.getConcentrations()(row, col+1) // + timestep/(grid.getDeltaCol()*grid.getDeltaCol()) * (calc_alpha_intercell(grid.getAlphaX()(row, col+1), grid.getAlphaX()(row, col)) * grid.getConcentrations()(row, col+1)
- (calc_alpha_intercell(grid.getAlphaX()(row, col+1), grid.getAlphaX()(row, col)) + calc_alpha_intercell(grid.getAlphaX()(row, col-1), grid.getAlphaX()(row, col))) * grid.getConcentrations()(row, col) // - (calc_alpha_intercell(grid.getAlphaX()(row, col+1), grid.getAlphaX()(row, col)) + calc_alpha_intercell(grid.getAlphaX()(row, col-1), grid.getAlphaX()(row, col))) * grid.getConcentrations()(row, col)
+ calc_alpha_intercell(grid.getAlphaX()(row, col-1), grid.getAlphaX()(row, col-1)) * grid.getConcentrations()(row, col-1)); // + calc_alpha_intercell(grid.getAlphaX()(row, col-1), grid.getAlphaX()(row, col-1)) * grid.getConcentrations()(row, col-1));
} // }
concentrations_t1(0,0) = 0; concentrations_t1(0,0) = 0;
@ -117,16 +124,16 @@ auto FTCS_constant(Grid &grid, Boundary &bc, double timestep) {
concentrations_t1(0,colMax-1) = 0; concentrations_t1(0,colMax-1) = 0;
concentrations_t1(rowMax-1,colMax-1) = 0; concentrations_t1(rowMax-1,colMax-1) = 0;
grid.setConcentrations(concentrations_t1); return concentrations_t1;
} }
auto FTCS_closed(Grid &grid, Boundary &bc, double timestep) { void FTCS_closed(Grid grid, Boundary bc, double timestep) {
return 0; return;
} }
void FTCS(Grid &grid, Boundary &bc, double timestep) { MatrixXd FTCS(Grid grid, Boundary bc, double timestep) {
if (bc.getBoundaryConditionType() == BC_TYPE_CONSTANT) { if (bc.getBoundaryConditionType() == BC_TYPE_CONSTANT) {
FTCS_constant(grid, bc, timestep); return FTCS_constant(grid, bc, timestep);
} else if (bc.getBoundaryConditionType() == BC_TYPE_CLOSED) { } else if (bc.getBoundaryConditionType() == BC_TYPE_CLOSED) {
FTCS_closed(grid, bc, timestep); FTCS_closed(grid, bc, timestep);
} }

View File

@ -59,15 +59,15 @@ int Grid::getCol() {
void Grid::setDomain(int domain_col) { void Grid::setDomain(int domain_col) {
this->domain_col = domain_col; this->domain_col = domain_col;
this->delta_col = this->domain_col/this->col; this->delta_col = double(this->domain_col)/this->col;
} }
void Grid::setDomain(int domain_row, int domain_col) { void Grid::setDomain(int domain_row, int domain_col) {
this->domain_row = domain_row; this->domain_row = domain_row;
this->domain_col = domain_col; this->domain_col = domain_col;
this->domain_row = this->domain_row/this->row; this->delta_row = double(this->domain_row)/this->row;
this->domain_col = this->domain_col/this->col; this->delta_col = double(this->domain_col)/this->col;
} }
double Grid::getDeltaCol() { double Grid::getDeltaCol() {

View File

@ -9,7 +9,7 @@ using namespace std;
// } // }
Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach) : grid(grid), bc(bc) { Simulation::Simulation(Grid grid, Boundary bc, APPROACH approach) : grid(grid), bc(bc) {
//probably to DEBUG assignment of grid and bc //probably to DEBUG assignment of grid and bc
this->grid = grid; this->grid = grid;
this->approach = approach; this->approach = approach;
@ -48,9 +48,16 @@ auto Simulation::getIterations() {
void Simulation::run() { void Simulation::run() {
if (approach == FTCS_APPROACH) { if (approach == FTCS_APPROACH) {
cout << grid.getConcentrations() << endl;
cout << endl;
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
FTCS(grid, bc, timestep); grid.setConcentrations(FTCS(grid, bc, timestep));
if (i == 10) {
cout << grid.getConcentrations() << endl;
cout << endl;
}
} }
cout << grid.getConcentrations() << endl;
} else if (approach == BTCS_APPROACH) { } else if (approach == BTCS_APPROACH) {
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
//TODO //TODO