Package: pystein
General Relativity Symbolic UtilitiesThepysteinpackage contains utilities for computing symbolic utilities for computing various
quantities that arise in general relativity. Presently, this package is essentially asympyextension that computes
components of tensors directly.Symbolic ToolsThepysteinpackage makes use ofsympyto compute symbolic curvature equations (EFE).Example Computation: FLRW Cosmology# Load the predefined FLRW metricfrompysteinimportmetric,gravityfrompysteinimportutilitiesflrw=metric.flrw().subs({'c':1})flrwefe_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 ToolsCoordinate Systemsfromsympy.diffgeomimportManifold,Patchfrompysteinimportcoords# The pystein CoordinateSystem extends the sympy.diffgeom api to make parameters more accessibleM=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 easyfromsympyimportArray,symbolsfrompysteinimportmetricfrompystein.utilitiesimporttensor_powastpow# 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 systema,b=symbols('a b')# some constants to use in the metricdx,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 twoformg1# Now let's create the same metric from a matrix# First let's create a Matrixmatrix=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 representationg2# Metrics can be inverted, and produce other metricsg3=g2.inverseg3Curvature# Now let's compute curvature termsfromsympyimportFunctionfrompysteinimportcurvature# Let's create a metric with some curvature..x,y=cs.base_symbols()# grab the coordinate parametersF=Function('F')(x,y)# Define an arbitrary function that depends on x and yg4=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 1Dfrompysteinimportmatter# Need to quickly redefine the coordinates to have a temporal coordinatet,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 yS=Function('S')(t,x)# Define an arbitrary function that depends on x and yg5=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 fluidT=matter.perfect_fluid(g5)Tutilities.clean_expr(curvature.einstein_tensor_component(0,0,g5))# Note that in the limit Q -> 1g5_lim=g5.subs({Q:1})T_lim=matter.perfect_fluid(g5_lim)T_limutilities.clean_expr(curvature.einstein_tensor_component(0,0,g5_lim))Gravity# One can also directly compute the Einstein Equationsfrompysteinimportgravityutilities.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 metricflrw=metric.flrw(cartesian=True)flrwT=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 ToolsThepysteinpackage contains some limited numerical utilities, including:ability to numerically integrate the geodesic equationsgeodesic.numerical_geodesicconvenience functions to compute multiple geodesics from a variety of initial conditions (2D)These utilities are compatible with the symbolic tools thanks tosympy.lambdify, which is used to convert symbolic
equations into numeric equations.*Note that the numeric tools inpysteinare still in beta.Example Geodesic UsageConstruct a metric from a twoformM=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)gCompute the symbolic geodesic equationsfull_simplify(geodesic.geodesic_equation(0,sympy.symbols('lambda'),g))Numerically integrate the geodesic equationsinit=(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()rhophi00.7071070.00000010.7078140.00078520.7085200.00156830.7092260.00234940.7099310.003129