1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use crate::compute_method::{
    math::{AsPrimitive, BitAnd, CmpNe, Float, FloatVector, FromPrimitive, Sum, Zero, SIMD},
    tree::{
        partition::{BoundingBox, SubDivide},
        Node, NodeID, Orthtree,
    },
    ComputeMethod,
};

/// Point-mass representation of an object in space.
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct PointMass<V, S> {
    /// Position of the object.
    pub position: V,
    /// Mass of the object.
    pub mass: S,
}

impl<V: Zero, S: Zero> PointMass<V, S> {
    /// [`PointMass`] with position and mass set to [`Zero::ZERO`].
    pub const ZERO: Self = PointMass::new(V::ZERO, S::ZERO);
}

impl<V, S> PointMass<V, S> {
    /// Creates a new [`PointMass`] with the given position and mass.
    #[inline]
    pub const fn new(position: V, mass: S) -> Self {
        Self { position, mass }
    }

    /// Creates a new [`PointMass`] with the given lanes of positions and masses.
    #[inline]
    pub fn new_lane(position: V::Lane, mass: S::Lane) -> Self
    where
        V: SIMD,
        S: SIMD,
    {
        Self::new(V::new_lane(position), S::new_lane(mass))
    }

    /// Returns the [`PointMass`] corresponding to the center of mass and total mass of the given
    /// slice of point-masses.
    #[inline]
    pub fn new_com(data: &[Self]) -> Self
    where
        V: FloatVector<Float = S> + Sum + Copy,
        S: Float + FromPrimitive<usize> + Sum + Copy,
    {
        let tot = data.iter().map(|p| p.mass).sum();
        let com = if tot == S::ZERO {
            data.iter().map(|p| p.position).sum::<V>() / data.len().as_()
        } else {
            data.iter().map(|p| p.position * (p.mass / tot)).sum()
        };

        Self::new(com, tot)
    }

    /// Creates a new [`PointMass`] with all lanes set to the given position and mass.
    #[inline]
    pub fn splat_lane(position: V::Element, mass: S::Element) -> Self
    where
        V: SIMD,
        S: SIMD,
    {
        Self::new(V::splat(position), S::splat(mass))
    }

    /// Returns a [`SIMD`] point-masses from a slice of [`SIMD::Element`] point-masses.
    #[inline]
    pub fn slice_to_lane<const L: usize, T, E>(slice: &[PointMass<T, E>]) -> Self
    where
        T: Clone + Zero,
        E: Clone + Zero,
        V: SIMD<Lane = [T; L], Element = T>,
        S: SIMD<Lane = [E; L], Element = E>,
    {
        let mut lane = [PointMass::ZERO; L];
        lane[..slice.len()].clone_from_slice(slice);
        Self::new_lane(lane.clone().map(|p| p.position), lane.map(|p| p.mass))
    }

