Molecular Dynamics  v0.4
Project for the Practical hosted by the Scientific Computing Chair
LinkedCellsContainer.h
Go to the documentation of this file.
1 //
2 // Created by daniel on 21.05.24.
3 //
4 
5 #pragma once
6 #include <vector>
7 #ifdef _OPENMP
8 #include <omp.h>
9 #endif
10 
11 #include "../ParticleContainer.h"
14 #include "utils/enumsStructs.h"
16 
17 using namespace enumsStructs;
18 
23 private:
24  //Data structure
25 
30  std::vector<std::vector<Particle> > cells;
31 
36  size_t currentSize;
37 
38 
39  //Precomputed indices:
40  //Most of the indices are precomputed in the constructor and stored in memory to provide fast access in future iterations.
41  //We invest here additional memory space to avoid recomputing all indices in each iteration again.
42 
46  std::array<std::vector<int>, 6> haloCells;
50  std::array<std::vector<int>, 6> boundaries;
54  std::vector<std::vector<int> > domainCellIterationScheme;
55 
56 
57  //Scheduling for parallelization
58  std::vector<int> scheduleParallelNaive;
60 
61  //Locks for parallelization
62 #ifdef _OPENMP
63  std::vector<omp_lock_t> locks;
64 #endif
65 
66 
67  //All parameters used in this model:
68 
72  int nX;
76  int nY;
80  int nZ;
84  int baseY;
88  int baseZ;
93 
98 
102  double cellSizeX;
106  double cellSizeY;
110  double cellSizeZ;
114  double rCutOff;
118  bool twoD;
119 
123  std::array<double, 3> domainSize;
124 
126 
128 
130 
131 
132  //Helper methods for index calculation
133 
137  void calculateHaloCellIndices();
138 
142  void calculateBoundaryCellIndices();
143 
147  void calculateDomainCellsIterationScheme();
148 
149  void calculateScheduleParallelSophisticatedHelper(int xStart, int yStart, int zStart);
150 
151  void calculateScheduleNaive();
152 
153  void calculateScheduleParallelSophisticated();
154 
164  double calcDistanceFromBoundary(Particle &p, Side side);
165 
174  std::array<double, 3> calcGhostParticle(Particle &p, Side side);
175 
185  void teleportParticlesToOppositeSideHelper(Side sideStart, int dimension, int modus);
186 
194  void applyForceToOppositeCellsHelper(Side side, std::array<int, 3> cellToProcess);
195 
205  void applyForceToOppositeEdgeHelper(std::array<int, 3> cellToProcess, std::array<int, 3> offsetCell,
206  std::array<double, 3> offsetPosition, int dim);
207 
214  [[nodiscard]] bool isCellInDomain(std::array<int, 3> cell) const;
215 
222  [[nodiscard]] bool isParticleInDomain(const std::array<double, 3> &position) const;
223 
231  void applyForcesBetweenTwoCells(int cellTarget, int cellSource, std::array<double, 3> offsetSource);
232 
233 public:
245  LinkedCellsContainer(std::array<double, 3> domainSize, double rCutOff, BoundarySet boundarySet);
246 
254  int calcCellIndex(const std::array<double, 3> &position);
255 
264  void add(Particle &p) override;
265 
275  [[nodiscard]] int threeDToOneD(int x, int y, int z) const;
276 
277  int threeDToOneDDomain(int x, int y, int z) const;
278 
286  [[nodiscard]] std::array<int, 3> oneDToThreeD(int index) const;
287 
291  void updateCells();
292 
298  void clearHaloCells(Side side);
299 
305  void applyToEachParticle(const std::function<void(Particle &)> &function) override;
306 
312  void applyToEachParticleInDomain(const std::function<void(Particle &)> &function) override;
313 
324  void applyToAllUniquePairsInDomain(const std::function<void(Particle &, Particle &)> &function) override;
325 
332  void applyToAllUniquePairsInDomainOptimized(
333  const std::function<void(Particle &, Particle &, std::array<double, 3>, double)> &function);
334 
346  void applyToAllBoundaryParticles(const std::function<void(Particle &, std::array<double, 3> &)> &function,
347  Side boundary);
348 
354  [[nodiscard]] size_t size() const override;
355 
365  std::array<double, 3> fromLowToHigh(const std::array<double, 3> &position, int dimension);
366 
376  std::array<double, 3> fromHighToLow(const std::array<double, 3> &position, int dimension);
377 
386  void teleportParticlesToOppositeSide(Side sideStart);
387 
388 
395  void applyForcesFromOppositeSide(Side side);
396 
397 //The following methods are only needed for parallelization
398 
399 #ifdef _OPENMP
405  void applyToAllUniquePairsInDomainParallelReduction(const std::function<void(Particle &, Particle &, int)> &function);
406 
413  void applyToEachParticleInDomainParallel(const std::function<void(Particle &)> &function);
414 
421  void applyToAllUniquePairsInDomainParallelHelper(const std::function<void(Particle &, Particle &)> &function, std::vector<int>& scheduling);
422 
428  void applyToAllUniquePairsInDomainParallelSophisticated(
429  const std::function<void(Particle &, Particle &)> &function);
430 
436  void applyToAllUniquePairsInDomainParallelNaive(const std::function<void(Particle &, Particle &)> &function);
437 #endif
438 
439 
440  //Getter and setters. Especially the setters should only by used for testing purposes.
441 
442  std::vector<std::vector<Particle> > &getCells() {
443  return cells;
444  }
445 
446  std::array<std::vector<int>, 6> &getHaloCells() {
447  return haloCells;
448  }
449 
450  std::array<std::vector<int>, 6> &getBoundaries() {
451  return boundaries;
452  }
453 
454  std::vector<std::vector<int> > &getDomainCellIterationScheme() {
455  return domainCellIterationScheme;
456  }
457 
458  [[nodiscard]] int getNX() const {
459  return nX;
460  }
461 
462  [[nodiscard]] int getNY() const {
463  return nY;
464  }
465 
466  [[nodiscard]] int getNZ() const {
467  return nZ;
468  }
469 
470  [[nodiscard]] double getCellSizeX() const {
471  return cellSizeX;
472  }
473 
474  [[nodiscard]] double getCellSizeY() const {
475  return cellSizeY;
476  }
477 
478  [[nodiscard]] double getCellSizeZ() const {
479  return cellSizeZ;
480  }
481 
482  [[nodiscard]] double getRCutOff() const {
483  return rCutOff;
484  }
485 
486  [[nodiscard]] bool isTwoD() const {
487  return twoD;
488  }
489 
490  [[nodiscard]] std::array<double, 3> getDomainSize() const {
491  return domainSize;
492  }
493 };
Definition: LennardJonesForce.h:8
Container to store the particles for simulation using the linked cells algorithm.
Definition: LinkedCellsContainer.h:22
int getNZ() const
Definition: LinkedCellsContainer.h:466
std::vector< std::vector< int > > & getDomainCellIterationScheme()
Definition: LinkedCellsContainer.h:454
int baseYDomain
Definition: LinkedCellsContainer.h:92
bool twoD
Definition: LinkedCellsContainer.h:118
std::array< std::vector< int >, 6 > boundaries
Definition: LinkedCellsContainer.h:50
int getNY() const
Definition: LinkedCellsContainer.h:462
int nZ
Definition: LinkedCellsContainer.h:80
bool isTwoD() const
Definition: LinkedCellsContainer.h:486
std::array< std::vector< int >, 6 > & getHaloCells()
Definition: LinkedCellsContainer.h:446
std::vector< std::vector< Particle > > cells
Definition: LinkedCellsContainer.h:30
std::array< std::vector< int >, 6 > haloCells
Definition: LinkedCellsContainer.h:46
double getCellSizeX() const
Definition: LinkedCellsContainer.h:470
std::array< double, 3 > getDomainSize() const
Definition: LinkedCellsContainer.h:490
double getCellSizeZ() const
Definition: LinkedCellsContainer.h:478
double getCellSizeY() const
Definition: LinkedCellsContainer.h:474
BoundarySet boundariesSet
Definition: LinkedCellsContainer.h:129
int baseZDomain
Definition: LinkedCellsContainer.h:97
double cellSizeZ
Definition: LinkedCellsContainer.h:110
outputWriter::VTKWriter vtk_writer
Definition: LinkedCellsContainer.h:125
std::vector< int > scheduleParallelNaive
Definition: LinkedCellsContainer.h:58
std::array< double, 3 > domainSize
Definition: LinkedCellsContainer.h:123
std::vector< std::vector< Particle > > & getCells()
Definition: LinkedCellsContainer.h:442
size_t currentSize
Definition: LinkedCellsContainer.h:36
double rCutOff
Definition: LinkedCellsContainer.h:114
int getNX() const
Definition: LinkedCellsContainer.h:458
std::vector< int > scheduleParallelSophisticated
Definition: LinkedCellsContainer.h:59
std::vector< std::vector< int > > domainCellIterationScheme
Definition: LinkedCellsContainer.h:54
int nX
Definition: LinkedCellsContainer.h:72
int nY
Definition: LinkedCellsContainer.h:76
int baseY
Definition: LinkedCellsContainer.h:84
std::array< std::vector< int >, 6 > & getBoundaries()
Definition: LinkedCellsContainer.h:450
int baseZ
Definition: LinkedCellsContainer.h:88
double cellSizeY
Definition: LinkedCellsContainer.h:106
double getRCutOff() const
Definition: LinkedCellsContainer.h:482
LennardJonesForce lJF
Definition: LinkedCellsContainer.h:127
double cellSizeX
Definition: LinkedCellsContainer.h:102
Definition: ParticleContainer.h:11
Definition: Particle.h:16
Class corresponding to the boundaries schema type.
Definition: ConfigurationFile.h:11189
Definition: VTKWriter.h:23
Class corresponding to the rCutOff schema type.
Definition: ConfigurationFile.h:6444
Definition: enumsStructs.h:9
Side
Definition: enumsStructs.h:13
Definition: enumsStructs.h:38