Skip to main content

General Relativity Symbolic Utilities

PyPI version PyPI downloads PyPI versions Build CodeFactor codecov License

The pystein package contains utilities for computing symbolic utilities for computing various quantities that arise in general relativity. Presently, this package is essentially a sympy extension that computes components of tensors directly.

Symbolic Tools

The pystein package makes use of sympy to compute symbolic curvature equations (EFE).

Example Computation: FLRW Cosmology

# Load the predefined FLRW metric
from pystein import metric, gravity
from pystein import utilities

flrw = metric.flrw().subs({'c': 1})
flrw

efe_00 = utilities.full_simplify(gravity.einstein_equation(0, 0, flrw))
efe_00

# Can simplify notation using "dots"
metric.simplify_deriv_notation(efe_00, flrw, use_dots=True)

Symbolic Tools

Coordinate Systems

from sympy.diffgeom import Manifold, Patch
from pystein import coords
# The pystein CoordinateSystem extends the sympy.diffgeom api to make parameters more accessible
M = Manifold('M', dim=2)
P = Patch('origin', M)
cs = coords.CoordSystem('cartesian', P, ['x', 'y'])
cs

# In sympy it is difficult to access underlying parameters, but the new base_symbols function makes it easy:
cs.base_symbols()

Metrics

# Assembling a metric is easy
from sympy import Array, symbols
from pystein import metric
from pystein.utilities import tensor_pow as tpow
# Metrics can be created either from a (Matrix, Coords) combo or from a TwoForm Expression
# Let's create a metric from a twoform expression, using the basis of oneforms from the coordinate system
a, b = symbols('a b')  # some constants to use in the metric
dx, dy = cs.base_oneforms()
form = a ** 2 * tpow(dx, 2) + b ** 2 * tpow(dy, 2)
g1 = metric.Metric(twoform=form)  # Note: don't have to specify coords since implied by basis of one-forms
# Notice that the Metric class will represent itself as a twoform
g1

# Now let's create the same metric from a matrix
# First let's create a Matrix
matrix = Array([[a ** 2, 0], [0, b ** 2]])
matrix

# Creating a Metric from a matrix also requires you to specify the coordinate system (so the axes can be labeled)
g2 = metric.Metric(matrix=matrix, coord_system=cs)
# Note that the Metric class automatically computes the two-form and uses it for representation
g2

# Metrics can be inverted, and produce other metrics
g3 = g2.inverse
g3

Curvature

# Now let's compute curvature terms
from sympy import Function
from pystein import curvature
# Let's create a metric with some curvature..
x, y = cs.base_symbols()  # grab the coordinate parameters
F = Function('F')(x, y)  # Define an arbitrary function that depends on x and y
g4 = metric.Metric(twoform=F ** 2 * tpow(dx, 2) + b ** 2 * tpow(dy, 2))
curvature.ricci_tensor_component(0, 0, g4).doit()

Matter

# Let's compute the matter stress energy tensor of a perfect fluid in 1D
from pystein import matter
# Need to quickly redefine the coordinates to have a temporal coordinate
t, x, y = symbols('t x y')
M = Manifold('M', dim=3)
P = Patch('origin', M)
cs = coords.CoordSystem('OneDim', P, [t, x, y])

dt, dx, dy = cs.base_oneforms()
Q = Function('Q')(t, y)  # Define an arbitrary function that depends on x and y
S = Function('S')(t, x)  # Define an arbitrary function that depends on x and y
g5 = metric.Metric(twoform=- Q ** 2 * tpow(dt, 2) + b ** 2 * tpow(dx, 2) + S ** 2 * tpow(dy, 2), components=(Q, S, b))
g5

# Now use the matter module to create the stress energy tensor for perfect fluid
T = matter.perfect_fluid(g5)
T

utilities.clean_expr(curvature.einstein_tensor_component(0, 0, g5))

# Note that in the limit Q -> 1
g5_lim = g5.subs({Q: 1})
T_lim = matter.perfect_fluid(g5_lim)
T_lim

utilities.clean_expr(curvature.einstein_tensor_component(0, 0, g5_lim))

Gravity

# One can also directly compute the Einstein Equations
from pystein import gravity
utilities.clean_expr(gravity.einstein_equation(0, 0, g5, T))

# Similarly in the limit:
utilities.clean_expr(gravity.einstein_equation(0, 0, g5_lim, T_lim))

Full Example: FLRW Cosmology

# Load the predefined FLRW metric
flrw = metric.flrw(cartesian=True)
flrw

T = matter.perfect_fluid(flrw)
efe_00 = utilities.clean_expr(gravity.einstein_equation(0, 0, flrw, T).doit())
efe_00

# Simplify derivative notation:
metric.simplify_deriv_notation(efe_00, flrw)

# Can also use "dots"
metric.simplify_deriv_notation(efe_00, flrw, use_dots=True)

Numeric Tools

The pystein package contains some limited numerical utilities, including:

  • ability to numerically integrate the geodesic equations geodesic.numerical_geodesic
  • convenience functions to compute multiple geodesics from a variety of initial conditions (2D)

These utilities are compatible with the symbolic tools thanks to sympy.lambdify, which is used to convert symbolic equations into numeric equations.

*Note that the numeric tools in pystein are still in beta.

Example Geodesic Usage

Construct a metric from a twoform

M = Manifold('M', dim=2)
P = Patch('origin', M)

rho, phi, a = sympy.symbols('rho phi a', nonnegative=True)
cs = coords.CoordSystem('schw', P, [rho, phi])
drho, dphi = cs.base_oneforms()
ds2 = a ** 2 * ((1 / (1 - rho ** 2)) * tpow(drho, 2) + rho ** 2 * tpow(dphi, 2))
g = metric.Metric(twoform=ds2)
g

Compute the symbolic geodesic equations

full_simplify(geodesic.geodesic_equation(0, sympy.symbols('lambda'), g))

Numerically integrate the geodesic equations

init = (numpy.sin(numpy.pi / 4), 0.0, numpy.cos(numpy.pi / 4), numpy.pi / 4)
lambdas = numpy.arange(0, 2.1, 0.001)
df = geodesic.numerical_geodesic(g, init, lambdas)
df.head()
rho phi
0 0.707107 0.000000
1 0.707814 0.000785
2 0.708520 0.001568
3 0.709226 0.002349
4 0.709931 0.003129

Release files for pystein 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystein 0.5.3
File Size Uploaded
pystein-0.5.3.tar.gz 26.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pystein 0.5.3
File Interpreter ABI Platform
pystein-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 55.3 kB

Release files / pystein-0.5.3.tar.gz

Download URL pystein-0.5.3.tar.gz
Size 26.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d33d7ad8f061d72ab09b91d2b19cad09e85c462cdb9667466fb989d215abfb6d
BLAKE2b-256 checksum
How to use checksums
a48994b805990856bf613cb9febdbf7245074945cab9f1e6ced903ab1b9c4382
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.10

Release files / pystein-0.5.3-py3-none-any.whl

Download URL pystein-0.5.3-py3-none-any.whl
Size 28.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b72f78ea13137ce89102abaa69a8b1c842e56ed1450135afb25de5d979ab3cd
BLAKE2b-256 checksum
How to use checksums
56d61bc07ce54cfba78c7bd1c9e34923881729d7df970b3a147dc94a06a7440d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.10

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page