|
14 | 14 | #include "floatdef.h" |
15 | 15 | #include "octree.h" |
16 | 16 | #include <vector> |
| 17 | +#include <memory> |
| 18 | +#include <algorithm> |
17 | 19 |
|
18 | 20 | inline void Step(std::vector<Particle> &p, real dt) { |
19 | | - if (p.empty()) |
20 | | - return; |
| 21 | + if (p.empty()) return; |
21 | 22 |
|
22 | 23 | real theta = 0.5; |
23 | 24 | real half = dt * real(0.5); |
24 | 25 |
|
25 | | - auto buildTree = [&](Octree *&root) { |
26 | | - // Compute bounding box |
| 26 | + // Helper lambda that returns a unique_ptr |
| 27 | + auto buildTree = [&]() -> std::unique_ptr<Octree> { |
27 | 28 | real minx = +1e30, miny = +1e30, minz = +1e30; |
28 | 29 | real maxx = -1e30, maxy = -1e30, maxz = -1e30; |
29 | 30 |
|
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); |
37 | 34 | } |
38 | 35 |
|
39 | 36 | real cx = (minx + maxx) * 0.5; |
40 | 37 | real cy = (miny + maxy) * 0.5; |
41 | 38 | 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); |
45 | 40 |
|
46 | | - real size = std::max(dx, std::max(dy, dz)) * real(0.5); |
| 41 | + if (size <= 0) size = 1; |
47 | 42 |
|
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); |
52 | 45 |
|
53 | 46 | for (auto &a : p) |
54 | 47 | root->insert(&a); |
55 | 48 |
|
56 | 49 | root->computeMass(); |
| 50 | + return root; |
57 | 51 | }; |
58 | 52 |
|
59 | | - // ========================= |
60 | | - // First Kick (dt/2) |
61 | | - // ========================= |
| 53 | + // --- First Kick (dt/2) --- |
62 | 54 | { |
63 | | - Octree *root = nullptr; |
64 | | - buildTree(root); |
| 55 | + std::unique_ptr<Octree> root = buildTree(); |
65 | 56 |
|
66 | 57 | #pragma omp parallel for schedule(static) |
67 | 58 | for (int i = 0; i < (int)p.size(); i++) { |
68 | 59 | 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); |
70 | 62 |
|
71 | 63 | p[i].vx += ax * half; |
72 | 64 | p[i].vy += ay * half; |
73 | 65 | p[i].vz += az * half; |
74 | 66 | } |
75 | | - |
76 | | - delete root; |
| 67 | + // No 'delete root' needed! It happens automatically here. |
77 | 68 | } |
78 | 69 |
|
79 | | -// ========================= |
80 | | -// Drift (dt) |
81 | | -// ========================= |
| 70 | + // --- Drift (dt) --- |
82 | 71 | #pragma omp parallel for schedule(static) |
83 | 72 | for (int i = 0; i < (int)p.size(); i++) { |
84 | 73 | p[i].x += p[i].vx * dt; |
85 | 74 | p[i].y += p[i].vy * dt; |
86 | 75 | p[i].z += p[i].vz * dt; |
87 | 76 | } |
88 | 77 |
|
89 | | - // ========================= |
90 | | - // Second Kick (dt/2) |
91 | | - // ========================= |
| 78 | + // --- Second Kick (dt/2) --- |
92 | 79 | { |
93 | | - Octree *root = nullptr; |
94 | | - buildTree(root); |
| 80 | + std::unique_ptr<Octree> root = buildTree(); |
95 | 81 |
|
96 | 82 | #pragma omp parallel for schedule(static) |
97 | 83 | for (int i = 0; i < (int)p.size(); i++) { |
98 | 84 | 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); |
100 | 86 |
|
101 | 87 | p[i].vx += ax * half; |
102 | 88 | p[i].vy += ay * half; |
103 | 89 | p[i].vz += az * half; |
104 | 90 | } |
105 | | - |
106 | | - delete root; |
107 | 91 | } |
108 | 92 | } |
0 commit comments