Skip to content

Commit ca71eec

Browse files
Update step.h
1 parent 97fada3 commit ca71eec

1 file changed

Lines changed: 22 additions & 38 deletions

File tree

src/gravity/step.h

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,95 +14,79 @@
1414
#include "floatdef.h"
1515
#include "octree.h"
1616
#include <vector>
17+
#include <memory>
18+
#include <algorithm>
1719

1820
inline void Step(std::vector<Particle> &p, real dt) {
19-
if (p.empty())
20-
return;
21+
if (p.empty()) return;
2122

2223
real theta = 0.5;
2324
real half = dt * real(0.5);
2425

25-
auto buildTree = [&](Octree *&root) {
26-
// Compute bounding box
26+
// Helper lambda that returns a unique_ptr
27+
auto buildTree = [&]() -> std::unique_ptr<Octree> {
2728
real minx = +1e30, miny = +1e30, minz = +1e30;
2829
real maxx = -1e30, maxy = -1e30, maxz = -1e30;
2930

30-
for (auto &a : p) {
31-
minx = std::min(minx, a.x);
32-
miny = std::min(miny, a.y);
33-
minz = std::min(minz, a.z);
34-
maxx = std::max(maxx, a.x);
35-
maxy = std::max(maxy, a.y);
36-
maxz = std::max(maxz, a.z);
31+
for (const auto &a : p) {
32+
minx = std::min(minx, a.x); miny = std::min(miny, a.y); minz = std::min(minz, a.z);
33+
maxx = std::max(maxx, a.x); maxy = std::max(maxy, a.y); maxz = std::max(maxz, a.z);
3734
}
3835

3936
real cx = (minx + maxx) * 0.5;
4037
real cy = (miny + maxy) * 0.5;
4138
real cz = (minz + maxz) * 0.5;
42-
real dx = maxx - minx;
43-
real dy = maxy - miny;
44-
real dz = maxz - minz;
39+
real size = std::max({maxx - minx, maxy - miny, maxz - minz}) * real(0.5);
4540

46-
real size = std::max(dx, std::max(dy, dz)) * real(0.5);
41+
if (size <= 0) size = 1;
4742

48-
if (size <= 0)
49-
size = 1; // safety
50-
51-
root = new Octree(cx, cy, cz, size);
43+
// Create the owned root
44+
auto root = std::make_unique<Octree>(cx, cy, cz, size);
5245

5346
for (auto &a : p)
5447
root->insert(&a);
5548

5649
root->computeMass();
50+
return root;
5751
};
5852

59-
// =========================
60-
// First Kick (dt/2)
61-
// =========================
53+
// --- First Kick (dt/2) ---
6254
{
63-
Octree *root = nullptr;
64-
buildTree(root);
55+
std::unique_ptr<Octree> root = buildTree();
6556

6657
#pragma omp parallel for schedule(static)
6758
for (int i = 0; i < (int)p.size(); i++) {
6859
real ax = 0, ay = 0, az = 0;
69-
bhAccel(root, p[i], theta, ax, ay, az);
60+
// Pass the raw pointer via .get() for the traversal
61+
bhAccel(root.get(), p[i], theta, ax, ay, az);
7062

7163
p[i].vx += ax * half;
7264
p[i].vy += ay * half;
7365
p[i].vz += az * half;
7466
}
75-
76-
delete root;
67+
// No 'delete root' needed! It happens automatically here.
7768
}
7869

79-
// =========================
80-
// Drift (dt)
81-
// =========================
70+
// --- Drift (dt) ---
8271
#pragma omp parallel for schedule(static)
8372
for (int i = 0; i < (int)p.size(); i++) {
8473
p[i].x += p[i].vx * dt;
8574
p[i].y += p[i].vy * dt;
8675
p[i].z += p[i].vz * dt;
8776
}
8877

89-
// =========================
90-
// Second Kick (dt/2)
91-
// =========================
78+
// --- Second Kick (dt/2) ---
9279
{
93-
Octree *root = nullptr;
94-
buildTree(root);
80+
std::unique_ptr<Octree> root = buildTree();
9581

9682
#pragma omp parallel for schedule(static)
9783
for (int i = 0; i < (int)p.size(); i++) {
9884
real ax = 0, ay = 0, az = 0;
99-
bhAccel(root, p[i], theta, ax, ay, az);
85+
bhAccel(root.get(), p[i], theta, ax, ay, az);
10086

10187
p[i].vx += ax * half;
10288
p[i].vy += ay * half;
10389
p[i].vz += az * half;
10490
}
105-
106-
delete root;
10791
}
10892
}

0 commit comments

Comments
 (0)