File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22#include " ../struct/particle.h"
33#include < vector>
44
5+ void bhForce (Octree* node, Particle& p, real theta, real dt);
6+
57struct Octree {
68 real cx, cy, cz; // center of mass
79 real m; // total mass
@@ -83,4 +85,31 @@ struct Octree {
8385 cz /= m;
8486 }
8587 }
86- };
88+ };
89+
90+ inline void bhForce (Octree* node, Particle& p, real theta, real dt)
91+ {
92+ if (!node || node->m == 0 ) return ;
93+
94+ real dx = node->cx - p.x ;
95+ real dy = node->cy - p.y ;
96+ real dz = node->cz - p.z ;
97+
98+ real dist = sqrt (dx*dx + dy*dy + dz*dz) + 1e-6 ;
99+
100+ // Barnes–Hut acceptance criterion
101+ if (node->leaf || (node->size / dist) < theta) {
102+ real inv = 1.0 / (dist * dist * dist);
103+ real f = node->m * inv * dt;
104+ p.vx += dx * f;
105+ p.vy += dy * f;
106+ p.vz += dz * f;
107+ return ;
108+ }
109+
110+ // Otherwise recurse into children
111+ for (int i = 0 ; i < 8 ; i++) {
112+ if (node->child [i])
113+ bhForce (node->child [i], p, theta, dt);
114+ }
115+ }
You can’t perform that action at this time.
0 commit comments