-
Notifications
You must be signed in to change notification settings - Fork 1
/
barnes-hut.h
117 lines (100 loc) · 3 KB
/
barnes-hut.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Copyright (C) 2018 Greenweaves Software Limited
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _BARNES_HUT_H
#define _BARNES_HUT_H
#include <vector>
#include "particle.h"
#include "treecode.h"
#include "utils.h"
/**
* Calculate acceleration for all particles
*/
void get_acceleration(std::vector<Particle*>&,const double theta,const double G,const double softening_length);
/**
* Construct oct-tree from particles
*
* particles
*/
Node * create_tree(std::vector<Particle*>& particles);
/**
* Calculate acceleration for one specific particle
*/
void get_acceleration(int i, std::vector<Particle*>& particles,Node * root,const double theta,const double G,const double a);
/**
* This class is used to calculate the acceleration of one particle
* using the Barnes Hut algorithm
*
*/
class BarnesHutVisitor : public Node::Visitor{
public:
/**
* Initialize BarnesHutVisitor for a specific particle
*
* index Keep track of particle index so we don't calculate acceleration of particle caused by itself!
* me Particle being processed
* theta Angle for Barnes G=Hut cutoff
* G Gravitational constant
* a Softening length
*/
BarnesHutVisitor(const int index,Particle* me,const double theta, const double G,const double a);
/**
* Used to accumulate accelerations for each node
*/
virtual Node::Visitor::Status visit(Node * node);
/**
* Used at the end of calculation to store accelerations back into particle
*/
void store_accelerations();
private:
/**
* Keep track of particle index so we don't calculate acceleration of particle caused by itself!
*/
const int _index;
/**
* Used to add in the contribution to the acceleration from one Node
*/
void _accumulate_acceleration(double m,double x,double y,double z,double dsq);
/**
* The particle whose acceleration is being calculated
*/
Particle * _me;
/**
* Store squared theta to simplify comparisons
*/
const double _theta_squared;
/**
* Position of the particle whose acceleration is being calculated
*/
double _x;
double _y;
double _z;
/**
* Gravitational constant
*/
const double _G;
/**
* Softening length
*/
const double _a;
/**
* We accumulate the acceleration here
*/
double _acc_x;
double _acc_y;
double _acc_z;
};
#endif