Skip to content

Commit 0629f8b

Browse files
Update to KDK
1 parent 4f38178 commit 0629f8b

1 file changed

Lines changed: 67 additions & 31 deletions

File tree

src/gravity/step.h

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,67 @@
1-
#pragma once
2-
#include "../struct/particle.h"
3-
#include <vector>
4-
#include "octree.h"
5-
#include "floatdef.h"
6-
7-
inline void Step(std::vector<Particle>& p, real dt) {
8-
Octree root(0,0,0, 1e9);
9-
10-
for (auto& a : p) {
11-
root.insert(&a);
12-
}
13-
14-
root.computeMass();
15-
16-
real theta = 0.5;
17-
18-
#pragma omp parallel for schedule(static)
19-
for (int i = 0; i < p.size(); i++) {
20-
bhForce(&root, p[i], theta, dt);
21-
}
22-
23-
#pragma omp parallel for
24-
for (int i = 0; i < p.size(); i++) {
25-
p[i].x += p[i].vx * dt;
26-
p[i].y += p[i].vy * dt;
27-
p[i].z += p[i].vz * dt;
28-
}
29-
30-
31-
}
1+
// src/gravity/step.h
2+
#pragma once
3+
#include "../struct/particle.h"
4+
#include <vector>
5+
#include "octree.h"
6+
#include "floatdef.h"
7+
8+
inline void Step(std::vector<Particle>& p, real dt) {
9+
if (p.empty()) return;
10+
11+
real theta = 0.5;
12+
real half = dt * real(0.5);
13+
14+
// =========================
15+
// First Kick (dt/2)
16+
// =========================
17+
{
18+
Octree root(0,0,0, 1e9);
19+
20+
for (auto& a : p) {
21+
root.insert(&a);
22+
}
23+
root.computeMass();
24+
25+
#pragma omp parallel for schedule(static)
26+
for (int i = 0; i < (int)p.size(); i++) {
27+
real ax = 0, ay = 0, az = 0;
28+
bhAccel(&root, p[i], theta, ax, ay, az);
29+
30+
p[i].vx += ax * half;
31+
p[i].vy += ay * half;
32+
p[i].vz += az * half;
33+
}
34+
}
35+
36+
// =========================
37+
// Drift (dt)
38+
// =========================
39+
#pragma omp parallel for schedule(static)
40+
for (int i = 0; i < (int)p.size(); i++) {
41+
p[i].x += p[i].vx * dt;
42+
p[i].y += p[i].vy * dt;
43+
p[i].z += p[i].vz * dt;
44+
}
45+
46+
// =========================
47+
// Second Kick (dt/2)
48+
// =========================
49+
{
50+
Octree root(0,0,0, 1e9);
51+
52+
for (auto& a : p) {
53+
root.insert(&a);
54+
}
55+
root.computeMass();
56+
57+
#pragma omp parallel for schedule(static)
58+
for (int i = 0; i < (int)p.size(); i++) {
59+
real ax = 0, ay = 0, az = 0;
60+
bhAccel(&root, p[i], theta, ax, ay, az);
61+
62+
p[i].vx += ax * half;
63+
p[i].vy += ay * half;
64+
p[i].vz += az * half;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)