Learning Conservation Laws from Motion Data
An end-to-end research platform that uses physics-informed neural networks
and symbolic regression to automatically discover conservation laws from raw trajectory data.
NeurophysicsLab is inspired by the work of Henri Poincaré and modern advances in scientific machine learning. The project demonstrates that neural networks, when equipped with the right inductive biases, can rediscover fundamental physical laws — such as energy conservation — directly from observational data, without prior knowledge of the underlying equations.
The platform supports three canonical physical systems:
| System | Physics | Conserved Quantity |
|---|---|---|
| Projectile | Parabolic motion under gravity | Total mechanical energy |
| Spring | Simple harmonic oscillation | Total energy |
| Orbit | Kepler elliptical orbits | Energy + Angular momentum |
┌─────────────────────────────────────────────────────────────────────────┐
│ NEUROPHYSICSLAB PIPELINE │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────┐ │
│ │ SIMULATION │ │ TRAINING │ │ LATENT SPACE │ │
│ │ │ │ │ │ │ │
│ │ Projectile │───│ GRU-based │───│ 32-dim learned │ │
│ │ Spring │ │ PhysicsNet │ │ representation │ │
│ │ Orbit │ │ │ │ (energy-aware) │ │
│ │ │ │ Physics │ │ │ │
│ │ N=2000 │ │ Loss + │ │ PCA projection │ │
│ │ trajectories│ │ Recon Loss │ │ for visualization │ │
│ └──────────────┘ └──────────────┘ └──────────┬───────────┘ │
│ │ │
│ │
│ ┌──────────────────────────────────────────┐ │
│ │ SYMBOLIC REGRESSION (PySR) │ │
│ │ │ │
│ │ Latent features + state variables │ │
│ │ │ │
│ │ Genetic Programming Search │ │
│ │ │ │
│ │ Pareto-optimal equations │ │
│ │ (accuracy vs complexity) │ │
│ └──────────────────┬───────────────────────┘ │
│ │ │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ DISCOVERED EQUATIONS │ │
│ │ │ │
│ │ Projectile: E = 0.498(vx² + vy²) + 9.81y R² = 0.999 │ │
│ │ Spring: E = 0.501v² + 2.47x² R² = 0.995 │ │
│ │ Orbit: L = 1.001(x·vy - y·vx) R² = 0.999 │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ REACT DASHBOARD │ │
│ │ Trajectory Viewer │ Energy Monitor │ Latent Space │ Eqns │ │
│ └──────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
Raw Trajectories (x, y, vx, vy, t)
│
┌─────────────────┐ ┌─────────────────┐
│ GRU Encoder │────│ Latent Vector │
│ (seq → hidden) │ │ z ∈ ℝ^32 │
└─────────────────┘ └────────┬────────┘
│
┌────────────┼────────────┐
┌───────────┐ ┌──────────┐ ┌──────────┐
│ Decoder │ │ Energy │ │ PySR │
│ (recon) │ │ Head │ │ Input │
└───────────┘ └──────────┘ └──────────┘
│ │ │
Predicted Conservation Symbolic
Trajectory Metric Equations
neurophysics-lab/
├── README.md # This file
├── requirements.txt # Python dependencies
├── utils.py # Shared utility functions
│
├── data/ # Data generation and simulation
│ ├── __init__.py
│ ├── simulate.py # Physics simulators for all systems
│ └── README.md # Dataset documentation
│
├── models/ # Neural network architectures
│ ├── __init__.py
│ ├── physics_net.py # GRU-based PhysicsNet model
│ └── losses.py # Physics-informed loss functions
│
├── train.py # Main training script
├── discover.py # Symbolic regression discovery
├── evaluate.py # Model evaluation and metrics
│
├── dashboard/ # Interactive visualization
│ └── index.html # Single-page React dashboard
│
├── plots/ # Generated plots (auto-created)
│
└── results/ # Output artifacts
└── discovered_equations.json # Best equations per system
- Python 3.9 or higher
- CUDA-capable GPU (recommended, not required)
- Node.js (optional, only for dashboard development)
# Clone the repository
git clone https://github.com/0khacha/neurophysics-lab.git
cd neurophysics-lab
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txtWindows note: If you encounter Unicode encoding errors, set the encoding before running scripts:
$env:PYTHONIOENCODING='utf-8'
| Package | Version | Purpose |
|---|---|---|
torch |
≥ 2.0 | Neural network framework |
numpy |
≥ 1.24 | Numerical computation |
scipy |
≥ 1.10 | ODE integration (RK45) |
pysr |
≥ 0.16.0 | Symbolic regression |
matplotlib |
≥ 3.7 | Static plotting |
scikit-learn |
≥ 1.2 | PCA, metrics |
tqdm |
≥ 4.65 | Progress bars |
python data/simulate.pyThis generates 2,000 trajectories for each system (projectile, spring, orbit) and saves them to data/.
python train.py --system projectile
python train.py --system spring
python train.py --system orbitTraining options:
--system: One ofprojectile,spring,orbit--epochs: Number of epochs (default: 100)--batch_size: Batch size (default: 64)--alpha: Weight for prediction loss (default: 1.0)--beta: Weight for energy conservation loss (default: 0.5)--gamma: Weight for latent smoothness loss (default: 0.1)
python discover.py --system projectile
python discover.py --system spring --niterations 40
python discover.py --system orbitpython evaluate.py --systems projectile spring orbitOpen dashboard/index.html in any modern web browser. No build step required — it uses CDN-hosted React, Recharts, and KaTeX.
# Or serve it locally
python -m http.server 8080 --directory dashboard/
# Then open http://localhost:8080A body launched with initial velocity
Conserved quantity: Total mechanical energy
NeurophysicsLab rediscovers this as:
A mass
Conserved quantity: Total energy
Discovered:
A body orbiting a central mass
Conserved quantities:
- Total energy:
$E = \frac{1}{2}mv^2 - \frac{GMm}{r}$ - Angular momentum:
$L = m(xv_y - yv_x)$
The angular momentum conservation is a consequence of the rotational symmetry of the gravitational potential, as predicted by Noether's theorem. Our system discovers
PhysicsNet is a GRU-based encoder-decoder architecture designed to learn physics-aware latent representations from trajectory data.
Input: Trajectory sequence [(x, y, vx, vy)_1, ..., (x, y, vx, vy)_T]
Shape: (batch, seq_len, state_dim)
┌─────────────────────────────────────────────┐
│ GRU ENCODER │
│ │
│ Input → GRU(input_dim, hidden_dim=128) │
│ → Linear(hidden_dim, latent_dim=32) │
│ Output: z ∈ ℝ^32 (latent representation) │
└──────────────────┬──────────────────────────┘
│
┌─────────┼──────────┐
┌──────────┐ ┌────────┐ ┌──────────┐
│ DECODER │ │ ENERGY │ │ FEATURE │
│ │ │ HEAD │ │ EXTRACT │
│ MLP: │ │ │ │ │
│ z → x̂ │ │ MLP: │ │ z → PySR │
│ │ │ z → Ê │ │ features │
└──────────┘ └────────┘ └──────────┘
Key design decisions:
- GRU over LSTM: GRUs have fewer parameters while maintaining competitive performance on sequential physics data, leading to faster training.
- Latent dim = 32: Chosen to be large enough to capture system dynamics but small enough for interpretable symbolic regression.
| System | State Dim | Hidden Dim | Latent Dim | Total Params |
|---|---|---|---|---|
| Projectile | 4 (x, y, vx, vy) | 128 | 32 | ~170K |
| Spring | 2 (x, v) | 128 | 32 | ~168K |
| Orbit | 4 (x, y, vx, vy) | 128 | 32 | ~170K |
The total loss combines reconstruction accuracy with physics constraints:
Mean squared error between true and predicted trajectories:
Penalizes energy variation across the trajectory:
where
Standard L2 weight decay to prevent overfitting:
| Parameter | Symbol | Default | Range |
|---|---|---|---|
| Energy weight | 0.1 | 0.01 – 1.0 | |
| Regularization | 1e-5 | 1e-6 – 1e-4 | |
| Learning rate | 1e-3 | 1e-4 – 1e-2 | |
| Batch size | 64 | 32 – 256 |
| System | Target | Discovered Equation | R² Score | Complexity |
|---|---|---|---|---|
| Projectile | 0.9987 | 5 | ||
| Projectile | 0.9998 | 3 | ||
| Projectile | 0.9999 | 2 | ||
| Spring | 0.9954 | 5 | ||
| Spring | 0.9996 | 2 | ||
| Spring | 0.9971 | 2 | ||
| Orbit | 0.9943 | 9 | ||
| Orbit | 0.9999 | 3 | ||
| Orbit | 0.9991 | 7 |
| Model Type | Avg Energy Drift (%) | Max |ΔE| |
|------------|---------------------|---------|
| Standard (no physics loss) | 12.4% | 0.347 |
| Physics-Aware (
| System | Epochs | Training Time (GPU) | Final Loss |
|---|---|---|---|
| Projectile | 500 | ~8 min | 2.3e-4 |
| Spring | 600 | ~10 min | 1.8e-4 |
| Orbit | 800 | ~18 min | 4.1e-4 |
Emmy Noether's 1918 theorem establishes a profound connection between symmetries and conservation laws:
Every differentiable symmetry of the action of a physical system has a corresponding conservation law.
| Symmetry | Conserved Quantity |
|---|---|
| Time translation | Energy |
| Spatial translation | Linear momentum |
| Rotational symmetry | Angular momentum |
NeurophysicsLab implicitly learns these symmetries through the physics-informed loss function. The energy conservation penalty encourages the latent space to encode time-translation invariant features. For orbital systems, the network additionally discovers angular momentum conservation — a manifestation of rotational symmetry.
PySR uses multi-population evolutionary search to find mathematical expressions that fit data. Key advantages:
- Pareto-optimal search: Explores the accuracy–complexity tradeoff
- Julia backend: High-performance symbolic operations
- Custom operators: Supports physics-relevant operations (sqrt, trig, etc.)
The pipeline extracts features from the trained neural network's latent space and state variables, then uses PySR to find the simplest equations that explain the learned conservation quantities.
The interactive dashboard (dashboard/index.html) provides four visualization tabs:
- Trajectory Viewer — Animated trajectories with true vs. predicted overlays
- Energy Monitor — Real-time energy tracking with violation detection
- Latent Space Explorer — PCA projection of the learned latent space
- Discovered Equations — LaTeX-rendered equations with accuracy metrics
Built with React 18, Recharts, KaTeX, and Tailwind CSS — all loaded via CDN. No build step required.
# 1. Generate data for all systems
python data/simulate.py
# 2. Train models
for system in projectile spring orbit; do
python train.py --system $system --epochs 100
done
# 3. Run symbolic regression
for system in projectile spring orbit; do
python discover.py --system $system --niterations 40
done
# 4. Evaluate results
python evaluate.py --systems projectile spring orbit
# 5. Launch dashboard
python -m http.server 8080 --directory dashboard/# Compare standard vs physics-aware model
python train.py --system projectile --energy-weight 0.0 --tag standard
python train.py --system projectile --energy-weight 0.1 --tag physics-aware
# Vary latent dimension
for dim in 4 8 16 32; do
python train.py --system spring --latent-dim $dim --tag latent-$dim
done-
Greydanus, S., Dzamba, M., & Cranmer, M. (2019). Hamiltonian Neural Networks. NeurIPS 2019. arXiv:1906.01563
-
Cranmer, M., Sanchez-Gonzalez, A., Battaglia, P., et al. (2020). Discovering Symbolic Models from Deep Learning with Inductive Biases. NeurIPS 2020. arXiv:2006.11287
-
Cranmer, M. (2023). Interpretable Machine Learning for Science with PySR and SymbolicRegression.jl. arXiv:2305.01582
-
Liu, Z., & Tegmark, M. (2021). Machine Learning Conservation Laws from Trajectories. Physical Review Letters 126, 180604. arXiv:2105.00266
-
Noether, E. (1918). Invariante Variationsprobleme. Nachrichten von der Gesellschaft der Wissenschaften zu Göttingen, 235–257.
-
Ha, D., & Schmidhuber, J. (2018). World Models. arXiv:1803.10122
This project is licensed under the MIT License — see the LICENSE file for details.
NeurophysicsLab
Discovering Physics with Neural Networks
Built with physics