A Python solver for the Lane–Emden equation of stellar structure, tested against the known analytic solutions, together with notes on the solution families I studied as an undergraduate.
A self-gravitating gas sphere in hydrostatic equilibrium with a polytropic equation of state
where
In my final undergraduate year (2023–24) I worked on this equation with Prof. Yu-Qing Lou (Department of Physics, Tsinghua University). Using MATLAB ODE solvers and Simpson quadrature, I explored the solution families at
This repository is a clean re-implementation in Python, written in 2026 to revisit the problem with reproducible, tested code.
- Solves regular polytropes with a series expansion at the centre, SciPy's DOP853 integrator (rtol 1e-10, atol 1e-12) and an event that stops at the surface.
- Computes the enclosed mass two independent ways, from
$-\xi^2\theta'$ and by Simpson quadrature of$\xi^2\theta^n$ , and checks that they agree. - Maps the equation for
$n > 1$ to an autonomous system in$t = \ln\xi$ and shows that$n = 5$ has a conserved energy, which explains the double-lobed phase portrait (docs/model.md).
| surface |
mass |
slope vs quadrature | |
|---|---|---|---|
| 1 | 3.14159 ( |
3.14159 | 8e-11 |
| 3.5 | 9.53581 | 1.89056 | 4e-14 |
| 4 | 14.97155 | 1.79723 | 1e-13 |
| 4.5 | 31.83646 | 1.73780 | 9e-14 |
| 5 | no surface ( |
1.72719 at |
8e-14 |
Against the analytic solutions for
Requires Python 3.10+.
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python -m unittest discover -s tests -v # analytic, mass and invariant checks
python -m lane_emden # regenerate results/ and the figures
python -m lane_emden --output /tmp/run # or write somewhere elseCI runs the tests and the full regeneration on every push.
| Path | Contents |
|---|---|
lane_emden/model.py |
radial solver, mass diagnostics, phase-space vector field and energy |
lane_emden/__main__.py |
regenerates every CSV and figure in results/
|
tests/test_model.py |
analytic profiles, surface positions, mass quadrature, |
docs/theory.md |
derivation of the Lane–Emden equation and the mass relation |
docs/model.md |
the autonomous transformation, fixed points and the |
docs/undergraduate-research.md |
the 2023–24 project and its numerical results |
data/historical/ |
values from the 2023 progress report, kept separate from new results |
Code: MIT (see LICENSE).