    /// Returns an iterator of [`SIMD`] point-masses from a slice of [`SIMD::Element`] point-masses.
    #[inline]
    pub fn slice_to_lanes<'a, const L: usize, T, E>(
        slice: &'a [PointMass<T, E>],
    ) -> impl Iterator<Item = Self> + 'a
    where
        T: Clone + Zero,
        E: Clone + Zero,
        V: SIMD<Lane = [T; L], Element = T> + 'a,
        S: SIMD<Lane = [E; L], Element = E> + 'a,
    {
        slice.chunks(L).map(Self::slice_to_lane)
    }

    /// Returns true if the mass is zero.
    #[inline]
    pub fn is_massless(&self) -> bool
    where
        S: PartialEq + Zero,
    {
        self.mass == S::ZERO
    }

    /// Returns false if the mass is zero.
    #[inline]
    pub fn is_massive(&self) -> bool
    where
        S: PartialEq + Zero,
    {
        self.mass != S::ZERO
    }

    /// Computes the gravitational force exerted on the current point-mass using the given position
    /// and mass. This method is optimised in the case where `V` and `S` are scalar types.
    ///
    /// If the position of the current point-mass is guaranteed to be different from the given
    /// position, this computation can be more efficient with `CHECK_ZERO` set to false.
    #[inline]
    pub fn force_scalar<const CHECK_ZERO: bool>(&self, position: V, mass: S, softening: S) -> V
    where
        V: FloatVector<Float = S> + Copy,
        S: Float + Copy,
    {
        let dir = position - self.position;
        let norm = dir.norm_squared();
        let norm_s = norm + (softening * softening);

        // Branch removed by the compiler when `CHECK_ZERO` is false.
        if CHECK_ZERO && norm == S::ZERO {
            dir
        } else {
            dir * (mass / (norm_s * norm_s.sqrt()))
        }
    }

    /// Computes the gravitational force exerted on the current point-mass using the given position
    /// and mass. This method is optimised in the case where `V` and `S` are simd types.
    ///
    /// If the position of the current point-mass is guaranteed to be different from the given
    /// position, this computation can be more efficient with `CHECK_ZERO` set to false.
    #[inline]
    pub fn force_simd<const CHECK_ZERO: bool>(&self, position: V, mass: S, softening: S) -> V
    where
        V: FloatVector<Float = S> + Copy,
        S: Float + BitAnd<Output = S> + CmpNe<Output = S> + Copy,
    {
        let dir = position - self.position;
        let norm = dir.norm_squared();
        let norm_s = norm + (softening * softening);
        let f = mass * (norm_s * norm_s * norm_s).rsqrt();

        // Branch removed by the compiler when `CHECK_ZERO` is false.
        if CHECK_ZERO {
            dir * f.bitand(norm.cmp_ne(S::ZERO))
        } else {
            dir * f
        }
    }

    /// Computes the gravitational acceleration exerted on the current point-mass by the specified
    /// node of the given [`Orthtree`] following the [Barnes-Hut](https://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation)
    /// approximation with the given `theta` parameter, provided `V` and `S` are scalar types.
    #[inline]
    pub fn acceleration_tree<const X: usize, const D: usize>(
        &self,
        tree: &Orthtree<X, D, S, PointMass<V, S>>,
        node: Option<NodeID>,
        theta: S,
        softening: S,
    ) -> V
    where
        V: FloatVector<Float = S> + Copy + Sum,
        S: Float + PartialOrd + Copy,
    {
        let mut acceleration = V::ZERO;

        let estimate = X * (tree.nodes.len() as f32).ln() as usize; // TODO: find a proper estimate
        let mut stack = Vec::with_capacity(estimate);
        stack.push(node);

        while let Some(node) = stack.pop() {
            let id = match node {
                Some(id) => id as usize,
                None => continue,
            };

            let p2 = tree.data[id];
            let dir = p2.position - self.position;
            let norm = dir.norm_squared();

            if norm == S::ZERO {
                continue;
            }

            match tree.nodes[id] {
                Node::Internal(node) if theta < node.bbox.width() / norm.sqrt() => {
                    stack.extend(node.orthant);
                }
                _ => {
                    let norm_s = norm + (softening * softening);
                    acceleration += dir * (p2.mass / (norm_s * norm_s.sqrt()));
                }
            }
        }

        acceleration
    }
}

/// Flexible, copyable storage with references to affected particles and a generic massive storage.
#[derive(Debug)]
pub struct ParticleSystem<'p, V, S, T: ?Sized> {
    /// Particles for which the acceleration is computed.
    pub affected: &'p [PointMass<V, S>],
    /// Particles responsible for the acceleration exerted on the `affected` particles, in a
    /// storage `T`.
    pub massive: &'p T,
}

impl<V, S, T: ?Sized> Clone for ParticleSystem<'_, V, S, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<V, S, T: ?Sized> Copy for ParticleSystem<'_, V, S, T> {}

impl<'p, V, S, T: ?Sized> ParticleSystem<'p, V, S, T> {
    /// Creates a new [`ParticleSystem`] with the given slice of particles and massive storage.
    #[inline]
    pub const fn with(affected: &'p [PointMass<V, S>], massive: &'p T) -> Self {
        Self { affected, massive }
    }
}

/// [`ParticleSystem`] with a slice of particles for the massive storage.
pub type ParticleSliceSystem<'p, V, S> = ParticleSystem<'p, V, S, [PointMass<V, S>]>;

