Skip to main content

A JAX simulator for N body problems.

Project description

NBODYX: Simulating N-Body Systems with Jax

This repository derives ODEs for N-Body systems and simulates them using Jax. The main goal is to provide a simple and efficient way to simulate N-Body systems using Jax. It is heavily inspired by the amazing ThreeBodyBot and specifically the NumericsTutorial.

3-body problem

Installation

You can install the package using pip:

pip install nbodyx

or locally in editable mode:

pip install -e .

For animations, we require ffmpeg to be installed. On Ubuntu, you can install it using:

sudo apt-get install ffmpeg

or using conda:

conda install -c conda-forge ffmpeg

Usage

The following example simulates a 3-body system:

from diffrax import diffeqsolve, Dopri5, ODETerm, SaveAt
import jax.numpy as jnp
from nbodyx.constants import G
from nbodyx.ode import make_n_body_ode
from nbodyx.rendering.opencv import animate_n_body, render_n_body

if __name__ == "__main__":
    body_masses = jnp.array([1.0, 1.0, 1.0]) / G
    ode_fn = make_n_body_ode(body_masses)

    # initial positions
    x0 = jnp.array(
        [
            [-0.97000436, 0.24208753],
            [0.0, 0.0],
            [0.97000436, -0.24208753],
        ]
    )
    # initial velocities
    v0 = jnp.array(
        [
            [0.4662036850, 0.4323657300],
            [-0.93240737, -0.86473146],
            [0.4662036850, 0.4323657300],
        ]
    )

    # initial state
    y0 = jnp.stack([x0, v0], axis=0).reshape(-1)
    # external torques
    tau = jnp.zeros((6,))

    # state bounds
    x_min, x_max = -1.5 * jnp.ones((1,)), 1.5 * jnp.ones((1,))

    # simulation settings
    duration = 6.3259
    ts = jnp.linspace(0.0, duration, 1001)
    dt = ts[-1] * 1e-4

    # solve the ODE
    ode_term = ODETerm(ode_fn)
    sol = diffeqsolve(ode_term, Dopri5(), ts[0], ts[-1], dt, y0, tau, saveat=SaveAt(ts=ts), max_steps=None)
    # extract the solution
    y_bds_ts = sol.ys.reshape((len(ts), 2, 3, 2))  # shape: (timesteps, 2, num_bodies, num_dims)

    # animate the solution
    animate_n_body(
        ts,
        x_bds_ts=y_bds_ts[:, 0, ...],
        width=500,
        height=500,
        video_path="examples/outputs/three_body.mp4",
        speed_up=ts[-1] / 10,
        x_min=x_min,
        x_max=x_max,
        timestamp_unit="s",
    )

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

nbodyx-0.0.1rc54.post1.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

nbodyx-0.0.1rc54.post1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file nbodyx-0.0.1rc54.post1.tar.gz.

File metadata

  • Download URL: nbodyx-0.0.1rc54.post1.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for nbodyx-0.0.1rc54.post1.tar.gz
Algorithm Hash digest
SHA256 b885d4e63278dc1d251d01fcf8e41ed8b4cc6a2a28fc9a259d2529078380d882
MD5 11d737e6201f1e95128a27c22ac7f0ad
BLAKE2b-256 d1ec934a1ed14b0e4c74a9b95ec47566c92d7cf25d2782b56c4b2127444b6df3

See more details on using hashes here.

Provenance

File details

Details for the file nbodyx-0.0.1rc54.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for nbodyx-0.0.1rc54.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 d39ad2c26090f7d47b2bdcaf379c5e74668672d0299c628f1593413a75b3aaf9
MD5 521c9baa8be63a02ab755e67308a38d2
BLAKE2b-256 31559c17b08b0fe93c0b7025f922b53ed5119ab3ce20321be30c6380ce9673a8

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page