Simulate and visualise the collision of galaxies which includes N-body gravitational force, using the Barnes-Hut algorithm. It models a spherical galaxy with a Hernquist profile.
This is the product of final project for the course "Computational Astrophysics", ASTR660 in NTHU. The main simulation is written in C++ and the visualisation is done using three.js.
- Python 3, numpy
- C++11 compiler, OpenMP
-
Clone the repository
git clone https://github.com/snowmeow2/galaxy-nbody-simulation.git
-
Compile the C++ code
cd galaxy-nbody-simulation make
-
Prepare the initial conditions
python3 particle.py
This will generate a file called
halo.txt
. -
Run the simulation
./simuation
A directory called
out
will be created and the simulation outputhalo_NUM.txt
will be saved in it. -
Visualise the simulation (optional)
First set up a local server on the repository directory:
python3 -m http.server
Then open the browser and go to
localhost:8000
to view the simulation.
The simulation can be configured by changing the parameters in config.ini
, parameter.h
and visual.js
.
Parameters in config.ini
are used to generate the initial positions and velocities of the particles. The parameters are:
a
: the scale radius of the galaxy [kpc]M
: the total mass of the galaxy [$M_\odot$]r_max
: the maximum radius of the galaxy [kpc]N
: the number of particles in the galaxycollision
: whether to generate an additional galaxy for collisionseparation
: the separation between the two galaxies [kpc]rel_vel
: the relative velocity between the two galaxies [km/s]
Parameters in parameter.h
are used to configure the simulation. The parameters are:
Mass
: the mass of each particle [$M_\odot$]dt
: the time step of the simulation [Gyr]softening
: the softening length of the gravitational force [kpc]N
: the total iteration of the simulationsave_iter
: how often to save the simulation data [iteration]
Parameters in visual.js
are used to configure the animation. The parameters are:
particles_pos_file_prefix
: the prefix of the files containing the simulation data. The files should be named asparticles_pos_file_prefix + frame_number + ".txt"
.frames
: the number of frames, 1 for a static imagerate
: theframe_number
of files (it readshalo_NUM.txt
as index) that displayed, roughly the frame rate of the animation
This work used high-performance computing facilities operated by the Center for Informatics and Computation in Astronomy (CICA) at National Tsing Hua University. This equipment was funded by the Ministry of Education of Taiwan, the Ministry of Science and Technology of Taiwan, and National Tsing Hua University.