/// Storage with particles in an [`Orthtree`] and its root.
#[derive(Clone, Debug)]
pub struct ParticleTree<const X: usize, const D: usize, V, S> {
    root: Option<NodeID>,
    tree: Orthtree<X, D, S, PointMass<V, S>>,
}

impl<const X: usize, const D: usize, V, S> ParticleTree<X, D, V, S> {
    /// Returns the root of the [`Orthtree`].
    #[inline]
    pub const fn root(&self) -> Option<NodeID> {
        self.root
    }

    /// Returns a reference to the [`Orthtree`].
    #[inline]
    pub const fn get(&self) -> &Orthtree<X, D, S, PointMass<V, S>> {
        &self.tree
    }
}

impl<const X: usize, const D: usize, V, S> From<&[PointMass<V, S>]> for ParticleTree<X, D, V, S>
where
    V: Copy + FloatVector<Float = S, Array = [S; D]>,
    S: Copy + Float + Sum + PartialOrd + FromPrimitive<usize>,
    BoundingBox<[S; D]>: SubDivide<Division = [BoundingBox<[S; D]>; X]>,
{
    #[inline]
    fn from(slice: &[PointMass<V, S>]) -> Self {
        let mut tree = Orthtree::with_capacity(slice.len());
        let root = tree.build_node(slice, |p| p.position.into(), PointMass::new_com);

        Self { root, tree }
    }
}

/// [`ParticleSystem`] with a [`ParticleTree`] for the massive storage.
pub type ParticleTreeSystem<'p, const X: usize, const D: usize, V, S> =
    ParticleSystem<'p, V, S, ParticleTree<X, D, V, S>>;

/// Storage inside of which the massive particles are placed before the massless ones.
///
/// Allows for easy optimisation of the computation of forces between massive and massless
/// particles.
#[derive(Clone, Debug)]
pub struct ParticleOrdered<V, S> {
    massive_len: usize,
    particles: Vec<PointMass<V, S>>,
}

impl<V, S> ParticleOrdered<V, S> {
    /// Creates a new [`ParticleOrdered`] with the given massive and massless particles.
    #[inline]
    pub fn with<I, U>(massive: I, massless: U) -> Self
    where
        S: PartialEq + Zero,
        I: IntoIterator<Item = PointMass<V, S>>,
        U: IntoIterator<Item = PointMass<V, S>>,
    {
        let particles = massive.into_iter().chain(massless).collect::<Vec<_>>();
        let massive_len = particles
            .iter()
            .position(PointMass::is_massless)
            .unwrap_or(particles.len());

        Self {
            massive_len,
            particles,
        }
    }

    /// Returns the number of stored massive particles.
    #[inline]
    pub const fn massive_len(&self) -> usize {
        self.massive_len
    }

    /// Returns a reference to the massive particles.
    #[inline]
    pub fn massive(&self) -> &[PointMass<V, S>] {
        &self.particles[..self.massive_len]
    }

    /// Returns a reference to the massless particles.
    #[inline]
    pub fn massless(&self) -> &[PointMass<V, S>] {
        &self.particles[self.massive_len..]
    }

    /// Returns a reference to the particles.
    #[inline]
    pub fn particles(&self) -> &[PointMass<V, S>] {
        &self.particles
    }

    /// Returns a mutable reference to the massive particles.
    #[inline]
    pub fn massive_mut(&mut self) -> &mut [PointMass<V, S>] {
        &mut self.particles[..self.massive_len]
    }

    /// Returns a mutable reference to the massless particles.
    #[inline]
    pub fn massless_mut(&mut self) -> &mut [PointMass<V, S>] {
        &mut self.particles[self.massive_len..]
    }

    /// Returns a mutable reference to the stored ordered particles.
    #[inline]
    pub fn particles_mut(&mut self) -> &mut [PointMass<V, S>] {
        &mut self.particles
    }
}

