NAME
galaxy, mkgalaxy – galactic n–body simulator

SYNOPSIS
games/galaxy [ options ] [ –i ] [ file ]
games/mkgalaxy [ options ] [ –i ] [ –f file ] size

DESCRIPTION
Galaxy is an n–body simulator that uses a Barnes–Hut quad–tree to calculate gravitational interactions. Typical usage is to read a galaxy file (see galaxy(6)) from standard input using the –i command–line option or from a file using the –f option. If no file is read then the simulator starts with an empty universe.

Mouse commands

Holding mouse button 1 while dragging repositions the visible region of the galaxy. Holding mouse button 2 while dragging up or down zooms the visible region of the galaxy in or out, respectively. Mouse button 3 opens a menu with the following options:
new body
Creates a new galactic body. Holding button 1 positions the body. Holding a button 1–2 chord changes the mass/size of the body. Holding a button 1–3 chord changes the initial velocity of the body. Releasing button 1 restarts the simulator with the new body in motion. When new bodies are created, the simulator maintains the Galilean (inertial) reference frame where the center of mass of the galaxy is at rest.
speed
Prompts for a floating point value to change the speed of the simulation. E.g. a value of 2 will double the speed of the simulation and a value of 0.5 will halve the speed. Accuracy is sacrificed for greater speed.
gravity
Prompts for a floating point value to change the gravitational constant. E.g. a value of 2 will double the force exerted by gravity and a value of 0.5 will halve it.
savePrompts for a file name to save the current galaxy as a galaxy(6) file.
loadPrompts for a file name to load the galaxy from the galaxy(6) file.
exitExits the simulator.

Keyboard commands
The following keys are recognized as commands:
a     Show accelerations as vectors.
v     Show velocities as vectors.
s     Show statistics such as the number of bodies being simulated, the maximum depth of the quad–tree, and the average number of calculations made per body.
q     Exit the simulator.
space
Pause and unpause the simulator.
del   Exit the simulator.

Command–line options
Certain aspects of the galaxy simulator are controlled by the following options:
G gravity
Sets the gravitational constant to gravity. The default value is 1.
f file
Reads the galaxy file file (see galaxy(6)).
i    Reads a galaxy file from standard input.
p procs
Specifies the number of extra processes to use in order to calculate the gravitational force on each body in parallel. The default value is $NPROC–1.
t throttle
Causes the process that calculates forces to relinquish the processor for throttle milliseconds after each calculation.
ε softening
Sets the softening factor to prevent gravitational singularities during collisions or near–collisions. The default value is 500.

Mkgalaxy
Mkgalaxy is a utility to create galaxies for simulation. Galaxies can be assembled incrementally by reading an existing galaxy file from standard input with the –i command–line option or from a file with the –f option. Mkgalaxy then writes to standard output a galaxy(6) file with a galaxy of the given size together with the previously read galaxy. Galaxies generated by mkgalaxy have characteristics determined by the following options:
d distance
Distance
determines the spacing between bodies. The default value is 100.
s size
Bodies have the given size. The default value is 25.
v velocity
Bodies have the given velocity in a random direction. The default value is 0.
av angular velocity
Bodies have the given angular velocity relative to the center of mass of the new galaxy being generated. The default value is 0.
gv x,y
The entire galaxy being generated is given the directional velocity determined by the vector (x,y). The default value is (0, 0).
o x,y
The entire galaxy being generated is offset by the vector (x,y). The default value is (0, 0).
sq   The galaxy being generated is a square. Without this option, the galaxy will be circular.

The arguments to the –d, –s, –v, and –av arguments have the form s or s±r where s and r are double–precision floating point numbers. S is the base value and r if given determines a range in which the value will vary randomly from the base.

EXAMPLES
Two rotating circles destroy each other:
games/mkgalaxy –av 100 –d 60±50 –v 10 2000 |
games/mkgalaxy –i –av –70 –d 80±50 –v 10 –o 6000,2000 –gv –80,40 3000 |
games/galaxy –i

Cool patterns made by a square galaxy:
games/mkgalaxy –sq –av 20 5000 | games/galaxy –i

SOURCE
/sys/src/games/galaxy

SEE ALSO
J. Barnes & P. Hut (December 1986). "A hierarchical O(N log N) force–calculation algorithm". Nature. 324 (4): 446–449.

galaxy(6)

HISTORY
Galaxy and mkgalaxy first appeared in 9front (Feb, 2017).