A comprehensive solar system simulation implementing Newtonian gravity with optional post-Newtonian relativistic corrections. Features real-time 3D visualization, orbital mechanics calculations, and energy/momentum conservation monitoring.
📄 Read handwritten calculations (PDF) | 📝 Read Documentation (Word)
- Features
- Project Structure
- Installation
- Usage
- Physics Implementation
- Testing
- Performance
- Accuracy
- Customization
- Known Limitations
- Future Enhancements
- References
- Insipiration
- License
- Accurate Physics: Implements Newtonian gravity with velocity-Verlet integration
- Relativistic Corrections: Optional 1PN (post-Newtonian) corrections for Mercury's perihelion precession
- Real-time Visualization: Interactive 3D matplotlib-based visualization with adjustable camera controls
- Orbital Analysis: Calculate and display osculating orbital elements (semi-major axis, eccentricity, inclination, etc.)
- Conservation Monitoring: Track energy and angular momentum conservation over time with diagnostics
- Flexible Configuration: JSON-based planetary parameter configuration for easy customization
- Labeling & Tracking: Dynamically updated body labels for each planet, moon, or object in the system
- Scalable System: Extendable to include moons, asteroids, comets, and custom celestial bodies
- Orbit Trails: Option to display trajectory lines for visualizing orbital paths
- Performance Optimized: Efficient handling of N-body calculations with support for large simulations
- Educational Use: Designed for both research and teaching, making orbital mechanics concepts more intuitive
solar_sim/
├── constants.py # Physical constants and simulation parameters
├── main.py # Main entry point
├── test_simulation.py
├── vpy_simulation.py
├── physics/ # Core physics engine
│ ├── elements.py # Orbital elements ↔ state vector conversion
│ ├── nbody.py # N-body gravity and integrator
│ ├── pn1.py # Post-Newtonian corrections
│ ├── osculating.py # Instantaneous orbital element calculation
│ └── diagnostics.py # Energy/momentum conservation checks
├── model/ # Data structures
│ ├── body.py # Body class (mass, position, velocity, etc.)
│ └── system.py # Solar system loading and management
├── viz/ # Visualization system
│ ├── scene.py # Main rendering coordinator
│ ├── camera.py # Camera controls and modes
│ ├── surface.py # Gravitational potential surface
│ └── ui.py # User interface and controls
├── data/
│ └── solar_params.json # Planetary parameters
- Python 3.7+
- NumPy
- Matplotlib
git clone https://github.com/your-username/solar_sim.git
cd solar_sim
pip install numpy matplotlib
### Setup
1. Clone or download the project files
2. Install dependencies:
```bash
pip install numpy matplotlib vpythonRun the full solar system 3d grid simulation:
python main.py
Another Render engine (web based) for a unique View:
python vpy_simulation.py
Run with a simple test system (Sun + Earth):
python main.py --test
Run headless (no visualization) for performance testing:
python main.py --headless --steps 1000When running the interactive visualization:
- Mouse: Click to select celestial bodies
- Mouse Drag: Rotate camera view
- Space: Pause/Resume simulation
- R: Reset camera to default position
- C: Clear orbital trails
- T: Toggle trail visibility
- S: Toggle gravitational potential surface
- F: Focus camera on selected body
- 1-8: Select planets by number (1=Mercury, 2=Venus, etc.)
- +/-: Increase/decrease simulation time scale
- Q: Quit simulation
Edit data/solar_params.json to modify planetary parameters:
{
"sun": {
"name": "Sun",
"mass": 1.0,
"radius": 0.00465,
"color": [1.0, 1.0, 0.0]
},
"planets": [
{
"name": "Earth",
"mass": 3.003e-6,
"radius": 4.26e-5,
"a": 1.000,
"e": 0.017,
"i": 0.000,
"Omega": -0.196,
"omega": 1.796,
"M": 0.0,
"color": [0.2, 0.4, 1.0]
}
]
}The simulation uses astronomical units for consistency:
- Length: Astronomical Units (AU)
- Time: Julian years (365.25 days)
- Mass: Solar masses (M☉)
- Gravitational constant: G = 4π² (AU³/yr²/M☉)
Uses velocity-Verlet integration for excellent long-term energy conservation:
- Update velocities by half-step:
v += 0.5 * a * dt - Update positions:
r += v * dt - Compute new accelerations
- Complete velocity update:
v += 0.5 * a_new * dt
Optional 1PN (first post-Newtonian) corrections capture general relativistic effects:
- Mercury's perihelion precession
- Time dilation effects
- Gravitational redshift corrections
Enable with ENABLE_1PN_DEFAULT = True in constants.py.
Run the test suite to verify correct operation:
python test_simulation.py
Tests include:
- Energy conservation verification
- Orbital element calculations
- Full system loading and simulation
- Visualization component initialization
Typical performance on modern hardware:
- 2-body system: ~30,000 steps/second
- 9-body solar system: ~3,000 steps/second
- Real-time visualization: ~20 FPS
The simulation achieves excellent accuracy for solar system dynamics:
- Energy conservation: < 10⁻⁵ relative error over 100 time steps
- Angular momentum conservation: < 10⁻⁹ relative error
- Orbital period accuracy: < 0.1% for major planets
- Add parameters to
data/solar_params.json - Optionally create a specific module in
planets/ - Restart the simulation
- Time step: Adjust
DTinconstants.py - Softening: Modify
EPS_ACCELfor collision handling - Relativistic effects: Toggle
ENABLE_1PN_DEFAULT
- Trail length: Adjust
TRAIL_DECIMATEinconstants.py - Surface resolution: Modify
GRID_COARSE_NandGRID_FOCUS_N - Visual scaling: Change
VISUAL_RADIUS_SCALEfor body sizes
🛠️ Contributing Contributions are welcome! Fork this repository Create a feature branch (git checkout -b feature-name) Commit changes (git commit -m "Add feature name") Push to branch (git push origin feature-name) Open a Pull Request 🎉
- Point masses: Bodies are treated as point masses (no rotation, tides)
- No collisions: Bodies can pass through each other
- No moons: Currently only includes major planets
- Matplotlib rendering: Limited to basic 3D visualization
- OpenGL-based high-performance rendering
- Collision detection and merging
- Moon and asteroid support
- Variable time-stepping for close encounters
- Export capabilities (animations, data)
-
Wisdom, J. & Holman, M. (1991). Symplectic maps for the n-body problem. Astronomical Journal, 102, 1528-1538.
-
Will, C. M. (2014). Theory and Experiment in Gravitational Physics. Cambridge University Press.
-
Murray, C. D. & Dermott, S. F. (1999). Solar System Dynamics. Cambridge University Press.
This project was sparked by a LinkedIn post that truly enlightened me:t Dhrubajyoti Hazarika post
Additionally, I owe a lot to a brilliant YouTuber whose video helped me understand how to integrate physics and mathematics into code. My prior knowledge of both Python and C allowed me to absorb his explanations more effectively and apply them to my project: Kavan's YT video
This project is provided as-is for educational and research purposes. Licence