1#ifndef ONKO3D_3_0_DIFFUSIONGRID_HPP
2#define ONKO3D_3_0_DIFFUSIONGRID_HPP
14#include <unordered_map>
60 for (
int i = 0; i < size; ++i) {
86 for (
int i = 0; i <
grid.size(); ++i) {
87 for (
int j = 0; j <
grid.size(); ++j) {
88 for(
int m = 0; m <
molecules.size(); ++m){
89 grid[i][j].prevQuantities[m] =
grid[i][j].quantities[m];
90 grid[i][j].quantities[m] = 0.;
91 grid[i][j].consumptions[m] = 0.;
106 for (
int i = -1; i <= 1; ++i) {
107 for (
int j = -1; j <= 1; ++j) {
108 if(i != 0 || j != 0){
112 if(xi < 0) xi =
width -1;
113 if(xi >=
width) xi = 0;
114 if(yj < 0) yj =
height -1;
116 for(
int m = 0; m <
molecules.size(); ++m){
120 if(xi < 0) xi =
width -1;
121 if(xi >=
width) xi = 0;
122 if(yj >= 0 && yj <
height){
123 for(
int m = 0; m <
molecules.size(); ++m){
126 }
else nbNeighbors --;
128 if(yj < 0) yj =
height -1;
130 if(xi >= 0 && xi <
width){
131 for(
int m = 0; m <
molecules.size(); ++m){
134 }
else nbNeighbors --;
136 if(xi >= 0 && xi < width && yj >= 0 && yj <
height){
137 for(
int m = 0; m <
molecules.size(); ++m){
146 for(
int m = 0; m <
molecules.size(); ++m){
158 for (
int i = 0; i <
width; ++i) {
159 for (
int j = 0; j <
height; ++j) {
171 template<
typename world_t>
173 for(
auto cell : w->cells){
174 int x = cell->getBody().get2DPosition().x();
175 int y = cell->getBody().get2DPosition().y();
176 for(
int m = 0; m <
molecules.size(); ++m){
177 grid[x][y].consumptions[m] += cell->getBody().getConsumptions()[m];
263 for(
int i = 0; i < w; ++i){
265 for(
int j = 0; j < h; ++j){
286 template<
typename world_t>
Manages a grid of molecules for diffusion.
std::vector< std::vector< GridCell > > & getGrid()
Gets the grid.
std::vector< Molecule > getMolecules() const
Gets the molecules in the grid.
void initGrid(int size)
Initializes the square grid.
void initGrid(int w, int h)
Initializes the grid with dimensions.
double getMolecule(int x, int y, int m)
Gets the quantity of a molecule at a specific position.
int getWidth() const
Gets the width of the grid.
void setToreX(bool b)
Sets the toroidal property on the x-axis.
int getHeight() const
Gets the height of the grid.
std::unordered_map< int, int > moleculesDict
void diffuse(int x, int y)
Applies diffusion to the molecule at position (x, y).
std::vector< std::vector< GridCell > > grid
void updateConsumptions(world_t *w)
Updates the consumptions for each cell.
void setToreY(bool b)
Sets the toroidal property on the y-axis.
void preStep()
Initializes cells for the next step.
std::vector< Molecule > molecules
void computeStep()
Updates the quantity of molecules for each cell.
void setTore(bool x, bool y)
Sets the toroidal properties on both axes.
void addMolecule(int id, Molecule m)
Adds a molecule to the grid.
void computeMolecules(world_t *w)
Computes the quantities of molecules in the grid.
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 2D diffusion-related classes and functions.
Represents a cell in the diffusion grid.
GridCell(std::vector< Molecule > m)
Constructor with existing molecules.
std::vector< double > quantities
std::vector< double > prevQuantities
GridCell()=default
Default constructor.
std::vector< double > consumptions
Represents a molecule with diffusion properties.
Molecule(double dc, double dq, double de)
double defaultEvaporation