Skip to content

Commit b46e681

Browse files
Update octree.h
1 parent dec80b0 commit b46e681

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

src/gravity/octree.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "../struct/particle.h"
33
#include <vector>
44

5+
void bhForce(Octree* node, Particle& p, real theta, real dt);
6+
57
struct 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+
}

0 commit comments

Comments
 (0)