HGrid.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef HGRID_H
27 #define HGRID_H
28 
29 #include <vector>
30 #include "Math/ExtendedMath.h"
31 #include "Particles/HGridCell.h"
32 
33 class BaseParticle;
34 
42 class HGrid
43 {
44 public:
48  HGrid();
49 
53  HGrid(unsigned int num_buckets, double cellOverSizeRatio, std::vector<double>& cellSizes);
54 
58  ~HGrid();
59 
64 
76  inline unsigned int computeHashBucketIndex(int x, int y, int z, unsigned int l) const
77  {
78  static const unsigned int h1 = 0x8da6b343u; // Large multiplicative constants;
79  static const unsigned int h2 = 0xd8163841u; // here arbitrarily chosen primes
80  static const unsigned int h3 = 0xcb1ab31fu;
81  static const unsigned int h4 = 0x165667b1u;
82 
83  return (h1 * x + h2 * y + h3 * z + h4 * l) % numberOfBuckets_;
84  }
85 
89  inline unsigned int computeHashBucketIndex(HGridCell hGridCell) const
90  {
91  return computeHashBucketIndex(hGridCell.getHGridX(),
92  hGridCell.getHGridY(),
93  hGridCell.getHGridZ(),
94  hGridCell.getHGridLevel());
95  }
96 
100  unsigned int computeHashBucketIndex(int x, int y, unsigned int l) const;
101 
105  void clearBucketIsChecked();
106 
111 
118  { firstBaseParticleInBucket_[i] = p; }
119 
124  void setBucketIsChecked(unsigned int i)
125  { bucketIsChecked_[i] = true; }
126 
132  bool getBucketIsChecked(unsigned int i) const
133  { return bucketIsChecked_[i]; }
134 
140  { return cellOverSizeRatio_; }
141 
147  double getCellSize(unsigned int i) const
148  { return cellSizes_[i]; }
149 
154  const std::vector<double>& getCellSizes() const
155  { return cellSizes_; }
156 
162  const BaseParticle* getFirstBaseParticleInBucket(unsigned int i) const
163  { return firstBaseParticleInBucket_[i]; }
164 
171  { return firstBaseParticleInBucket_[i]; }
172 
178  double getInvCellSize(unsigned int i) const
179  { return invCellSizes_[i]; }
180 
185  const std::vector<double>& getInvCellSizes() const
186  { return invCellSizes_; }
187 
192  bool getNeedsRebuilding() const
193  { return needsRebuilding_; }
194 
199  unsigned int getNumberOfBuckets() const
200  { return numberOfBuckets_; }
201 
206  unsigned long getNumberOfLevels() const
207  { return cellSizes_.size(); }
208 
214  { return occupiedLevelsMask_; }
215 
222  void info() const;
223 
224 private:
229 
235  unsigned int numberOfBuckets_;
236 
241 
248 
255  std::vector<double> cellSizes_;
256 
260  std::vector<double> invCellSizes_;
261 
268  std::vector<BaseParticle*> firstBaseParticleInBucket_;
269 
273  std::vector<bool> bucketIsChecked_;
274 };
275 
276 #endif
Definition: BaseParticle.h:54
Definition: HGridCell.h:33
unsigned int getHGridLevel() const
Definition: HGridCell.h:86
int getHGridX() const
Definition: HGridCell.h:56
int getHGridZ() const
Definition: HGridCell.h:76
int getHGridY() const
Definition: HGridCell.h:66
In the HGrid class, here all information about the HGrid is stored.
Definition: HGrid.h:43
bool needsRebuilding_
Flag sets if the HGrid needs to be rebuilt.
Definition: HGrid.h:228
~HGrid()
Destructor.
Definition: HGrid.cc:71
std::vector< bool > bucketIsChecked_
BucketIsChecked stores if hash bucket b is checked already; initially all false.
Definition: HGrid.h:273
std::vector< double > invCellSizes_
The inverse sizes of the cells in the different grids, where the inverse is defined as 1/cellSizes_.
Definition: HGrid.h:260
bool getBucketIsChecked(unsigned int i) const
Gets whether or not the bucket with index i is checked.
Definition: HGrid.h:132
Mdouble cellOverSizeRatio_
The maximum ratio between the size of the cell and the size of a particle it contains.
Definition: HGrid.h:240
void setFirstBaseParticleInBucket(unsigned int i, BaseParticle *p)
Sets the first particle in bucket i to be the given BaseParticle.
Definition: HGrid.h:117
unsigned int computeHashBucketIndex(int x, int y, int z, unsigned int l) const
Computes hash bucket index in range [0, NUM_BUCKETS-1] for a 3D domain.
Definition: HGrid.h:76
unsigned int numberOfBuckets_
The number of buckets in the current HGrid.
Definition: HGrid.h:235
unsigned long getNumberOfLevels() const
Gets the number of levels of this HGrid.
Definition: HGrid.h:206
const std::vector< double > & getCellSizes() const
Gets the sizes of the cells at all levels as a vector.
Definition: HGrid.h:154
bool getNeedsRebuilding() const
Gets whether or not the grid needs to be rebuilt before something else is done with it.
Definition: HGrid.h:192
std::vector< double > cellSizes_
The sizes of the cells in the different grids.
Definition: HGrid.h:255
double getInvCellSize(unsigned int i) const
Gets 1/cellSize for the cells on level i.
Definition: HGrid.h:178
void info() const
Displays the member variables of the hGrid object. This function is intended for debugging the hGrid,...
Definition: HGrid.cc:151
void setBucketIsChecked(unsigned int i)
Sets that the bucket with the given index is checked to true.
Definition: HGrid.h:124
const BaseParticle * getFirstBaseParticleInBucket(unsigned int i) const
Gets the first BaseParticle in the given bucket, const version.
Definition: HGrid.h:162
unsigned int computeHashBucketIndex(HGridCell hGridCell) const
Computes hash bucket index in range [0, NUM_BUCKETS-1] for a 3D domain.
Definition: HGrid.h:89
std::vector< BaseParticle * > firstBaseParticleInBucket_
Stores a pointer to first element in hash bucket b.
Definition: HGrid.h:268
const std::vector< double > & getInvCellSizes() const
Gets all the inverse cell sizes (1/cellSize) for all levels as a vector.
Definition: HGrid.h:185
HGrid()
Default constructor, it sets the parameters to some sensible defaults.
Definition: HGrid.cc:30
BaseParticle * getFirstBaseParticleInBucket(unsigned int i)
Gets the first BaseParticle in the given bucket.
Definition: HGrid.h:170
void insertParticleToHgrid(BaseParticle *obj)
Inserts the given BaseParticle in to the HGrid.
Definition: HGrid.cc:89
void clearFirstBaseParticleInBucket()
For all buckets, it removes the pointer to the first BaseParticle in it, practically emptying the buc...
Definition: HGrid.cc:145
void clearBucketIsChecked()
Sets all buckets to not-checked.
Definition: HGrid.cc:140
int occupiedLevelsMask_
Marks if there are particles at certain levels.
Definition: HGrid.h:247
int getOccupiedLevelsMask() const
Gets the integer that represents which levels are occupied.
Definition: HGrid.h:213
unsigned int getNumberOfBuckets() const
Gets the number of buckets of this HGrid.
Definition: HGrid.h:199
double getCellSize(unsigned int i) const
Gets the size of the cells at the given level.
Definition: HGrid.h:147
Mdouble getCellOverSizeRatio() const
Gets the maximum ratio of the cell to a particle it contains.
Definition: HGrid.h:139
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51