Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lane-Emden self-gravitating spheres

The question this method answers

Given a self-gravitating sphere in hydrostatic equilibrium with a prescribed equation of state, what dimensionless density and enclosed-mass profile does that equilibrium imply, and where is its edge? Jaxstro solves the Lane-Emden equation in two branches: the polytropic branch (a finite polytrope of index nn, whose surface is the first zero of the solution) and the isothermal Bonnor-Ebert branch (no intrinsic edge -- the truncation radius is a genuine physical input set by the confining external pressure). Both branches are differentiable, which is what lets them feed gradient-based inference and hydrostatic initial conditions downstream.

Before computation: what should be true?

The model must be a spherically symmetric, self-gravitating equilibrium whose pressure-density relation is either a polytrope P=KργP=K\rho^{\gamma} (with n=1/(γ1)n=1/(\gamma-1)) or isothermal PρP\propto\rho. Choose xi_max and n_points knowing they are static: they size the output grid, not the physics. For a polytrope, xi_max should not exceed the surface ξ1\xi_1 if you intend to differentiate (see below). These ordinary-differential solves are singular at the origin, so the integration starts just off it at XI_0 = 1e-6.

Scientific state representations and fixed-shape PyTrees are connected in PyTrees as scientific state, and the model-to-program framing is in From mathematical relations to differentiable programs.

Define the mathematical objects

Hydrostatic equilibrium plus Poisson’s equation for a self-gravitating sphere, nondimensionalized with a density scale ρc\rho_c (the central density) and a length scale, collapses to a single second-order ordinary differential equation in the dimensionless radius ξ\xi. The dependent variable is ψ(ξ)\psi(\xi) (isothermal, with ρ=ρceψ\rho=\rho_c e^{-\psi}) or θ(ξ)\theta(\xi) (polytropic, with ρ=ρcθn\rho=\rho_c\theta^{n}). The dimensionless enclosed mass m(ξ)m(\xi) is the radial integral of 4πr2ρ4\pi r^2\rho in scaled variables. The polytropic index nn is the one traced, differentiable parameter; ξ1\xi_1, the first zero of θ\theta, is the polytrope’s surface and exists (is finite) only for n<5n<5.

Derive the method

The isothermal (Bonnor-Ebert) branch writes the scaled Poisson equation with an exponential source and boundary conditions at the center:

ψ+2ξψ=eψ,ψ(0)=0,ψ(0)=0,ρ=ρceψ.\psi'' + \frac{2}{\xi}\psi' = e^{-\psi}, \qquad \psi(0)=0,\quad \psi'(0)=0,\quad \rho=\rho_c\,e^{-\psi}.

The polytropic branch replaces the exponential source with the power law θn-\theta^{n} and normalizes the center to unity:

θ+2ξθ=θn,θ(0)=1,θ(0)=0,ρ=ρcθn.\theta'' + \frac{2}{\xi}\theta' = -\theta^{n}, \qquad \theta(0)=1,\quad \theta'(0)=0,\quad \rho=\rho_c\,\theta^{n}.

Integrating 4πr2ρ4\pi r^2\rho in scaled variables, and using each equation to eliminate the second derivative, gives the dimensionless enclosed mass in closed form on each branch, with a common cubic behavior near the origin:

m(ξ)=ξ2ψ (isothermal),m(ξ)=ξ2θ (polytropic),m(ξ)ξ33 as ξ0.m(\xi)=\xi^2\psi' \ \text{(isothermal)},\qquad m(\xi)=-\xi^2\theta' \ \text{(polytropic)},\qquad m(\xi)\to\frac{\xi^3}{3}\ \text{as}\ \xi\to 0.

Both right-hand sides carry a 2/ξ2/\xi term that is singular at ξ=0\xi=0, so the solve cannot start from the bare boundary condition. Substituting a Taylor expansion into each equation fixes the low-order coefficients and seeds the state at XI_0:

ψ(ξ)=ξ26ξ4120+O(ξ6),θ(ξ)=1ξ26+nξ4120+O(ξ6).\psi(\xi)=\frac{\xi^2}{6}-\frac{\xi^4}{120}+O(\xi^6), \qquad \theta(\xi)=1-\frac{\xi^2}{6}+\frac{n\,\xi^4}{120}+O(\xi^6).

