From 29fc70ce1ab18ae46cc8d0fb841e2af190f755c5 Mon Sep 17 00:00:00 2001 From: Max Luebke Date: Mon, 17 Jan 2022 14:20:53 +0100 Subject: [PATCH] rewrite initialization of module ATTENTION: code will throw errors on compilation! --- src/BTCSDiffusion.cpp | 63 ++++++++++++++++++++++++++++++++----------- src/BTCSDiffusion.hpp | 58 ++++++++++++++++++++------------------- 2 files changed, 79 insertions(+), 42 deletions(-) diff --git a/src/BTCSDiffusion.cpp b/src/BTCSDiffusion.cpp index 1db5f60..1b45749 100644 --- a/src/BTCSDiffusion.cpp +++ b/src/BTCSDiffusion.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -12,26 +13,58 @@ const int BTCSDiffusion::BC_CONSTANT = 0; const int BTCSDiffusion::BC_CLOSED = 1; const int BTCSDiffusion::BC_FLUX = 2; -BTCSDiffusion::BTCSDiffusion(int x) : n_x(x) { - this->grid_dim = 1; - this->dx = 1. / (x - 1); +BTCSDiffusion::BTCSDiffusion(unsigned int dim) : grid_dim(dim) { + assert(dim <= 3); - // per default use Neumann condition with gradient of 0 at the end of the grid - this->bc.resize(2, std::tuple(BTCSDiffusion::BC_CONSTANT, 0.)); + grid_cells.resize(dim, 1); + spatial_discretization.resize(dim, 1); + deltas.resize(dim, 1); } -BTCSDiffusion::BTCSDiffusion(int x, int y) : n_x(x), n_y(y) { - // this->grid_dim = 2; - - // this->bc.reserve(x * 2 + y * 2); - // // per default use Neumann condition with gradient of 0 at the end of the - // grid std::fill(this->bc.begin(), this->bc.end(), -1); +std::vector BTCSDiffusion::getNumberOfGridCells() { + return this->grid_cells; } -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 +std::vector BTCSDiffusion::getSpatialDiscretization() { + return this->spatial_discretization; } +void BTCSDiffusion::setNumberOfGridCells(std::vector &n_grid) { + grid_cells = n_grid; + assert(grid_cells.size() == grid_dim); + updateDeltas(); +} +void BTCSDiffusion::setSpatialDiscretization(std::vector &s_grid) { + spatial_discretization = s_grid; + assert(spatial_discretization.size() == grid_dim); + updateDeltas(); +} + +void BTCSDiffusion::updateDeltas() { + for (int i = 0; i < grid_dim; i++) { + deltas[i] = (double)spatial_discretization[i] / grid_cells[i]; + } +} +// 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(BTCSDiffusion::BC_CONSTANT, 0.)); +// } +// BTCSDiffusion::BTCSDiffusion(int x, int y) : n_x(x), n_y(y) { + +// // this->grid_dim = 2; + +// // this->bc.reserve(x * 2 + y * 2); +// // // 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) : 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, diff --git a/src/BTCSDiffusion.hpp b/src/BTCSDiffusion.hpp index 9720ec9..e849224 100644 --- a/src/BTCSDiffusion.hpp +++ b/src/BTCSDiffusion.hpp @@ -42,36 +42,41 @@ public: /*! * Defines a closed/Neumann boundary condition. */ - static const int BC_CLOSED; + static const int BC_CLOSED; - /*! - * Defines a flux/Cauchy boundary condition. - */ - static const int BC_FLUX; + /*! + * Defines a flux/Cauchy boundary condition. + */ + static const int BC_FLUX; /*! * Create 1D-diffusion module. * * @param x Count of cells in x direction. */ - explicit BTCSDiffusion(int x); + BTCSDiffusion(unsigned int dim); - /*! - * Currently not implemented: Create 2D-diffusion module. - * - * @param x Count of cells in x direction. - * @param y Count of cells in y direction. - */ - explicit BTCSDiffusion(int x, int y); + std::vector getNumberOfGridCells(); + std::vector getSpatialDiscretization(); + void setNumberOfGridCells(std::vector &n_grid); + void setSpatialDiscretization(std::vector &s_grid); - /*! - * Currently not implemented: Create 3D-diffusion module. - * - * @param x Count of cells in x direction. - * @param y Count of cells in y direction. - * @param z Count of cells in z direction. - */ - explicit BTCSDiffusion(int x, int y, int z); + // /*! + // * Currently not implemented: Create 2D-diffusion module. + // * + // * @param x Count of cells in x direction. + // * @param y Count of cells in y direction. + // */ + // explicit BTCSDiffusion(int x, int y); + + // /*! + // * Currently not implemented: Create 3D-diffusion module. + // * + // * @param x Count of cells in x direction. + // * @param y Count of cells in y direction. + // * @param z Count of cells in z direction. + // */ + // explicit BTCSDiffusion(int x, int y, int z); /*! * With given ghost zones simulate diffusion. Only 1D allowed at this moment. @@ -109,6 +114,8 @@ private: double getBCFromTuple(int index, double nearest_value, double neighbor_alpha); + void updateDeltas(); + boundary_condition bc; Eigen::SparseMatrix A_matrix; @@ -118,12 +125,9 @@ private: double time_step; int grid_dim; - int n_x; - double dx; - int n_y; - double dy; - int n_z; - double dz; + std::vector grid_cells; + std::vector spatial_discretization; + std::vector deltas; }; #endif // BTCSDIFFUSION_H_