1#ifndef ONKO3D_3_0_DIFFUSIONGRID_HPP
2#define ONKO3D_3_0_DIFFUSIONGRID_HPP
14#include <unordered_map>
69 for (
int i = 0; i < size; ++i) {
84 unordered_map<MecaCell::Vec, GridCell>
grid;
98 inline double deriv2nd(
double xm1,
double x,
double xp1) {
return ((xm1 - (2.0 * x) + xp1) / (
dx *
dx)); }
105 for (
auto &c :
grid) {
106 c.second.prevQuantities[i] = c.second.quantities[i];
114 for (
auto &c :
grid) {
115 for (
int i = 0; i <
molecules.size(); ++i) {
116 c.second.consumptions[i] = 0.;
125 for (
auto &c:
grid) {
146 inline double getDt(
double D)
const {
return (
dx *
dx) / (6 * D); }
156 double dt =
getDt(D);
158 for (
auto &c :
grid) {
228 if (
grid.count(v) <= 0)
231 return(
grid[forward<MecaCell::Vec>(v)].quantities[m]);
244 inline unordered_map<MecaCell::Vec, GridCell> &
getGrid() {
return grid; }
273 template<
typename world_t>
278 for (
auto &c : w->cells) {
282 if(center.
x()<min.
x()) min.
setX(center.
x());
283 if(center.
y()<min.
y()) min.
setY(center.
y());
284 if(center.
z()<min.
z()) min.
setZ(center.
z());
286 if(center.
x()>max.
x()) max.
setX(center.
x());
287 if(center.
y()>max.
y()) max.
setY(center.
y());
288 if(center.
z()>max.
z()) max.
setZ(center.
z());
291 if (
grid.count(forward<MecaCell::Vec>(center)) <= 0) {
293 for (
int i = 0; i <
molecules.size(); ++i) {
294 gc.
consumptions[i] += c->getBody().getConsumptionByIndex(i);
296 grid[forward<MecaCell::Vec>(center)] = gc;
298 for (
int i = 0; i <
molecules.size(); ++i) {
299 grid[forward<MecaCell::Vec>(center)].consumptions[i] += c->getBody().getConsumptionByIndex(i);
310 if (
grid.count(forward<MecaCell::Vec>(
pos)) <= 0) {
323 template<
typename world_t>
329 for (
int i = 0; i <
molecules.size(); ++i) {
332 while (
abs(lasts[i] - news[i]) /
grid.size() >=
accuracy && t < w->dt) {
Class representing a grid for molecule diffusion.
double getDx() const
Gets the grid cell size.
unordered_map< int, int > moleculesDict
double getCellSize() const
Gets the grid cell size.
bool isOutBoundary(const MecaCell::Vec &v)
Checks if a position is out of boundaries.
vector< Molecule > molecules
void setDx(double _dx)
Sets the grid cell size.
double getDt(double D) const
Gets the diffusion time step.
void setAccuracy(double a)
Sets the diffusion accuracy.
void cleanCellOutBoundary()
Cleans cells that are out of boundaries.
size_t size() const
Gets the size of the grid.
DiffusionGrid()=default
Default constructor.
void updateBoundary(world_t *w)
Updates the grid boundaries and consumptions.
void addMolecule(int n, Molecule m)
Adds a molecule to the grid.
double getMoleculeRealPos(const MecaCell::Vec &v, int mol)
Gets the quantity of a molecule at a real position.
MecaCell::Vec getIndexFromPosition(const MecaCell::Vec &v) const
Gets the grid index from a position.
DiffusionGrid(double dx, double accuracy)
Constructor with grid parameters.
unordered_map< MecaCell::Vec, GridCell > & getGrid()
Gets the grid.
MecaCell::Vec minBoundary
unordered_map< MecaCell::Vec, GridCell > grid
double computeStep(int i)
Updates the quantity of a molecule for each cell and returns the total amount of this molecule.
double deriv2nd(double xm1, double x, double xp1)
Calculates the second derivative.
void nextStep(int i)
Initializes the next step for a molecule.
double getMolecule(MecaCell::Vec v, int m)
Gets the quantity of a molecule at a position.
void computeMolecules(world_t *w)
Updates the quantities of molecules in the grid.
MecaCell::Vec maxBoundary
vector< Molecule > getMolecules() const
Gets the molecules in the grid.
void allCellEmpty()
Empties all cells.
general purpose 3D vector/point class.
void setX(const double f)
setter for x coordinate
void setY(const double f)
setter for y coordinate
void setZ(const double f)
setter for z coordinate
void resize(size_t newSize)
Resizes the vector to contain the specified number of elements.
size_t size() const
Returns the number of elements in the vector.
Namespace for diffusion-related structures and classes.
Vector3D Vec
alias for Vector3D
int abs(int x)
Computes the absolute value of an integer.
double floor(double x)
Computes the floor of a number.
Structure representing a cell in the diffusion grid.
GridCell()=default
Default constructor.
vector< double > prevQuantities
vector< double > consumptions
vector< double > quantities
GridCell(vector< Molecule > m)
Constructor to initialize a grid cell with given molecules.
Structure representing a molecule in the diffusion grid.
double defaultEvaporation
static constexpr double spheroidDensity
static constexpr double henryConst
Molecule(double d, double dQ, double density, double dE)
Constructor to initialize a molecule with given parameters.