implement changes as discussed in meeting on 12-13-2021

This commit is contained in:
Max Luebke 2021-12-13 19:36:06 +01:00
parent 30a9dbafb8
commit ca0fe9678b
2 changed files with 32 additions and 30 deletions

View File

@ -11,13 +11,14 @@
const int BTCSDiffusion::BC_NEUMANN = 0; const int BTCSDiffusion::BC_NEUMANN = 0;
const int BTCSDiffusion::BC_DIRICHLET = 1; const int BTCSDiffusion::BC_DIRICHLET = 1;
BTCSDiffusion::BTCSDiffusion(int x) : dim_x(x) { BTCSDiffusion::BTCSDiffusion(int x) : n_x(x) {
this->grid_dim = 1; this->grid_dim = 1;
this->dx = 1. / (x - 1);
// per default use Neumann condition with gradient of 0 at the end of the grid // per default use Neumann condition with gradient of 0 at the end of the grid
this->bc.resize(2, std::tuple<bctype, double>(0, 0.)); this->bc.resize(2, std::tuple<bctype, double>(BTCSDiffusion::BC_NEUMANN, 0.));
} }
BTCSDiffusion::BTCSDiffusion(int x, int y) : dim_x(x), dim_y(y) { BTCSDiffusion::BTCSDiffusion(int x, int y) : n_x(x), n_y(y) {
// this->grid_dim = 2; // this->grid_dim = 2;
@ -25,20 +26,19 @@ BTCSDiffusion::BTCSDiffusion(int x, int y) : dim_x(x), dim_y(y) {
// // per default use Neumann condition with gradient of 0 at the end of the // // per default use Neumann condition with gradient of 0 at the end of the
// grid std::fill(this->bc.begin(), this->bc.end(), -1); // grid std::fill(this->bc.begin(), this->bc.end(), -1);
} }
BTCSDiffusion::BTCSDiffusion(int x, int y, int z) BTCSDiffusion::BTCSDiffusion(int x, int y, int z) : n_x(x), n_y(y), n_z(z) {
: dim_x(x), dim_y(y), dim_z(z) {
// this->grid_dim = 3; // this->grid_dim = 3;
// TODO: reserve memory for boundary conditions // TODO: reserve memory for boundary conditions
} }
void BTCSDiffusion::simulate1D(std::vector<double> &c, double bc_left, void BTCSDiffusion::simulate1D(std::vector<double> &c, double bc_left,
double bc_right, std::vector<double> &alpha) { double bc_right,
// calculate dx const std::vector<double> &alpha, double dx,
double dx = 1. / (this->dim_x - 1); int size) {
// calculate size needed for A matrix and b,x vectors // we need 2 more grid cells for ghost cells
int size = this->dim_x + 2; size = size + 2;
// set sizes of private and yet allocated vectors // set sizes of private and yet allocated vectors
b_vector.resize(size); b_vector.resize(size);
@ -62,7 +62,7 @@ void BTCSDiffusion::simulate1D(std::vector<double> &c, double bc_left,
b_vector[0] = bc_left; b_vector[0] = bc_left;
b_vector[size - 1] = bc_right; b_vector[size - 1] = bc_right;
for (int i = 1; i < this->dim_x + 1; i++) { for (int i = 1; i < this->n_x + 1; i++) {
double sx = (alpha[i - 1] * time_step) / (dx * dx); double sx = (alpha[i - 1] * time_step) / (dx * dx);
A_matrix.insert(i, i) = -1. - 2. * sx; A_matrix.insert(i, i) = -1. - 2. * sx;
@ -70,7 +70,6 @@ void BTCSDiffusion::simulate1D(std::vector<double> &c, double bc_left,
A_matrix.insert(i, i + 1) = sx; A_matrix.insert(i, i + 1) = sx;
b_vector[i] = -c[i - 1]; b_vector[i] = -c[i - 1];
} }
Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int>> Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int>>
@ -95,24 +94,24 @@ void BTCSDiffusion::setTimestep(double time_step) {
} }
void BTCSDiffusion::simulate(std::vector<double> &c, void BTCSDiffusion::simulate(std::vector<double> &c,
std::vector<double> &alpha) { const std::vector<double> &alpha) {
if (this->grid_dim == 1) { if (this->grid_dim == 1) {
double bc_left = getBCFromTuple(0, c[0]); double bc_left = getBCFromTuple(0, c[0], alpha[0]);
double bc_right = getBCFromTuple(1, c[c.size() - 1]); double bc_right =
// double bc_left = 5. * std::pow(10,-6); getBCFromTuple(1, c[c.size() - 1], alpha[alpha.size() - 1]);
// double bc_right = c[this->dim_x -1];
simulate1D(c, bc_left, bc_right, alpha); simulate1D(c, bc_left, bc_right, alpha, this->dx, this->n_x);
} }
} }
double BTCSDiffusion::getBCFromTuple(int index, double nearest_value) { double BTCSDiffusion::getBCFromTuple(int index, double neighbor_c,
double neighbor_alpha) {
double val = -1; double val = -1;
int type = std::get<0>(bc[index]); int type = std::get<0>(bc[index]);
if (type == BTCSDiffusion::BC_NEUMANN) { if (type == BTCSDiffusion::BC_NEUMANN) {
// TODO implement gradient here val = neighbor_c + (this->time_step / (dx * dx)) * neighbor_alpha *
val = nearest_value; std::get<1>(bc[index]);
} else if (type == BTCSDiffusion::BC_DIRICHLET) { } else if (type == BTCSDiffusion::BC_DIRICHLET) {
val = std::get<1>(bc[index]); val = std::get<1>(bc[index]);
} else { } else {

View File

@ -48,7 +48,7 @@ public:
* *
* @param x Count of cells in x direction. * @param x Count of cells in x direction.
*/ */
BTCSDiffusion(int x); explicit BTCSDiffusion(int x);
/*! /*!
* Currently not implemented: Create 2D-diffusion module. * Currently not implemented: Create 2D-diffusion module.
@ -56,7 +56,7 @@ public:
* @param x Count of cells in x direction. * @param x Count of cells in x direction.
* @param y Count of cells in y direction. * @param y Count of cells in y direction.
*/ */
BTCSDiffusion(int x, int y); explicit BTCSDiffusion(int x, int y);
/*! /*!
* Currently not implemented: Create 3D-diffusion module. * Currently not implemented: Create 3D-diffusion module.
@ -65,7 +65,7 @@ public:
* @param y Count of cells in y direction. * @param y Count of cells in y direction.
* @param z Count of cells in z direction. * @param z Count of cells in z direction.
*/ */
BTCSDiffusion(int x, int y, int z); explicit BTCSDiffusion(int x, int y, int z);
/*! /*!
* With given ghost zones simulate diffusion. Only 1D allowed at this moment. * With given ghost zones simulate diffusion. Only 1D allowed at this moment.
@ -74,7 +74,7 @@ public:
* continious memory (Row-wise). * continious memory (Row-wise).
* @param alpha Vector of diffusioncoefficients for each grid element. * @param alpha Vector of diffusioncoefficients for each grid element.
*/ */
void simulate(std::vector<double> &c, std::vector<double> &alpha); void simulate(std::vector<double> &c, const std::vector<double> &alpha);
/*! /*!
* Set the timestep of the simulation * Set the timestep of the simulation
@ -97,11 +97,11 @@ public:
private: private:
void simulate1D(std::vector<double> &c, double bc_left, double bc_right, void simulate1D(std::vector<double> &c, double bc_left, double bc_right,
std::vector<double> &alpha); const std::vector<double> &alpha, double dx, int size);
void simulate2D(std::vector<double> &c); void simulate2D(std::vector<double> &c);
void simulate3D(std::vector<double> &c); void simulate3D(std::vector<double> &c);
double getBCFromTuple(int index, double nearest_value); double getBCFromTuple(int index, double nearest_value, double neighbor_alpha);
boundary_condition bc; boundary_condition bc;
@ -112,9 +112,12 @@ private:
double time_step; double time_step;
int grid_dim; int grid_dim;
int dim_x; int n_x;
int dim_y; double dx;
int dim_z; int n_y;
double dy;
int n_z;
double dz;
}; };
#endif // BTCSDIFFUSION_H_ #endif // BTCSDIFFUSION_H_