Version: SMASH-3.4
grid.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2015,2017-2022
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_GRID_H_
11 #define SRC_INCLUDE_SMASH_GRID_H_
12 
13 #include <array>
14 #include <cmath>
15 #include <functional>
16 #include <utility>
17 #include <vector>
18 
19 #include "forwarddeclarations.h"
20 #include "particles.h"
21 
22 namespace smash {
23 
24 /// Identifies the mode of the Grid.
25 enum class GridOptions : char {
26  /// Without ghost cells
27  Normal = 0,
28  /// With ghost cells for periodic boundaries
30 };
31 
32 /// Indentifies the strategy of determining the cell size.
33 enum class CellSizeStrategy : char {
34  /// Look for optimal cell size.
35  Optimal,
36 
37  /**
38  * Make cells as large as possible.
39  *
40  * This means a single cell for normal boundary conditions and 8 cells
41  * for periodic boundary conditions.
42  */
43  Largest
44 };
45 
46 /**
47  * Identifies whether the number of cells should be limited. For the geometric
48  * criterion it makes sense to not have less than 1 particle in each cell, since
49  * the grid cell search is an optimization and the cells can be always made
50  * larger. For the stochastic collision criterion, the cell size is an
51  * important calculation parameter, which should be kept constant.
52  * The number of cells therefore cannot be limited as the medium grows even
53  * though this might be inefficient for large systems.
54  */
55 enum class CellNumberLimitation : char {
56  /// No cell number limitation.
57  None,
58 
59  /// Limit the number of cells to the number of particles
61 };
62 
63 /**
64  * Base class for Grid to host common functions that do not depend on the
65  * GridOptions parameter.
66  */
67 class GridBase {
68  public:
69  /// A type to store the sizes
70  typedef int SizeType;
71 
72  protected:
73  /**
74  * \return the minimum x,y,z coordinates and the largest dx,dy,dz distances of
75  * the particles in \p particles.
76  *
77  * \param[in] particles Particles in the system
78  */
79  static std::pair<std::array<double, 3>, std::array<double, 3>>
80  find_min_and_length(const Particles &particles);
81 };
82 
83 /**
84  * Abstracts a list of cells that partition the particles in the experiment into
85  * regions of space that can interact / cannot interact.
86  *
87  * This class is used to construct a helper data structure to reduce the
88  * combinatorics of finding particle pairs that could interact (scatter). It
89  * takes a list of ParticleData objects and sorts them in such a way that it is
90  * easy to look only at lists of particles that have a chance of interacting.
91  *
92  * \tparam Options This policy parameter determines whether ghost cells are
93  * created to support periodic boundaries, or not.
94  */
95 template <GridOptions Options = GridOptions::Normal>
96 class Grid : public GridBase {
97  public:
98  /**
99  * Constructs a grid from the given particle list \p particles. It
100  * automatically determines the necessary size for the grid from the positions
101  * of the particles.
102  *
103  * \param[in] particles The particles to place onto the grid.
104  * \param[in] min_cell_length The minimal length a cell must have.
105  * \param[in] timestep_duration Duration of the timestep. It is necessary for
106  * formation times treatment: if particle is fully or partially formed before
107  * the end of the timestep, it has to be on the grid.
108  * \param[in] limit Limitation of cell number
109  * \param[in] include_unformed_particles include unformed particles from
110  the grid (worsens runtime)
111  * \param[in] strategy The strategy for determining the cell size
112  */
113  Grid(const Particles &particles, double min_cell_length,
114  double timestep_duration, CellNumberLimitation limit,
115  const bool include_unformed_particles = false,
117  : Grid{find_min_and_length(particles),
118  std::move(particles),
119  min_cell_length,
120  timestep_duration,
121  limit,
122  include_unformed_particles,
123  strategy} {}
124 
125  /**
126  * Constructs a grid with the given minimum grid coordinates and grid length.
127  * If you need periodic boundaries you have to use this constructor to set the
128  * correct length to use for wrapping particles around the borders.
129  *
130  * \param[in] min_and_length A pair consisting of the three min coordinates
131  * and the three lengths.
132  * \param[in] particles The particles to place onto the grid.
133  * \param[in] min_cell_length The minimal length a cell must have.
134  * \param[in] timestep_duration Duration of the timestep in fm.
135  * \param[in] limit Limitation of cell number.
136  * \param[in] include_unformed_particles include unformed particles from
137  the grid (worsens runtime).
138  * \param[in] strategy The strategy for determining the cell size.
139  * \throws runtime_error if your box length is smaller than the grid length.
140  */
141  Grid(const std::pair<std::array<double, 3>, std::array<double, 3>>
142  &min_and_length,
143  const Particles &particles, double min_cell_length,
144  double timestep_duration, CellNumberLimitation limit,
145  const bool include_unformed_particles = false,
147 
148  /**
149  * Iterates over all cells in the grid and calls the callback arguments with
150  * a search cell and 0 to 13 neighbor cells.
151  *
152  * The neighbor cells are constructed like this:
153  * - one cell at x+1
154  * - three cells (x-1,x,x+1) at y+1
155  * - nine cells (x-1, y-1)...(x+1, y+1) at z+1
156  *
157  * \param[in] search_cell_callback A callable called for/with every non-empty
158  * cell in the grid.
159  * \param[in] neighbor_cell_callback A callable called for/with every
160  * non-empty cell and adjacent cell combination.
161  * For a periodic grid, the first argument will
162  * be adjusted to wrap around the grid.
163  */
165  const std::function<void(const ParticleList &)> &search_cell_callback,
166  const std::function<void(const ParticleList &, const ParticleList &)>
167  &neighbor_cell_callback) const;
168 
169  /**
170  * \return the volume of a single grid cell
171  */
172  double cell_volume() const { return cell_volume_; }
173 
174  private:
175  /**
176  * \return the one-dimensional cell-index from the 3-dim index \p x, \p y, \p
177  * z.
178  */
180 
181  /**
182  * \return the one-dimensional cell-index from the 3-dim index \p idx.
183  * This is a convenience overload for the above function.
184  */
185  SizeType make_index(std::array<SizeType, 3> idx) const {
186  return make_index(idx[0], idx[1], idx[2]);
187  }
188 
189  /// The 3 lengths of the complete grid. Used for periodic boundary wrapping.
190  const std::array<double, 3> length_;
191 
192  /// The volume of a single cell.
193  double cell_volume_;
194 
195  /// The number of cells in x, y, and z direction.
196  std::array<int, 3> number_of_cells_;
197 
198  /// The cell storage.
199  std::vector<ParticleList> cells_;
200 };
201 
202 } // namespace smash
203 
204 #endif // SRC_INCLUDE_SMASH_GRID_H_
Base class for Grid to host common functions that do not depend on the GridOptions parameter.
Definition: grid.h:67
int SizeType
A type to store the sizes.
Definition: grid.h:70
static std::pair< std::array< double, 3 >, std::array< double, 3 > > find_min_and_length(const Particles &particles)
Definition: grid.cc:88
Abstracts a list of cells that partition the particles in the experiment into regions of space that c...
Definition: grid.h:96
const std::array< double, 3 > length_
The 3 lengths of the complete grid. Used for periodic boundary wrapping.
Definition: grid.h:190
std::array< int, 3 > number_of_cells_
The number of cells in x, y, and z direction.
Definition: grid.h:196
double cell_volume() const
Definition: grid.h:172
double cell_volume_
The volume of a single cell.
Definition: grid.h:193
std::vector< ParticleList > cells_
The cell storage.
Definition: grid.h:199
SizeType make_index(std::array< SizeType, 3 > idx) const
Definition: grid.h:185
Grid(const Particles &particles, double min_cell_length, double timestep_duration, CellNumberLimitation limit, const bool include_unformed_particles=false, CellSizeStrategy strategy=CellSizeStrategy::Optimal)
Constructs a grid from the given particle list particles.
Definition: grid.h:113
void iterate_cells(const std::function< void(const ParticleList &)> &search_cell_callback, const std::function< void(const ParticleList &, const ParticleList &)> &neighbor_cell_callback) const
Iterates over all cells in the grid and calls the callback arguments with a search cell and 0 to 13 n...
SizeType make_index(SizeType x, SizeType y, SizeType z) const
Definition: grid.cc:293
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
Definition: action.h:24
CellNumberLimitation
Identifies whether the number of cells should be limited.
Definition: grid.h:55
@ ParticleNumber
Limit the number of cells to the number of particles.
@ None
No cell number limitation.
GridOptions
Identifies the mode of the Grid.
Definition: grid.h:25
@ Normal
Without ghost cells.
@ PeriodicBoundaries
With ghost cells for periodic boundaries.
CellSizeStrategy
Indentifies the strategy of determining the cell size.
Definition: grid.h:33
@ Optimal
Look for optimal cell size.
@ Largest
Make cells as large as possible.