Molecular Dynamics Simulation  1.0
LinkedCellsContainer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iterator>
4 #include <ranges>
5 #include <vector>
6 
7 #include "cells/Cell.h"
9 
15  std::vector<std::vector<Cell*>> iteration_orders;
16 
17  public:
22 
26  enum class BoundarySide { LEFT, RIGHT, BOTTOM, TOP, BACK, FRONT };
27 
28  public:
47  LinkedCellsContainer(const std::array<double, 3>& domain_size, double cutoff_radius,
48  const std::array<BoundaryCondition, 6>& boundary_types = {BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW,
51  int n = 0);
52 
60  void addParticle(const Particle& p) override;
61 
69  void addParticle(Particle&& p) override;
70 
74  void prepareForceCalculation() override;
75 
83  void applySimpleForces(const std::vector<std::shared_ptr<SimpleForceSource>>& simple_force_sources) override;
84 
95  void applyPairwiseForces(const std::vector<std::shared_ptr<PairwiseForceSource>>& force_sources) override;
96 
105  void applyTargettedForces(const std::vector<std::shared_ptr<TargettedForceSource>>& targetted_force_sources,
106  double curr_simulation_time) override;
107 
114  void reserve(size_t n) override;
115 
121  size_t size() const override;
122 
129  Particle& operator[](int i) override;
130 
136  std::vector<Particle>::iterator begin() override;
137 
143  std::vector<Particle>::iterator end() override;
144 
150  std::vector<Particle>::const_iterator begin() const override;
151 
157  std::vector<Particle>::const_iterator end() const override;
158 
163  [[nodiscard]] const std::vector<Particle>& getParticles() const override;
164 
172  const std::array<double, 3>& getDomainSize() const;
173 
181  double getCutoffRadius() const;
182 
190  const std::vector<Cell>& getCells();
191 
199  const std::vector<Cell*>& getBoundaryCells() const;
200 
208  const std::array<double, 3>& getCellSize() const;
209 
217  const std::array<int, 3>& getDomainNumCells() const;
218 
227  int cellCoordToCellIndex(int cx, int cy, int cz) const;
228 
235  Cell* particlePosToCell(const std::array<double, 3>& pos);
236 
245  Cell* particlePosToCell(double x, double y, double z);
246 
250  static std::string boundaryConditionToString(const BoundaryCondition& bc);
251 
252  private:
256  void initCells();
257 
262 
266  void initIterationOrders();
267 
272 
277  void deleteHaloParticles();
278 
283 
287  friend class OutflowBoundaryType;
288 
292  friend class PeriodicBoundaryType;
293 
294  private:
298  std::vector<Particle> particles;
299 
303  std::array<double, 3> domain_size;
304 
309 
313  std::array<BoundaryCondition, 6> boundary_types;
314 
318  std::array<double, 3> cell_size;
319 
323  std::array<int, 3> domain_num_cells;
324 
328  std::vector<Cell> cells;
329 
333  std::vector<Cell*> domain_cell_references;
334 
338  std::vector<Cell*> boundary_cell_references;
339 
343  std::vector<Cell*> halo_cell_references;
344 
345  // Boundary cell references with respect to x-axis pointing to the right, y-axis pointing up and z axis pointing out of the screen
346 
350  std::vector<Cell*> left_boundary_cell_references;
351 
355  std::vector<Cell*> right_boundary_cell_references;
356 
361 
365  std::vector<Cell*> top_boundary_cell_references;
366 
370  std::vector<Cell*> back_boundary_cell_references;
371 
375  std::vector<Cell*> front_boundary_cell_references;
376 
377  // Halo cell references with respect to x-axis pointing to the right, y-axis pointing up and z axis pointing out of the screen
378 
382  std::vector<Cell*> left_halo_cell_references;
383 
387  std::vector<Cell*> right_halo_cell_references;
388 
392  std::vector<Cell*> bottom_halo_cell_references;
393 
397  std::vector<Cell*> top_halo_cell_references;
398 
402  std::vector<Cell*> back_halo_cell_references;
403 
407  std::vector<Cell*> front_halo_cell_references;
408 };
Class representing a cell in the linked cells algorithm.
Definition: Cell.h:12
Extension of the ParticleContainer class using a linked cells data structure for improved performance...
void deleteHaloParticles()
Removes all particles in the halo cells from the particles vector. ATTENTION: A particle reference up...
std::vector< Cell * > right_halo_cell_references
References to the halo cells on the right (x = domain_num_cells[0])
void reserve(size_t n) override
Reserves space for n particles. This is useful if the number of particles is known in advance and pre...
std::vector< Cell * > bottom_boundary_cell_references
References to the boundary cells on the bottom (y = 0)
void applySimpleForces(const std::vector< std::shared_ptr< SimpleForceSource >> &simple_force_sources) override
Applies the given simple force sources to the particles.
const std::vector< Cell * > & getBoundaryCells() const
Returns the pointers of the boundary cells.
LinkedCellsContainer(const std::array< double, 3 > &domain_size, double cutoff_radius, const std::array< BoundaryCondition, 6 > &boundary_types={BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW, BoundaryCondition::OUTFLOW}, int n=0)
Construct a new Linked Cells Particle Container object.
void applyTargettedForces(const std::vector< std::shared_ptr< TargettedForceSource >> &targetted_force_sources, double curr_simulation_time) override
Applies the given targetted force sources to the particles.
BoundarySide
Boundary side enum for labeling the sides of the domain.
std::vector< Cell * > top_boundary_cell_references
References to the boundary cells on the top (y = domain_num_cells[1]-1)
void updateCellsParticleReferences()
Updates the particle references in the cells. This is necessary after a reallocation of the internal ...
std::vector< Cell * > domain_cell_references
References to the domain cells.
std::vector< std::vector< Cell * > > iteration_orders
void addParticle(const Particle &p) override
Adds a particle to the container.
std::vector< Cell * > boundary_cell_references
References to the boundary cells.
std::vector< Cell * > front_halo_cell_references
References to the halo cells on the front (z = domain_num_cells[2])
void initIterationOrders()
Sets the iteration orders for parallel execution.
std::vector< Cell * > bottom_halo_cell_references
References to the halo cells on the bottom (y = -1)
std::array< double, 3 > cell_size
Cell size in each dimension.
std::vector< Particle >::iterator begin() override
The begin iterator for the internal data structure.
std::array< double, 3 > domain_size
Domain size in each dimension.
std::vector< Particle >::iterator end() override
The end iterator for the internal data structure.
int cellCoordToCellIndex(int cx, int cy, int cz) const
Maps the cell coordinates to the corresponding index in the internal cell vector.
Cell * particlePosToCell(const std::array< double, 3 > &pos)
Maps the particle position to the corresponding cell index in the internal cell vector.
std::vector< Cell * > left_halo_cell_references
References to the halo cells on the left (x = -1)
Particle & operator[](int i) override
Overload of the [] operator to access the particles in the container.
const std::vector< Particle > & getParticles() const override
Returns a vector of all particles in the container.
std::array< int, 3 > domain_num_cells
Number of cells in each dimension.
std::vector< Cell > cells
Internal data structure for the cells.
const std::array< double, 3 > & getCellSize() const
Returns the cell size.
double getCutoffRadius() const
Returns the cutoff radius.
void initCells()
Populates the cell vector and sets the cells types.
void prepareForceCalculation() override
Prepares everything for the force calculations (must be called before applySimpleForces and applyPair...
const std::vector< Cell > & getCells()
Returns the cells.
void initCellNeighbourReferences()
Sets the neighbour references for each cell in the cell vector.
std::vector< Cell * > right_boundary_cell_references
References to the boundary cells on the right (x = domain_num_cells[0]-1)
std::vector< Cell * > halo_cell_references
References to the halo cells.
const std::array< double, 3 > & getDomainSize() const
Returns the domain size.
BoundaryCondition
Boundary type enum for labeling the sides of the domain.
std::vector< Cell * > top_halo_cell_references
References to the halo cells on the top (y = domain_num_cells[1])
std::vector< Cell * > left_boundary_cell_references
References to the boundary cells on the left (x = 0)
std::vector< Cell * > back_halo_cell_references
References to the halo cells on the back (z = -1)
std::vector< Cell * > back_boundary_cell_references
References to the boundary cells on the back (z = 0)
size_t size() const override
Returns the number of particles in the container.
static std::string boundaryConditionToString(const BoundaryCondition &bc)
Returns a string description of a boundary condition.
double cutoff_radius
Cutoff radius for the force calculation.
void applyPairwiseForces(const std::vector< std::shared_ptr< PairwiseForceSource >> &force_sources) override
Applies the given force sources to the particles.
std::vector< Cell * > front_boundary_cell_references
References to the boundary cells on the front (z = domain_num_cells[2]-1)
std::array< BoundaryCondition, 6 > boundary_types
The boundary types for each side of the domain (order in array: left, right, bottom,...
std::vector< Particle > particles
Internal data structure for the particles.
const std::array< int, 3 > & getDomainNumCells() const
Returns the number of cells in each dimension.
Interface for particle containers.
Class to represent a particle.
Definition: Particle.h:26