SPH
BarnesHut.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/ForwardDecl.h"
9 #include "gravity/IGravity.h"
11 #include "objects/finders/KdTree.h"
13 #include "physics/Constants.h"
15 #include <atomic>
16 
18 
19 enum class MultipoleOrder;
20 
21 struct BarnesHutNode : public KdNode {
24 
27 
31 
33  : KdNode(type) {
34 #ifdef SPH_DEBUG
35  com = Vector(NAN);
36  r_open = NAN;
37 #endif
38  }
39 };
40 
41 
43 class BarnesHut : public IGravity {
44 protected:
47 
50 
53 
56 
59 
62 
67 
73 
74 public:
81  BarnesHut(const Float theta,
82  const MultipoleOrder order,
83  const Size leafSize = 25,
84  const Size maxDepth = 50,
86 
94  BarnesHut(const Float theta,
95  const MultipoleOrder order,
97  const Size leafSize = 25,
98  const Size maxDepth = 50,
100 
102  virtual void build(IScheduler& pool, const Storage& storage) override;
103 
104  virtual void evalAll(IScheduler& pool, ArrayView<Vector> dv, Statistics& stats) const override;
105 
106  virtual Vector eval(const Vector& r0) const override;
107 
108  virtual Float evalEnergy(IScheduler& scheduler, Statistics& stats) const override;
109 
110  virtual RawPtr<const IBasicFinder> getFinder() const override;
111 
116 
117 protected:
119  Vector evalImpl(const Vector& r0, const Size idx) const;
120 
122  class NodeTask;
123 
125  struct TreeWalkState {
131 
134 
137 
139  Size depth = 0;
140 
142  TreeWalkState clone() const;
143  };
144 
145  struct TreeWalkResult {
147  std::atomic_int approximatedNodes = { 0 };
148 
150  std::atomic_int exactNodes = { 0 };
151  };
152 
163  void evalNode(IScheduler& scheduler,
165  const Size nodeIdx,
166  TreeWalkState data,
167  TreeWalkResult& result) const;
168 
169  void evalParticleList(const LeafNode<BarnesHutNode>& leaf,
170  ArrayView<Size> particleList,
171  ArrayView<Vector> dv) const;
172 
173  void evalNodeList(const LeafNode<BarnesHutNode>& leaf,
174  ArrayView<Size> nodeList,
175  ArrayView<Vector> dv) const;
176 
177  Vector evalExact(const LeafNode<BarnesHutNode>& node, const Vector& r0, const Size idx) const;
178 
179  void buildLeaf(BarnesHutNode& node);
180 
181  void buildInner(BarnesHutNode& node, BarnesHutNode& left, BarnesHutNode& right);
182 };
183 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Definitions of physical constaints (in SI units).
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
Smoothing kernels for including gravity into SPH.
Base class for solvers of gravity.
K-d tree for efficient search of neighbouring particles.
Doubly-linked list.
MultipoleOrder
Definition: Moments.h:309
#define NAMESPACE_SPH_END
Definition: Object.h:12
BasicVector< Float > Vector
Definition: Vector.h:539
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
Multipole approximation of distance particle.
Definition: BarnesHut.h:43
virtual void evalAll(IScheduler &pool, ArrayView< Vector > dv, Statistics &stats) const override
Evaluates the gravitational acceleration concurrently.
Definition: BarnesHut.cpp:83
void evalNode(IScheduler &scheduler, ArrayView< Vector > dv, const Size nodeIdx, TreeWalkState data, TreeWalkResult &result) const
Performs a recursive treewalk evaluating gravity for all particles.
Definition: BarnesHut.cpp:185
ArrayView< const Vector > r
View of a position buffer in the storage.
Definition: BarnesHut.h:46
MultipoleOrder order
Order of multipole approximation.
Definition: BarnesHut.h:61
GravityLutKernel kernel
Kernel used to evaluate gravity of close particles.
Definition: BarnesHut.h:55
BarnesHut(const Float theta, const MultipoleOrder order, const Size leafSize=25, const Size maxDepth=50, const Float gravityConstant=Constants::gravity)
Constructs the Barnes-Hut gravity assuming point-like particles (with zero radius).
Definition: BarnesHut.cpp:17
virtual Vector eval(const Vector &r0) const override
Evaluates the gravitational acceleration at given point.
Definition: BarnesHut.cpp:99
MultipoleExpansion< 3 > getMoments() const
Returns the multipole moments computed from root node.
Definition: BarnesHut.cpp:465
void buildInner(BarnesHutNode &node, BarnesHutNode &left, BarnesHutNode &right)
Definition: BarnesHut.cpp:413
Vector evalExact(const LeafNode< BarnesHutNode > &node, const Vector &r0, const Size idx) const
Definition: BarnesHut.cpp:339
Size maxDepth
Maximum depth at which the nodes evaluation is parallelized.
Definition: BarnesHut.h:66
Vector evalImpl(const Vector &r0, const Size idx) const
Evaluates the gravity at a single point in space.
Definition: BarnesHut.cpp:107
void evalParticleList(const LeafNode< BarnesHutNode > &leaf, ArrayView< Size > particleList, ArrayView< Vector > dv) const
Definition: BarnesHut.cpp:288
virtual RawPtr< const IBasicFinder > getFinder() const override
Optionally returns a finder used by the gravity implementation.
Definition: BarnesHut.cpp:470
Array< Float > m
Particle masses multiplied by gravitational constant.
Definition: BarnesHut.h:49
void evalNodeList(const LeafNode< BarnesHutNode > &leaf, ArrayView< Size > nodeList, ArrayView< Vector > dv) const
Definition: BarnesHut.cpp:325
void buildLeaf(BarnesHutNode &node)
Definition: BarnesHut.cpp:351
KdTree< BarnesHutNode > kdTree
K-d tree storing gravitational moments.
Definition: BarnesHut.h:52
virtual Float evalEnergy(IScheduler &scheduler, Statistics &stats) const override
Computes the total potential energy of the particles.
Definition: BarnesHut.cpp:103
Float gravityConstant
Gravitational constant in the selected unit system.
Definition: BarnesHut.h:72
Float thetaInv
Inverted value of the opening angle for multipole approximation (in radians)
Definition: BarnesHut.h:58
virtual void build(IScheduler &pool, const Storage &storage) override
Masses of particles must be strictly positive, otherwise center of mass would be undefined.
Definition: BarnesHut.cpp:49
Gravitational kernel approximated by LUT for close particles.
Definition: GravityKernel.h:35
Interface for computing gravitational interactions of particles.
Definition: IGravity.h:14
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
constexpr Float gravity
Gravitational constant (CODATA 2014)
Definition: Constants.h:29
MultipoleExpansion< 3 > moments
Gravitational moments with a respect to the center of mass, using expansion to octupole order.
Definition: BarnesHut.h:26
Vector com
Center of mass of contained particles.
Definition: BarnesHut.h:23
BarnesHutNode(const Type &type)
Definition: BarnesHut.h:32
Float r_open
Definition: BarnesHut.h:30
std::atomic_int approximatedNodes
Number of nodes approximated using multipole expansion.
Definition: BarnesHut.h:147
std::atomic_int exactNodes
Number of opened leafs where exact (pair-wise) solution has been used.
Definition: BarnesHut.h:150
Data passed into each node during treewalk.
Definition: BarnesHut.h:125
TreeWalkState clone() const
Clones all lists in the state object.
Definition: BarnesHut.cpp:175
Array< Size > nodeList
Indices of nodes that shall be evaluated using multipole approximation.
Definition: BarnesHut.h:136
Size depth
Current depth in the tree; root node has depth equal to zero.
Definition: BarnesHut.h:139
List< Size > checkList
Definition: BarnesHut.h:130
Array< Size > particleList
Indices of nodes where the exact (pair-wise) gravity solution have to be used.
Definition: BarnesHut.h:133
Base class for nodes of K-d tree.
Definition: KdTree.h:24
Type
Here X, Y, Z must be 0, 1, 2.
Definition: KdTree.h:26
Type type
Definition: KdTree.h:27
Leaf (bucket) node of K-d tree.
Definition: KdTree.h:61