Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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  # duration of the simulation [s]
    ts = jnp.linspace(0.0, duration, 1001)
    dt = 2e-4  # time step [s]

    # 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",
    )

Metadata

Release files for nbodyx 0.0.1rc56.post1

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

Source distribution (sdist)

Source distribution for nbodyx 0.0.1rc56.post1
File Size Uploaded Provenance Actions
nbodyx-0.0.1rc56.post1.tar.gz 1.2 MB Without provenance Details for nbodyx-0.0.1rc56.post1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nbodyx 0.0.1rc56.post1
File Interpreter ABI Platform Actions
nbodyx-0.0.1rc56.post1-py3-none-any.whl Python 3 none any Details for nbodyx-0.0.1rc56.post1-py3-none-any.whl

Total release size: 1.2 MB

Release files / nbodyx-0.0.1rc56.post1.tar.gz

Download URL nbodyx-0.0.1rc56.post1.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
204c4b2a08b9e3057544680c5ce6e94033fa61af84e7515af37fb7549fb7c3e3
BLAKE2b-256 checksum
How to use checksums
e6294ba49c4f04e27c390b2590dea0fb109f5ab394c80839d366faba500e02f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / nbodyx-0.0.1rc56.post1-py3-none-any.whl

Download URL nbodyx-0.0.1rc56.post1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18757c61c33c1bcc7eafe7e559473762d170ba8d54b5c86056242581c3888617
BLAKE2b-256 checksum
How to use checksums
a5a999bb2472678b480c35d6036259da030a27a6b40c2c2565643235fb0c581e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Security for nbodyx 0.0.1rc56.post1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

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