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_DIRICHLET = 1;
BTCSDiffusion::BTCSDiffusion(int x) : dim_x(x) {
BTCSDiffusion::BTCSDiffusion(int x) : n_x(x) {
this->grid_dim = 1;
this->dx = 1. / (x - 1);
// 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;
@ -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
// grid std::fill(this->bc.begin(), this->bc.end(), -1);
}
BTCSDiffusion::BTCSDiffusion(int x, int y, int z)
: dim_x(x), dim_y(y), dim_z(z) {
BTCSDiffusion::BTCSDiffusion(int x, int y, int z) : n_x(x), n_y(y), n_z(z) {
// this->grid_dim = 3;
// TODO: reserve memory for boundary conditions
}
void BTCSDiffusion::simulate1D(std::vector<double> &c, double bc_left,
double bc_right, std::vector<double> &alpha) {
// calculate dx
double dx = 1. / (this->dim_x - 1);
double bc_right,
const std::vector<double> &alpha, double dx,
int size) {
// calculate size needed for A matrix and b,x vectors
int size = this->dim_x + 2;
// we need 2 more grid cells for ghost cells
size = size + 2;
// set sizes of private and yet allocated vectors
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[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);
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;
b_vector[i] = -c[i - 1];
}
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,
std::vector<double> &alpha) {
const std::vector<double> &alpha) {
if (this->grid_dim == 1) {
double bc_left = getBCFromTuple(0, c[0]);
double bc_right = getBCFromTuple(1, c[c.size() - 1]);
// double bc_left = 5. * std::pow(10,-6);
// double bc_right = c[this->dim_x -1];
double bc_left = getBCFromTuple(0, c[0], alpha[0]);
double bc_right =
getBCFromTuple(1, c[c.size() - 1], alpha[alpha.size() - 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;
int type = std::get<0>(bc[index]);
if (type == BTCSDiffusion::BC_NEUMANN) {
// TODO implement gradient here
val = nearest_value;
val = neighbor_c + (this->time_step / (dx * dx)) * neighbor_alpha *
std::get<1>(bc[index]);
} else if (type == BTCSDiffusion::BC_DIRICHLET) {
val = std::get<1>(bc[index]);
} else {

View File

@ -48,7 +48,7 @@ public:
*
* @param x Count of cells in x direction.
*/
BTCSDiffusion(int x);
explicit BTCSDiffusion(int x);
/*!
* Currently not implemented: Create 2D-diffusion module.
@ -56,7 +56,7 @@ public:
* @param x Count of cells in x 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.
@ -65,7 +65,7 @@ public:
* @param y Count of cells in y 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.
@ -74,7 +74,7 @@ public:
* continious memory (Row-wise).
* @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
@ -97,11 +97,11 @@ public:
private:
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 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;
@ -112,9 +112,12 @@ private:
double time_step;
int grid_dim;
int dim_x;
int dim_y;
int dim_z;
int n_x;
double dx;
int n_y;
double dy;
int n_z;
double dz;
};
#endif // BTCSDIFFUSION_H_