' https://go.googlesource.com/exp/+/master/shootout/nbody.c
' The Computer Language Benchmarks Game
' https://benchmarksgame-team.pages.debian.net/benchmarksgame/
' contributed by Christoph Bauer
'
' translated to QB64 by Jack
' modified a bit to avoid using pointer to array
CONST pi
= 3.141592653589793 CONST solar_mass
= (4 * pi
) * pi
CONST days_per_year
= 365.24
FOR j
= i
+ 1 TO nbodies
- 1 dx = bodies(i).x - bodies(j).x
dy = bodies(i).y - bodies(j).y
dz = bodies(i).z - bodies(j).z
distance
= SQR(((dx
* dx
) + (dy
* dy
)) + (dz
* dz
)) mag = dt / ((distance * distance) * distance)
bodies(i).vx = bodies(i).vx - (dx * bodies(j).mass) * mag
bodies(i).vy = bodies(i).vy - (dy * bodies(j).mass) * mag
bodies(i).vz = bodies(i).vz - (dz * bodies(j).mass) * mag
bodies(j).vx = bodies(j).vx + (dx * bodies(i).mass) * mag
bodies(j).vy = bodies(j).vy + (dy * bodies(i).mass) * mag
bodies(j).vz = bodies(j).vz + (dz * bodies(i).mass) * mag
bodies(i).x = bodies(i).x + dt * bodies(i).vx
bodies(i).y = bodies(i).y + dt * bodies(i).vy
bodies(i).z = bodies(i).z + dt * bodies(i).vz
e = 0.0
e = e + (0.5 * bodies(i).mass) * (((bodies(i).vx * bodies(i).vx) + (bodies(i).vy * bodies(i).vy)) + (bodies(i).vz * bodies(i).vz))
FOR j
= i
+ 1 TO nbodies
- 1 dx = bodies(i).x - bodies(j).x
dy = bodies(i).y - bodies(j).y
dz = bodies(i).z - bodies(j).z
distance
= SQR(((dx
* dx
) + (dy
* dy
)) + (dz
* dz
)) e = e - (bodies(i).mass * bodies(j).mass) / distance
energy# = e
SUB offset_momentum
(nbody
AS LONG, bodies
() AS planet
) px = px + bodies(I).vx * bodies(I).mass
py = py + bodies(I).vy * bodies(I).mass
pz = pz + bodies(I).vz * bodies(I).mass
bodies(0).vx = (-px) / ((4 * pi) * pi)
bodies(0).vy = (-py) / ((4 * pi) * pi)
bodies(0).vz = (-pz) / ((4 * pi) * pi)
bodies(0).x = 0: bodies(0).y = 0: bodies(0).z = 0: bodies(0).vx = 0: bodies(0).vy = 0: bodies(0).vz = 0: bodies(0).mass = (4 * pi) * pi
bodies(1).x = 4.84143144246472090D+00: bodies(1).y = -1.16032004402742839D+00: bodies(1).z = -1.03622044471123109D-01: bodies(1).vx = 1.66007664274403694D-03 * days_per_year: bodies(1).vy = 7.69901118419740425D-03 * days_per_year: bodies(1).vz = (-6.90460016972063023D-05) * days_per_year: bodies(1).mass = 9.54791938424326609D-04 * ((4 * pi) * pi)
bodies(2).x = 8.34336671824457987D+00: bodies(2).y = 4.12479856412430479D+00: bodies(2).z = -4.03523417114321381D-01: bodies(2).vx = (-2.76742510726862411D-03) * days_per_year: bodies(2).vy = 4.99852801234917238D-03 * days_per_year: bodies(2).vz = 2.30417297573763929D-05 * days_per_year: bodies(2).mass = 2.85885980666130812D-04 * ((4 * pi) * pi)
bodies(3).x = 1.28943695621391310D+01: bodies(3).y = -1.51111514016986312D+01: bodies(3).z = -2.23307578892655734D-01: bodies(3).vx = 2.96460137564761618D-03 * days_per_year: bodies(3).vy = 2.37847173959480950D-03 * days_per_year: bodies(3).vz = (-2.96589568540237556D-05) * days_per_year: bodies(3).mass = 4.36624404335156298D-05 * ((4 * pi) * pi)
bodies(4).x = 1.53796971148509165D+01: bodies(4).y = -2.59193146099879641D+01: bodies(4).z = 1.79258772950371181D-01: bodies(4).vx = 2.68067772490389322D-03 * days_per_year: bodies(4).vy = 1.62824170038242295D-03 * days_per_year: bodies(4).vz = (-9.51592254519715870D-05) * days_per_year: bodies(4).mass = 5.15138902046611451D-05 * ((4 * pi) * pi)
CALL offset_momentum
(NBODIES
, bodies
()) PRINT USING "##.#########"; energy#
(NBODIES
, bodies
()) CALL advance
(NBODIES
, bodies
(), 0.01) PRINT USING "##.#########"; energy#
(NBODIES
, bodies
())