MolSim
Loading...
Searching...
No Matches
CellContainer.h
Go to the documentation of this file.
1
9#pragma once
10#include "Cell.h"
11#include "Particle.h"
12#include "ParticleContainer.h"
13#include "io/input/FileReader.h"
15#include "utils/CellUtils.h"
16#include "utils/OMPWrapper.h"
17#include <array>
18#include <cmath>
19#include <iostream>
20#include <string>
21#include <utility>
22#include <vector>
23
24// NOTE 1: you could probably use arrays instead of vectors and make this a template class,
25// since we can compute the number of cells beforehand.
26
27// NOTE 2: i only extended halo cells one cell past the boundary. theoretically, they could continue infinitely, but
28// that would be a nightmare to manage.
29
33 using ContainerType = std::vector<Cell>;
34
35 private:
39 std::vector<omp_lock_t> cellLocks;
41 std::vector<std::reference_wrapper<Cell>> borderCells;
43 std::vector<std::reference_wrapper<Cell>> haloCells;
45 std::vector<std::reference_wrapper<Cell>> iterableCells;
47 std::array<double, 3> domainSize;
49 std::array<double, 3> cellSize{0, 0, 0};
51 std::array<size_t, 3> numCells{1, 1, 1};
53 std::array<BoundaryCondition, 6> conditions;
55 double cutoff;
57 size_t dim;
59 bool anyPeriodic{false};
62
63 public:
73 CellContainer(const std::array<double, 3> &domainSize, const std::array<BoundaryCondition, 6> &conditions,
74 double cutoff, ParticleContainer &particles, size_t dim = 3);
75
78
84 ContainerType::iterator begin();
85
91 ContainerType::iterator end();
92
101
110
117 ContainerType::const_iterator begin() const;
118
124 ContainerType::const_iterator end() const;
125
128 private:
130 std::vector<std::reference_wrapper<Cell>>::iterator outerIt;
132 std::vector<std::reference_wrapper<Cell>>::iterator outerEnd;
134 std::forward_list<std::reference_wrapper<Particle>>::iterator innerIt;
136 std::forward_list<std::reference_wrapper<Particle>>::iterator innerEnd;
139 void advance();
140
141 public:
149 SpecialParticleIterator(std::vector<std::reference_wrapper<Cell>>::iterator start,
150 std::vector<std::reference_wrapper<Cell>>::iterator end);
151
160
169
177 bool operator!=(const SpecialParticleIterator &other) const;
178
186 bool operator==(const SpecialParticleIterator &other) const;
187 };
188
195
202
209
216
225 Cell &operator[](size_t index);
226
235 const Cell &operator[](size_t index) const;
236
242 std::vector<Cell> &getCells();
243
249 const std::vector<Cell> &getCells() const;
250
256 std::vector<std::reference_wrapper<Cell>> &getBorderCells();
257
263 const std::vector<std::reference_wrapper<Cell>> &getBorderCells() const;
264
270 std::vector<std::reference_wrapper<Cell>> &getHaloCells();
271
277 const std::vector<std::reference_wrapper<Cell>> &getHaloCells() const;
278
284 std::vector<std::reference_wrapper<Cell>> &getIterableCells();
285
291 const std::vector<std::reference_wrapper<Cell>> &getIterableCells() const;
292
301
308 int getCellIndex(const std::array<double, 3> &position);
309
320
332
344
351 std::array<int, 3> getVirtualCellCoordinates(int index) const;
352
364 std::array<double, 3> getMirrorPosition(const std::array<double, 3> &position, const Cell &from, const Cell &to,
365 int direction) const;
366
376 int getOppositeNeighbor(int cellIndex, HaloLocation direction) const;
377
383 void calculateNeighbors(int cellIndex);
384
391 const std::vector<int> &getNeighbors(int cellIndex) const;
392
393 std::vector<std::reference_wrapper<Cell>> getNeighborCells(int cellIndex);
394
402 int getOppositeOfHalo(const Cell &from, HaloLocation location);
403
411 int getOppositeOfBorder(const Cell &from, BorderLocation location);
412
420 std::vector<int> getOppositeOfBorderCorner(const Cell &from, std::vector<BorderLocation> &locations);
421
428 std::vector<std::vector<BorderLocation>> getBorderCombinations(std::vector<BorderLocation> &locations);
429
435 const std::array<double, 3> &getDomainSize() const;
436
442 const std::array<double, 3> &getCellSize() const;
443
449 const std::array<size_t, 3> &getNumCells() const;
450
456 const std::array<BoundaryCondition, 6> &getConditions() const;
457
463 double getCutoff() const;
464
470 size_t getDim() const;
471
478 bool getAnyPeriodic() const;
479
486
493
499 size_t size() const;
500
506 size_t activeSize() const;
507
514 void printCellIndices() const;
515
520 void printCellContents() const;
521};
Utility functions for Cell and CellContainer objects.
HaloLocation
Enum containing the possible corner and edge directions of halo cells.
Definition CellUtils.h:22
BorderLocation
Enum containing the possible corner and edge directions of border cells.
Definition CellUtils.h:24
A single Cell in a CellContainer, for use with the linked cells method.
General class used to read and parse file input into some container.
Helper class for systems that may not have OpenMP functionality.
Class for encapsulating and iterating over multiple Particle objects.
Model of a single particle.
Factory class for creating different types of file output generators.
A special iterator for iterating over the boundary and halo cell containers.
Definition CellContainer.h:127
void advance()
Helper function to move to the next Cell which contains at least one particle. Stops when no further ...
std::vector< std::reference_wrapper< Cell > >::iterator outerEnd
The end of the outer iterator, iterating over each Cell pointer.
Definition CellContainer.h:132
std::vector< std::reference_wrapper< Cell > >::iterator outerIt
The outer iterator, iterating over each Cell pointer.
Definition CellContainer.h:130
SpecialParticleIterator(std::vector< std::reference_wrapper< Cell > >::iterator start, std::vector< std::reference_wrapper< Cell > >::iterator end)
Construct a new special iterator.
SpecialParticleIterator & operator++()
Overload of the preincrement operator.
std::forward_list< std::reference_wrapper< Particle > >::iterator innerIt
The inner iterator, iterating over each Particle pointer in a Cell.
Definition CellContainer.h:134
bool operator!=(const SpecialParticleIterator &other) const
Overload of the inequality operator.
std::forward_list< std::reference_wrapper< Particle > >::iterator innerEnd
The end of the inner iterator, iterating over each Particle pointer in a Cell.
Definition CellContainer.h:136
bool operator==(const SpecialParticleIterator &other) const
Overload of the equality operator.
Particle & operator*()
Overload of the dereference operator.
Cell encapsulation class for implementing the linked cell method.
Definition CellContainer.h:31
const Cell & operator[](size_t index) const
Dereference of the [] operator (const).
void removeHaloCellParticles()
Removes the active halo Cell Particle objects.
std::array< BoundaryCondition, 6 > conditions
An array of boundary conditions to be applied at each boundary (North, South, West,...
Definition CellContainer.h:53
std::array< size_t, 3 > numCells
The number of cells in each dimension (default: 1, 1, 1).
Definition CellContainer.h:51
int getCellIndex(const std::array< double, 3 > &position)
Gets the 1D index in the Cell container based on the given position.
bool moveParticle(Particle &p)
Moves a particle from one Cell to another.
size_t dim
The number of dimensions (2/3)
Definition CellContainer.h:57
ParticleContainer::PairIterator endPairs()
Direct access to the end of the ParticleContainer's PairIterator.
ContainerType::iterator begin()
Standard library iterator function for marking the beginning of the iteration process.
int getOppositeNeighbor(int cellIndex, HaloLocation direction) const
Gets the index of the opposing Cell in the specified direction.
double getCutoff() const
Gets the cutoff radius used for calculating the cell size.
bool anyPeriodic
Determines if there are any periodic halo cells.
Definition CellContainer.h:59
std::array< double, 3 > getMirrorPosition(const std::array< double, 3 > &position, const Cell &from, const Cell &to, int direction) const
Mirrors a position from a source Cell to a destination Cell.
const std::vector< std::reference_wrapper< Cell > > & getHaloCells() const
Gets a const reference to the halo Cell container.
SpecialParticleIterator boundaryBegin()
Gets a SpecialParticleIterator pointing to the beginning of the boundary Cell pointer container.
void printCellContents() const
Debug function to print the contents of each cell.
ContainerType::const_iterator end() const
Standard library iterator function for marking the end of the iteration process of a const CellContai...
const std::array< BoundaryCondition, 6 > & getConditions() const
Gets the boundary conditions at each boundary.
std::vector< omp_lock_t > cellLocks
Container of mutual exclusion locks for each cell.
Definition CellContainer.h:39
const std::vector< std::reference_wrapper< Cell > > & getIterableCells() const
Gets a const reference to the iterable Cell container.
ParticleContainer & getParticles()
Gets a reference to the primary ParticleContainer.
SpecialParticleIterator haloBegin()
Gets a SpecialParticleIterator pointing to the beginning of the halo Cell pointer container.
size_t activeSize() const
Gets the amount of active Particle objects in the ParticleContainer.
std::array< double, 3 > cellSize
The size of each cell in each dimension (default: 0, 0, 0).
Definition CellContainer.h:49
std::vector< std::reference_wrapper< Cell > > & getBorderCells()
Gets a reference to the border Cell container.
std::vector< std::reference_wrapper< Cell > > getNeighborCells(int cellIndex)
std::vector< std::reference_wrapper< Cell > > iterableCells
Container of references to cells to iterate over when calculating the forces between particles.
Definition CellContainer.h:45
void deleteParticle(Particle &p)
Removes a Particle from a Cell and marks it inactive.
ContainerType cells
The container of Cell objects itself.
Definition CellContainer.h:37
int getOppositeOfBorder(const Cell &from, BorderLocation location)
For a border cell returns the index of the halo cell on the opposite side of the domain.
const ParticleContainer & getParticles() const
Gets a const reference to the primary ParticleContainer.
std::vector< Cell > ContainerType
Typedef for the underlying Cell container type.
Definition CellContainer.h:33
size_t size() const
Gets the size of the ParticleContainer, including all inactive Cells.
int getOppositeOfHalo(const Cell &from, HaloLocation location)
For a halo cell returns the index of the border cell on the opposite side of the domain.
CellContainer(const std::array< double, 3 > &domainSize, const std::array< BoundaryCondition, 6 > &conditions, double cutoff, ParticleContainer &particles, size_t dim=3)
Constructs a new CellContainer and initializes all Cell objects and locks.
double cutoff
The cutoff radius.
Definition CellContainer.h:55
std::vector< std::vector< BorderLocation > > getBorderCombinations(std::vector< BorderLocation > &locations)
For a collection of border locations, returns all unique pairs.
ContainerType::const_iterator begin() const
Standard library iterator function for marking the beginning of the iteration process of a const Cell...
std::vector< std::reference_wrapper< Cell > > & getHaloCells()
Gets a reference to the halo Cell container.
const std::vector< int > & getNeighbors(int cellIndex) const
Gets a vector of neighbouring Cell indices, including the Cell itself.
size_t getDim() const
Gets the number of dimensions of the linked cells.
std::vector< std::reference_wrapper< Cell > > & getIterableCells()
Gets a reference to the iterable Cell container.
const std::array< double, 3 > & getDomainSize() const
Gets a const reference to the CellContainer's domain size.
SpecialParticleIterator haloEnd()
Gets a SpecialParticleIterator pointing to the end of the halo Cell pointer container.
Cell & operator[](size_t index)
Dereference of the [] operator.
const std::array< double, 3 > & getCellSize() const
Gets a const reference to the CellContainer's cell size.
bool getAnyPeriodic() const
Checks if any boundary condition is periodic.
ContainerType::iterator end()
Standard library iterator function for marking the end of the iteration process.
ParticleContainer & particles
A reference to the overarching ParticleContainer.
Definition CellContainer.h:61
void calculateNeighbors(int cellIndex)
Computes the vector of neighbouring Cell indices, including the Cell itself.
std::vector< Cell > & getCells()
Gets a reference to the Cell container.
bool addParticle(Particle &p)
Adds a Particle to the Cell container.
std::vector< std::reference_wrapper< Cell > > haloCells
Container of references to halo cells.
Definition CellContainer.h:43
SpecialParticleIterator boundaryEnd()
Gets a SpecialParticleIterator pointing to the end of the boundary Cell pointer container.
std::vector< int > getOppositeOfBorderCorner(const Cell &from, std::vector< BorderLocation > &locations)
For a border cell returns the indices of the corner cells on the opposite sides of the domain.
const std::vector< Cell > & getCells() const
Gets a const reference to the Cell container.
std::array< int, 3 > getVirtualCellCoordinates(int index) const
Gets the coordinates of a Cell from its index.
std::vector< std::reference_wrapper< Cell > > borderCells
Container of references to border cells.
Definition CellContainer.h:41
const std::vector< std::reference_wrapper< Cell > > & getBorderCells() const
Gets a const reference to the border Cell container.
~CellContainer()
Destroys the CellContainer object and frees the reserved locks.
const std::array< size_t, 3 > & getNumCells() const
Gets a const reference to the CellContainer's cell count in each dimension.
void printCellIndices() const
Debug function to print the indices of all cells for a 2D container.
std::array< double, 3 > domainSize
The size of the domain in each dimension.
Definition CellContainer.h:47
ParticleContainer::PairIterator beginPairs()
Direct access to the beginning of the ParticleContainer's PairIterator.
Class for storing data of a single Cell in a domain split into cells.
Definition Cell.h:23
Custom pair iterator for iterating through all possible pairs of Particle objects.
Definition ParticleContainer.h:50
Class for encapsulating multiple Particle objects..
Definition ParticleContainer.h:17
Particle class modeling a particle's position, velocity, force, mass and type.
Definition Particle.h:46