Skip to content

About

Orbital mechanics simulation project

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Orbit Simulator



Overview

This is a project that simulates orbital physics, allowing for scenario calculations and verification. It is made as a learning project exploring orbital mechanics and simulation.

Features

Category Features
Integrators • Forward Euler
• Semi-Implicit Euler
• Velocity Verlet
Simulation • Simulate N-body orbital mechanics based on input parameters
• Optional collision checking
Diagnostics • Calculation of orbital variables (e.g. eccentricity, semi-major axis, periapsis/apoapsis)
• Calculation of events (e.g. periapsis/apoapsis target time, escape velocity)
• Calculation of system-wide variables (e.g. total linear momentum, center of mass)
Plots • Plotting of system trajectory
• Plotting of specific values of interest via tables
Tests • Verification of calculation functions against known correct values
Maneuvers • Applying impulsive velocity ΔV to a body
Collisions • Checking of the state of collision of the system
• Return calculated collision timestamp
Benchmark • Benchmarking of hardware speed in simulating systems

Physics model

  • N-body (Legacy 2-body still supported)
  • Two-dimensional
  • Newtonian gravity
  • SI units
  • No atmospheric calculations
    Example gravitational acceleration equation; Input parameters Vector2(x, y):
    $\mu = GM;\ \mu_{Earth} = 3.98589196\times 10^{14}\ m^{3}/s^{2}$
    $r = \sqrt{x^{2}+y^{2}}$
    $a_{x} = -(\frac{\mu_{Earth}\times x}{r^{3}})$
    $a_{y} = -(\frac{\mu_{Earth}\times y}{r^{3}})$

Numerical integration

Integration is a method to simulate orbital paths, this project implements 3 methods with varying accuracy: Forward Euler, Semi-Implicit Euler, Velocity Verlet. Precise accuracy percentage can be viewed in scenario_plots/integrator_validation_6.png.

Installation

Requires Python and Matplotlib. If your device doesn't have Matplotlib installed, run this command in the terminal: pip install matplotlib.

Download the repository as a .zip file then extract it in your preferred location.

Usage

After a successful installation, open a terminal window in the installed folder and run these commands:

Command Description
python main.py integrators Comparison and visualisation of different integration methods
python main.py orbits Simulate and plot elliptical and hyperbolic escape orbits
python main.py maneuvers Examples of in-orbit maneuvering
python main.py collision Demonstration of collision via a descending orbit
python main.py nbody Examples of n-body systems
python main.py convergence Demonstration of precession convergence as Δt decreases
python -m unittest discover -s tests Test unit of diagnostic functions

Validation and results

Comparison of integrators

Note: Using Earth as reference body at origin (0, 0) Integrator trajectory comparison Results show Velocity Verlet reaches a drift of 0.0000273% at Δt=30 over the tested parameters, followed by Semi-Implicit Euler with 0.1046%, and lastly Forward Euler with a 23.82% drift. Hohmann transfer ellipse from 7,000,000m to a 10,000,000m orbit Trajectory plot of an impulsive prograde burn, specifically a Hohmann transfer ellipse, raising apoapsis to 10,000,000 meters from 7,000,000 meters.

Precession convergence

Note: Using Earth and the Moon as the target pair in a solar system running for 30 years with Δt=3, 6 and 12 minutes. Keep in mind that the Δt in scenarios.py are increased to 3, 6 and 12 hours for running performance. Earth-Moon precession convergence and conservation table Results show that as smaller Δt are used in simulating, the measured convergence order will eventually converge to 2. Earth-Moon periapsis angle comparison for multiple timesteps Comparison plot of 3 timesteps, showing that at lower Δt, cutting Δt down even more will lead to diminishing returns when considering huge amount of the increase in computation needed for a minuscule increase in accuracy.

Optimisation

Note: Performance benchmark values are measured with the ASUS TUF Gaming A15 (2024) running benchmark.py 5 times and taking the average.

Detailed benchmark specification

  • Device specification
    • AMD Ryzen 7-7735
    • NVIDIA GeForce RTX 4050 Laptop Edition
    • 16GB DDR5 4800MHz
  • Benchmark scenario
    • 10-body solar system
    • Velocity verlet integration
    • Collision checks enabled
    • No history recording

Below will be a table of changes made to optimise the code and its effect on the performance.

Optimisation Steps per second Change in percentage from last optimisation Change in factor from pre-optimisation
Pre-optimisation 2,202.26 - -
slots=True in state.py 2,255.80 +2.43% 1.02x
Velocity verlet acceleration handover in integrators.py 3,846.71 +70.53% 1.75x
System gravitational acceleration calculation refactor in gravity.py 5,021.45 +30.54% 2.28x
Scalarise gravitational acceleration calculation in gravity.py 11,016.55 +119.39% 5.00x
System collision check refactor in collision.py 14,827.21 +34.59% 6.73x
Python version update from 3.14 to 3.15 18,255.11 +23.12% 8.29x

Project structure

Module Purpose(s)
benchmark.py Benchmark system
collision.py Collision check functions
constants.py Constant definitions
diagnostics.py Orbital variables calculation functions
gravity.py Gravitational acceleration calculation
integrators.py Integrator methods functions
main.py Main scenario simulation launcher
maneuvers.py Orbital maneuver functions
plotter.py Trajectory/table plotting functions
scenarios.py Function cluster of different scenarios
simulation.py Modular simulation function
state.py Dataclass library
system_diagnostics.py System variables calculation functions
validation.py Validation of energy drift for each integrators
tests/test_collisions.py Test unit for collisions
tests/test_diagnostics.py Test unit for diagnostics
tests/test_gravity.py Test unit for gravity
tests/test_integrators.py Test unit for integrators
tests/test_maneuvers.py Test unit for maneuvers
tests/test_state.py Test unit for state data classes
tests/test_system.py Test unit for n-body system calculations

Limitations

  • Two-dimensional
  • No atmospheric resistance
  • Unrealistic ideal impulsive burns
  • Approximate collision interpolation
  • Finite timesteps, causing numerical errors

Future work

  • More accurate collision roots
  • Interactive control playground
  • Time acceleration

About

Orbital mechanics simulation project

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages