From 02a95315448a7061a1814ad8c1183c60d395b174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Wed, 24 Aug 2022 09:28:54 +0200 Subject: [PATCH] feat: add helper functions to TugInput struct --- include/Diffusion.hpp | 76 ++++++++++++++++++++++++++++++++++++++---- test/testDiffusion.cpp | 14 +++----- 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/include/Diffusion.hpp b/include/Diffusion.hpp index 983293d..0f03088 100644 --- a/include/Diffusion.hpp +++ b/include/Diffusion.hpp @@ -2,6 +2,7 @@ #define DIFFUSION_H_ #include "BoundaryCondition.hpp" +#include "Solver.hpp" #include #include #include @@ -15,19 +16,80 @@ namespace diffusion { * Defines grid dimensions and boundary conditions. */ typedef struct { - uint32_t grid_cells[3]; - double domain_size[3]; - bc::BoundaryCondition *bc; + uint32_t + grid_cells[3]; /**< Count of grid cells in each of the 3 directions.*/ + double domain_size[3]; /**< Domain sizes in each of the 3 directions.*/ + bc::BoundaryCondition *bc; /**< Boundary conditions for the grid.*/ } TugGrid; /** * Besides containing the grid structure it holds also information about the * desired time step to simulate and which solver to use. */ -typedef struct { - double time_step; - Eigen::VectorXd (*solver)(Eigen::SparseMatrix, Eigen::VectorXd); - TugGrid grid; +typedef struct tug_input_s { + double time_step; /**< Time step which should be simulated by diffusion.*/ + Eigen::VectorXd (*solver)(Eigen::SparseMatrix, Eigen::VectorXd) = + tug::solver::ThomasAlgorithm; /**< Solver function to use.*/ + TugGrid grid; /**< Grid specification.*/ + + /** + * Set the desired time step for diffusion simulation. + * + * \param dt Time step in seconds. + */ + void setTimestep(double dt) { time_step = dt; } + + /** + * Set the count of grid cells in each dimension. + * + * \param x Count of grid cells in x direction. + * \param y Count of grid cells in y direction. + * \param z Count of grid cells in z direction. + */ + void setGridCellN(uint32_t x, uint32_t y = 0, uint32_t z = 0) { + grid.grid_cells[0] = x; + grid.grid_cells[1] = y; + grid.grid_cells[2] = z; + } + + /** + * Set the domain size of the grid in each direction. + + * \param Domain size in x direction. + * \param Domain size in y direction. + * \param Domain size in z direction. + */ + void setDomainSize(double x, double y = 0, double z = 0) { + grid.domain_size[0] = x; + grid.domain_size[1] = y; + grid.domain_size[2] = z; + } + + /** + * Set boundary conditions for grid instance. + * + * \param bc Boundary conditions to be set. + */ + void setBoundaryCondition(bc::BoundaryCondition &bc) { grid.bc = &bc; } + + /** + * Retrieve the set boundary condition from grid instance. + * + * \return Boundary condition object if boundary conditions were set, + * otherwise NULL. + */ + auto getBoundaryCondition() -> bc::BoundaryCondition { return *(grid.bc); } + + /** + * Set the solver function. + * + * \param f_in Pointer to function which takes a sparse matrix and a vector as + * input and returns another vector. + */ + void setSolverFunction(Eigen::VectorXd (*f_in)(Eigen::SparseMatrix, + Eigen::VectorXd)) { + solver = f_in; + } } TugInput; /** diff --git a/test/testDiffusion.cpp b/test/testDiffusion.cpp index 2def0c3..1ecb321 100644 --- a/test/testDiffusion.cpp +++ b/test/testDiffusion.cpp @@ -18,16 +18,10 @@ static std::vector alpha(N *M, 1e-3); static TugInput setupDiffu(BoundaryCondition &bc) { TugInput diffu; - diffu.time_step = 1.; - diffu.solver = tug::solver::ThomasAlgorithm; - - diffu.grid.grid_cells[0] = N; - diffu.grid.grid_cells[1] = M; - - diffu.grid.domain_size[0] = N; - diffu.grid.domain_size[1] = M; - - diffu.grid.bc = &bc; + diffu.setTimestep(1); + diffu.setGridCellN(N, M); + diffu.setDomainSize(N, M); + diffu.setBoundaryCondition(bc); return diffu; }