Skip to content

Commit 4445426

Browse files
Added optimizations + changed Gravity kernel a bit'
1 parent c072520 commit 4445426

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

src/struct/particle.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#pragma once
1515
#include <cmath>
1616
#include "floatdef.h"
17+
#include "dt/softening.h"
1718

1819
struct alignas(32) Particle {
1920
real x, y, z;
@@ -23,22 +24,19 @@ struct alignas(32) Particle {
2324

2425
inline void Gravity(Particle& a, Particle& b, real dt)
2526
{
26-
constexpr real G = real(1.0);
27-
constexpr real eps2 = real(1e-4);
27+
constexpr real G = real(1.0);
28+
real eps2 = pairSoftening(a.m, b.m); // must be epsilon^2
2829

29-
// Relative position
3030
real dx = b.x - a.x;
3131
real dy = b.y - a.y;
3232
real dz = b.z - a.z;
3333

34-
// Softened squared distance
3534
real r2 = dx*dx + dy*dy + dz*dz + eps2;
3635

37-
// Inverse distance and inverse distance cubed
38-
real invR = std::sqrt(real(1.0) / r2);
39-
real invR3 = invR * invR * invR;
36+
real invR2 = real(1.0) / r2;
37+
real invR = std::sqrt(invR2);
38+
real invR3 = invR * invR2;
4039

41-
// Accelerations (pairwise, symmetric)
4240
real ax = G * b.m * dx * invR3;
4341
real ay = G * b.m * dy * invR3;
4442
real az = G * b.m * dz * invR3;

0 commit comments

Comments
 (0)