gravitas

Gravitas

A gravitational N-body simulator written in Crystal.

Gravitas explores the emergence of complex gravitational systems from simple physical laws, while serving as a playground for computational performance, parallelism, spatial data structures, and algorithmic optimisation.

The project starts with a straightforward Newtonian gravity implementation and evolves towards large-scale simulations through incremental, measurable improvements.

Features

  • Two-dimensional N-body gravitational simulation.
  • Newtonian gravity with gravitational softening.
  • Leapfrog numerical integration.
  • Deterministic initial conditions.
  • Interactive browser-based visualisation.
  • Particle trails, zooming and simulation pausing.
  • Configurable initial scenarios.
  • Headless performance benchmarking.
  • Regression tests for fundamental physical behaviour.
  • No external runtime dependencies.

Getting started

Requirements

  • Crystal 1.22.3 or later.
  • A modern web browser.

Running the simulation

Start the built-in HTTP server:

crystal run src/gravitas.cr --release -- serve

Open http://127.0.0.1:8080 in your browser.

To simulate a rotating stellar disk:

crystal run src/gravitas.cr --release -- serve --galaxy

Controls

Input Action
Space Pause or resume simulation
T Toggle particle trails
Mouse wheel Zoom in or out

Simulation scenarios

Random cloud

A collection of randomly distributed bodies with varying masses and initial velocities. Gravitational interactions gradually produce clustering and complex trajectories.

Rotating galaxy

A massive central body surrounded by particles with approximately circular initial orbital velocities. Interactions between the bodies introduce perturbations and gradually reshape the system.

This scenario represents an idealised stellar disk rather than a scientifically accurate galaxy.

Physics

Gravitas models gravitational interactions between bodies using Newtonian gravity.

Each body has a position, velocity and mass. During each simulation timestep, gravitational accelerations are calculated from the positions and masses of the surrounding bodies.

Gravitational softening prevents numerical singularities when bodies approach each other closely.

The simulation uses a kick-drift-kick leapfrog integrator, providing better long-term numerical stability than simple Euler integration for gravitational systems.

The simulation operates in two dimensions using normalised physical units.

Architecture

The physics engine is independent of the visualisation layer, allowing simulations to run interactively or as standalone computational benchmarks.

gravitas/
├── src/
│   ├── gravitas.cr       # Application entry point
│   ├── body.cr           # Particle representation
│   ├── physics.cr        # Gravitational force calculation
│   ├── simulation.cr     # Numerical integration
│   ├── scenarios.cr      # Initial conditions
│   └── server.cr         # HTTP interface
├── public/
│   └── index.html        # Canvas-based visualisation
├── spec/                 # Regression tests
└── shard.yml

The simulation runs entirely in Crystal. The browser is responsible only for rendering the simulation state and handling user interaction.

Performance

A central goal of Gravitas is to explore the relationship between algorithmic complexity, parallel execution and practical hardware performance.

Run the headless benchmark:

crystal run src/gravitas.cr --release

Initial benchmark

The initial sequential implementation achieved the following results:

Metric Result
Bodies 1,000
Timesteps 100
Execution time 239 ms
Time per timestep 2.39 ms
Timesteps per second 418

These figures establish an early performance baseline and are hardware-dependent.

The current direct force calculation has quadratic time complexity, O(n²). This makes it an excellent starting point for exploring parallelism and more efficient gravitational algorithms.

Development roadmap

Phase 1: Simulation foundation

  • Body representation
  • Direct gravitational force calculation
  • Gravitational softening
  • Leapfrog integration
  • Headless benchmarking
  • Browser visualisation
  • Deterministic initial conditions
  • Rotating galaxy scenario
  • Physics regression tests

Phase 2: Parallel execution

  • Crystal parallel execution contexts
  • Parallel gravitational force calculation
  • Configurable worker count
  • Sequential versus parallel benchmarks
  • CPU scaling and utilisation analysis

Phase 3: Algorithmic optimisation

  • Quadtree spatial partitioning
  • Barnes-Hut gravitational approximation
  • Configurable approximation threshold
  • Accuracy and performance comparison
  • Parallel Barnes-Hut implementation

Phase 4: Large-scale simulation

  • 100,000-body simulation
  • Memory layout and cache locality optimisation
  • Binary simulation state streaming
  • WebGL rendering
  • Real-time performance metrics
  • Exploration of million-body simulations

Phase 5: Simulation playground

  • Binary star systems
  • Galaxy collisions
  • Three-body systems
  • Interactive scenario configuration
  • Energy and momentum diagnostics
  • Spatial partitioning visualisation

Testing

Run the regression tests:

crystal spec

Physical correctness and reproducibility are important throughout the optimisation process. Improvements should be evaluated against consistent initial conditions and known physical behaviour.

Project philosophy

  • Start simple.
  • Measure before optimising.
  • Prefer algorithmic improvements over brute computational force.
  • Preserve correctness throughout optimisation.
  • Introduce complexity only when it solves a demonstrated problem.
  • Make computational performance visible.
  • DO IT WITHOUT AI!

Gravitas is an experimental project, not a scientific research simulator. Its purpose is to explore physics, algorithms and systems programming through a visually engaging computational problem.

License

MIT

Repository

gravitas

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 1 hour ago
  • November 27, 2026
License

MIT License

Links
Synced at

Fri, 27 Nov 2026 19:08:45 GMT

Languages