Simple N-body code for Python
Project description
GravHopper
"They told me computers could only do arithmetic." -- Grace Hopper
Gravitational N-body simulation code written by Jeremy Bailin.
Named in honour of pioneering computer scientist Grace Hopper. Doubly appropriate because it uses a leapfrog integrator.
This is a simple Python-interface code for performing gravitational N-body simulations. It combines a simple Python interface for ease of use with a C backend for speed, and has the following features:
- Choice of Barnes-Hut tree or direct summation algorithm.
- Ability to include external potentials from galpy, gala, or user-supplied functions.
- Ability to return output as pynbody snapshots.
- Functions that generate equilibrium or near-equilibrium initial conditions (ICs) for several density profiles (Plummer, Hernquist, exponential disk), along with the ability to create ICs from galpy distribution function objects or pynbody snapshots.
- Utility functions for plotting snapshots and making movies.
For now, it uses a constant uniform timestep and constant uniform softening length.
Requirements:
- Astropy
- NumPy, SciPy, Matplotlib
- C compiler
- To use galpy, gala, or pynbody interface functions, they will need to be installed.
- Saving movies requires ffmpeg.
For example, this will create a Plummer sphere with 2000 particles, run it for a few dynamical times, and plot the particle positions before and after to show that it is in equilibrium:
from gravhopper import Simulation, IC
from astropy import units as u
import matplotlib.pyplot as plt
# Create Plummer initial conditions.
Plummer_IC = IC.Plummer(N=2000, b=1*u.pc, totmass=1e6*u.Msun)
# Create a new simulation with a time step of 0.005 Myr and a softening of 0.05 pc.
sim = Simulation(dt=0.005*u.Myr, eps=0.05*u.pc)
# Add the Plummer model to the simulation
sim.add_IC(Plummer_IC)
# Run for 400 time steps
sim.run(400)
# Plot the x-y positions at the beginning and end.
fig = plt.figure(figsize=(12,4))
ax1 = fig.add_subplot(121, aspect=1.0)
ax2 = fig.add_subplot(122, aspect=1.0)
sim.plot_particles(snap='IC', unit=u.pc, xlim=[-10,10], ylim=[-10,10], ax=ax1)
sim.plot_particles(snap='final', unit=u.pc, xlim=[-10,10], ylim=[-10,10], ax=ax2)
Installation
The code does not yet fully incorporate setuptools, so for now you will need to manually place the package in a subdirectory of the code that uses it. Fixing this is high on the priority list! Until then:
- Clone or download the git repository
git clone https://github.com/jbailinua/gravhopper.git
- Go into the gravhopper directory and build the code.
cd gravhopper
python setup.py build_ext --inplace
- Copy the gravhopper subdirectory to wherever you want to use it.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for gravhopper-1.0.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e22a88851a4f39c6fba191bcc0c4a3724148abd9a287e8f0bceac0016497d1aa |
|
MD5 | 69e608a037e532c8ad29c1e78857218e |
|
BLAKE2b-256 | 0afd93e11e7820de0b94f3a369655a8b7b2adb887e41615f10e279c56dbba37a |