impl<V, S> From<&[PointMass<V, S>]> for ParticleOrdered<V, S>
where
    V: Clone,
    S: Clone + PartialEq + Zero,
{
    #[inline]
    fn from(particles: &[PointMass<V, S>]) -> Self {
        Self::with(
            particles.iter().filter(|p| p.is_massive()).cloned(),
            particles.iter().filter(|p| p.is_massless()).cloned(),
        )
    }
}

/// Storage for particles which has a copy of the stored particles inside a [`ParticleOrdered`].
#[derive(Clone, Debug)]
pub struct ParticleReordered<'p, V, S> {
    /// Original, unordered particles.
    pub unordered: &'p [PointMass<V, S>],
    ordered: ParticleOrdered<V, S>,
}

impl<V, S> ParticleReordered<'_, V, S> {
    /// Returns a reference to the [`ParticleOrdered`].
    #[inline]
    pub const fn ordered(&self) -> &ParticleOrdered<V, S> {
        &self.ordered
    }

    /// Returns the number of stored massive particles.
    #[inline]
    pub const fn massive_len(&self) -> usize {
        self.ordered.massive_len()
    }

    /// Returns a reference to the massive particles.
    #[inline]
    pub fn massive(&self) -> &[PointMass<V, S>] {
        self.ordered.massive()
    }

    /// Returns a reference to the massless particles.
    #[inline]
    pub fn massless(&self) -> &[PointMass<V, S>] {
        self.ordered.massless()
    }

    /// Returns a reference to the stored ordered particles.
    #[inline]
    pub fn reordered(&self) -> &[PointMass<V, S>] {
        self.ordered.particles()
    }
}

impl<'p, V, S> From<&'p [PointMass<V, S>]> for ParticleReordered<'p, V, S>
where
    V: Clone,
    S: Clone + Zero + PartialEq,
{
    #[inline]
    fn from(affected: &'p [PointMass<V, S>]) -> Self {
        Self {
            unordered: affected,
            ordered: ParticleOrdered::from(affected),
        }
    }
}

impl<V, S, C, O> ComputeMethod<&[PointMass<V, S>]> for C
where
    O: IntoIterator,
    for<'a> C: ComputeMethod<ParticleSliceSystem<'a, V, S>, Output = O>,
{
    type Output = O;

    #[inline]
    fn compute(&mut self, slice: &[PointMass<V, S>]) -> Self::Output {
        self.compute(ParticleSliceSystem {
            affected: slice,
            massive: slice,
        })
    }
}

impl<V, S, C, O> ComputeMethod<&ParticleOrdered<V, S>> for C
where
    O: IntoIterator,
    for<'a> C: ComputeMethod<ParticleSliceSystem<'a, V, S>, Output = O>,
{
    type Output = O;

    #[inline]
    fn compute(&mut self, ordered: &ParticleOrdered<V, S>) -> Self::Output {
        self.compute(ParticleSliceSystem {
            affected: ordered.particles(),
            massive: ordered.massive(),
        })
    }
}

impl<V, S, C, O> ComputeMethod<&ParticleReordered<'_, V, S>> for C
where
    O: IntoIterator,
    for<'a> C: ComputeMethod<ParticleSliceSystem<'a, V, S>, Output = O>,
{
    type Output = O;

    #[inline]
    fn compute(&mut self, reordered: &ParticleReordered<V, S>) -> Self::Output {
        self.compute(ParticleSliceSystem {
            affected: reordered.unordered,
            massive: reordered.massive(),
        })
    }
}

impl<const X: usize, const D: usize, V, S, C, O> ComputeMethod<ParticleSliceSystem<'_, V, S>> for C
where
    O: IntoIterator,
    for<'a> C: ComputeMethod<ParticleTreeSystem<'a, X, D, V, S>, Output = O>,
    V: Copy + FloatVector<Float = S, Array = [S; D]>,
    S: Copy + Float + Sum + PartialOrd + FromPrimitive<usize>,
    BoundingBox<[S; D]>: SubDivide<Division = [BoundingBox<[S; D]>; X]>,
{
    type Output = O;

    #[inline]
    fn compute(&mut self, system: ParticleSliceSystem<V, S>) -> Self::Output {
        self.compute(ParticleTreeSystem {
            affected: system.affected,
            massive: &ParticleTree::from(system.massive),
        })
    }
}