@@ -90,32 +90,33 @@ struct Octree {
9090 }
9191};
9292
93- // =========================
94- // Barnes–Hut force function
95- // =========================
96- inline void bhForce (Octree* node, Particle& p, real theta, real dt)
93+ inline void bhAccel (Octree* node, const Particle& p, real theta,
94+ real& ax, real& ay, real& az)
9795{
9896 if (!node || node->m == 0 ) return ;
9997
10098 real dx = node->cx - p.x ;
10199 real dy = node->cy - p.y ;
102100 real dz = node->cz - p.z ;
103101
104- real dist = sqrt (dx*dx + dy*dy + dz*dz) + 1e-6 ;
102+ real dist2 = dx*dx + dy*dy + dz*dz + real (1e-6 );
103+ real dist = std::sqrt (dist2);
105104
106105 // Barnes–Hut acceptance criterion
107106 if (node->leaf || (node->size / dist) < theta) {
108- real inv = 1.0 / (dist * dist * dist);
109- real f = node->m * inv * dt;
110- p.vx += dx * f;
111- p.vy += dy * f;
112- p.vz += dz * f;
107+ constexpr real G = real (6.67430e-11 );
108+ real inv = real (1 ) / (dist2 * dist); // 1 / r^3
109+ real f = G * node->m * inv;
110+
111+ ax += dx * f;
112+ ay += dy * f;
113+ az += dz * f;
113114 return ;
114115 }
115116
116117 // Otherwise recurse into children
117118 for (int i = 0 ; i < 8 ; i++) {
118119 if (node->child [i])
119- bhForce (node->child [i], p, theta, dt );
120+ bhAccel (node->child [i], p, theta, ax, ay, az );
120121 }
121- }
122+ }
0 commit comments