SPH
HashMapFinder.cpp
Go to the documentation of this file.
3 #include "sph/kernel/Kernel.h"
4 #include "system/Factory.h"
5 
7 
8 HashMapFinder::Cell::Cell() = default;
9 
10 HashMapFinder::Cell::~Cell() = default;
11 
12 HashMapFinder::HashMapFinder(const RunSettings& settings, const Float cellMult)
13  : cellMult(cellMult) {
14  kernelRadius = Factory::getKernel<3>(settings).radius();
15 }
16 
18 
20  map.clear();
21  cellSize = 0._f;
22  for (Size i = 0; i < points.size(); ++i) {
23  cellSize = max(cellSize, kernelRadius * points[i][H]);
24  }
25  cellSize *= cellMult;
26 
27  for (Size i = 0; i < points.size(); ++i) {
28  const Indices idxs = floor(points[i] / cellSize);
29  Cell& cell = map[idxs];
30  cell.points.push(i);
31  cell.box.extend(points[i]);
32  }
33 }
34 
35 template <bool FindAll>
37  const Size index,
38  const Float radius,
39  Array<NeighbourRecord>& neighs) const {
40  SPH_ASSERT(neighs.empty());
41  const Indices idxs0 = floor(pos / cellSize);
42  Sphere sphere(pos, radius);
43  for (int x = -1; x <= 1; ++x) {
44  for (int y = -1; y <= 1; ++y) {
45  for (int z = -1; z <= 1; ++z) {
46  const Indices idxs = idxs0 + Indices(x, y, z);
47  const auto iter = map.find(idxs);
48  if (iter != map.end()) {
49  if (!sphere.overlaps(iter->second.box)) {
50  continue;
51  }
52  const Array<Size>& cell = iter->second.points;
53  for (Size i : cell) {
54  const Float distSqr = getSqrLength(values[i] - pos);
55  if (distSqr < sqr(radius) && (FindAll || rank[i] < rank[index])) {
56  neighs.emplaceBack(NeighbourRecord{ i, distSqr });
57  }
58  }
59  }
60  }
61  }
62  }
63  return neighs.size();
64 }
65 
66 template Size HashMapFinder::find<true>(const Vector& pos,
67  const Size index,
68  const Float radius,
69  Array<NeighbourRecord>& neighs) const;
70 
71 template Size HashMapFinder::find<false>(const Vector& pos,
72  const Size index,
73  const Float radius,
74  Array<NeighbourRecord>& neighs) const;
75 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
const float radius
Definition: CurveDialog.cpp:18
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Finding neighbors using hash map.
SPH kernels.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE auto floor(const T &f)
Definition: MathUtils.h:346
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a three-dimensional sphere.
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
@ H
Definition: Vector.h:25
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Definition: Array.h:332
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
INLINE void extend(const Vector &v)
Enlarges the box to contain the vector.
Definition: Box.h:49
virtual void buildImpl(IScheduler &scheduler, ArrayView< const Vector > points) override
Builds finder from set of vectors.
HashMapFinder(const RunSettings &settings, const Float cellMult=1._f)
Size find(const Vector &pos, const Size index, const Float radius, Array< NeighbourRecord > &neighs) const
ArrayView< const Vector > values
View of the source datapoints, updated every time (re)build is called.
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Order rank
Ranks of particles according to their smoothing lengths.
Helper object for storing three (possibly four) int or bool values.
Definition: Indices.h:16
Definition: Sphere.h:18
INLINE bool overlaps(const Box &box) const
Checks whether the sphere partially or fully overlaps given box.
Definition: Sphere.h:73
Creating code components based on values from settings.
Array< Size > points
Definition: HashMapFinder.h:19
Holds information about a neighbour particles.