Skip to main content

GravityLab: Astrophysical N-body simulator.

GitHub PyPI

A fast astrophysical N-body simulator.

GravityLab is written in Python and Cython, with performance-critical parts implemented in Cython and the main simulation module written in Python. This makes GravityLab flexible and very fast. The core physics solvers are written in Cython and are optimized to make GravityLab fast. The ODEs governing motions of the bodies are solved using the Leapfrog integration scheme. The Python module is flexible and powerful, while keeping it simple for users to use it.

Installation

Dependencies

The following are required to run GravityLab:

  • Cython
  • NumPy
  • Matplotlib

Install

To install this code, run the following commands in your terminal:

> git clone https://github.com/Kushaalkumar-pothula/gravitylab.git
> cd gravitylab
> python setup.py build_ext --inplace
> python setup.py install

Usage

You can launch a Python prompt or IPython shell from the gravitylab/ directory, which allows you to import the modules directly. You should also all the gravitylab to PYTHONPATH to be able to access it from anywhere throughout the system. To run gravitylab in a Python prompt or IPython shell, you can do:

> cd gravitylab
> python

or if you use IPython (recommended):

> cd gravitylab
> ipython

You can run the simulation using the gravitylab.simulate module. This module provides a powerful (yet simple to use) class Simulation. An instance of the Simulation class can be instantiated with the following command (parameters not shown here):

from gravitylab.simulate import Simulation
my_simulation = Simulation()

Or if you are inside the gravitylab directory:

from simulate import Simulation
my_simulation = Simulation()

Set up simulations

Add initial conditions to a simulation instance:

my_simulation.initial_conditions()

And then run a simulation using the .run() method:

my_simulation.run()

You can also get the calculated positions returned by the above command:

positions = my_simulation.run()

Next, you can create a plot of the evolution positions throughout the simulation:

my_simulation.plot()

The .plot() method takes a many parameters, including 3D plotting.

An preset example (two equally massive bodies with random positions ans velocities) for running GravityLab simulations can be found in gravitylab/example.py:

> python gravitylab/example.py

Viewing help

If you are using the IPython shell
In [2]: Simulation?
Init signature: Simulation(N, dt, t, tEnd)
Docstring:
Main simulation class for GravityLab

Attributes
----------
N    : Number of bodies
dt   : Timestep
t    : Start time
tEnd : End time

Methods
-------
initial_conditions : Initial conditions for simulation
run                : Run simulation
plot               : Plot positions
Init docstring:
Initialize a simulation

Parameters
----------
N    : Number of bodies
dt   : Timestep
t    : Start time
tEnd : End time
If you are using the Python prompt
>>> help(Simulation)
Help on class Simulation in module simulate:

class Simulation(builtins.object)
|  Simulation(N, dt, t, tEnd)
|
|  Main simulation class for GravityLab
|
|  Attributes
|  ----------
|  N    : Number of bodies
|  dt   : Timestep
|  t    : Start time
|  tEnd : End time
|
|  Methods
|  -------
|  initial_conditions : Initial conditions for simulation
|  run                : Run simulation
|  plot               : Plot positions
|
|  Methods defined here:
|
|  __init__(self, N, dt, t, tEnd)
|      Initialize a simulation
|
|      Parameters
|      ----------
|      N    : Number of bodies
|      dt   : Timestep
|      t    : Start time
|      tEnd : End time
|
|  animate(self)
|      Animate
|
|  animate_func(self, i)
|      Animate positions
|
|  init_animation(self)
|      Initialize animation
|
|  initial_conditions(self, pos, vel, mass)
|      Initial conditions for a simulation
|
|      Parameters
|      ----------
|      pos : N x 3 array of positions
|      vel : N x 3 array of velocities
|      mass : N x 1 array of masses
|
|  plot(self, three_dimensional=False, start_pos=True, color='blue', alpha=0.6)
|      Plot positions of bodies
|
|      Parameters
|      ----------
|      three_dimensional (optional) : 3D plotting
|      color (optional)             : Desired colour of markers
|      alpha (optional)             : Opacity of markers
|
|  run(self, verbose=False)
|      Run the simulation
|
|      Parameters
|      ----------
|      verbose (optional) : Give more verbose output
|
|      Returns
|      -------
|      pos_arr : N x 3 array of positions
|
|  ----------------------------------------------------------------------

The same step is applicable for methods of Simulation().

Author

This code was written by Kushaal Kumar Pothula. I wrote this code during my 10th grade while learning about numerical simulations during the summer as a learning project.

Metadata

Release files for GravityLab 10.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for GravityLab 10.0.0
File Interpreter ABI Platform
GravityLab-10.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64, Linux glibc 2.5+ x86-64 Details
GravityLab-10.0.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64, Linux glibc 2.5+ x86-64 Details

Total release size: 793.1 kB

Release files / GravityLab-10.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL GravityLab-10.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 395.3 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
2d8280649ea8155c21d38a6c8d806fc2917a5bc425a02609008541e2113aee0e
BLAKE2b-256 checksum
How to use checksums
4b299e26cf4cdfd83fbbc09f669116e0c72b0929aef504f33141736edc9e02b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

Release files / GravityLab-10.0.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL GravityLab-10.0.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 397.8 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
e95d55d1b5b319de46c3bc7cb49dc8eb75ece45e92c17c881a32233c7aac6c47
BLAKE2b-256 checksum
How to use checksums
bc33bbf63d18003fc6c9d5b925ac45a40a13485b24d1e39b26c18e195ae9ec16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

Release history Release notifications | RSS feed

This release

10.0.0 This release

2 release files

9.1.1

2 release files

7.0.0

2 release files

4.0.0

2 release files

2.2.7

1 release file

2.2.6

1 release file

2.2.5

1 release file

2.2.4

1 release file

2.2.3

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1

1 release file

2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page