The polytrope surface ξ1\xi_1 is the first root of θ\theta. It is located as a differentiable event root -- the implicit function theorem applied to the solver’s diffrax.Event -- rather than as a grid argmin, which would have no usable gradient in nn.

What the algorithm actually does

solve_isothermal and solve_polytrope build the corresponding first-order diffrax.ODETerm, seed the state at XI_0 from the series above, and integrate with adaptive Tsit5 under a PIDController(rtol=1e-8, atol=1e-10), saving on a linspace(XI_0, xi_max, n_points). Each returns a LaneEmdenSolution with fields xi, y (ψ\psi or θ\theta), dy, m, and dm, all length n_points. The enclosed-mass derivative dm is supplied analytically (ξ2eψ\xi^2 e^{-\psi} or ξ2θn\xi^2\theta^{n}), not by differencing. polytrope_xi1 runs the same term with a diffrax.Event whose condition is θ=0\theta=0, closed by an optimistix.Newton root finder, and returns the scalar edge. Past the first zero the polytropic source θn\theta^{n} is floored at zero (a non-integer power of a negative number is NaN); that floored continuation is not physical.

What JAX differentiates

jit, vmap, and grad are supported. The gradient in the polytropic index nn flows through the adaptive solve and through the polytrope_xi1 event root via the implicit function theorem, so dξ1/dn\mathrm{d}\xi_1/\mathrm{d}n is well defined. On the isothermal branch the truncation radius xi_max is a real physical input, and the edge mass is differentiable in it. xi_max and n_points are static and must not be differentiated.

Using it in Jaxstro

import jax

jax.config.update("jax_enable_x64", True)

import jax.numpy as jnp

from jaxstro.numerics.lane_emden import polytrope_xi1, solve_polytrope

# n = 1 has the closed-form solution theta = sin(xi)/xi with surface xi_1 = pi.
xi1 = polytrope_xi1(1.0)
assert abs(float(xi1) - jnp.pi) < 1e-4

solution = solve_polytrope(1.0, xi_max=float(jnp.pi), n_points=200)
assert solution.xi.shape == (200,)
assert solution.m.shape == (200,)

# The event root is differentiable in the polytropic index.
grad_xi1 = jax.grad(lambda n: polytrope_xi1(n))(1.5)
assert jnp.isfinite(grad_xi1)

n is a traced scalar and may be differentiated; xi_max and n_points size the output grid and are static. Enable the intended precision before creating arrays, since the adaptive tolerances are tight.

How to audit the result

Compare against the three closed-form polytropes: n=0n=0 gives θ=1ξ2/6\theta=1-\xi^2/6 with ξ1=6\xi_1=\sqrt{6}; n=1n=1 gives θ=sinξ/ξ\theta=\sin\xi/\xi with ξ1=π\xi_1=\pi; n=5n=5 gives θ=(1+ξ2/3)1/2\theta=(1+\xi^2/3)^{-1/2}, which never reaches zero (infinite extent). Check the origin behavior against the series ψ=ξ2/6ξ4/120\psi=\xi^2/6-\xi^4/120 and θ=1ξ2/6+nξ4/120\theta=1-\xi^2/6+n\xi^4/120, and confirm the solver’s ξ4\xi^4 leading error shrinks at the expected rate under grid or tolerance refinement. Verify the enclosed mass m(ξ)m(\xi) approaches ξ3/3\xi^3/3 near the origin and is monotone where the density is positive. Cross-check dξ1/dn\mathrm{d}\xi_1/\mathrm{d}n from the event root against a central finite difference. The executable audit map is in Validation methods.

Where the claim stops

Jaxstro solves the dimensionless equilibrium; it does not choose a physically adequate xi_max, restore dimensions, or model stability, rotation, magnetic support, or time evolution. Agreement at the closed-form indices validates the solver, not any particular astrophysical application of the resulting profile. The n5n\ge 5 regime and any xi_max beyond ξ1\xi_1 are outside the differentiable contract and must be handled by the caller.

Connected ideas