From ca0fe9678b0fa37a0bfffe1e3538971bd212bdd8 Mon Sep 17 00:00:00 2001 From: Max Luebke Date: Mon, 13 Dec 2021 19:36:06 +0100 Subject: [PATCH] implement changes as discussed in meeting on 12-13-2021 --- src/BTCSDiffusion.cpp | 41 ++++++++++++++++++++--------------------- src/BTCSDiffusion.hpp | 21 ++++++++++++--------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/BTCSDiffusion.cpp b/src/BTCSDiffusion.cpp index 369c525..d5d7ac1 100644 --- a/src/BTCSDiffusion.cpp +++ b/src/BTCSDiffusion.cpp @@ -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(0, 0.)); + this->bc.resize(2, std::tuple(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 &c, double bc_left, - double bc_right, std::vector &alpha) { - // calculate dx - double dx = 1. / (this->dim_x - 1); + double bc_right, + const std::vector &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 &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 &c, double bc_left, A_matrix.insert(i, i + 1) = sx; b_vector[i] = -c[i - 1]; - } Eigen::SparseLU, Eigen::COLAMDOrdering> @@ -95,24 +94,24 @@ void BTCSDiffusion::setTimestep(double time_step) { } void BTCSDiffusion::simulate(std::vector &c, - std::vector &alpha) { + const std::vector &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 { diff --git a/src/BTCSDiffusion.hpp b/src/BTCSDiffusion.hpp index c64e9db..355c646 100644 --- a/src/BTCSDiffusion.hpp +++ b/src/BTCSDiffusion.hpp @@ -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 &c, std::vector &alpha); + void simulate(std::vector &c, const std::vector &alpha); /*! * Set the timestep of the simulation @@ -97,11 +97,11 @@ public: private: void simulate1D(std::vector &c, double bc_left, double bc_right, - std::vector &alpha); + const std::vector &alpha, double dx, int size); void simulate2D(std::vector &c); void simulate3D(std::vector &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_