Solution (source code)

= Solution

Assemble the star by adding spherical shells. A shell of mass $dm$ at radius $r$ has <gravitational potential energy> $-Gm(r)\,dm/r$ relative to infinity, giving
$$
\boxed{E_{\mathrm{grav}}=-\int_0^R\frac{Gm(r)}r\,dm=-4\pi G\int_0^R m(r)\rho(r)r\,dr.}
$$
Multiplication of the <hydrostatic equilibrium> equation by $4\pi r^3$ and <integration by parts> gives
$$
E_{\mathrm{grav}}=\int_0^R4\pi r^3P'(r)dr=4\pi R^3P(R)-3\int_V P\,dV.
$$
For an isolated star with negligible surface pressure, $P(R)=0$. Using $E_{\mathrm{kin}}=(3/2)\int_V P\,dV$, the <virial theorem> becomes $\boxed{2E_{\mathrm{kin}}+E_{\mathrm{grav}}=0}$. If a nonzero external pressure is present, the surface term must be retained: $2E_{\mathrm{kin}}+E_{\mathrm{grav}}=3P(R)V$. Thus the printed zero-surface-term identity implicitly assumes an isolated star.