This notebook demonstrates some SageMath tools for pseudo-Riemannian geometry, developed through the SageManifolds project, by these authors.
This notebook requires a version of SageMath at least equal to 9.0:
version()
'SageMath version 9.8.beta4, Release Date: 2022-11-21'
First we set up the notebook to display outputs via LaTeX rendering:
%display latex
Since some computations are quite heavy, we ask for running them in parallel on 8 threads:
Parallelism().set(nproc=8)
We introduce the Schwarzschild spacetime, which is the spacetime of a static black hole in general relativity, as a 4-dimensional Lorentzian manifold $M$:
M = Manifold(4, 'M', structure='Lorentzian')
M
print(M)
4-dimensional Lorentzian manifold M
$M$ is in the category of smooth manifolds over the real field:
M.category()
M.base_field()
At the moment, the real field is modeled by 53-bit floating-point approximations, but this plays no role in the manifold implementation:
print(M.base_field())
Real Field with 53 bits of precision
The function Manifold
generates a manifold with no-predefined coordinate chart, so that the manifold (user) atlas is empty:
M.atlas()
We introduce the standard Schwarzchild-Droste coordinates $(t,r,\theta,\phi)$ on $M$, via the method chart
:
SD.<t, r, th, ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi:periodic")
Note that the argument of chart()
is a raw string (hence the prefix r
in front of it), which defines the range of each coordinate, if different from $(-\infty, +\infty)$, as well as its LaTeX symbol, if different from the Python symbol to denote the coordinate. The Python variables for each coordinate are declared within the <...>
operator on the left-hand side, SD
denoting the Python variable chosen for the coordinate chart.
SD
print(SD)
Chart (M, (t, r, th, ph))
SD.coord_range()
Thanks to the SageMath operator <...>
used in the chart declaration, the coordinates are immediately available:
th
They are SageMath's symbolic expressions:
th.parent()
They are also accessible as items of the chart:
SD[0], SD[3]
SD[:]
The manifold (user) atlas is no longer empty:
M.atlas()
Let us introduce a second chart on the manifold, that of Eddington-Finkelstein coordinates $(T,r,\theta,\phi)$:
EF.<T, r, th, ph> = M.chart(r"T r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi:periodic")
EF
The transition map from Schwarzschild-Droste coordinates (chart SD
) to Eddington-Finkelstein ones (chart EF
) depends on a parameter $m$, the mass of the Schwarzschild black hole:
m = var('m')
assume(m > 0)
We provide the explicit coordinate transformation via the method transition_map
:
SD_to_EF = SD.transition_map(EF, [t +2*m*ln(abs(r/(2*m)-1)), r, th, ph])
SD_to_EF.display()
SD_to_EF.inverse().display()
There are now two charts in the manifold atlas:
M.atlas()
One of them is the so-called default chart: it is the chart used by any function that requires a chart as argument and none is provided by the user. At this stage, the default chart is the first chart defined on the manifold, but this can be changed by the manifold method set_default_chart
.
M.default_chart()
One can plot the SD
chart in terms of EF
one:
plot1 = SD.plot(EF, ranges={t:(0, 8), r:(2.1, 10)}, fixed_coords={th:pi/2, ph:0},
ambient_coords=(r,T), style={t:'--', r:'-'}, parameters={m: 1}) \
+ SD.plot(EF, ranges={t:(0, 8), r:(0.1, 1.9)}, fixed_coords={th:pi/2, ph:0},
ambient_coords=(r,T), number_values={t: 9, r: 3},
style={t:'--', r:'-'}, parameters={m: 1})
plot1
To create a point on $M$, we use SageMath's parent/element syntax, i.e. the call operator M(...)
acting on the parent M
, with the point's coordinates in some chart as argument:
p = M((m, 8*m, pi/2, 0), name='p')
print(p)
Point p on the 4-dimensional Lorentzian manifold M
Since the chart has not been specified, the default chart (i.e. SD
) is meant:
SD(p)
Thanks to the transition map declared above, the coordinates of $p$ in the Eddington-Finkelstein chart can computed:
EF(p)
Manifold points have a plot
method:
plot1 += p.plot(EF, color='blue', ambient_coords=(r,T),
parameters={m: 1}, label_offset=0.4, fontsize=14)
plot1
When a chart is declared, the manifold is automatically endowed with some vector fields, those of the coordinate vector frame:
SD.frame()
EF.frame()
M.frames()
As for charts, there is a default frame, i.e. a vector frame that is used by default in functions having a vector frame in their arguments. The default frame can be changed by the method set_default_frame
.
M.default_frame()
The first vector of the Schwarzschild-Droste coordinate frame:
vt = SD.frame()[0]
vt
vt.display()
vt.display(EF.frame())
The second vector of the Schwarzschild-Droste coordinate frame:
vr = SD.frame()[1]
vr
vr.display(EF.frame())
Creating a vector field from scratch, by providing its components with rest to a given vector frame:
k = M.vector_field(1, -1, 0, 0, frame=EF.frame(), name='k')
k.display()
k.display(EF.frame())
Plot with respect to Schwarzschild-Droste coordinates (default chart):
k.plot(ambient_coords=(r,t), fixed_coords={th: pi/2, ph: 0},
ranges={t: (0, 6), r: (0.1, 6)}, number_values=9,
parameters={m: 1}, color='green', scale=0.3)
Plot with respect to Eddington-Finkelstein coordinates:
k.plot(chart=EF, ambient_coords=(r,T), chart_domain=EF,
fixed_coords={th: pi/2, ph: 0}, ranges={T: (0, 8), r: (0.1, 10)},
number_values=5, parameters={m: 1}, color='green')
TM = M.tangent_bundle()
print(TM)
Tangent bundle TM over the 4-dimensional Lorentzian manifold M
k1 = TM.section({EF.frame(): [1, -1, 0, 0]})
print(k1)
Vector field on the 4-dimensional Lorentzian manifold M
k1 == k
XM = M.vector_field_module()
print(XM)
XM
Free module X(M) of vector fields on the 4-dimensional Lorentzian manifold M
XM.base_ring()
print(XM.base_ring())
Algebra of differentiable scalar fields on the 4-dimensional Lorentzian manifold M
XM.base_ring().an_element().display()
The value of a vector field, and more generally of any tensor field, at a given point is obtained by the method at
:
kp = k.at(p)
print(kp)
Tangent vector k at Point p on the 4-dimensional Lorentzian manifold M
kp.display()
The parent of kp
is the tangent space at p
:
kp.parent()
print(kp.parent())
Tangent space at Point p on the 4-dimensional Lorentzian manifold M
It is accessible from the manifold via the method tangent_space
:
kp.parent() is M.tangent_space(p)
$T_p M$ is the fiber over $p$ in the tangent bundle $TM$:
kp.parent() is TM.fiber(p)
Tangent vectors have a method plot
:
plot1 += kp.plot(EF, color='green', ambient_coords=(r,T),
parameters={m: 1}, scale=2, label_offset=0.5,
fontsize=16)
plot1
Let us introduce the exterior $E$ of the black hole as an open subset of $M$:
E = M.open_subset('E', coord_def = {SD: r>2*m})
SD.restrict(E).coord_range()
p in E
u = E.vector_field(name='u')
u[0] = 1/sqrt(1-2*m/r)
u.display()
SD_to_EF.restrict(E)
E.atlas()
u.display(EF.frame().restrict(E))
u.plot(ambient_coords=(r, t), fixed_coords={th: pi/2, ph: 0},
ranges={t: (0, 6), r: (2.1, 6)}, number_values=5,
parameters={m: 1}, scale=0.3)
u.at(p).display()
We define next the metric tensor $g$ from its non-vanishing components in the manifold's default frame, namely the coordinate frame associated to Schwarzschild-Droste coordinate:
g = M.metric()
g[0, 0] = - (1 - 2*m/r)
g[1, 1] = 1/(1 - 2*m/r)
g[2, 2] = r^2
g[3, 3] = r^2*sin(th)^2
g.display()
SD.coframe()
g[:]
g[1,1]
$g_{rr}$ is diverging at $r=2m$: this is a singularity of the Schwarszchild-Droste coordinates.
g.display_comp()
The components of $g$ with respect to the Eddington-Finkelstein frame are evaluated via the methods comp
or display
:
g.display(EF.frame())
g[EF.frame(),:]
Note that these components are regular at $r=2m$, contrary to the components in Schwarzschild-Droste coordinates.
The metric tensor is a twice-covariant tensor (actually a field of symmetric bilinear forms):
g.tensor_type()
It can be thus be applied to a pair of vector fields, for instance $u$ and $k$:
s = g(u, k)
s
print(s)
Scalar field g(u,k) on the Open subset E of the 4-dimensional Lorentzian manifold M
s.display()
s.expr()
g(k, k).display()
$u$ is a unit timelike vector:
g(u, u).display()
Scalar products returned by the method dot
are actually those formed with $g$:
u.dot(u)
u.dot(u).display()
nabla = g.connection()
print(nabla)
Levi-Civita connection nabla_g associated with the Lorentzian metric g on the 4-dimensional Lorentzian manifold M
nabla.display()
Dk = nabla(k)
Dk
print(Dk)
Tensor field nabla_g(k) of type (1,1) on the 4-dimensional Lorentzian manifold M
Dk.display()
Dk.display_comp()
The acceleration $\nabla_k k$ of the vector field $k$:
Ak = Dk.contract(k)
print(Ak)
Vector field on the 4-dimensional Lorentzian manifold M
Ak.display()
$k$ is thus geodesic vector. The field lines of $k$ are actually the ingoing radial null geodesics of Schwarzschild spacetime.
Instead of the method contract, one can use index notations to compute $\nabla_k k$ as $k^b \nabla_b k^a$:
Ak == Dk['^a_b']*k['^b']
The acceleration $\nabla_u u$ of the vector field $u$:
Au = nabla(u).contract(u)
Au.display()
$\nabla g$ is identically zero, since the connection $\nabla$ is compatible with $g$:
Dg = nabla(g)
print(Dg)
Tensor field nabla_g(g) of type (0,3) on the 4-dimensional Lorentzian manifold M
Dg.display()
The Riemann curvature tensor is computed as
Riem = g.riemann()
print(Riem)
Tensor field Riem(g) of type (1,3) on the 4-dimensional Lorentzian manifold M
Riem.display_comp(only_nonredundant=True)
The component $\mathrm{Riem}(g)^t_{\ \, rtr} = \mathrm{Riem}(g)^0_{\ \, 101}$ is returned by
Riem[0,1,0,1]
Riem.display_comp(EF.frame(), only_nonredundant=True)
The Kretschmann scalar is the "square" of the Riemann tensor defined by
$$K = R_{abcd} \, R^{abcd}, \qquad R := \mathrm{Riem}(g)$$
To compute it, we must first form the tensor fields whose components are $R_{abcd}$ and
$R^{abcd}$. They are obtained by respectively lowering and raising the indices of the components $R^a_{\ \, bcd}$ of the Riemann tensor, via the metric $g$. These two operations are performed by the methods down()
and up()
. The contraction is performed by summation on repeated indices, using LaTeX notations:
K = Riem.down(g)['_{abcd}'] * Riem.up(g)['^{abcd}']
print(K)
K.display()
Scalar field on the 4-dimensional Lorentzian manifold M
K.expr()
Since $\lim_{r\to 0} K = +\infty$, we may say that $r=0$ is a curvature singularity of Schwarzschild spacetime.
Ric = g.ricci()
print(Ric)
Field of symmetric bilinear forms Ric(g) on the 4-dimensional Lorentzian manifold M
We check that the Schwarzschild metric is a solution of the vacuum Einstein equation:
Ric.display()
First, for graphical purposes, we introduce the Euclidean space $\mathbb{E}^3$ and some map $M\to \mathbb{E}^3$:
E3.<x,y,z> = EuclideanSpace()
X3 = E3.cartesian_coordinates()
to_E3 = M.diff_map(E3, {(SD, X3):
[r*sin(th)*cos(ph), r*sin(th)*sin(ph), r*cos(th)]})
to_E3.display()
Let us consider the geodesic starting at point $p$ and having the following tangent vector at $p$ (note that the tangent vector $v_0$ is constructed by means of the call operator ()
acting of the parent, which is the tangent space to $M$ at $p$):
v0 = M.tangent_space(p)((1.3, 0, 0, 0.064/m), name='v_0')
v0.display()
We declare the geodesic with such initial conditions, denoting by $s$ the affine parameter (proper time), with $(s_{\rm min}, s_{\rm max})=(0, 1500\,m)$:
s = var('s')
geod = M.integrated_geodesic(g, (s, 0, 2000), v0)
geod
Note that the initial point p
is not explicitely passed in the argument list of integrated_geodesic
, because this piece of information is contained in v0
:
p is v0.parent().base_point()
sol = geod.solve(parameters_values={m: 1}) # numerical integration
interp = geod.interpolate() # interpolation of the solution for the plot
geod.plot_integrated(chart=X3, mapping=to_E3, plot_points=1000,
thickness=2, label_axes=False) \
+ p.plot(chart=X3, mapping=to_E3, size=4, parameters={m: 1}) \
+ sphere(size=2, color='grey')
A 2D view by suppressing $z$ from the ambient coordinates:
bh_plot = circle((0, 0), 2, edgecolor='black', fill=True, facecolor='grey', alpha=0.5)
geod.plot_integrated(chart=X3, mapping=to_E3, ambient_coords=(x,y), plot_points=1000,
thickness=2) \
+ p.plot(chart=X3, mapping=to_E3, ambient_coords=(x,y), size=4, parameters={m: 1}) \
+ bh_plot
Let us consider a null geodesique $\mathscr{L}$ in the equatorial plane ($\theta = \pi/2$). The null vector $v$ tangent to $\mathscr{L}$ and associated to some affine parameter $\lambda$ is given by the following first integrals of the geodesic equation: $$ v^t = \frac{\mathrm{d}t}{\mathrm{d}\lambda} = \left(1 - \frac{2m}{r} \right)^{-1} $$ $$ v^r = \frac{\mathrm{d}r}{\mathrm{d}\lambda} = \pm \sqrt{1 - \frac{b^2}{r^2} \left(1 - \frac{2m}{r} \right) } $$ $$ v^\theta = \frac{\mathrm{d}\theta}{\mathrm{d}\lambda} = 0 $$ $$ v^\varphi = \frac{\mathrm{d}\varphi}{\mathrm{d}\lambda} = \frac{b}{r^2} $$ where the constant $b$ is related to the conserved energy $E$ and conserved angular momentum $L$ along the geodesic by $$ b = \frac{L}{E} . $$ For a geodesic arising from infinity, $b$ is the impact parameter.
To set up the initial vector for the computation of a null geodesic, let us define a function that takes $b$ and some initial radius $r_0$ as input and returns the initial vector $v_0$. We take advantage that SageMath is built atop Python to construct this function as a pure Python function:
def initial_vector(r0, b, phi0=0, inward=True):
r"""
Evaluate the initial tangent vector along a null geodesic.
INPUT:
- r0: radial SD coordinate of the initial point
- b: impact parameter
- phi0: azimuthal SD coordinate of the initial point (default: 0)
- inward: determines whether the geodesic has initially v^r < 0 (default: True)
"""
vt0 = 1/(1 - 2*m/r0)
vr0 = sqrt(1 - b^2/r0^2*(1 - 2*m/r0))
if inward:
vr0 = - vr0
vth0 = 0
vph0 = b / r0^2
p0 = M((0, r0, pi/2, phi0), name='p_0') # initial point
return M.tangent_space(p0)((vt0, vr0, vth0, vph0), name='v_0')
Let us use this function to construct the initial vector for $r_0 = 10m$ and $b=7m$:
v0 = initial_vector(10*m, 7*m)
v0.display()
Let us check that $v_0$ is a null vector:
p0 = v0.parent().base_point()
g.at(p0)(v0, v0)
geod = M.integrated_geodesic(g, (s, 0, 40), v0)
sol = geod.solve(step=0.01, parameters_values={m: 1})
interp = geod.interpolate()
plot2 = geod.plot_integrated(chart=X3, mapping=to_E3, ambient_coords=(x,y),
plot_points=500, color='green', thickness=1.5, display_tangent=True,
color_tangent='green', plot_points_tangent=4, scale=1)
plot2 += bh_plot
plot2
A null geodesic plunging into the black hole:
v0 = initial_vector(10*m, 5*m)
v0.display()
geod = M.integrated_geodesic(g, (s, 0, 13), v0)
sol = geod.solve(step=0.01, parameters_values={m: 1})
interp = geod.interpolate()
plot2 += geod.plot_integrated(chart=X3, mapping=to_E3, ambient_coords=(x,y),
plot_points=500, color='green', thickness=1.5, display_tangent=True,
color_tangent='green', plot_points_tangent=3, scale=0.2)
plot2
The photon orbit corresponds to $r_0=3m$ with the following critical value of the impact parameter: $$ b_{\rm c} = 3\sqrt{3}\, m $$
bc = 3*sqrt(3)*m
n(bc/m)
v0 = initial_vector(3*m, bc)
v0.display()
geod = M.integrated_geodesic(g, (s, 0, 100), v0)
sol = geod.solve(step=0.01, parameters_values={m: 1})
interp = geod.interpolate()
plot2 += geod.plot_integrated(chart=X3, mapping=to_E3, ambient_coords=(x,y),
plot_points=500, color='green', thickness=1.5)
plot2
A geodesic with $b$ close to $b_{\rm c}$ wraps around the circular orbit:
v0 = initial_vector(10*m, 5.2025*m)
geod = M.integrated_geodesic(g, (s, 0, 40), v0)
sol = geod.solve(step=0.01, parameters_values={m: 1})
interp = geod.interpolate()
plot2 += geod.plot_integrated(chart=X3, mapping=to_E3, ambient_coords=(x,y),
plot_points=500, color='orange', thickness=1.5,
display_tangent=True, color_tangent='orange',
plot_points_tangent=4, scale=1)
plot2
By default, the symbolic backend used in tensor calculus is SageMath's one (Pynac + Maxima), implemented via the symbolic ring SR
. We can choose to use SymPy instead:
M.set_calculus_method('sympy')
v = 2*k
v.display()
v[0]
v[0].expr()
type(v[0].expr())
M.set_calculus_method('SR')
type(v[0].expr())
Visit the SageManifolds examples and take a look at the video tutorials.