Skip to main content

Collapse

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystein-0.5.3.tar.gz (26.4 kB view hashes)

Uploaded Source

Built Distribution

pystein-0.5.3-py3-none-any.whl (28.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page