sourcedemos/nbody/nbody.xtl
1⍝!/usr/bin/env xetal
2⍝# N-body gravity with no loops. Every pair of bodies at once: the
3⍝# displacements of all pairs form a cube (2 x N x N, built by
4⍝# broadcasting with t_able), the cube gives every pair's softened
5⍝# inverse-square pull, and one reduce over the second body sums the
6⍝# pulls into each body's acceleration. A kick-drift-kick (leapfrog)
7⍝# step moves them all. Run it with "xetal run demos/nbody/nbody.xtl";
8⍝# it prints the cube's shape, its x plane, the starting
9⍝# accelerations, the state after 50 steps, and the total momentum
10⍝# (which stays 0). The live page has four presets (the figure-eight,
11⍝# a binary star, a collapsing cluster, a Kepler ellipse) and lets you
12⍝# click a body for its row of the cube:
13⍝# https://softwarewrighter.github.io/X_eTaL-demos/nbody/
14
15⍝# g, eps2, dt, m, steps: G = 1, the softening (keeps close passes
16⍝# finite), the time step, the masses, and how many steps to run. The
17⍝# figure-eight starting masses: three equal, chasing each other
18⍝# round one curve (Chenciner and Montgomery's orbit).
19g ← 1.0
20eps2 ← 0.000001
21dt ← 0.01
22m ← 1.0 1.0 1.0
23steps ← 50
24
25⍝# -- the bodies' masses, spread over every pair. n, mj: mj is N x N,
26⍝# row i column j holds m_j, the mass pulling on body i.
27n ← t̲ally m
28mj ← (o̲ffsets n) 'r̲ight t̲able m
29⍝# u:p_lane a: a matrix as one plane of a rank-3 array, to stack planes.
30ᵘp̲lane ← { a → (1 c̲at s̲hape a) r̲eshape a }
31
32⍝## The cube: every pair's displacement, by broadcasting
33
34⍝# u:c_ube p: p is 2 x N, the x row and the y row. A table of
35⍝# differences gives x_i - x_j for every pair, N x N; the tables for x
36⍝# and y stack into a 2 x N x N cube.
37ᵘc̲ube ← { p →
38 x ← 1 s̲elect p
39 y ← 2 s̲elect p
40 (ᵘp̲lane x '− t̲able x) c̲at ᵘp̲lane y '− t̲able y
41}
42
43⍝## The pull: softened inverse-square
44
45⍝# u:p_ull d: g m_j / (r^2 + eps2)^(3/2) for every pair: the inverse
46⍝# square, and one more 1 / r because the displacement in the cube has
47⍝# length r.
48ᵘp̲ull ← { d → g × mj ÷ (('+ r̲/ d × d) + eps2) ^ 1.5 }
49
50⍝## The reduction: sum over the second body
51
52⍝# u:a_cc p: each plane of the cube times the pull, summed along j
53⍝# (axis 3): the 2 x N accelerations. The diagonal, a body and itself,
54⍝# is 0 times its pull.
55ᵘa̲cc ← { p →
56 d ← ᵘc̲ube p
57 w ← ᵘp̲ull d
58 n̲eg '+ r̲/₃ d × (2 c̲at s̲hape w) r̲eshape w
59}
60
61⍝## One step: kick, drift, kick
62
63⍝# u:s_tep s: the state s is 4 x N, the x and y rows, then the
64⍝# velocities' x and y rows.
65ᵘs̲tep ← { s →
66 p ← 2 t̲ake s
67 h ← (2 d̲rop s) + (dt ÷ 2.0) × ᵘa̲cc p
68 p1 ← p + dt × h
69 p1 c̲at h + (dt ÷ 2.0) × ᵘa̲cc p1
70}
71⍝ -- end of the core -----------------------------------
72
73ᵘr̲ound ← { a → (f̲loat f̲loor 0.5 + a × 10000.0) ÷ 10000.0 }
74p0 ← 2 3 r̲eshape 0.97000436 -0.97000436 0.0 -0.24308753 0.24308753 0.0
75v0 ← 2 3 r̲eshape 0.466203685 0.466203685 -0.93240737 0.43236573 0.43236573 -0.86473146
76s0 ← p0 c̲at v0
77
78⍝ The cube for the start, its shape, and its x plane.
79s̲hape ᵘc̲ube p0
80ᵘr̲ound 1 s̲elect ᵘc̲ube p0
81⍝ The accelerations at the start: one column per body.
82ᵘr̲ound ᵘa̲cc p0
83⍝ After 50 steps (t = 0.5): positions, then velocities.
84s ← steps 'ᵘs̲tep p̲ower s0
85ᵘr̲ound s
86⍝ The total momentum stays 0: the sum over bodies of m v.
87ᵘr̲ound '+ r̲/₂ (2 d̲rop s) × (2 c̲at n) r̲eshape m